Jump to content


Photo

urgent help required!


  • Please log in to reply
5 replies to this topic

#1 id123

id123
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 14 July 2006 - 10:10 AM

Hi, i have a shopping basket that uses a for loop to show contents of the basket. There is then a qty field for each item with the name of the field being qty and the value of $i in the for loop. I need to be able to refer to this field on the next page using the same loop to show the final values.

The problem i have is being able to reference qty1 for example. How can i get the form field of name qty$i?

Here is an example of code from the second page
<? 
for ($i=1 ; $i <= $_SESSION["item_count"]; $i++) {
$item_cost = $_SESSION["item_cost[$i]"];

?>
<tr>

	<td><?php echo $_SESSION["items_tray[$i]"]; ?></td>
	<td align=center><?php echo $_SESSION["quantity[$i]"]; ?></td>
	<td align=center>£<?php echo number_format("$item_cost","2",".",","); ?></td>
	<td align=center>£<? echo (number_format(($item_cost * $qty$i),"2")); ?></td>
</tr>
<? } ?>

obviously this doesnt work, can someone help me as im really stuck!



#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 14 July 2006 - 10:29 AM

You'll want to change this:
$_SESSION["item_cost[$i]"]; to this:
$_SESSION['item_cost'][$i];

Also you'll want to change the other session vars too to $_SESSION['sessname'][$i]

#3 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 14 July 2006 - 10:31 AM

I'm not entirely sure if you can use square brackets like you have within your session array. You could try one of the following:
<?php
$_SESSION["quantity$i"];
$_SESSION["quantity".$i];
$_SESSION["quantity{$i}"];
?>
EDIT: Or, yes, use wildteen88s method if you have a multi-dimensional array, I may have misunderstood what you were trying to achieve.

#4 id123

id123
  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 14 July 2006 - 10:32 AM

hi thanks for the reply,

the sesion variables are working fine. its the $qty$i line that is causing me problem. i need to be able to retrie the form field qty+counter of the loop ($i)



#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 14 July 2006 - 10:36 AM

Is qty an array? If it use this: $qty[$i] instead

#6 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 14 July 2006 - 10:48 AM

I think he's trying to use a variable variable. Here's an example of how you might go about this:
<?php
$qty0 = 4;
$qty1 = 8;
$qty2 = 16;

for($i=0; $i<=2; $i++) {
    $q = "qty".$i;
    echo $$q;
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users