-1

Cant figuer out the problem with my code. Theres a syntax error,and i just cant spot it Checked it here - http://phpcodechecker.com/ , and got this as an answer -

Parse error: syntax error, unexpected ''.$val['' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in your code on line 10

Here is the code -

<?php
  include "products.php";

  foreach($products as $key=>$val)
  {
   echo '<div style="float:right;text-align:center;margin:20px;">';
   echo '<IMG src="img/'.$val['category'].'/'.$val['image'].'" WIDTH="94" HEIGHT="94" BORDER="0" ALT=""><br />';
   echo $val['name'].'<br />';
   echo '₪ '.$val['price'].'<br />';
   echo '<a onmousedown="parent.AddItemToCart(''.$val['id'].'' ,''.$val['name'].'','img/'.$val['category'].'/'.$val['image'].'','.$val['price'].'')"></a><br/>';
   echo '</div>';
  }
?>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
Boiko
  • 19
  • 1
  • The issue is your double single quotes around your concats and when using them for empty values. I think you meant to escape the first ones, i.e. `\''`. – Jonnix Jan 25 '16 at 09:33
  • Yes because you're escaping the string in line 10. – André Ferraz Jan 25 '16 at 09:33
  • I suggest you have a look through the [documentation](http://php.net/manual/en/language.types.string.php) on how to use strings in PHP. – Jonnix Jan 25 '16 at 09:36

4 Answers4

0

The unescaped ' terminates the string before you want it to. Try:

echo '<a onmousedown="parent.AddItemToCart(\''.$val['id'].'\' ,\''.$val['name'].'\','img/'.$val['category'].'/'.$val['image'].'','.$val['price'].'\')"></a><br/>';

or use double quotes:

echo "<a onmousedown=\"parent.AddItemToCart('".$val['id']."' ,'".$val['name']."','img/".$val['category']."/".$val['image']."','.$val['price']."')\"></a><br/>';

Reason: The reason of this happening, is that you use ' to start and end your string, but your string also contains the ' character. This way the string ends at the position you enter ' and is confused by the following symbols. To prevent this, you escape the symbols using a backslash \ like so: 'some string containing a \' character'

T3 H40
  • 2,326
  • 8
  • 32
  • 43
0

Replace

echo '<a onmousedown="parent.AddItemToCart(''.$val['id'].'' ,''.$val['name'].'','img/'.$val['category'].'/'.$val['image'].'','.$val['price'].'')"></a><br/>';

to

$imgtag = 'img/'.$val['category'].'/'.$val['image'];
echo '<a onmousedown="parent.AddItemToCart(\''.$val['id'].'\',\''.$val['name'].'\',\''.$imgtag.'\',\''.$val['price'].'\')"></a><br/>';
AnkiiG
  • 3,468
  • 1
  • 17
  • 28
0

Change:

echo '<a onmousedown="parent.AddItemToCart(''.$val['id'].'' ,''.$val['name'].'','img/'.$val['category'].'/'.$val['image'].'','.$val['price'].'')"></a><br/>'; 

For this:

echo "<a onmousedown='parent.AddItemToCart(".$val['id']. ',' .$val['name']. ',img/'.$val['category'].'/'.$val['image'].','.$val['price'].")></a><br/>";
Carlos Ost
  • 492
  • 7
  • 22
0

Change the code with given code

<?php
    include "products.php";

    foreach($products as $key=>$val)
    {
        echo '<div style="float:right;text-align:center;margin:20px;">';
        echo '<IMG src="img/'.$val['category'].'/'.$val['image'].'" WIDTH="94" HEIGHT="94" BORDER="0" ALT=""><br />';
        echo $val['name'].'<br />';
        echo '₪ '.$val['price'].'<br />';
        echo '<a onmousedown="parent.AddItemToCart('.$val['id'].','.$val['name'].'','img/'.$val['category'].'/'.$val['image'].','.$val['price'].')"></a><br/>';
        echo '</div>';
    }
?>
Gugan Abu
  • 546
  • 1
  • 4
  • 17