Forum Moderators: coopster

Message Too Old, No Replies

Array incompatibility?

this must be some kind of bug ....

         

locdog_i

7:28 pm on Jan 29, 2006 (gmt 0)

10+ Year Member



ok ... i have this script:

$num_items = 198;
for ($i = 1; $i < $num_items; $i++) {
$id = 103;
$cat_id[$i] = $id;
print $cat_id[$i]; // this returns: '1'
}

so ... inside the for, that print returns '1'!

I don't understand why that array can't take the real value of $id for the key $i.

grandpa

1:32 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your code returns the value of 103 for me. Maybe you have some formatting on the output that restricts the size of the field, so that you are only viewing the first digit?

Salsa

3:21 am on Jan 30, 2006 (gmt 0)

10+ Year Member



Don't know. I didn't test it, but it looks like it should assign the value of 103 to each member of the $cat_id array.

A couple of things for you to be aware of, though, is that your for loop will only count up to 197 unless you change "$i < $num_items;" to "$i <= $num_items;" or initially set "i = 0;".

Also, if you mean for the value of $id to remain a constant 103, as it is now, you ought to put the assingment before your for loop. As it is, you are unnecessarily assigning the value "$id = 103;" over and over again, 197 times.

To see just what's in your array after you've built it with your for loop, try temporarilly adding this line after you close the loop:

?><pre><?php echo $print_r($cat_id);?></pre><?php

Sorry I couldn't answer your specific question, but I hope this helps.

Salsa

jatar_k

4:44 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



good answer Salsa sorry I have one thing though

<?php echo $print_r($cat_id);?>

should be just

<?php print_r($cat_id);?>

;)

Salsa

5:39 am on Jan 30, 2006 (gmt 0)

10+ Year Member



Thanks Jatar. Of course you're right, the echo doesn't belong in there. I actually paused over it as I was typing, but I've called that line from my snippet library so many times without looking at anything but the variable that I've become blind to it, and it came out of my fingers wrong. As it happens, the echo doesn't seem to change the results except that in the test I just ran it makes a "1" appear after the array. I wonder what that's about?

Salsa

coopster

2:46 pm on Jan 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



bool print_r [php.net] ( mixed expression [, bool return] )

print_r() will return a boolean (

TRUE/FALSE
) value. So when you echo the value returned by this function it is showing you the "1" or
TRUE
value returned upon success.

Salsa

6:25 pm on Jan 30, 2006 (gmt 0)

10+ Year Member



Ah-- Perfect sense. Thanks Coopster.

locdog_i

7:26 pm on Jan 30, 2006 (gmt 0)

10+ Year Member



the script was a litle bit more complicated and I simplified it, but here is the whole thing:


for ($i = 1; $i <= $num_items; $i++) {


$query2 = "SELECT cat_id FROM tbl_category WHERE cat_name = '$cat_name[$i]'" ;
$result2 = mysql_query($query2) or die("Query Error: ".mysql_error());
$row2 = mysql_fetch_array($result2);

if (mysql_num_rows($result2) > 0) {

extract($row2);
$id = $cat_id;

$print $id; // this returns '103 1' (I don't know why, the value of that field in the DB is 103)

$print $id; // this returns '103'

$cat_id[$i] = $id;
print $cat_id[$i]; // this returns '1'

$query2= "..."; // I ain't gonna write it becouse it's irelevant, but I use the $cat_id[$i] in here
mysql_query($query2) ;

I already solved the problem by not using an array anymore ... i just used the $id variable in the second query.
eventhough ... i still don't get it what the problem was ...

jatar_k

8:34 pm on Jan 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try the exact same snippet as above but remove the $ from in front of those 2 prints

$print $id;

should be

print $id;

locdog_i

11:26 am on Jan 31, 2006 (gmt 0)

10+ Year Member



sorry, that was a typing error ...

tomda

11:37 am on Jan 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



for ($i = 1; $i <= $num_items; $i++) {
$query2 = "SELECT cat_id FROM tbl_category WHERE cat_name='$cat_name[$i]'" ;
$result2 = mysql_query($query2) or die("Query Error: ".mysql_error());

if (mysql_num_rows($result2)!="0") {
while ($row2=mysql_fetch_array($result2)){
$id=$row2[cat_id];
print $id;

// SIMPLY USED ID IN YOUR QUERY3 (NOTE THAT YOU BETTER
// CHANGE NAME TO QUERY3, RESULT3 AND ROW3 SO THAT YOU
// DO NOT OVERWRITE THE FIRST QUERY
$query3 = "SELECT * FROM #*$!x WHERE #*$!xx='$id'" ;
}}}