简体   繁体   中英

php echo href='javascript:void(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

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 - ie

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.

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

Use HEREDOC s:

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.

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 ?>

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM