Forum Moderators: coopster

Message Too Old, No Replies

Array loop does not clear

how to

         

closer

3:36 am on Jun 3, 2007 (gmt 0)

10+ Year Member



I am having difficulties in getting this correct, what I want to do is sort the prices low to high and display them. That works, but as there are 5 products per page it adds all prices from product to product 2, all prices from product 1 and 2 to product 3 etc..

How to tell php that the array needs to be reset / cleared / set to zero?

I've tried reset ($ar) and unset ($ar) but both won't work :(


$ar = array( $this->_movieValues['1'] => $row['1price'], $this->_movieValues['2'] => $row['2price'], $this->_movieValues['3'] => $row['3price'], $this->_movieValues['4'] => $row['4price'], $this->_movieValues['5'] => $row['5price'], $this->_movieValues['6'] => $row['6price'], $this->_movieValues['7'] => $row['7price'], $this->_movieValues['8'] => $row['8price'], $this->_movieValues['9'] => $row['9price']);

asort ($ar);
foreach ($ar as $key => &$val){
$this->_movieValues['sorted'] .="$key";
}

Any help on this matter is much appreciated..

Habtom

4:58 am on Jun 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can simply do the following:

$ar = array();

Habtom

closer

7:40 am on Jun 3, 2007 (gmt 0)

10+ Year Member



Unfortunately this did not solve the issue, I'm still getting the prices from product 1 added to product 2, product 1 and 2 to product 3 etc ...

I used yur suggestion as follows:


foreach ($ar as $key => &$val){
$this->_movieValues['sorted'] .="$key";
$ar = array();

}

any other ideas?

Habtom

7:47 am on Jun 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



//How to tell php that the array needs to be reset / cleared / set to zero?

$arr = array();

This much I am clear.

The code you just posted doesn't make much sense. What are you trying to achieve?

Habtom

closer

7:54 am on Jun 3, 2007 (gmt 0)

10+ Year Member



A loop is called, limited to 5 per page.

with the code above, it does give me the correct prices with each product but displays them like this:

PRODUCT 1
- PRODUCT 1 PRICE 1
to
- PRODUCT 1 PRICE 9

PRODUCT 2
- PRODUCT 1 PRICE 1
to
- PRODUCT 1 PRICE 9
- PRODUCT 2 PRICE 1
to
- PRODUCT 2 PRICE 9

PRODUCT 3
- PRODUCT 1 PRICE 1
to
- PRODUCT 1 PRICE 9
- PRODUCT 2 PRICE 1
to
- PRODUCT 2 PRICE 9
- PRODUCT 3 PRICE 1
to
- PRODUCT 3 PRICE 9

So I need to show only the price of PRODUCT 3 with PRODUCT 3 and was guessing that this error was caused due to the array not being cleared.

Habtom

8:16 am on Jun 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




If you understand you correctly, if you do the following, what do you get?

foreach ($ar as $key => $val){
$new_output = "Product: ".$key." - Price: ".$val;
}
echo $new_output;

Habtom

closer

8:31 am on Jun 3, 2007 (gmt 0)

10+ Year Member



it gives me the highest price per product, so

PRODUCT 1
- PRICE 9 (which is the highest)

PRODUCT 2
- PRICE 9

etc... to PRODUCT 5 (per page)

I want to show price1 to 9 per product, sorted from cheapest to most expensive, cheapest on top

closer

8:35 am on Jun 3, 2007 (gmt 0)

10+ Year Member



To clarify everything even more, an explenation about the vars used

$ar = array(

$this->_movieValues['1'] < this is the HTML that has to be shown

=> $row['1price'] < this is the price in the DB

, $this->_movieValues['2'] => $row['2price'], $this->_movieValues['3'] => $row['3price'], $this->_movieValues['4'] => $row['4price'], $this->_movieValues['5'] => $row['5price'], $this->_movieValues['6'] => $row['6price'], $this->_movieValues['7'] => $row['7price'], $this->_movieValues['8'] => $row['8price'], $this->_movieValues['9'] => $row['9price']);

asort ($ar); < sort the array on price, low to high

foreach ($ar as $key => &$val){

$this->_movieValues['sorted'] .="$key"; < for every product show all prices ($row['1price'] to $row['9price']) with their correspondent HTML

}