zero477 Posted December 1, 2011 Share Posted December 1, 2011 Hello everyone, I have a question with the use of the functon in_array... I have a list of categories in my table and I want to know if a specific category exists so I am doing this: $categoriasexistentes= mysql_query("SELECT * FROM LugaresTuristicos WHERE DestinoPeg='$LinkDestino' GROUP BY Clasificacion ") or die(mysql_error()); $array = mysql_fetch_array($categoriasexistentes); print_r($array); WHAT IS DISPLAYED BY print_r (there should be much more categories) IS THIS: Array ( [0] => Bar [Clasificacion] => Bar ) If I ad this all categories are displayed: while($categoria = mysql_fetch_array($categoriasexistentes)) { $todas=$categoria['Clasificacion']; ?> <?php echo"{$todas} - "; ?> <?php }; ?> Quote Link to comment https://forums.phpfreaks.com/topic/252168-simple-array-trouble/ Share on other sites More sharing options...
Pikachu2000 Posted December 1, 2011 Share Posted December 1, 2011 That can't be the output from that query. Quote Link to comment https://forums.phpfreaks.com/topic/252168-simple-array-trouble/#findComment-1292874 Share on other sites More sharing options...
scootstah Posted December 1, 2011 Share Posted December 1, 2011 Please use code tags. I don't really know what the question is here. When you have multiple rows returned you have to loop through them. If you just do $array = mysql_fetch_array() it's only going to return one row. So you have to loop through them. If you want to see if a specific category exists you can do one of these two things: 1. Select all rows and use in_array: $query = mysql_query("SELECT * FROM categories"); $categories = array(); while($row = mysql_fetch_assoc($query)) { $categories[] = $row; } if (in_array('specific category', $categories)) { // category exists } 2. Select only the specific row and then see if any rows were returned $query = mysql_query("SELECT * FROM categories WHERE category='specific category'"); if (mysql_num_rows($query) > 0) { // category exists } Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/252168-simple-array-trouble/#findComment-1292901 Share on other sites More sharing options...
zero477 Posted December 1, 2011 Author Share Posted December 1, 2011 I am sorry, I am still not able to fix this problem. I have 1000 rows with products divided into 10 categories (one column called categories). This products are displayed in 10 different stores with a specific url. I have to something if a category exists within each store. For example: San Antonio Store has X category of products with a promotion. or Houston Store has X category of products. Houston Store has Y category of products. How do I do this? Quote Link to comment https://forums.phpfreaks.com/topic/252168-simple-array-trouble/#findComment-1293140 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.