PDA

View Full Version : Populating an array problem !!


wap3
11-28-2002, 11:58 AM
Hello

Ok. I have an array which is stored as a session:

$items = array();
session_register("items");

How can I add items to this array which come from the url. If there is not a matching item in the array it should add one, but if there is one it should increase the value by 1.

This is what I have got now. I can add one item to it but if I try to add another it won't work. Nor will it increase the value by one when the item is already there.


if($items[$_GET[itemid]]) {
$items[$_GET[itemid]]++;
}
else {
$items[$_GET[itemid]] = 1;
}


what I get when I add one to it is this as a printout.

Array(64 => 1)

Whicc is ok but i cant get it to add any other etc.

Can somebody help me here ??

Thanks

:thumbsup:

Ökii
11-29-2002, 10:22 AM
rather confused :confused:

session
$items[0] = 4;
$items[1] = 6;
$items[2] = 3;

$_SESSION['items'] = serialize($items);

url - page.php?itemid=1

$items = unserialize($_SESSION['items']);
if($items[$itemid] !== "")
{
$items[$itemid]++;
}
else
{
$items[$itemid] = 1;
}

$array_index = -1;
while(++$array_index < count($items))
{
echo $items[$array_index].'<br />';
}

having to guess lots at exactly what syntaxes you were trying anyway

wap3
11-29-2002, 10:51 AM
Hey okii thanks for replying. Perhaps I didn't explain it very well.

I have a page with links in the form of page.php?itemid=23 as you guessed.

When this is clicked it goes to the page with the script on where the item id is added to the session in the array.

So in the end after adding various itemid's I will end up with an array, which is filled with the itemid numbers.

e.g. array(23 =>1, 34 => 2, 45 =>1)

that explain it any better.

<edit>I have got the solution now so not to worry anyway partly due to your answer oki</edit>

thank you

:thumbsup:

wap3
11-29-2002, 08:21 PM
Hi everyone,

Well I have another problem. As you may have gathered I am trying to create a shopping cart system. .The previous question was to do with the start of the shopping cart system. Since then I have added functions for delete item by item, deleting all items adding items and I have got it to look up in my database etc when showing the cart.

Anyway my problem is when I show the cart what happens is the item_id's in the session array are used to look up the details in the database. These are then used to write the cart on the screen. The follwoing code is the main part of this:





<form name="basket" action="add_to_basket.php?action=update" method="post">

<?
foreach($_SESSION['cart'] as $Item_id => $quantity) {
$item = get_items_forbasket($Item_id);
foreach ($item as $row)
echo "<tr>
<td height=\"24\"><!--DWLayoutEmptyCell-->&nbsp;</td>
<td><!--DWLayoutEmptyCell-->&nbsp;</td>
<td colspan=\"2\" align=\"center\" valign=\"bottom\"><input name=\"$Item_id\" type=\"text\" class=\"basket\" value=\"$quantity\" size=\"2\" maxlength=\"2\">
</td>
<td colspan=\"4\" class=\"catpri\"><input name=\"$row[Item_id]description\" type=\"text\" class=\"basketblk\" value=\"$row[Description]\" size=\"70\" readonly=\"true\"></td>
<td class=\"catpri\"><input name=\"$row[Item_id]weekrate\" type=\"text\" class=\"basketblk\" value=\"$row[One_Week]\" readonly=\"true\" size=\"6\"></td>
<td width=\"34\"><!--DWLayoutEmptyCell-->&nbsp;</td>
<td width=\"63\" valign=\"bottom\"><a href=\"add_to_basket.php?action=delete&itemid=$row[Item_id]\"><img src=\"../Images/minus_button.gif\" alt=\"Remove item from basket\" width=\"22\" height=\"21\" border=\"0\"></a></td>
</tr>";
}
?>
<tr>
<td height="24" colspan="11" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
</tr>
<tr>
<td height="51" colspan="11"><!--DWLayoutEmptyCell-->&nbsp;</td>
</tr>
<tr>
<td height="24" colspan="2"><!--DWLayoutEmptyCell-->&nbsp;</td>
<td colspan="7" class="cathead"><!--DWLayoutEmptyCell-->&nbsp;</td>
<td colspan="2"><!--DWLayoutEmptyCell-->&nbsp;</td>
</tr>
<tr class="catsub">
<td height="25" colspan="11"><!--DWLayoutEmptyCell-->&nbsp;</td>
</tr>
<tr class="catsub">
<td height="26" colspan="3"><!--DWLayoutEmptyCell-->&nbsp;</td>
<td colspan="2"><a href="brochure.php"><img src="../Images/continue_button.gif" alt="Continue shopping . ." width="93" height="22" border="0"></a></td>
<td colspan="2" align="left"><a href="add_to_basket.php?action=delall"><img src="../Images/deleteall_button.gif" alt="Delete all items" width="93" height="22" border="0"></a></td>
<td colspan="2"><input name="Submit" type="image" value="Submit" src="../Images/checkout_button.gif" alt="Checkout"></a></td>
<td colspan="2"><!--DWLayoutEmptyCell-->&nbsp;</td>
</tr>
</form>



As you can see it basically makes a form putting details in form fields. What I am trying to do is create a function which will be triggered by the submit button. The purpose of this function is to check the values in the 'quantity' box for each item and amend the session array.

Does this make sense ?? Basically the user can change the quanity of an item in the cart then when the form is submitted I need the session ammended.

I have tried lots to get this to work but with no sucess.
This is one of the things I have tried. Perhaps it will explain further what I am trying to do.



function update_cart() {
foreach($_SESSION['cart'] as $Item_id => $quantity) {
if ($_POST[$Item_id] == '0')
unset($_SESSION['cart'][$_POST[$Item_id]]) ;
else
$_SESSION['cart'] = $_POST[$Item_id];
}}




Thanks

wap3
11-30-2002, 09:18 AM
Hi

I could really do with some help here. So can some of you php gods give me some advice. If I have not explained it very well let me know so I can try again.

Thank you

:thumbsup:

wap3
11-30-2002, 09:42 AM
Nevermind I have figured it out now.

I tried something new it and works just fine.
I wrote this:


function update_cart() {
reset ($_SESSION['cart']);
while (list($key,$value) = each($_SESSION['cart'])) {
if ($_POST[$key] == "0") {
unset($_SESSION['cart'][$key]);
}
else {
$_SESSION['cart'][$key] = $_POST[$key];
}
}
}


ha it worked woooohooooo.

:p