Simple array problem help needed

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
zero477
Forum Newbie
Posts: 4
Joined: Wed Nov 23, 2011 11:17 am

Simple array problem help needed

Post 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

}; ?>
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Simple array problem help needed

Post 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
Post Reply