Re: [PHP] Re: irrational behavior

2009-05-12 Thread PJ
Shawn McKenzie wrote:
> PJ wrote:
>   
>> 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)
>> 
>
> Sure it makes sense and is rational, you just have no idea what you're
> doing :-)
>
>   
You're absolutely right! :-)
>> 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) ) {
>> 
>
> You assign your categories to an associative array, so it might look
> like this $category['bob'] = array
>
>   
>> $category[$row['name']] = $row;
>> }
>> //sort($category);
>> $count = mysql_num_rows($results);
>> $lastIndex = ceil($count/2 -1); //echo $lastIndex;
>> $ii = 0;
>> while ($ii <= $lastIndex) {
>> 
>
> But then you try and loop through them like they are numerically
> indexed, like this $category[0]['name'] = array
>
> So either change this part to foreach through the associative array, or
> change the assignment above to $category[] = $row;
>
>   
>> $cat = $category[$ii]['name']; //===this is line 36==
>> $catn = preg_replace("/[^a-zA-Z0-9]/", "", $cat);
>> echo "", $cat, "";
>> $ii++;
>> }
>>
>> $ii = $lastIndex;
>> //echo $category[$ii]['category'];
>> while ($ii <= $count -1) {
>> 
>
> You will get the same problems here.  Make one of the changes suggested
> above.
>
>   
>> $cat = $category[$ii]['name'];
>> $catn = preg_replace("/[^a-zA-Z0-9]/", "", $cat);
>> echo "", $cat, "" ;
>> $ii++;
>> }
>> echo "" ;
>> }
>> ?>
>>
>> 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. :-(
>>
>> 
>
> The sorting actually re-indexes your associative array into a numerical
> one that makes the code that follows work more correctly.
>   
Thanks much. This helps to understand the arrays.

-- 
Hervé Kempf: "Pour sauver la planète, sortez du capitalisme."
-
Phil Jourdan --- p...@ptahhotep.com
   http://www.ptahhotep.com
   http://www.chiccantine.com/andypantry.php


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: irrational behavior

2009-05-12 Thread Shawn McKenzie
PJ wrote:
> 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)

Sure it makes sense and is rational, you just have no idea what you're
doing :-)

> 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) ) {

You assign your categories to an associative array, so it might look
like this $category['bob'] = array

> $category[$row['name']] = $row;
> }
> //sort($category);
> $count = mysql_num_rows($results);
> $lastIndex = ceil($count/2 -1); //echo $lastIndex;
> $ii = 0;
> while ($ii <= $lastIndex) {

But then you try and loop through them like they are numerically
indexed, like this $category[0]['name'] = array

So either change this part to foreach through the associative array, or
change the assignment above to $category[] = $row;

> $cat = $category[$ii]['name']; //===this is line 36==
> $catn = preg_replace("/[^a-zA-Z0-9]/", "", $cat);
> echo "", $cat, "";
> $ii++;
> }
> 
> $ii = $lastIndex;
> //echo $category[$ii]['category'];
> while ($ii <= $count -1) {

You will get the same problems here.  Make one of the changes suggested
above.

> $cat = $category[$ii]['name'];
> $catn = preg_replace("/[^a-zA-Z0-9]/", "", $cat);
> echo "", $cat, "" ;
> $ii++;
> }
> echo "" ;
> }
> ?>
> 
> 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. :-(
>

The sorting actually re-indexes your associative array into a numerical
one that makes the code that follows work more correctly.

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: irrational behavior

2009-05-12 Thread Nathan Rixham

PJ wrote:

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:
", $cat, "";
$ii++;
}

$ii = $lastIndex;
//echo $category[$ii]['category'];
while ($ii <= $count -1) {
$cat = $category[$ii]['name'];
$catn = preg_replace("/[^a-zA-Z0-9]/", "", $cat);
echo "", $cat, "" ;
$ii++;
}
echo "" ;
}
?>

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



what your doing is:
$category[$row['name']]

which sets
$category['some name']

an associative array.

now when you do
sort($category);
you're sorting all results and assigning them to numerical indexes..

so now $category contains:
$category[0]
$category[1]
$category[2]
...

which is what you need seeing as you are cycling them as a numerical 
array. so sort is turning your associative array in to the needed 
indexed array.


if you remove sort($category) and then change this line:
$category[$row['name']] = $row;
to
$category[] = $row;
you'll find it works without error.

regards,

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php