Jump to content

[SOLVED] How can i get get a variable from a while loop


Recommended Posts

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

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.

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>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.