If variable is marked 1, show text

I have products that if marked SOLD, I need it to show sold on the website.

I have it to where if the item is marked sold it is labled as 1 in the database and if its not sold its marked as 0.

How can I make it echo Sold on the website?

I read something like this would work but I dont know how to put it in my html.

<?php
if (isset($variable))
{
?>
YOUR BLOCK OF CODE HERE
<?php 
} 
?>

My html code is pretty simple, I have this at the top
PHP

<?php
 include_once('../mysql_connect.php');
 if(isset($_POST['marksold']) && isset($_POST['id']))
 {
     mysql_query("UPDATE new_equip SET sold='1' WHERE id='" . mysql_real_escape_string($_POST['id']) . "'");
 } 
?>

html

<td><?php echo $itemname; ?></td>

You could use something like

<?php echo (isset($variable) ? "SOLD: " : "") + $itemname; ?>

Try a switch statement. That way you can control the output

$sold = “”;
switch( $data )
{
case 1:
$status = “sold”;
break;

case 0:
$status = “available”;
break;
}

Beyond that you can add a default for any value not 1 or 0 just in case you have a wrong insert.

Just a few things to note about your code:

  1. The old mysql_* extension was depreceated as of version 5.5 of PHP, you should be migrating over to either the mysqli_* extension or PDO.
  2. Once you’ve migrated over to either the mysqli_* extension or PDO you should be using prepared statements when sending queries to the database that include user submitted data.
  3. When dealing with user submitted data you should be sanitizing it to make sure that it’s what you expect. I’m assuming that the id field is a numeric type, when processing the $_POST[‘id’] you could use the is_integer() function to check if the user has submitted a number.
  4. Whether you migrate over to either the mysqli_* extension or PDO you should use exceptions (try-catch blocks) to handle any errors that get thrown back at you by the MySQL server.

I tried this code and didnt work. I keep looking for ways to do this but cannot find anything.

He did say “something like” :wink:

Other than a misplaced parenthesis and a math operator for concatenation, I’m not sure you can echo an isset like that, at least I’ve never tried to.

Maybe this would be a bit better?

<?php
$sell_state = (isset($variable)) ? "SOLD: " : ""; 
echo $sell_state . $itemname;
?>

Yes, you are asking how to display an item as sold but you really haven’t shown your display query and the code for that section. In the examples given in this thread, the $variable would represent $row[‘sold’] in your result set. So make sure you have sold as a selected field and try implementing one of these examples. Reply with your query and display section if you have problems.

EDIT: I also wanted to note that isset() might not be your best or complete solution as $row[‘sold’] WILL BE set if you are calling that field. I would also compare to your expected value, which was 1. Using Mittineague’s example from above, you have something like this.

<?php
$sell_state = (isset($row['sold']) && $row['sold'] == 1 ? "SOLD: " : "");
echo $sell_state . $row['itemname'];
?>

@Drummin Would an if statement work just as well here?

Changing your example a bit:

<?php
         //      Will be empty until set
         $sold_status;
         //      Check if row exists and if value is 1
         if(isset($row["sold"]) &&  $row["sold"] == 1){
              $sold_status = "Sold!";
         } else {
              $sold_status = "Not sold";      
         }
         echo  $row["item"] . " is " . $sold_status;
?>

This worked perfect. Thank You

Sure, you could do it that way. But you really don’t need this part

//      Will be empty until set
         $sold_status;

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.