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! Link to comment https://forums.phpfreaks.com/topic/93091-blank-table-value-based-on-another-value/ 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'; } .... Link to comment https://forums.phpfreaks.com/topic/93091-blank-table-value-based-on-another-value/#findComment-476967 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. Link to comment https://forums.phpfreaks.com/topic/93091-blank-table-value-based-on-another-value/#findComment-476976 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.