Jump to content

Simple array trouble


zero477

Recommended Posts

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

 

}; ?>

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/252168-simple-array-trouble/
Share on other sites

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.

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?

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.