Newbie New
Newbie New

Reputation: 9

Why do I get this syntax error about T_CONSTANT_ENCAPSED_STRING?

I have a function that I use to display the items in the user's shopping cart in a table:

function products() {
    $get = mysql_query('SELECT id, name, description, price FROM menu WHERE quantity > 0');
    if (mysql_num_rows($get) == 0) {
        echo"Sorry, There is no products to display.";
    }
    else {
        while ($get_row = mysql_fetch_assoc($get)) {
            echo '<p>''<table>''<tr>''<td>'.$get_row['name'].'</td>''<td>'.$get_row['description'].'</td>''<td>$'.number_format($get_row['price'], 2).'</td>''<td>''<a href="cart.php?add='.$get_row['id'].'">Add To Cart</a></td></tr></table></p>';
        }
    }
}

Unfortunately, when I try to use that code, I get this error:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting , or ; in C:\xampp\htdocs\shopping\cart.php on line 37

Line 37 is this line:

echo '<p>''<table>''<tr>''<td>'.$get_row['name'].'</td>''<td>'.$get_row['description'].'</td>''<td>$'.number_format($get_row['price'], 2).'</td>''<td>''<a href="cart.php?add='.$get_row['id'].'">Add To Cart</a></td></tr></table></p>';

Why am I getting this error and how can I resolve it?

Upvotes: 0

Views: 175

Answers (7)

GautamD31
GautamD31

Reputation: 28753

Edit like this

echo'<p><table><tr><td>'.$get_row['name'].'</td><td>'.$get_row['description'].'</td><td>'.$number_format($get_row['price'], 2).'</td><td><a href="cart.php?add='.$get_row['id'].'">Add To Cart</a></td></tr></table></p>';

Simple friend.accept answer if it is works for you. You also done a wrong at

<td>'.$number_format($get_row['price'],2).'</td>//you coded like <td>$'.number_format()...

Upvotes: 0

G10DRA
G10DRA

Reputation: 61

you have to write like this in while loop

echo '<p><table><tr><td>'.$get_row['name'].'</td><td>'.$get_row['description'].'</td><td>$'.number_format($get_row['price'], 2).'</td><td><a href="cart.php?add='.$get_row['id'].'">Add To Cart</a></td></tr></table></p>';

Upvotes: 0

ianace
ianace

Reputation: 1635

echo '<p><table><tr><td>'.$get_row['name'].'</td>
<td>'.$get_row['description'].'</td>
<td>'$.number_format($get_row['price'], 2).'</td>
<td><a href="cart.php?add='.$get_row['id'].'">Add To Cart</a></td>
</tr></table></p>';

Upvotes: 0

icktoofay
icktoofay

Reputation: 129059

PHP, unlike some other languages, does not automatically concatenate consecutive string literals. You'll have to either explicitly concatenate:

echo 'a' . 'b' . 'c' . $someVar . 'd';

Or use commas:

echo 'a', 'b', 'c', $someVar, 'd';

Or just join the literals yourself:

echo 'abc' . $someVar . 'd';

Upvotes: 3

Grzegorz
Grzegorz

Reputation: 3335

Things like this '<p>''<table>' are no good. Why don't you put them all into one '<p><table>' instead?

Upvotes: 0

Parallelis
Parallelis

Reputation: 729

Try this

 echo '<p><table><tr><td>'.$get_row['name'].'</td><td>'.$get_row['description'].'</td><td>$'.number_format($get_row['price'], 2).'</td><td><a href="cart.php?add='.$get_row['id'].'">Add To Cart</a></td></tr></table></p>';

Upvotes: 0

Tynarus
Tynarus

Reputation: 141

Shouldn't have the little '' in there. IE:

<p>''<table>''<tr>''<td>

change it to...

<p><table><tr><td>

And everywhere else that you have a '' like above.

Upvotes: 0

Related Questions