views:

29

answers:

1

Hi Guys,

I'm not sure if anyone can help me with a problem I have..

I have a parse function that includes the code:

foreach ($values as $element) {

$nodeName = $element["tag"];

switch ($element['type']) {

case 'open':

if ($nodeName == "SHOPPINGLIST") {
$this->publicationID = $element["attributes"]["PUBLICATIONID"]; 
}

if ($nodeName == "ENTRY") {

$entry = new ShoppingCartEntry();
$entry->quantity = $element["attributes"]["QUANTITY"];

}

if ($nodeName == "ITEM") {

$entry->productID = $element["attributes"]["PRODUCTID"];
$entry->price = $element["attributes"]["PRICE"];

array_push($this->entries, $entry);
}   

break;

case 'complete':

if ($nodeName == "NAME") {
$entry->name = $element["value"];
}

if ($nodeName == "DESCRIPTION") {
$entry->description = $element["value"];
}   

break;

}
}

As a result I have an array in the format of:

Array ( 
[0] => ShoppingCartEntry Object ( 
[quantity] => 3 
[productID] => 00001 
[price] => 4.99 
[name] => Full English 
[description] => Eggs, Beans, Sheesh Kebab Served with Toast )

At the minute I am printing the item entries using:

foreach ($entries as $entry) { 
}

However now I need an entry to be created for each quantity value there is of a product. So if the quantity of the product in the entry is 3, Then either an alteration needs to be made so that 3 entries are created or even if there is someway of editing the foreach loop, so that it loops for "$quantity" amount of times..

Any pointers in the right direction would be appreciated.

Thanks

A: 

Like this?

foreach ($entries as $entry)
{ 
    for ($i = 0; $i < $entry->quantity; $i++)
    {
        echo $entry->name . ': ' . ($i + 1) . ' of ' . $entry->quantity . '<br>';
    }
}
Greg
Hi Greg, Thanks for your reply. So simple but yes using the line: for ($i = 0; $i < $entry->quantity; $i++)in my foreach loop solves the problem and prints a line for every item.Thank you verymuch. Much appreciated!
Wes