Page 1 of 1

Wh does my output echo twice?

Posted: Thu Mar 09, 2006 5:27 am
by mattcooper
Hi,



I've written this code to output the items in a user's shopping cart in basic format for now, andit works fine except that it echoes the output twice and I can't figure out why.


Code: Select all

$oid=$_GET['order_id'];
$sql="SELECT items FROM orders WHERE order_ref='$oid'";
$result=mysql_query($sql);
$i=mysql_fetch_array($result);

foreach($i as $key => $value){

$items=explode(",",$value);
sort($items);
//For ech of the results returned by the above...
foreach($items as $value){

//...grab the details of each item in $value

$sql="SELECT * FROM catalogue WHERE prod_id='$value'";
$result=mysql_query($sql);
$num=mysql_num_rows($result);

//display the results
$i=0;
while ($i<$num){
$prodid=mysql_result($result,$i,"prod_id");
$prodname=mysql_result($result,$i,"prod_name");
$price=mysql_result($result,$i,"price");
//$short_desc=mysql_result($result,$i,"short_desc");
echo "$prodid, <b>$prodname</b>, $price<br>";
$i++;
//}
}
}
}


Can anyone see what's going wrong here? I'm baffled...



Thanks in advance,



Matt

Posted: Thu Mar 09, 2006 6:13 am
by phpScott
nothing jumps out at me, what does $num equal?

Posted: Thu Mar 09, 2006 6:18 am
by matthijs
Can't exactly see what's going on but you can start by setting error reporting on and echo'ing out every variable and array in your code. So after each line echo the results. Then you know very quick what's going on.

By the way, do you do any input filtering on $_GET['order_id']?

Posted: Thu Mar 09, 2006 8:29 am
by feyd
mysql_fetch_array() by default returns not only numeric but named indexes of each field you've selected. To see what I mean, var_dump($i)

Posted: Thu Mar 09, 2006 10:48 am
by mattcooper
matthijs wrote:By the way, do you do any input filtering on $_GET['order_id']?
Haven't applied any filtering to the $_GETs yet, but will once I can get this problem fixed.

Feyd: I have performed a var_dump on $i and the result was int(1). I'm not sure how this explains the duplication of the output - can you elaborate?

Posted: Thu Mar 09, 2006 10:52 am
by feyd
You have two different uses of $i. The first one is the one I was talking about, not the second. ;)

Posted: Thu Mar 09, 2006 11:08 am
by mattcooper
var_dump on the first $i returns the following:

array(2) { [0]=> string(23) "1,1,1,1,3,3,3,3,3,3,3,3" ["items"]=> string(23) "1,1,1,1,3,3,3,3,3,3,3,3" }

Does this help out? Thanks for your time so far... :D

Posted: Thu Mar 09, 2006 11:11 am
by Burrito
notice the association and numeric representation of the array?

Posted: Thu Mar 09, 2006 11:25 am
by mattcooper
This is new territory for me, I'm not too hot with arrays to be honest... it looks as though there are two arrays, one on [0] and one on [items]. It looks like MySQL is returning an array for another field...

How can this be eliminated?

Wish I was better at this... :oops:

Posted: Thu Mar 09, 2006 11:30 am
by Burrito
hint: second argument in mysql_array()

MYSQL_NUM, MYSQL_ASSOC, MYSQL_BOTH

Posted: Thu Mar 09, 2006 11:43 am
by mattcooper
Burrito wrote:hint: second argument in mysql_array()
mysql_fetch_assoc($) does the trick for this... I will do the right thing now and read the manual thoroughly!! At least I'll understand why...

Thank you for your help everyone...

:)