Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange behaviour with a repeated function
    text
    copied!<p>I have a function that: gets a serialized array out of a database, unserializes it, adds an item onto the end of the array, serializes it and updates the database with the new array.</p> <p>This works fine when I run the function on its own. I can refresh the page and each time it will add an item to the array. When I print_r the array I can see it has added my new item onto the array, assigning it the next available numeric key.</p> <p>However when I run this function within a foreach loop instead of adding an item on each cycle, it adds an item during the first cycle then every cycle after that it just amends the item that was created in the first cycle.</p> <pre><code>public function updateImage($image, $product_id) { // Some code here to write the file, not relevant to my question. // Process the $image parameter to get $image_src. // The product images are held in an array. // Get the serialized array from the database $objProduct = $this-&gt;Database-&gt;prepare("SELECT name, images FROM tl_iso_products WHERE id = ?")-&gt;execute($product_id); $images = array(); // Unserialize the result to get the array of images $images = unserialize($objProduct-&gt;images); // Add the new image to the array $images[] = array( 'src' =&gt; $image_src, 'alt' =&gt; $objProduct-&gt;name, 'link' =&gt; '', 'desc' =&gt; '', 'translate' =&gt; '', ); // Update the database with the new array $this-&gt;Database-&gt;prepare("UPDATE tl_iso_products SET images = ? WHERE id = ?")-&gt;execute(serialize($images),$product_id); } </code></pre> <p>This is what the array starts like, with one image:</p> <pre><code>Array ( [0] =&gt; Array ( [src] =&gt; image1.jpg [alt] =&gt; Image alt text [link] =&gt; [desc] =&gt; [translate] =&gt; ) ) </code></pre> <p>Refresh the page so the function runs a couple of times, changing the image name each time:</p> <pre><code>Array ( [0] =&gt; Array ( [src] =&gt; image1.jpg [alt] =&gt; Image alt text [link] =&gt; [desc] =&gt; [translate] =&gt; ) [1] =&gt; Array ( [src] =&gt; image2.jpg [alt] =&gt; Image alt text [link] =&gt; [desc] =&gt; [translate] =&gt; ) [2] =&gt; Array ( [src] =&gt; image3.jpg [alt] =&gt; Image alt text [link] =&gt; [desc] =&gt; [translate] =&gt; ) ) </code></pre> <p>Run the function in a foreach loop, which should have the same effect as above:</p> <pre><code>Array ( [0] =&gt; Array ( [src] =&gt; image1.jpg [alt] =&gt; Image alt text [link] =&gt; [desc] =&gt; [translate] =&gt; ) [1] =&gt; Array ( [src] =&gt; image3.jpg [alt] =&gt; Image alt text [link] =&gt; [desc] =&gt; [translate] =&gt; ) ) </code></pre> <p>Instead of adding a new item each time it loops, it adds one new item in the first loop then just overwrites it with each loop after.</p> <p>Every time it loops it should fetch the array from the database, which will have been updated by the previous loop. Any idea as to why this doesn't happen?</p> <p>Here's the foreach loop as requested, it runs through an array which contains image urls and the ids of the product they need to be added to:</p> <pre><code>foreach($arrImages as $image) { $this-&gt;updateImage($image['url'], $image['product_id']); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload