tags:

views:

64

answers:

3

I was wondering how would go about displaying five images in a row using lists. For example, how would I generate the following XHTML code below using PHP & MySQL?

Here is the XHTML code.

<ul>
    <li><a href="#" title=""><img src="" /></a></li>
    <li><a href="#" title=""><img src="" /></a></li>
    <li><a href="#" title=""><img src="" /></a></li>
    <li><a href="#" title=""><img src="" /></a></li>
    <li><a href="#" title=""><img src="" /></a></li>
</ul>

<ul>
    <li><a href="#" title=""><img src="" /></a></li>
    <li><a href="#" title=""><img src="" /></a></li>
    <li><a href="#" title=""><img src="" /></a></li>
    <li><a href="#" title=""><img src="" /></a></li>
    <li><a href="#" title=""><img src="" /></a></li>
</ul>

Here is my PHP & MySQL code so far.

$url = array();
$title = array();

$dbc = mysqli_query($mysqli,"SELECT *
                             FROM images
                             GROUP BY images.id");

if (!$dbc) {
    print mysqli_error($mysqli);
} else {
    while($row = mysqli_fetch_array($dbc)){ 
        $url[] = $row["url"];
        $title[] = $row["title"];
    }
}
+2  A: 

just echo it out it your while loop: EDIT: noticed your example has a new ul every 5 images, code has been adjusted to handle that.

if (!$dbc) {
    print mysqli_error($mysqli);
} else {
    $rowCount = 0;
    while($row = mysqli_fetch_array($dbc)){ 
        if($rowCount % 5 == 0){
         echo "<ul>";
        }
       echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
        echo "<img src='".$row['src']."'></a></li>";
       if($rowCount % 5 == 4) {
         echo "</ul>";
       }
       $rowCount++;
    }

}
GSto
would this work if I only have six images? would it add a new <ul> tag after 5 images?
pOp
You would still need to split them into groups of 5 like his example. A counter and some modulo division should do the trick.
Eric Petroelje
Is there a better way to do this or is this the best way?
pOp
edited to make a new list every 5 images.
GSto
**another templateless** PHP'ish lame code.
Col. Shrapnel
@GSto - problem now is that it won't close the `<ul>` tag if the number of items isn't an exact multiple of 5.
Eric Petroelje
Your list element might not close properly.
BojanG
@Eric Petroelje how can I fix this problem so it will close even if its not the exact number of five so it will close after six, seven and so on.
pOp
Do another test once you exit the loop and close `<ul>` if $rowCount % 5 != 4
BojanG
And list your SQL fields explicitly, don't use `SELECT *`.
BojanG
+1  A: 

you've got it perfect! now you have just to add an HTML part. Though, separate arrays is not good, it would be better to use a single one

$DATA = array();
while($row = mysqli_fetch_array($dbc)){ 
  $DATA[] = $row;
}

and then you're going to print out HTML

<ul>
<? foreach ($DATA as $row): ?>
    <li><a href="<?=$row['url']?>" title="<?=$row['title']?>"><img src="" /></a></li>
<? endforeach ?>
</ul>

though if you need to split your lists into smaller parts, you will need another code

there are many ways to do it. the easiest one is to add some markers to array.
and than use it at the template:

<? foreach ($DATA as $row): ?>
  <? if($row['ul']): ?><ul><? endif ?>
      <li><a href="<?=$row['url']?>" title="<?=$row['title']?>"><img src="" /></a></li>
  <? if($row['/ul']): ?></ul><? endif ?>
<? endforeach ?>
Col. Shrapnel
A: 

The easiest way to do this without having lots of crazy statements to determine when to split the list is to take advantage of array_chunk:

<?php
$url = array();
$title = array();

$dbc = mysqli_query($mysqli,"SELECT *
                             FROM images
                             GROUP BY images.id");

if (!$dbc) {
    print mysqli_error($mysqli);
} else {
    while($row = mysqli_fetch_array($dbc)){ 
        $rows = $row;
    }
}


$row_groups = array_chunk($rows, 5);

foreach($row_groups as $row_group ) {
   echo "<ul>";
   foreach( $row_group as $row ) {
?>
       <li><a href="<?=$row['url'] ?>" title="<?=$row['title'] ?>"><img src="<?=$row['url'] ?>"/></a></li>';
<?php
   }
   echo "</ul>";
}
?>
Eric Petroelje
The OP has the code already template ready. But you all pull him down to the usual templateless approach, used by the millions of ignorant PHP lemmings.
Col. Shrapnel
@Col - He has the code in a template? I didn't see anything about a template engine in his question? Could you enlighten me as to what engine he is using? smarty? xtemplate?
Eric Petroelje
it doesn't really matter. All templates are very common. Even with xtpl he have to parse template in the view part, not in the controller.
Col. Shrapnel
@Col - I applaud the use of templates correction on your side, BUT, the question was NOT about template use. Please realize that many people here are learning new skills - which is better done gradually. First, php developer needs to know how to do it without templates, the add the templating engine of choice ( i prefer Smarty).
gnomixa
@gnomixa - Agreed. I could just as well respond to every PHP question with a quip about "another lemming using PHP who should be using ASP.NET"
Eric Petroelje
@gnomixa that's the problem. Everyone's just talking about good practices, in the form of wise wordy advise. But every piece of real code example is always terrible as it can be. That's why PHP will always remain with "lame language" label. Though I wouldn't raise that argue if I haven't seen the OP's code is ready for the template. Very few newbies do collect data first, not making output right in the database loop making perfect spaghetty. Thet's why these answers saddens me.
Col. Shrapnel