0

I have the following image list on an html page that I am converting into php to be driven from a mysql database. While I thought it was a simple enough procedure -I am struggling to avoid syntax errors because of the existing use of ' in my code.

I need to convert a simple list of images into an array based on what is in the database. The html list is:

<li><a  href='javascript:void(0);' rel="{gallery: 'gal1', smallimage: './imgProd/2.jpg',largeimage: './imgProd/2.jpg'}">
<img src='imgProd/2.jpg' style="width:110px; height:110px;"></a></li>

<li><a  href='javascript:void(0);' rel="{gallery: 'gal1', smallimage: './imgProd/3.jpg',largeimage: './imgProd/3.jpg'}">
<img src='imgProd/3.jpg' style="width:110px; height:110px;"></a></li>

<li><a  href='javascript:void(0);' rel="{gallery: 'gal1', smallimage: './imgProd/4.jpg',largeimage: './imgProd/4.jpg'}">
<img src='imgProd/4.jpg' style="width:110px; height:110px;"></a></li>

I am trying to generate this from php using:

 <?php

$result = mysql_query("SELECT * FROM table WHERE id='$id'");
while($row = mysql_fetch_array($result))
{
    echo '<li>';
    echo '<a href='javascript:void(0);' rel="{gallery: 'gal1', smallimage: './' . $row['photo'] . '',largeimage: './' . $row['photo'] . ''}">';
    echo '<img src=' . $row['photo'] . '' style="width:110px; height:110px;">';
    echo '</a>';
    echo '</li>';
}
?>

but am obviously getting loads of syntax errors as I am attempting to use single quotes within single quotes (I assume!?). Does anyone know how I can incoporate this list into an array??

Thanks very much in advance

JD

2
  • Why javascript:void(0);? Why not just #? Commented Oct 18, 2011 at 22:01
  • Why not use a proper fallback URL? Commented Oct 18, 2011 at 22:12

5 Answers 5

1

Escaping the quotes is a bad idea.

You can either simply close and reopen your <?php ?> tags or use Heredoc syntax.

Using tags (PHP is a templating language, anything that isn't between <?php ?> tags will be send to the client exactly as it is):

<?php    
$result = mysql_query("SELECT * FROM table WHERE id='$id'");
while($row = mysql_fetch_array($result))
{
?>
<li>
<a href='javascript:void(0);' rel="{gallery: 'gal1', smallimage: './<?php echo $row['photo']; ?>',largeimage: './<?php echo $row['photo']; ?>'}">
<img src='<?php echo $row['photo']; ?>' style="width:110px; height:110px;">
</a>
</li><?php
}
?>

Using Heredoc (the closing HTML; should be on its on line, unindented):

<?php    
$result = mysql_query("SELECT * FROM table WHERE id='$id'");
while($row = mysql_fetch_array($result))
{    
    echo <<<HTML
    <li>
    <a href='javascript:void(0);' rel="{gallery: 'gal1', smallimage: './{$row['photo']}',largeimage: './{$row['photo']}'}">
    <img src='{$row['photo']}' style="width:110px; height:110px;">
    </a>
    </li>
HTML;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You're right that you can't use single quotes within a single-quoted string, you need to use double quotes, or escape them with a backslash - i.e.

echo '<a href=\'javascript:void(0);\' rel="{gallery: \'gal1\', smallimage: \'./' . $row['photo'] . '\',largeimage: \'./' . $row['photo'] . '\'}">';

The backslash tells PHP to treat the following quote character as a character inside the string, rather than the end of the string.

Comments

1

You are messing with quotes. PHP doesn't know anything about JS or HTML and yoiu have to escape quotes. A way is the following:

[...]
echo '<a href=\'javascript:void(0);\' rel="{gallery: \'gal1\', smallimage: \'./' . $row['photo'] . '\',largeimage: \'./' . $row['photo'] . '\'}">';
echo '<img src=\'' . $row['photo'] . '\' style="width:110px; height:110px;">';
[...]

for more: Please see http://php.net/string

Comments

1

Use HEREDOCs:

echo <<<EOL
<li>
    <a href="javascript:void(0);" rel="{gallery: 'gal1', smallimage: './{$row['photo']}' , largeimage: './{$row['photo']}'}">
        <img src="{$row['photo']}" style="width:110px; height:110px;">
    </a>
</li>
EOL;

Presto magico! No more quote issues.

Comments

1

As a general rule-of-thumb, building blocks of HTML server-side is generally a bad idea. Try using this sort of syntax instead...

while($row = mysql_fetch_array($result)) : ?>

<li>
    <a href="javascript:void(0)">
        <img src="<?php echo htmlspecialchars($row['photo']) ?>" style="...">
    </a>
</li>

<?php endwhile ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.