dongyange1101
2014-02-21 10:23 阅读 51
已采纳

使用PHP显示带有图像的表

Here's my code :

<?php
$items = array();
$images = node_files(request('nid')) ; 
$gallery_cover = db_fetch_row("node_meta","nid='".request('nid')."' AND name='gallery_image'");


foreach ($images as $key => $value)
{
$items[$key]['weight'] = $value['fid'].'_'.$value['weight'] ;
$items[$key]['image'] = '<a href="'.image_url($value['uri']).'" data-lightbox="gallery-image_'.$value['fid'].'">'.image_load($value['uri'],array("class"=>"img-thumbnail","thumbs"=>"small")).'</a>' ;
$items[$key]['title'] = '<span id="title_file_'.$value['fid'].'">'.$value['title'].'</span>' ;

$cover = "<a href='javascript:void(0);' onclick='javascript:gallery_cover(\"".$value['fid']."\"); return false;' id='gallery_cover_".$value['fid']."' data-toggle='tooltip' class='data-tooltip' data-placement='top' data-original-title='Top' id='tooltip1'><span class='btn btn-default btn-sm glyphicon glyphicon-eye-".(isset($gallery_cover['value']) && $gallery_cover['value']==$value['fid']?'open':'close')."'></span></a>" ; 
$edit = "<a href='javascript:void(0);' onclick='javascript:update_file(\"".$value['fid']."\"); return false;' data-toggle='tooltip' class='data-tooltip' data-placement='top' data-original-title='Modifier' id='tooltip1'>".IMG_EDIT."</a>" ; 
$delete = "<a href='javascript:void(0);' onclick='javascript:delete_added_image(\"".$value['fid'].'_'.$value['weight']."\"); return false;' data-toggle='tooltip' class='data-tooltip' data-placement='top' data-original-title='Supprimer' id='tooltip1'>".IMG_DELETE."</a>" ; 

$items[$key]['actions'] = $cover." ".$edit." ".$delete ;    
}
foreach($images as $key => $value)
{
if ($key % 4 == 0)
echo "<div class=\"row\">" ; 
    echo "<div class=\"col-md-3\">" ;
echo "<div class='thumbnail'><img src=".image_url($value['uri'])"/>
    <div class='caption'>
        <h4>".$value['title']."</h4> // <--- HERE THE ERROR SHOW
        <p>$cover $edit $delete</p>
    </div></div>" ;
echo "</div>" ;
}
?>

I'm trying to dsiplay 4 images in row with the title and the actions $cover $edit $delete and this code generate errors. Where did I go wrong? this is the error :

syntax error, unexpected '"/> ' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';'

Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    duanliao3826 duanliao3826 2014-02-21 10:25

    You:

    • forgot to close the bracket at the very end
    • forgot to add a dot in the string concatenation

    --

    foreach($images as $key => $value)
    {
        if ($key % 4 == 0)
        echo "<div class=\"row\">" ; 
        echo "<div class=\"col-md-3\">" ;
        // added a dot in the line below (almost at the end)
        echo "<div class='thumbnail'><img src=".image_url($value['uri']) . "/>
            <div class='caption'>
            <h4>".$value['title']."</h4>
            <p>$cover $edit $delete</p>
            </div></div>" ;
        echo "</div>" ;
    } // <--- HERE
    ?>
    

    For the future, I advice using some (any) IDE, e.g. NetBeans (my favourite) or Eclipse. They're free open source very powerfull tools. If you can afford you can try with PHPStorm which is, IMHO, the best of those three but not free :)

    And also for future sakes, I would advice some other style for HTML outputting in PHP. If you're doing echo directly in the loop, why not doing it outside the PHP? Like this:

    foreach($images as $key => $value)
    {
        if ($key % 4 == 0)
        echo "<div class=\"row\">" ;
    
        // let's close the PHP block here
        ?>
        <div class="col-md-3">
            <div class="thumbnail">
                <img src="<?= image_url($value['uri']); ?>" />
                <div class="caption">
                    <h4><?= $value['title']; ?></h4>
                    <p><?= $cover . ' ' . $edit . ' ' . $delete; ?></p>
                </div>
            </div>
        </div>
        <? // let's open PHP block again
    }
    

    This method has lots of advantages:

    • HTML markup is more clear to read (helps you avoid syntax errors)
    • IDE will format the markup with the tabs
    • IDE will color the HTML syntax
    • if you're using CSS classes in the markup, IDE will link them to CSS files, it means it will auto-complete suggested classes when you're editing a CSS file
    • if any HTML syntax errors occur, IDE will show them
    • you can always inject PHP in the markup thanks to <?=. It opens the PHP stream and does the echo. It's simply the same as <? echo.
    点赞 评论 复制链接分享

相关推荐