Jump to content

Something stupidly simple, yet not working!


pete212

Recommended Posts

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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.