pete212 Posted April 24, 2009 Share Posted April 24, 2009 Hi everyone, Ok so i am expirmenting coding a PHP script for a shopping wesbite. The user ticks checkboxes that relate to products, and then on the next page is when my code runs: $map = $_REQUEST['map']; $n = count($map); $i = 0; $total = 0; [color=blue]$Cart = array($ID,$desc,$price);[/color] echo " \r\n" . "<ol>"; while ($i < $n) { echo "<li>{$map[$i]}</li> \r\n"; [color=blue] $Cart[0][$i] = $map[$i]; echo $Cart[0][$i] . " "; [/color] $query = "SELECT PRODUCTNAME FROM PRODUCT where PRODUCTID =\"$map[$i]\" "; $result = mysql_query ($query, $con); while ($row = mysql_fetch_row($result)) { //.....print out each element on $row for ($x=0; $x<mysql_num_fields($result); $x++) echo $row[$x] . " "; [color=red] $Cart[1][$x] = $row[$x]; echo $Cart[1][$x]; [/color] echo "£"; } $query = "SELECT PRICE FROM PRODUCT where PRODUCTID =\"$map[$i]\" "; $result = mysql_query ($query, $con); while ($row = mysql_fetch_row($result)) { //.....print out each element on $row for ($x=0; $x<mysql_num_fields($result); $x++) $price = (int)($row[$x]); $price = $price / 100; echo $price . " "; [color=blue] $Cart[2][$x] = (int)($price); echo $Cart[2][$x]; [/color] $total = $total + (int)($price); echo "<br>"; } $i++; } echo "</ol>"; echo "Total Price: £ "; echo $total; As you can see, it connects to the database and queries the product ID, the product description and the product price, each with a for loop to echo out the result on the screen. This works fine. Of course, i want these to be read in something permanent, rather than just the "$i" and "$x" that only exist for that 'loop' of the for loop. So i created an array. For testng purposes the code tries to write the $x variable into the array and then echo it out. (I know at the moment this results in everything being echoed out twice - its just there until i know the array is working.) The sections that are highlighted in blue work correctly, IE the product ID and price. However the product description (highlighted in red) doesnt echo out anything and i dont know why. I thought it was because its a string rather than an int but i tried $Cart[1][$x] = (string)($row[$x]); as well but that didnt work either. Am i missing sonething really simple? Why will the array accept the ints but not the strings? Link to comment https://forums.phpfreaks.com/topic/155489-something-stupidly-simple-yet-not-working/ Share on other sites More sharing options...
revraz Posted April 24, 2009 Share Posted April 24, 2009 I don't know why you just don't do this with 1 query and make it easy on yourself. Read a few tutorials on mysql and PHP. Link to comment https://forums.phpfreaks.com/topic/155489-something-stupidly-simple-yet-not-working/#findComment-818218 Share on other sites More sharing options...
NightMonkey Posted April 24, 2009 Share Posted April 24, 2009 Does the echo above the line "$Cart[1][$x] = $row[$x];" echo anything out? Because there is nothing wrong with code highlighted red. Link to comment https://forums.phpfreaks.com/topic/155489-something-stupidly-simple-yet-not-working/#findComment-818228 Share on other sites More sharing options...
radi8 Posted April 24, 2009 Share Posted April 24, 2009 What happens with the echo $row[$x] . " ";? Does this echo correctly? Link to comment https://forums.phpfreaks.com/topic/155489-something-stupidly-simple-yet-not-working/#findComment-818234 Share on other sites More sharing options...
Potatis Posted April 24, 2009 Share Posted April 24, 2009 What is wrong with my screen that I can't see any blue or red highlighting in the code? Just back code on a grey background? Link to comment https://forums.phpfreaks.com/topic/155489-something-stupidly-simple-yet-not-working/#findComment-818238 Share on other sites More sharing options...
pete212 Posted April 25, 2009 Author Share Posted April 25, 2009 Hi everyone, Yes the echo did display correctly, but its ok i've solved it now. The problem was due to the whole WHILE loop being flawed. I didnt actually need the seperate WHILE loops for each query or the FOR loops! Link to comment https://forums.phpfreaks.com/topic/155489-something-stupidly-simple-yet-not-working/#findComment-818992 Share on other sites More sharing options...
FaT3oYCG Posted April 25, 2009 Share Posted April 25, 2009 What is wrong with my screen that I can't see any blue or red highlighting in the code? Just back code on a grey background? just incase you still want to know it was because he used code tags and didnt put the opening and closing php tags in his code, he could have used php tags though whick would highlight the code correctly without having the opening and closing php tags Link to comment https://forums.phpfreaks.com/topic/155489-something-stupidly-simple-yet-not-working/#findComment-819025 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.