Welcome Guest, Not a member yet? Register   Sign In
Getting the lastest rowid from the Cart
#1

[eluser]jonny68[/eluser]
Hi,

if i loop through:
$a = 0;
foreach ($this->cart->contents() as $items) {

echo $items['rowid'].'<br />';
$a++;
};

i get all the rowid's from the cart, but what if i want the last inserted?

By increasing $a i get the number of items, and theorically that (number-1) should be the

last..

I'm i wrong? I can't find the way to get this straight something like
$this->cart->contents($a => 'rowid') or $this->cart->contents()[$a]['rowid'] etc,

nothing seems to work.. Sad

Thanks a lot!
#2

[eluser]CroNiX[/eluser]
You can just use php's end() to get the last element of an array without having to do that looping.
#3

[eluser]jonny68[/eluser]
Yes, end() is cool but then i have the same problem to save the nested rowid.

Like this it works fine:

$numItems = count($this->cart->contents());
$i = 0;
foreach($this->cart->contents() as $key) {
if($i+1 == $numItems) {
$saved_rowid = $key['rowid'];
}
$i++;
}

echo $saved_rowid;

Thank you for telling me about end().

Bye
#4

[eluser]CroNiX[/eluser]
If you think its more efficient to loop over the whole array just to get the last element, ok, but this seems much more efficient and compact to me Smile

Code:
$last = end($this->cart->contents());
echo $last['rowid'];

or even
Code:
$contents = $this->cart->contents();
$last = count($contents) - 1;
echo $contents[$last]['rowid'];
#5

[eluser]jonny68[/eluser]
Hi CroNix,

that's right!

Now i understand how it works thank to you!

Much compact an easy to read.

THANKS A LOT!






Theme © iAndrew 2016 - Forum software by © MyBB