Wh does my output echo twice?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Wh does my output echo twice?

Post 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
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

nothing jumps out at me, what does $num equal?
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post 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']?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You have two different uses of $i. The first one is the one I was talking about, not the second. ;)
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

notice the association and numeric representation of the array?
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post 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:
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

hint: second argument in mysql_array()

MYSQL_NUM, MYSQL_ASSOC, MYSQL_BOTH
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post 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...

:)
Post Reply