arrays

    Date: 03/12/06 (PHP Community)    Keywords: mysql, database, sql

    Hi,

    I'm sure there's a better way than the one I'm using to do what I want to do.
    I'm trying to select items from a database and order them by "category", which is one of the columns of the table I use.
    Then, I want the page to display the items with the category as a title, appearing only once.
    Hopefully someone can help me improve my code :)


    Here's what the table looks like :
    ____________________________
    | category | year | title | stuff |
    ------------------------------------------
    | theatre | year1 | title1| ... |
    -----------------------------------------
    | movie | year2 | title2 | ... |


    Here's what I want :

    theatre
    year1 title1
    year2 title2
    etc


    movies
    year1 title1
    year2 title2
    etc


    Here's the code I use :
    $categorie = mysql_query("SELECT categorie FROM $nom",$connexion) or die(mysql_error());
    $nb = mysql_num_rows($categorie);
    for ($i = 0; $i<$nb; $i++) {
    	$perf = mysql_result($categorie,$i);
    	$item[] = $perf;
    }
    $reduc = array_unique($item); 
    	foreach($reduc as $genre) {
    		echo "
    "; echo "\n$genre"; echo "\n"; $liste = mysql_query("SELECT * FROM $nom WHERE categorie LIKE '" . $genre . "' ORDER BY annee DESC",$connexion); while ($row = mysql_fetch_assoc($liste)) { $annee = $row['annee']; $titre = $row['titre']; $complement = $row['complement']; echo "\n "; } echo "\n
    $annee$titre$complement
    "; echo "
    "; }


    Any help would be greatly appreciated !

    Source: http://community.livejournal.com/php/424221.html

« checking a variable against... || Might be a daft question.. »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home