scarface83 Posted April 25, 2007 Share Posted April 25, 2007 Hi in the db there is more than one record but my code is only showing one line can anyone see where im going wrong ? <?php session_start(); include ('db_connect.inc'); $query = " SELECT TeamRef FROM a_users WHERE Login='$keepit' "; $result = mysql_query($query) or die (mysql_error); // runs the above query $row = mysql_fetch_array($result); $tm = $row['TeamRef'] ; $query2 = "SELECT datestamp, ID FROM absence_mgt WHERE viewable='1' AND TeamRef='$tm'"; $result2 = mysql_query($query2) or die (mysql_error); // runs the above query $row2 = mysql_fetch_assoc($result2); $date_stamp =$row2['datestamp'] ; $new_id = $row2['ID']; list($year,$month,$day) = split("-",$date_stamp); $newdate = $day."/".$month."/".$year; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <link href="/ab_mgt/style.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="title"> <u><b>Pending Absence for your Team</b></u> </div> <div class="pend_subtitle"> Please Confirm if this absence should be saved to your absence calendar ? </div> <div class="back"><a href="main.php">Back</a></div> <div class="pend_absence"> <?php $query3 = mysql_query("SELECT Forename,Surname FROM a_users WHERE In_use='Y' AND Login='$new_id'")or die (mysql_error); $row3 = mysql_fetch_assoc($query3); session_unregister('first'); session_unregister('second'); $first = $row3['Forename']; $second = $row3['Surname']; while ($row2 = mysql_fetch_assoc($result2));{ echo "Date of Absence - ". $newdate." - ".$new_id." - ". $first." ".$second." <a href= 'test' >Save</a>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/ Share on other sites More sharing options...
Eugene Posted April 25, 2007 Share Posted April 25, 2007 while ($row2 = mysql_fetch_assoc($result2)); Remove the ";" at the end. Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238149 Share on other sites More sharing options...
MadTechie Posted April 25, 2007 Share Posted April 25, 2007 <?php session_start(); include ('db_connect.inc'); $query = " SELECT TeamRef FROM a_users WHERE Login='$keepit' "; $result = mysql_query($query) or die (mysql_error); // runs the above query $row = mysql_fetch_array($result); $tm = $row['TeamRef'] ; $query2 = "SELECT datestamp, ID FROM absence_mgt WHERE viewable='1' AND TeamRef='$tm'"; $result2 = mysql_query($query2) or die (mysql_error); // runs the above query $row2 = mysql_fetch_assoc($result2); $date_stamp =$row2['datestamp'] ; $new_id = $row2['ID']; list($year,$month,$day) = split("-",$date_stamp); $newdate = $day."/".$month."/".$year; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <link href="/ab_mgt/style.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="title"> <u><b>Pending Absence for your Team</b></u> </div> <div class="pend_subtitle"> Please Confirm if this absence should be saved to your absence calendar ? </div> <div class="back"><a href="main.php">Back</a></div> <div class="pend_absence"> <?php $query3 = mysql_query("SELECT Forename,Surname FROM a_users WHERE In_use='Y' AND Login='$new_id'")or die (mysql_error); $row3 = mysql_fetch_assoc($query3); session_unregister('first'); session_unregister('second'); $first = $row3['Forename']; $second = $row3['Surname']; while ($row2 = mysql_fetch_assoc($result2)) { $first = $row2['Forename']; $second = $row2['Surname']; $second = $row2['Surname']; $new_id= $row2['new_id']; $newdate= $row2['newdate']; echo "Date of Absence - ". $newdate." - ".$new_id." - ". $first." ".$second." <a href= 'test' >Save</a>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238151 Share on other sites More sharing options...
dsaba Posted April 25, 2007 Share Posted April 25, 2007 * Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238153 Share on other sites More sharing options...
scarface83 Posted April 25, 2007 Author Share Posted April 25, 2007 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238154 Share on other sites More sharing options...
scarface83 Posted April 25, 2007 Author Share Posted April 25, 2007 still no joy ? any ideas ? Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238157 Share on other sites More sharing options...
MadTechie Posted April 25, 2007 Share Posted April 25, 2007 change while ($row2 = mysql_fetch_assoc($result2)) to while ($row2 == mysql_fetch_assoc($result3)) your need to update $query3 = mysql_query("SELECT Forename,Surname FROM a_users WHERE In_use='Y' AND Login='$new_id'")or die (mysql_error); with the extra fields Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238164 Share on other sites More sharing options...
scarface83 Posted April 25, 2007 Author Share Posted April 25, 2007 nope doesn't even echo anything with that ? thanks Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238166 Share on other sites More sharing options...
MadTechie Posted April 25, 2007 Share Posted April 25, 2007 while ($row2 != mysql_fetch_assoc($result3)) Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238172 Share on other sites More sharing options...
scarface83 Posted April 25, 2007 Author Share Posted April 25, 2007 that one just echo the same row over and over , i have tried phpmyadmin and it comes back with about 10 rows so im not sure why its only displaying the 1 thanks Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238174 Share on other sites More sharing options...
MadTechie Posted April 25, 2007 Share Posted April 25, 2007 can you post your updated code please Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238185 Share on other sites More sharing options...
scarface83 Posted April 25, 2007 Author Share Posted April 25, 2007 <?php session_start(); include ('db_connect.inc'); $query = " SELECT TeamRef FROM a_users WHERE Login='$keepit' "; $result = mysql_query($query) or die (mysql_error); // runs the above query $row = mysql_fetch_array($result); $tm = $row['TeamRef'] ; $query2 = "SELECT datestamp, ID FROM absence_mgt WHERE viewable='1' AND TeamRef='$tm'"; $result2 = mysql_query($query2) or die (mysql_error); // runs the above query $row2 = mysql_fetch_assoc($result2); $date_stamp =$row2['datestamp'] ; $new_id = $row2['ID']; list($year,$month,$day) = split("-",$date_stamp); $newdate = $day."/".$month."/".$year; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <link href="/ab_mgt/style.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="title"> <u><b>Pending Absence for your Team</b></u> </div> <div class="pend_subtitle"> Please Confirm if this absence should be saved to your absence calendar ? </div> <div class="back"><a href="main.php">Back</a></div> <div class="pend_absence"> <?php $query3 = mysql_query("SELECT Forename,Surname FROM a_users WHERE In_use='Y' AND Login='$new_id'")or die (mysql_error); $row3 = mysql_fetch_assoc($query3); session_unregister('first'); session_unregister('second'); $first = $row3['Forename']; $second = $row3['Surname']; while ($row2 = mysql_fetch_assoc($result2)){ echo "Date of Absence - ". $newdate." - ".$new_id." - ". $first." ".$second." <a href= 'test' >Save</a>"; } ?> </div> <?php mysql_close(); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238188 Share on other sites More sharing options...
MadTechie Posted April 25, 2007 Share Posted April 25, 2007 That seams to be missing a few of the updates!! Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238199 Share on other sites More sharing options...
scarface83 Posted April 25, 2007 Author Share Posted April 25, 2007 sorry i just removed the ; at the end of the loop as the others didn't work Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238212 Share on other sites More sharing options...
MadTechie Posted April 25, 2007 Share Posted April 25, 2007 theirs more than one problem with the code this is what you should have After looking over the code as a whole.. one question remains.. what are you trying to do ? a lot of the code isn't being used or is useless! <?php session_start(); include ('db_connect.inc'); $query = " SELECT TeamRef FROM a_users WHERE Login='$keepit' ";//WILL FAIL as $keepit isn't set $result = mysql_query($query) or die (mysql_error()); // runs the above query $row = mysql_fetch_array($result); $tm = $row['TeamRef']; $query2 = "SELECT datestamp, ID FROM absence_mgt WHERE viewable='1' AND TeamRef='$tm'"; $result2 = mysql_query($query2) or die (mysql_error()); // runs the above query $row2 = mysql_fetch_assoc($result2); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <link href="/ab_mgt/style.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="title"> <u><b>Pending Absence for your Team</b></u> </div> <div class="pend_subtitle"> Please Confirm if this absence should be saved to your absence calendar ? </div> <div class="back"><a href="main.php">Back</a></div> <div class="pend_absence"> <?php $query3 = mysql_query("SELECT datestamp, ID, Forename,Surname FROM a_users ")or die (mysql_error); // //Also remove the following while testing - WHERE In_use='Y' AND Login='$new_id' $row3 = mysql_fetch_assoc($query3); session_unregister('first'); session_unregister('second'); while ($row3 = mysql_fetch_assoc($result3)) { $first = $row3['Forename']; $second = $row3['Surname']; $date_stamp =$row2['datestamp']; $new_id = $row2['ID']; list($year,$month,$day) = split("-",$date_stamp); $newdate = $day."/".$month."/".$year; echo "Date of Absence - ". $newdate." - ".$new_id." - ". $first." ".$second." <a href= 'test' >Save</a>"; } mysql_close(); ?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238235 Share on other sites More sharing options...
scarface83 Posted April 25, 2007 Author Share Posted April 25, 2007 the purpose of the code is to display all rows of datestamp, ID from the db where the following conditions are met "viewable='1' AND TeamRef='$tm'" what bits of the code are not being used ? thank Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238244 Share on other sites More sharing options...
MadTechie Posted April 25, 2007 Share Posted April 25, 2007 from which database absence_mgt or a_users ? also do you have a field list for the tables Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238253 Share on other sites More sharing options...
scarface83 Posted April 25, 2007 Author Share Posted April 25, 2007 absence _mgt the a_users just grabs some values to add to the criteria. Field Type Null Default datestamp date Yes abs_value smallint(1) Yes ID varchar(10) Yes notes varchar(100) Yes viewable smallint(1) Yes TeamRef smallint(2) Yes Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238278 Share on other sites More sharing options...
MadTechie Posted April 25, 2007 Share Posted April 25, 2007 ok what table is that and whats the fields in the other one, i'm off home soon (lol at work at the mo) 2hours travel! Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238281 Share on other sites More sharing options...
scarface83 Posted April 25, 2007 Author Share Posted April 25, 2007 that table was the absence_mgt one , the other table is not really that relevant as it works fine and that query just sets the $tm = $row['TeamRef'] ; which is what team basically , im sure your boss will be proud your being so helpful lol thanks Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238293 Share on other sites More sharing options...
MadTechie Posted April 25, 2007 Share Posted April 25, 2007 erm.. i'm confused you say that this works "SELECT TeamRef FROM a_users WHERE Login='$keepit'" which is the a_users, which is just for the TeamRef, ok so far makes sence, BUT absence _mgt the a_users just grabs some values to add to the criteria. Field Type Null Default datestamp date Yes abs_value smallint(1) Yes ID varchar(10) Yes notes varchar(100) Yes viewable smallint(1) Yes TeamRef smallint(2) Yes Theirs no Forename or Surname in their so it must be in the a_users, so whats absence _mgt for ? Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-238474 Share on other sites More sharing options...
scarface83 Posted April 26, 2007 Author Share Posted April 26, 2007 he he sorry getting a little confused now so lets start again lol this code grabs the team reference of the users where login = $keepit - which is there login, i have etested this and it works if you echo it out after the query <?php session_start(); include ('db_connect.inc'); $query = " SELECT TeamRef FROM a_users WHERE Login='$keepit' ";//this is a session variable already set on another page $result = mysql_query($query) or die (mysql_error()); // runs the above query $row = mysql_fetch_array($result); $tm = $row['TeamRef']; this is the code that it important it grabs the datestamp and id where the team ref is the same as the user who login in $query2 = "SELECT datestamp, ID FROM absence_mgt WHERE viewable='1' AND TeamRef='$tm'"; // this query select the datestamp and users ID $result2 = mysql_query($query2) or die (mysql_error()); // runs the above query $row2 = mysql_fetch_assoc($result2); ?> see comments in the code but this next part should be a loop and show all entries where the teamref = is = to that of the person who is logged in , when i view the page i get one result which is correct but there are about 12 entries so it seems the while loop isn't working correclty <?php $query3 = mysql_query("SELECT datestamp, ID, Forename,Surname FROM a_users ")or die (mysql_error); // this can be ingnores as it works fine // //Also remove the following while testing - WHERE In_use='Y' AND Login='$new_id' $row3 = mysql_fetch_assoc($query3); session_unregister('first'); session_unregister('second'); while ($row3 = mysql_fetch_assoc($result3)) // the problem lies as it displays one row thats correct but doesnt show the others { $first = $row3['Forename']; $second = $row3['Surname']; $date_stamp =$row2['datestamp']; $new_id = $row2['ID']; list($year,$month,$day) = split("-",$date_stamp); $newdate = $day."/".$month."/".$year; echo "Date of Absence - ". $newdate." - ".$new_id." - ". $first." ".$second." <a href= 'test' >Save</a>"; } mysql_close(); thanks for your help with this Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-239125 Share on other sites More sharing options...
MadTechie Posted April 26, 2007 Share Posted April 26, 2007 <?php $query3 = mysql_query("SELECT datestamp, ID, Forename,Surname FROM a_users ")or die (mysql_error); // this can be ingnores as it works fine // //Also remove the following while testing - WHERE In_use='Y' AND Login='$new_id' $row3 = mysql_fetch_assoc($query3);// don't need this here session_unregister('first'); session_unregister('second'); while ($row3 = mysql_fetch_assoc($result3)) // the problem lies as it displays one row thats correct but doesnt show the others { $first = $row3['Forename']; $second = $row3['Surname']; $date_stamp =$row2['datestamp']; //<--Should be row3 $new_id = $row2['ID']; //<--Should be row3 list($year,$month,$day) = split("-",$date_stamp); $newdate = $day."/".$month."/".$year; echo "Date of Absence - ". $newdate." - ".$new_id." - ". $first." ".$second." <a href= 'test' >Save</a>"; } mysql_close(); Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-239147 Share on other sites More sharing options...
scarface83 Posted April 26, 2007 Author Share Posted April 26, 2007 these $date_stamp =$row2['datestamp']; //<--Should be row3 $new_id = $row2['ID']; //<--Should be row3 corresponds to this statement $query2 = "SELECT datestamp, ID FROM absence_mgt WHERE viewable='1' AND TeamRef='$tm'"; // this query select the datestamp and users ID $result2 = mysql_query($query2) or die (mysql_error()); // runs the above query $row2 = mysql_fetch_assoc($result2); so should be row2 ? thanks Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-239176 Share on other sites More sharing options...
scarface83 Posted April 26, 2007 Author Share Posted April 26, 2007 Can anyone Help please ? Quote Link to comment https://forums.phpfreaks.com/topic/48635-while-loop-not-working/#findComment-239427 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.