Darkwoods Posted February 15, 2010 Share Posted February 15, 2010 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 />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/192138-showing-and-disabling-specific-data-from-mysql/ Share on other sites More sharing options...
MadTechie Posted February 15, 2010 Share Posted February 15, 2010 Add to the WHERE AND ID NOT IN (2,5) OR at the start of a loop (after setting $id) add if($id == 2 || $id == 5) continue; Quote Link to comment https://forums.phpfreaks.com/topic/192138-showing-and-disabling-specific-data-from-mysql/#findComment-1012605 Share on other sites More sharing options...
gamblor01 Posted February 15, 2010 Share Posted February 15, 2010 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"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/192138-showing-and-disabling-specific-data-from-mysql/#findComment-1012615 Share on other sites More sharing options...
MadTechie Posted February 15, 2010 Share Posted February 15, 2010 Well written gamblor01, ~Cougths~@Typo if ($id !=2 && id !=5) should be if ($id !=2 && $id !=5) Quote Link to comment https://forums.phpfreaks.com/topic/192138-showing-and-disabling-specific-data-from-mysql/#findComment-1012619 Share on other sites More sharing options...
gamblor01 Posted February 15, 2010 Share Posted February 15, 2010 D'oh! Thanks for spotting that MadTechie. I have fixed it now (and also added the <?php ?> tags for syntax highlighting). Note however, that I might need to "cougth" at your typo too. Quote Link to comment https://forums.phpfreaks.com/topic/192138-showing-and-disabling-specific-data-from-mysql/#findComment-1012624 Share on other sites More sharing options...
Darkwoods Posted February 15, 2010 Author Share Posted February 15, 2010 Thank you guys, phpfreals.com helped me again today thanks for being so nice with helping people this code works perfect Quote Link to comment https://forums.phpfreaks.com/topic/192138-showing-and-disabling-specific-data-from-mysql/#findComment-1012640 Share on other sites More sharing options...
gamblor01 Posted February 15, 2010 Share Posted February 15, 2010 You're very welcome...glad to hear it works! If you are unfamiliar with the && and || operators, you will probably want to look them up in further detail. They are tremendously useful. Quote Link to comment https://forums.phpfreaks.com/topic/192138-showing-and-disabling-specific-data-from-mysql/#findComment-1012842 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.