MK-2 Posted July 5, 2011 Posted July 5, 2011 Just a pre-warning. The code I paste below is very rough, I'm still getting to grips with PHP so am doing things so they work first of all, before I even look at tidying them up. What I want to happen is the php part will check if a user has any open helpdesk tickets. if they do, it continues to display them. if not, it will stop doing the bit about the helpdesk and carry on below the ?> and show the other table links instead. i'm guessing the answer will be tidy the script up and make functions, but wanted to know if there was a short term fix for it while i still pick up php thanks in advance School :: Staff Intranet images/stafflogo.png images/google.png Current open helpdesk tickets - (click here to view/hide) $connect = mysql_connect('localhost', 'helpdesk', 'password'); if (!$connect) {die('Could not connect: ' . mysql_error());} mysql_select_db('helpdesk'); $initial = $_SERVER["LOGON_USER"]; $_SESSION['un'] = preg_replace("/.*\\\\/", "", $initial); $username = $_SESSION['un']; $query="SELECT * FROM client WHERE USER_NAME='$username'"; $result=mysql_query($query); $num=mysql_numrows($result); $i = 0; while ($i < $num) { $userid = mysql_result($result,$i,"client_id"); $query2="SELECT * FROM job_ticket WHERE client_id='$userid' AND status_type_id='1'"; $result2=mysql_query($query2); $num2=mysql_numrows($result2); If ($num2 == 0) {die('No helpdesk tickets found under your username');} $x = 0; while ($x < $num2) { $jobname=mysql_result($result2,$x,"subject"); $jobdesc=mysql_result($result2,$x,"question_text"); $ticketnum=mysql_result($result2,$x,"job_ticket_id"); $status=mysql_result($result2,$x,"status_type_id"); If ($status ==1) {$jobstatus="Open";} If ($status ==2) {$jobstatus="Awaiting User Input";} $x++;} echo "Ticket Number: $ticketnum "; echo "Job Name: $jobname "; echo "Job Description: $jobdesc "; echo "Job Status: $fontcolor $jobstatus $fontend "; echo " "; $i++; } ?> <br /> <br /> var parentAccordion=new TINY.accordion.slider("parentAccordion");<br /> parentAccordion.init("acc","h3",-1,-1);<br /> <br /> var nestedAccordion=new TINY.accordion.slider("nestedAccordion");<br /> nestedAccordion.init("nested","h3",1,-1,"acc-selected");<br /> <br /> Helpdesk images/helpdesk.png Booking System images/bookingsystem.png
Flakes Posted July 5, 2011 Posted July 5, 2011 (edited) try this: School :: Staff Intranet Current open helpdesk tickets - (click here to view/hide) $connect = mysql_connect('localhost', 'helpdesk', 'password'); if (!$connect) { die('Could not connect: ' . mysql_error()); } mysql_select_db('helpdesk'); $initial = $_SERVER["LOGON_USER"]; $_SESSION['un'] = preg_replace("/.*\\\\/", "", $initial); $username = $_SESSION['un']; $query="SELECT * FROM client WHERE USER_NAME='$username'"; $result=mysql_query($query); $num=mysql_numrows($result); $i = 0; while ($i < $num) { $userid = mysql_result($result,$i,"client_id"); $query2="SELECT * FROM job_ticket WHERE client_id='$userid' AND status_type_id='1'"; $result2=mysql_query($query2); $num2=mysql_numrows($result2); If ($num2 == 0) { echo 'No helpdesk tickets found under your username'; //removed the die statemant } else //added else statement { $x = 0; while ($x < $num2) { $jobname=mysql_result($result2,$x,"subject"); $jobdesc=mysql_result($result2,$x,"question_text"); $ticketnum=mysql_result($result2,$x,"job_ticket_id"); $status=mysql_result($result2,$x,"status_type_id"); If ($status ==1) { $jobstatus="Open"; } If ($status ==2) { $jobstatus="Awaiting User Input"; } $x++; } } echo "Ticket Number: $ticketnum "; echo "Job Name: $jobname "; echo "Job Description: $jobdesc "; echo "Job Status: $fontcolor $jobstatus $fontend "; echo " "; $i++; } ?> <br /> <br /> var parentAccordion=new TINY.accordion.slider("parentAccordion");<br /> parentAccordion.init("acc","h3",-1,-1);<br /> <br /> var nestedAccordion=new TINY.accordion.slider("nestedAccordion");<br /> nestedAccordion.init("nested","h3",1,-1,"acc-selected");<br /> <br /> Helpdesk Booking System its hard to tell in the forum editor but that should do it , also each to there own but i prefer calling sql cells using $row['cellname']; so $sql = "SELECT NAME, AGE FROM `TABLE` WHERE ROW = VARIABLE"; $rst = mysql_query($sql); while ($row = mysql_fetch_assoc($rst)) { $name = $row['NAME']; $age = $row['AGE']; } Edited July 5, 2011 by Flakes 1
localzuk Posted July 5, 2011 Posted July 5, 2011 Why not simply switch the if /die to an if else? ie. if ($num2 == 0) { echo('No helpdesk tickets found under your username'); } else { $x = 0; while ($x < $num2) { $jobname=mysql_result($result2,$x,"subject"); $jobdesc=mysql_result($result2,$x,"question_text"); $ticketnum=mysql_result($result2,$x,"job_ticket_id"); $status=mysql_result($result2,$x,"status_type_id"); if ($status ==1) { $jobstatus="Open"; } if ($status ==2) { $jobstatus="Awaiting User Input"; } $x++; } echo "Ticket Number: $ticketnum "; echo "Job Name: $jobname "; echo "Job Description: $jobdesc "; echo "Job Status: $fontcolor$jobstatus$fontend "; echo " "; } This will then continue on if the condition is met. 1
MK-2 Posted July 5, 2011 Author Posted July 5, 2011 Why not simply switch the if /die to an if else? sadly i thought of this about ten mins later, i hate not realising the blindingly obvious things!!! thanks to both of you
Flakes Posted July 5, 2011 Posted July 5, 2011 im a sucker for speed, i would also recommend using the least amount of echo statements possible, every echo requires a call to the parser, so the less you have the faster your scripts run, obviously the speed difference isnt alot in a small script like this but for bigger scripts it can become noticable. echo "Ticket Number: $ticketnum "; echo "Job Name: $jobname "; echo "Job Description: $jobdesc "; echo "Job Status: $fontcolor $jobstatus $fontend "; echo " "; can become echo " Ticket Number: $ticketnum Job Name: $jobname Job Description: $jobdesc Job Status: $fontcolor $jobstatus $fontend "; another warning tags, tags and other types of html text formatting are being phased out in preference for CSS styling (HTML tt, i, b, big, small tag) i recommend to use CSS, for font styles. 1
localzuk Posted July 5, 2011 Posted July 5, 2011 Also, a small tip - don't put everything on one line if you can break it up. ie. those { blah } bits, put the { and } on new lines. It makes it easier to track what's what in my experience. 1
MK-2 Posted July 6, 2011 Author Posted July 6, 2011 im a sucker for speed, i would also recommend using the least amount of echo statements possible, every echo requires a call to the parser, so the less you have the faster your scripts run, obviously the speed difference isnt alot in a small script like this but for bigger scripts it can become noticable. thanks for that. like i said i've only been looking into php the past couple of weeks so haven't got into the proper way of scripting yet. i'll most likely be altering the script with all these changes soon, but it was just a work in progress at the moment (this intranet page will go live september). thanks again for the hints
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now