mart94 Posted October 21, 2009 Share Posted October 21, 2009 Hi. I have a problem with while() loop. Code: <head> <style type="text/css"> .style2 { color: #00C000; font-weight: bold; } </style> </head> <body style="background-color: #202020"> <table cellspacing='1' class='listtable' width='100%'> <tr bgcolor="#000000"> <td height='16' width='5%' style="padding-left: 7px" align='center' class="style2">ID</td> <td height='16' width='40%' style="padding-left: 7px" align='center' class="style2">Nimi</td> <td height='16' width='35%' class='style2' style="padding-left: 7px" align='center'>Õigused</td> <td height='16' width='20%'class='style2' style="padding-left: 7px" align='center'>Lõpukuupäev</td> </tr> </table> <?php // Õiguste muutmine include("adminilist.php"); // Muu pask ka siin $serverivalik = $_POST['serverivalik']; function destroy_foo() { global $variables; unset($variables); } // Connect mysql_connect('localhost', 'root', '') or die("dont connect to server"); // Valib andmebaasi mysql_select_db("amxbans"); $variable = array(); $result = mysql_query("SELECT admin_id FROM amx_admins_servers WHERE server_id=1 ORDER BY admin_id ASC") or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { $variable[] = $row['admin_id']; foreach($variable as &$variables) { $value = $variables; } $foo = $variables; destroy_foo(); $resultnew = mysql_query("SELECT * FROM amx_amxadmins WHERE id='$foo' ORDER BY access"); while ($rownew = mysql_fetch_assoc($resultnew)) { $nickname = $rownew['username']; $endtime = $rownew['endtime']; $access = $rownew['access']; echo "<head>\n"; echo "<style type='text/css'>\n"; echo ".style2 {\n"; echo " color: #00C000;\n"; echo " font-weight: bold;\n"; echo "}\n"; echo ".style3 {\n"; echo " color: #E0A518;\n"; echo " font-weight: bold;\n"; echo "}\n"; echo ".style4 {\n"; echo " color: #C0C0FF;\n"; echo "}\n"; echo ".style5 {\n"; echo " color: #C0C0FF;\n"; echo " font-weight: bold;\n"; echo "}\n"; echo "</style>\n"; echo "</head>\n"; echo "<table cellspacing='1' class='listtable' width='100%'>\n"; echo "<tr bgcolor=/'#000000/'s>\n"; echo "<td height='16' width='5%' class='style4' style='padding-left: 7px' ><center>$foo</center></td>\n"; echo "<td height='16' width='40%' style='padding-left: 7px' class='style3'>$nickname</td> \n"; echo "<td height='16' width='35%' class='style4' style='padding-left: 7px' >$access</td>\n"; echo "<td height='16' width='20%' class='style5' style='padding-left: 7px' >$endtime</td>\n"; echo "</tr>\n"; echo "</table>\n"; } } ?> The second result $resultnew = mysql_query("SELECT * FROM amx_amxadmins WHERE id='$foo' ORDER BY access"); The ORDER BY access doesn't work. It orders that same way like the id's form fist tables has been getted. I need: $resultnew = mysql_query("SELECT * FROM amx_amxadmins WHERE id='$foo' ORDER BY access"); get working. Mart Quote Link to comment https://forums.phpfreaks.com/topic/178459-solved-problem-with-while-loop/ Share on other sites More sharing options...
kickstart Posted October 21, 2009 Share Posted October 21, 2009 Hi Can't see why that wouldn't work to order by access within admin_id order. Also not sure why you have the foreach loop, nor why haven't merged the 2 pieces of SQL. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/178459-solved-problem-with-while-loop/#findComment-941108 Share on other sites More sharing options...
mart94 Posted October 21, 2009 Author Share Posted October 21, 2009 Hi Can't see why that wouldn't work to order by access within admin_id order. Also not sure why you have the foreach loop, nor why haven't merged the 2 pieces of SQL. All the best Keith I tested any way but the output will be same... Only way to change order is in first result: $result = mysql_query("SELECT admin_id FROM amx_admins_servers WHERE server_id=1 ORDER BY admin_id ASC") or die(mysql_error()); If i write this so: $result = mysql_query("SELECT admin_id FROM amx_admins_servers WHERE server_id=1 ORDER BY admin_id DESC") or die(mysql_error()); Then the list will be inverted. Mart Quote Link to comment https://forums.phpfreaks.com/topic/178459-solved-problem-with-while-loop/#findComment-941112 Share on other sites More sharing options...
kickstart Posted October 21, 2009 Share Posted October 21, 2009 Hi But you are only getting the results of the 2nd piece of SQL for each record you retrieve from the first piece of SQL. It will give you them in access order but within admin_id order. What I think you want is something like:- <head> <style type="text/css"> .style2 { color: #00C000; font-weight: bold; } </style> </head> <body style="background-color: #202020"> <table cellspacing='1' class='listtable' width='100%'> <tr bgcolor="#000000"> <td height='16' width='5%' style="padding-left: 7px" align='center' class="style2">ID</td> <td height='16' width='40%' style="padding-left: 7px" align='center' class="style2">Nimi</td> <td height='16' width='35%' class='style2' style="padding-left: 7px" align='center'>Õigused</td> <td height='16' width='20%'class='style2' style="padding-left: 7px" align='center'>Lõpukuupäev</td> </tr> </table> <?php // Õiguste muutmine include("adminilist.php"); // Muu pask ka siin $serverivalik = $_POST['serverivalik']; function destroy_foo() { global $variables; unset($variables); } // Connect mysql_connect('localhost', 'root', '') or die("dont connect to server"); // Valib andmebaasi mysql_select_db("amxbans"); $variable = array(); $result = mysql_query("SELECT a.admin_id, b.username, b.endtime, b.access FROM amx_admins_servers a LEFT OUTER JOIN amx_amxadmins b ON a.admin_id = b.id WHERE a.server_id=1 ORDER BY b.access") or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { $nickname = $row['username']; $endtime = $row['endtime']; $access = $row['access']; echo "<head>\n"; echo "<style type='text/css'>\n"; echo ".style2 {\n"; echo " color: #00C000;\n"; echo " font-weight: bold;\n"; echo "}\n"; echo ".style3 {\n"; echo " color: #E0A518;\n"; echo " font-weight: bold;\n"; echo "}\n"; echo ".style4 {\n"; echo " color: #C0C0FF;\n"; echo "}\n"; echo ".style5 {\n"; echo " color: #C0C0FF;\n"; echo " font-weight: bold;\n"; echo "}\n"; echo "</style>\n"; echo "</head>\n"; echo "<table cellspacing='1' class='listtable' width='100%'>\n"; echo "<tr bgcolor=/'#000000/'s>\n"; echo "<td height='16' width='5%' class='style4' style='padding-left: 7px' ><center>$foo</center></td>\n"; echo "<td height='16' width='40%' style='padding-left: 7px' class='style3'>$nickname</td> \n"; echo "<td height='16' width='35%' class='style4' style='padding-left: 7px' >$access</td>\n"; echo "<td height='16' width='20%' class='style5' style='padding-left: 7px' >$endtime</td>\n"; echo "</tr>\n"; echo "</table>\n"; } ?> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/178459-solved-problem-with-while-loop/#findComment-941121 Share on other sites More sharing options...
mart94 Posted October 21, 2009 Author Share Posted October 21, 2009 Hi But you are only getting the results of the 2nd piece of SQL for each record you retrieve from the first piece of SQL. It will give you them in access order but within admin_id order. What I think you want is something like:- <head> <style type="text/css"> .style2 { color: #00C000; font-weight: bold; } </style> </head> <body style="background-color: #202020"> <table cellspacing='1' class='listtable' width='100%'> <tr bgcolor="#000000"> <td height='16' width='5%' style="padding-left: 7px" align='center' class="style2">ID</td> <td height='16' width='40%' style="padding-left: 7px" align='center' class="style2">Nimi</td> <td height='16' width='35%' class='style2' style="padding-left: 7px" align='center'>Õigused</td> <td height='16' width='20%'class='style2' style="padding-left: 7px" align='center'>Lõpukuupäev</td> </tr> </table> <?php // Õiguste muutmine include("adminilist.php"); // Muu pask ka siin $serverivalik = $_POST['serverivalik']; function destroy_foo() { global $variables; unset($variables); } // Connect mysql_connect('localhost', 'root', '') or die("dont connect to server"); // Valib andmebaasi mysql_select_db("amxbans"); $variable = array(); $result = mysql_query("SELECT a.admin_id, b.username, b.endtime, b.access FROM amx_admins_servers a LEFT OUTER JOIN amx_amxadmins b ON a.admin_id = b.id WHERE a.server_id=1 ORDER BY b.access") or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { $nickname = $row['username']; $endtime = $row['endtime']; $access = $row['access']; echo "<head>\n"; echo "<style type='text/css'>\n"; echo ".style2 {\n"; echo " color: #00C000;\n"; echo " font-weight: bold;\n"; echo "}\n"; echo ".style3 {\n"; echo " color: #E0A518;\n"; echo " font-weight: bold;\n"; echo "}\n"; echo ".style4 {\n"; echo " color: #C0C0FF;\n"; echo "}\n"; echo ".style5 {\n"; echo " color: #C0C0FF;\n"; echo " font-weight: bold;\n"; echo "}\n"; echo "</style>\n"; echo "</head>\n"; echo "<table cellspacing='1' class='listtable' width='100%'>\n"; echo "<tr bgcolor=/'#000000/'s>\n"; echo "<td height='16' width='5%' class='style4' style='padding-left: 7px' ><center>$foo</center></td>\n"; echo "<td height='16' width='40%' style='padding-left: 7px' class='style3'>$nickname</td> \n"; echo "<td height='16' width='35%' class='style4' style='padding-left: 7px' >$access</td>\n"; echo "<td height='16' width='20%' class='style5' style='padding-left: 7px' >$endtime</td>\n"; echo "</tr>\n"; echo "</table>\n"; } ?> All the best Keith Hi. This code is almost working but i dont get anything that site where i want to put this. Picture of output: http://www.upload.ee/image/233930/clipboard_upped.png There is exactly rows as much in database. Mart Quote Link to comment https://forums.phpfreaks.com/topic/178459-solved-problem-with-while-loop/#findComment-941130 Share on other sites More sharing options...
kickstart Posted October 21, 2009 Share Posted October 21, 2009 Hi One thing I missed. Add this line :- $foo = $row['admin_id']; $nickname = $row['username']; $endtime = $row['endtime']; $access = $row['access']; Can you execute the SQL against your database to check it is bringing back the right rows? Can you also do a "view source" to just make sure that there is no data sent to the screen in those rows. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/178459-solved-problem-with-while-loop/#findComment-941134 Share on other sites More sharing options...
mart94 Posted October 21, 2009 Author Share Posted October 21, 2009 Hi One thing I missed. Add this line :- $foo = $row['admin_id']; $nickname = $row['username']; $endtime = $row['endtime']; $access = $row['access']; Can you execute the SQL against your database to check it is bringing back the right rows? Can you also do a "view source" to just make sure that there is no data sent to the screen in those rows. All the best Keith Dude you are a awsome! This is working. Thank you ! Quote Link to comment https://forums.phpfreaks.com/topic/178459-solved-problem-with-while-loop/#findComment-941136 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.