Jump to content

Blank table value based on another value


lala

Recommended Posts

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
Share on other sites

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
Share on other sites

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.