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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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