madspof Posted February 25, 2009 Share Posted February 25, 2009 Hey everyone, I have this problem where i want to put a varaible into the style code at the top of my page but the variable depends on a while loop and i cannot get it to work does anyone no how to do this? here is the code i am using <title>Untitled Document</title> <style type="text/css"> <!-- .listbox { color: <?php echo $colour; ?>; } --> </style> </head> <body> <form name="form1" method="post" action="changearound.php"> <p align="center"> <?php include "connect.php"; echo'<select name="username" class="listbox" size="20">'; $res=mysql_query("select * from users"); if(mysql_num_rows($res)==0) echo "there is no data in table.."; else for($i=0;$i<mysql_num_rows($res);$i++) { $row=mysql_fetch_assoc($res); if($row[Authorised] == 1){ $colour = "black"; }else{ $colour = "red"; } echo"<option>$row[username]</option>"; } echo'</select>'; ?> thanks before hand madspof Quote Link to comment https://forums.phpfreaks.com/topic/146891-solved-how-can-i-get-get-a-variable-from-a-while-loop/ Share on other sites More sharing options...
KevinM1 Posted February 25, 2009 Share Posted February 25, 2009 Hey everyone, I have this problem where i want to put a varaible into the style code at the top of my page but the variable depends on a while loop and i cannot get it to work does anyone no how to do this? here is the code i am using <title>Untitled Document</title> <style type="text/css"> <!-- .listbox { color: <?php echo $colour; ?>; } --> </style> </head> <body> <form name="form1" method="post" action="changearound.php"> <p align="center"> <?php include "connect.php"; echo'<select name="username" class="listbox" size="20">'; $res=mysql_query("select * from users"); if(mysql_num_rows($res)==0) echo "there is no data in table.."; else for($i=0;$i<mysql_num_rows($res);$i++) { $row=mysql_fetch_assoc($res); if($row[Authorised] == 1){ $colour = "black"; }else{ $colour = "red"; } echo"<option>$row[username]</option>"; } echo'</select>'; ?> thanks before hand madspof Rethink your design. Instead of switching between PHP and HTML in a haphazard fashion, do all of your PHP processing first, then write the HTML output. It'll save you headaches like these, and make your code easier to maintain in the long run. Quote Link to comment https://forums.phpfreaks.com/topic/146891-solved-how-can-i-get-get-a-variable-from-a-while-loop/#findComment-771217 Share on other sites More sharing options...
madspof Posted February 25, 2009 Author Share Posted February 25, 2009 I cant have the while loop anywhere else but in the body of the script where the list form is can I ? Quote Link to comment https://forums.phpfreaks.com/topic/146891-solved-how-can-i-get-get-a-variable-from-a-while-loop/#findComment-771226 Share on other sites More sharing options...
wildteen88 Posted February 25, 2009 Share Posted February 25, 2009 Example of how I'd do it. <?php include "connect.php"; $res = mysql_query("SELECT * FROM users"); $listItems = null; while($row = mysql_fetch_assoc($res)) { $class = ($row['Authorised'] == 1) ? 'auth' : 'noauth'; // generate the <options></options> into the $listItems variable $listItems .= "<option class=\"$class\">{$row['Username']}</option>\n "; } ?> <html> <head> <title>Untitled Document</title> <style type="text/css"> <!-- .auth { color: black; } .noauth { color: red; } --> </style> </head> <body> <form name="form1" method="post" action="changearound.php"> <p align="center"> <select name="username" size="20"> <?php echo $listItems; ?> </select> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/146891-solved-how-can-i-get-get-a-variable-from-a-while-loop/#findComment-771235 Share on other sites More sharing options...
madspof Posted February 25, 2009 Author Share Posted February 25, 2009 Ok just so im not copying it and not learning what does the ? and the : bits do in the while loop ? $class = ($row['Authorised'] == 1) ? 'auth' : 'noauth'; Quote Link to comment https://forums.phpfreaks.com/topic/146891-solved-how-can-i-get-get-a-variable-from-a-while-loop/#findComment-771236 Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 Ternary operators. Basically a shortened if/else, If $row['auth'] is 1 then put 'auth' in the class variable. Else but 'noauth' Quote Link to comment https://forums.phpfreaks.com/topic/146891-solved-how-can-i-get-get-a-variable-from-a-while-loop/#findComment-771237 Share on other sites More sharing options...
madspof Posted February 25, 2009 Author Share Posted February 25, 2009 Wow thanks man never knew that you could use : like that and i didnt realy think it was possible of putting the while loop were youve put it :-) thanks anyway Quote Link to comment https://forums.phpfreaks.com/topic/146891-solved-how-can-i-get-get-a-variable-from-a-while-loop/#findComment-771247 Share on other sites More sharing options...
madspof Posted February 25, 2009 Author Share Posted February 25, 2009 how do i mark this as solved ? got it dmt lol Quote Link to comment https://forums.phpfreaks.com/topic/146891-solved-how-can-i-get-get-a-variable-from-a-while-loop/#findComment-771250 Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 Bottom left hand corner above the Quick Reply. Quote Link to comment https://forums.phpfreaks.com/topic/146891-solved-how-can-i-get-get-a-variable-from-a-while-loop/#findComment-771252 Share on other sites More sharing options...
KevinM1 Posted February 25, 2009 Share Posted February 25, 2009 Wow thanks man never knew that you could use : like that and i didnt realy think it was possible of putting the while loop were youve put it :-) thanks anyway Not only is it possible, but it's the preferred way to structure PHP applications. While it's possible to switch from PHP to HTML and back on the fly, it's best to keep the two as separate as you can. Process, then output. Quote Link to comment https://forums.phpfreaks.com/topic/146891-solved-how-can-i-get-get-a-variable-from-a-while-loop/#findComment-771255 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.