Jump to content

Recommended Posts

This si the code I currently have:

<?php

$result = mysql_query("SELECT * FROM Animals");
$url='.../animals/';
$numofrows = mysql_num_rows($result);

while($row = mysql_fetch_array($result)){
echo "<td><img src='".$url.$row['photoAnimal']."' height='100px' width='100px'></td>"; 
echo "<br>";
echo "<td><a href='http://www.cool.com/animal.php?animal={$row['nameAnimal']}'>{$row['nameAnimal']}</a></td>";
echo "<br>";
}
?>

 

The resulting display resembles this:

Photo

name animal

Photo

name animal

Photo

name animal

 

What I want is this:

Photo                  Photo                  Photo

Name Animal      Name Animal        Name Animal

 

Any ideas as to how I can achieve this result?

Link to comment
https://forums.phpfreaks.com/topic/203318-trying-to-change-display-of-results/
Share on other sites

Because your issue is in your presentation code, not your query, I'm going to move this thread to the php help section... It will likely get more responses there.

 

Could you show in your desired output what you want in the same table cell and where you want to start new table rows. Also, how many columns across do you want before you start a new table row?

<?php

$result = mysql_query("SELECT * FROM Animals");
$url='.../animals/';
$numofrows = mysql_num_rows($result);

while($row = mysql_fetch_array($result)){
echo "<td><img src='".$url.$row['photoAnimal']."' height='100px' width='100px'>"; 
echo "<br>";
echo "<a href='http://www.cool.com/animal.php?animal={$row['nameAnimal']}'>{$row['nameAnimal']}</a></td>";
}
?>

I've managed to get this:

<?php
$result = mysql_query("SELECT * FROM Animals");
$url='../animals/';
$numofrows = mysql_num_rows($result);

echo "<table>";
while($row = mysql_fetch_array($result)){
echo "<td><img src='".$url.$row['photoAnimal']."' height='100px' width='100px'>"; 
echo "<br>";
echo "<a href='http://www.cool.com/animals.php?animal={$row['nameAnimal']}'>{$row['nameAnimal']}</a></td>";
echo "<br>";
}
echo "</table>";
?>

 

The result displays what I want.... to a point.

 

What happens is the output is given in only one row.

 

I want 5 items (columns) per row and rows to display until no more items exist. I imagine a count variable would be required but I have no idea how to implement it with this code. Any help?

Getting closer!!

 

Here's what I've got:

<?php
$result = mysql_query("SELECT * FROM Animals");
$url='.../animals/';
$numofrows = mysql_num_rows($result);

while($row = mysql_fetch_array($result)){
   $cols = 0;
   echo "<table><tr>";
   while ($cols < 20) {
      echo ($cols % 5 == 0)? "</tr><tr>" : "";
      echo "<td><img src='".$url.$row['photoAnimal']."' height='100px' width='100px'>";
      echo "<br>";
      echo "<a href='http://www.cool.com/animal.php?animal={$row['nameAnimal']}'>{$row['nameAnimal']}</a></td>";
      $cols++;
   }
   echo "</tr></table>";
}
?>

 

I am now getting 5 columns with the photo and name of animal as I want it. There's one catch: I get a 5x4 table for each animal (20+ tables!) and the same animal is inside each cell of that table!

 

Ex Output:

Cow / Cow / Cow / Cow / Cow

Cow / Cow / Cow / Cow / Cow

Cow / Cow / Cow / Cow / Cow

Cow / Cow / Cow / Cow / Cow

 

Horse / Horse / Horse / Horse / Horse

Horse / Horse / Horse / Horse / Horse

Horse / Horse / Horse / Horse / Horse

Horse / Horse / Horse / Horse / Horse

 

Can anyone guide me as to how to make ONE table with a different animal in each cell? I.E. 5x(# of animals) table?

 

Wanted output:

Cow        / Horse / Sheep  / Pig    / Duck

Cat        / Dog    / Goose  / Turtle / Flamingo

Elephant / Zebra / Giraffe / Lion    / Llama

.... and so on

I've figured it out! For those curious, Final Code:

 

<?php
$columns = 5;

$result = mysql_query("SELECT * FROM Animals");
$url='.../animals/';

$numofrows = mysql_num_rows($result);
echo "<table>";

for($i = 0; $i < $numofrows; $i++) {
    $row = mysql_fetch_array($result);
    if($i % $columns == 0) {
        echo "<tr>";
    }
    echo "<td><img src='".$url.$row['photoAnimal']."' height='100px' width='100px'>";
    echo "<br>";
    echo "<a href='http://www.cool.com/animal.php?animal={$row['nameAnimal']}'>{$row['nameAnimal']}</a></td>";
    if(($i % $columns) == ($columns - 1) || ($i + 1) == $numofrows) {
        echo "</tr>";
    }
}
echo "</table>";
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.