Page 1 of 1

Simple array problem help needed

Posted: Wed Nov 30, 2011 7:09 pm
by zero477
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

}; ?>

Re: Simple array problem help needed

Posted: Wed Nov 30, 2011 7:37 pm
by twinedev
Try the following. Since you only listed one field from the results that your query would give that is what I told it to select, no need to retrieve more data than you are going to use.

Code: Select all

$aryCategories = array();
$SQL = 'SELECT DISTINCT `Clasification` FROM `LugaresTuristicos` WHERE `DestinoPeg`="'.mysql_real_escape_string($LinkDestino).'"';
$rsCategories = mysql_query($SQL);
if ($rsCategories && mysql_num_rows($rsCategories)>0) {
    while ($aryTemp = mysql_fetch_assoc($rsCategories)) {
        $aryCategories[] = $aryTemp['Clasificacion'];
    }
    mysql_free_result($rsCategories);
}
unset($rsCategories,$SQL);

print_r($aryCategories); //  Should be an array of items