SpencerX
SpencerX

Reputation: 5723

displaying table with images with 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

Upvotes: 0

Views: 97

Answers (1)

matewka
matewka

Reputation: 10148

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.

Upvotes: 4

Related Questions