php echo href='javascript:void(0);' - Stack Overflow

I have the following image list on an html page that I am converting into php to be driven from a mysql

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

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

Share Improve this question asked Oct 18, 2011 at 21:39 JD2011JD2011 2232 gold badges12 silver badges28 bronze badges 2
  • Why javascript:void(0);? Why not just #? – DaveRandom Commented Oct 18, 2011 at 22:01
  • Why not use a proper fallback URL? – Felix Kling Commented Oct 18, 2011 at 22:12
Add a ment  | 

5 Answers 5

Reset to default 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;
}

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.

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/string

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.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745227089a4617512.html

相关推荐

  • php echo href=&#39;javascript:void(0);&#39; - Stack Overflow

    I have the following image list on an html page that I am converting into php to be driven from a mysql

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信