Jump to content

Retrieve Name For Id In Cart


son

Recommended Posts

Having had my simple shopping cart application working in principle (adding/updating/removing items) I am now surprised to find that my addition to display the colour name for each product (if passed in query string) confuses rest of code and the values for quantities do not display correctly any more. The code I inserted is:

$fn_name = $_SESSION['cart'][$row['product_id']]['fn'];		if ($fn_name != 0)		{		$fn_query = "SELECT colour FROM colours WHERE colour_id  = $fn_name";		$fn_result = mysqli_query ($dbc, $fn_query);		$row=mysqli_fetch_assoc($fn_result); 			$fn_name2 = $row['finish'];			echo "Color: " . $fn_name2;		}

The code before, which worked without fault was:

		$fn_name = $_SESSION['cart'][$row['product_id']]['fn'];		if ($fn_name != 0)		{		$fn_query = "SELECT color FROM colours WHERE color_id  = $fn_name";		$fn_result = mysqli_query ($dbc, $fn_query);			if (mysqli_num_rows($fn_result) > 0)			{			echo "<br />Colour: {$_SESSION['cart'][$row['product_id']]['fn']}";			}		}

It works to 'hard-code' a loop as:

if ($fn_name == 1)				{				echo 'Colour: Black';				}				elseif ($fn_name == 2)				{				echo 'White';				}

but for obvious reasons that would not be a good idea. Colour might change in database (they can be changed from admin interface).How can I display the name, but not upset the rest of coding? SonReason for edit: Forgot to include the info about having a hard-coded loop to look up names...

Link to comment
Share on other sites

If it's not displaying the quantity correctly, isn't that the code to look at?
I would not think so as it works flawless when the problematic code is exchanged for working code (all given in post). Therefore, the mistake must be in the problematic code, especially the bit:$row=mysqli_fetch_assoc($fn_result); (please correct me if I am wrong). On item page I pass a number in querystring (integer) which is id for colour. Now, on shopping basket page I would like to display the choosen colour name. Therefore, I run a query to lookup the name for passed colour id... Quantity is stored in session as: $_SESSION['cart'][$row['product_id']]['qty'];, so I do not get why the colour code upsets the quantity code. This might be what you meants to explain, but I just did not understand...Son
Link to comment
Share on other sites

There's nothing in that code that affects the quantity value, if the quantity is not printing correctly then that code is the problem, not the code that's here.
I know it sounds strange, but taking the code out: All fine. Putting it in: not working (although it has nothing to do whatsover with quantity). This what I do not understand. For now I 'hard-coded' the loop as:
if ($fn_name == 1){echo 'Colour: Black';}elseif ($fn_name == 2){echo 'White';}

But will have to come up with a better solution...Son

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...