Re: irrational behavior

From: Date: Tue, 12 May 2009 15:19:32 +0000
Subject: Re: irrational behavior
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-292492@lists.php.net to get a copy of this message
Could somebody please explain this? When the line - sort($category) is commented out, the output returns Notice: Undefined offset: in the line "36" for all the repeats (29 in this case) The code below: <? $SQL = "SELECT name
        FROM categories ORDER BY category_id
        ";
$category = array(); if ( ( $results = mysql_query($SQL, $db) ) !== false ) {
    while ( $row = mysql_fetch_assoc($results) ) {
        $category[$row['name']] = $row;
        }
//sort($category); $count = mysql_num_rows($results); $lastIndex = ceil($count/2 -1); //echo $lastIndex; $ii = 0; while ($ii <= $lastIndex) {
    $cat = $category[$ii]['name']; //=======this is line 36==========
    $catn = preg_replace("/[^a-zA-Z0-9]/", "", $cat);
    echo "<a href='../categories/", $catn, ".php'>", $cat, "</a><br />";
    $ii++;
    }
$ii = $lastIndex; //echo $category[$ii]['category']; while ($ii <= $count -1) {
    $cat = $category[$ii]['name'];
    $catn = preg_replace("/[^a-zA-Z0-9]/", "", $cat);
    echo "<a href='../categories/", $catn, ".php'>", $cat, "</a><br>" ;
    $ii++;
    }
echo "</td></tr></table>" ; } ?> The same phenomenon happens in another application using the identical code. I don't want to sort the category; that has been taken care of in the query. It just doesn't make sense that sorting would affect the count. :-( Well a wild guess is this:
This function assigns new keys to the elements in /array/ . It will remove any existing keys that may have been assigned, rather than just reordering the keys http://www.php.net/manual/en/function.sort.php -- Thodoris

« previous php.general (#292492) next »