2

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

3 Answers 3

2

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

}
Sign up to request clarification or add additional context in comments.

8 Comments

would this work if I only have six images? would it add a new <ul> tag after 5 images?
You would still need to split them into groups of 5 like his example. A counter and some modulo division should do the trick.
Is there a better way to do this or is this the best way?
@GSto - problem now is that it won't close the <ul> tag if the number of items isn't an exact multiple of 5.
Your list element might not close properly.
|
1

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

Comments

0

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

6 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.