Jump to content

Showing and disabling specific data from mysql


Darkwoods

Recommended Posts

Hello im trying to disable some specific $id values from showing in the page lets say the values are number 2 and 5 that i want to disable and all the rest to show as normal...

 

i want the data to be on the page when i have it only on the id number 2 or 5

$id = $_GET['id'];

              if($id == '2,5') { }

 

how can i do that please?

 

here is the code im using


$result = mysql_query("SELECT * FROM cat_foodmenu ",$connect);
while($row = mysql_fetch_array($result))
{
			 $id = $row['id'];
			 $title = $row['title'];
        
        echo '<h1>'. $title . '</h1>'; 
	echo '</h1><img class="section_img" src="images/index_img.jpg" alt="Menu" />';


	$result2 = mysql_query("SELECT * FROM food_menu WHERE cat_id='$id' ORDER BY id ASC",$connect);
        while ($row = mysql_fetch_array($result2))
        { 
        
        $id = $row['id'];
        $food_title = $row['title']; 
        $price = $row['price'];
	$info = $row['info'];
                
        echo '<div class="recipe"><span class="name_of_the_recipe">' .$food_title .'</span>';
	echo '<div class="price">$' . $price .'</div>';
	echo '<div class="recipe_bg"> </div><div class="clear_container"></div></div>';
	echo '<p>(' . $info .')</p><br />';

        }
}

I believe what you are asking about is a simple OR statement?  You cannot do it as you have written though...it's not like you can test whether id is in the set '2,5' (though you could do this in a WHERE clause like 'AND NOT IN (2,5)').  The == operator will expect exactly one value on both sides.  Therefore this statement is incorrect:

 

<?php
if ($id == '2,5')
{
  // do stuff
}
?>

 

 

You need to test each value individually with an OR statement (written || ), so the following should work:

 

<?php
if ($id == 2 || $id == 5)
{
  // do stuff
}
?>

 

 

If you want to print every row EXCEPT rows 2 and 5 then you can either negate the OR statement, or you can distribute the negation over the entire statement (using DeMorgan's Law) to obtain an AND (&&) statement.  Both of the if tests below would be acceptable to print everything except for rows 2 and 5:

 

<?php
if (!($id == 2 || $id == 5))
?>

 

<?php
if ($id != 2 && $id != 5)
?>

 

 

So if you want to print every row except 2 and 5 in a table called foo (let's assume it only has 2 columns: id and name), then I would expect the code to look something like this:

 

<?php
$result = mysql_query("SELECT * FROM foo");
while($row = mysql_fetch_array($result))
{
  $id = $row['id'];

  // don't print rows 2 and 5
  if ($id !=2 && $id !=5)
  { 
    $name = $row['name'];
    echo "$id - $name";
  }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.