Link to post

Share on other sites

Advertising

+theblazingangel 137

The array is being currently overwrote by the last receipt #. I know that the root of all this evil is this portion of the code:

>
if(!in_array($factura,$this-&gt;trigger)) $this-&gt;trigger[] = $factura;
[b] $this-&gt;factura = [/b]array($factura =&gt; array('producto_id' =&gt; array($row['producto_id']),
'producto' =&gt; array($row['producto']),
'cantidad' =&gt; array($row['cantidad']),
'price_i' =&gt; array($row['price_i']),
'tax' =&gt; array($row['tax'])
)
);
[/CODE]
The bold part should actually be:
[CODE]
$this-&gt;factura[] =
[/CODE]
But by doing so, the whole schema breaks, and the receipts are not appended in order.
Its broken (with the above fix implemented) primarily because of your array_key_exists_r() function. I'm assuming that you do understand what that function does... When processing a new row from the database, you're scanning recursively through all nested arrays within $this-&gt;factura looking for [b][u]any[/u][/b] existence of a key with a value matching the receipt number! I.e. it's checking for the existence of the receipt number amongst all existing receipt number keys, and all item number keys!!
Let me walk you through the incorrect output you printed having made your suggested fix (from the perspective of processing each row in the result set returned by the database):</pre>
<ul>Row#1: The array is empty, so array_key_exists_r() returns false and the if statement passes. You therefore create a new array containing the receipt number, and the details of the first item, which is stored in a new array entry in the format of $this-&gt;factura[0][1][itemdata], where key [0] was automatically created and [1] is the receipt number.
Row#2: A second item for receipt 1 is being processed. array_key_exists_r() finds an existing array key with a value of 1, so attempts to add the new item data to the receipt, but the receipt is at $this-factura[0][1] not $this-&gt;factura[1], so inadvertently makes a new top level array entry.
Row#3: We are now processing the first item of receipt 2. There is no array entry anywhere in $this-&gt;factura, so array_key_exists_r() returns false, and as with row #1, a new array entry is to be created. The next numerical array key is [2], which coincidentally happens to be the same as the receipt number. We end up creating $this-&gt;factura[2][2][itemdata], with the first [2] being automatically created and the second representing the receipt number.
Row#4: We are now processing receipt 4. An array key with value 4 does not exist anywhere in $this-&gt;factura (as a key representing a receipt number, and item number, or anything else), so same situation as the previous row, we create the new entry $this-&gt;factura[3][4][itemdata].
Row#5: Still on receipt 4, an array key with value 4 does now exist, so we therefore do an update as with row #2. But similarly, since receipt 4 is at $this-&gt;factura[3][4] not $this-&gt;factura[4] we inadvertently create $this-&gt;factura[4].
</ul>
<p></p>
<p>Now, try this:</p>
<div>[code]while($row = $this-&gt;stmt-&gt;fetch())
{
$factura = $row['factura']; /*Receipt number*/
print($factura);
if(! array_key_exists($factura, $this-&gt;factura))
{
if(! in_array($factura, $this-&gt;trigger)) $this-&gt;trigger[] = $factura;
$this-&gt;factura[$factura] = array(
'producto_id' =&gt; array($row['producto_id']),
'producto' =&gt; array($row['producto']),
'cantidad' =&gt; array($row['cantidad']),
'price_i' =&gt; array($row['price_i']),
'tax' =&gt; array($row['tax'])
);
}
else
{
$this-&gt;factura[$factura]['producto_id'][] =$row['producto_id'];
$this-&gt;factura[$factura]['producto'][] =$row['producto'];
$this-&gt;factura[$factura]['cantidad'][]= $row['cantidad'];
$this-&gt;factura[$factura]['price_i'][] =$row['price_i'];
$this-&gt;factura[$factura]['tax'][] = $row['tax'];
}
}<

1

Share this post

Link to post

Share on other sites

Jose_49 610

Jose_49 610

Its broken (with the above fix implemented) primarily because of your array_key_exists_r() function. I'm assuming that you do understand what that function does... When processing a new row from the database, you're scanning recursively through all nested arrays within $this->factura looking for any existence of a key with a value matching the receipt number! I.e. it's checking for the existence of the receipt number amongst all existing receipt number keys, and all item number keys!!

First of all, thank you... thank you thank you...! It did the trick!!!!!!!!! :D :D :D :D:D

Before using the array_keys_r function, which I got from stackoverflow, I was using array_key_exists to verify the array, but it wasn't working as I wanted since the last receipt overwrote the previous one. I thought it was a multi-dimensional array problem so I looked up on Google and thought using that custom function. In the end it yielded the previous results I was having!