lala Posted February 26, 2008 Share Posted February 26, 2008 Hello, I have a simple script that loops through a table of available packages. What I want to do is set a value of a variable ($notes) in the table, based on the value of another variable ($remaining). If the value of remaining is zero, I want the value of $notes to be 'Sold Out'. Otherwise, the value should be 'Available'. I've got the table displaying with the values in all the fields. The notes field is blank by default, but I can't get the value of $notes to change with the value in $remaining. Here's the code: $query = "select * from theater"; if (!$result = mysql_query($query,$dbconnect)) { echo mysql_error(); exit; } if($remaining > 0) { $notes = "Available"; } else { $notes = "Sold Out"; } print ($notes); print ($remaining); ?> <form action="purchase.php" name="purchase"> <table border="1" bordercolor="#000099" class="text" cellpadding="2"> <tr> <th>Check</th> <th width="51" align="left">Package</th> <th width="49" align="left">Notes</th> <th width="49" align="left">Remaining</th> <th width="49" align="left">Notes</th> </tr> <? while ($row = mysql_fetch_row($result)) { ?> <tr> <td align="left"><input type="checkbox" name="select" value="<?=$row[2]?>"></td> <td align="left"><?=$row[2]?></td> <td align="left"><?=$notes?></td> <td align="left"><?=$row[4]?></td> <td align="left"><?=$row[5]?></td> </tr> <? $query = "UPDATE theater SET notes = '$notes' where number = '3' "; } ?> I'm struggling as someone new to PHP. Any help is appreciated! Quote Link to comment Share on other sites More sharing options...
fnairb Posted February 26, 2008 Share Posted February 26, 2008 Not familiar with the: <?=$row[4]?> notation in PHP but if it is working for you kudos. I would be using something along the lines of <?php echo $row[4]; ?>. Where are you setting the value for $remaining? Where you have it now the value for $notes will be the same for all rows. If I understand what you are trying to do the test needs to be inside the while loop as you run through the data. The logic of your if/else appears to be fine but you need to be testing $row[4] each time through. <?php while ($row = mysql_fetch_row($result)) { if ($row[4] > 0) { $notes = 'Availble'; } else { $notes = 'Sold Out'; } .... Quote Link to comment Share on other sites More sharing options...
lala Posted February 26, 2008 Author Share Posted February 26, 2008 That is exactly what I needed. Many many thanks. Quote Link to comment 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.