0

I have a function that pickup a purchased products to use for tracking purchased products with analytics.js but the result in the purchase page when I call the function only show the first product without doing a loop.

What I am doing wrong?

 public function AddProductosAna($result) {
        $result2 = mysql_query("SELECT order_id FROM tborder WHERE cart_id = '$this->id_cesta'");
        $OrderId = mysql_fetch_array($result2);
        $result = mysql_query("SELECT * FROM cesta_linea WHERE id_cesta = '$this->id_cesta'");

        while ($row = mysql_fetch_array($result)) {
            $producto = $row['id_producto'];

            return <<<HTML

ga('ecommerce:addItem', {

  'id': '{$OrderId['order_id']}',

  'name': '{$producto}',

  'price': '{$row['precio']}',

  'quantity': '{$row['cantidad']}'

});

HTML;

        }
    }
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Kio
  • 3
  • 2
  • 2
    If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5, and completely removed in PHP 7.0 (which is so old it no longer even receives active support). Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Mar 23 '18 at 21:09
  • 2
    He is returning not echoing it will show only one fetch, or I am stupid? –  Mar 23 '18 at 21:12
  • I call the function like this in other file: AddProductosAna($result);?> but not loop :( – Kio Mar 23 '18 at 21:16
  • As @StupidKid mentioned, your loop will end and return the results on the first iteration since you have a `return` instead of an `echo` in the loop. – M. Eriksson Mar 23 '18 at 21:20
  • He do not need echo he can store all data in array and then return it. But like this he is getting only one field and return is just closing the loop. So I ain't stupid –  Mar 23 '18 at 21:22
  • I don't get it, because I don't have real knowledge... Just I try to make this looking other functions in the site by copy-paste and change some strings... So I would need to change the return by echo? – Kio Mar 23 '18 at 21:31

1 Answers1

0

As Stupid Kid and Magnus Eriksson indicated, the function should return after the wile loop, not inside the while loop.

public function AddProductosAna($result) {
        $result2 = mysql_query("SELECT order_id FROM tborder WHERE cart_id = '$this->id_cesta'");
        $OrderId = mysql_fetch_array($result2);
        $result = mysql_query("SELECT * FROM cesta_linea WHERE id_cesta = '$this->id_cesta'");
        $js = '';
        while ($row = mysql_fetch_array($result)) {
            $producto = $row['id_producto'];

            $js .= <<<HTML

ga('ecommerce:addItem', {
  'id': '{$OrderId['order_id']}',
  'name': '{$producto}',
  'price': '{$row['precio']}',
  'quantity': '{$row['cantidad']}'
});

HTML;

        }
        return $js;
    }
PaulH
  • 2,918
  • 2
  • 15
  • 31