Jump to content

[SOLVED] while loop layout question


realjumper

Recommended Posts

Hi,

 

I currently have a static page for book reviews. The reviews consist of a title showing Book and Author.....next line is a left aligned image of the book, plus the text. The next review is the same as the above, except that the image is right aligned......the next review the image is left aligned and the next review the image is right aligned and so on.

 

I am going to make an admin panel so that the administrator can add/delete reviews without me having to physically type the html.

 

I have the DB setup ok as well as the image upload/resize etc and all that is good....BUT.....when they (1) add a new review, how can I code the loop to keep the first review image left aligned, the second review image right aligned etc etc.......and (2) how can I achieve the smae if the administrator deletes a review in the middle of the page?

 

I can write a loop to make the images always left, or right aligned (see code) but how do I make the images alternate between left and right alinement?

 


// Retrieve all the data from the table
$result = mysql_query("SELECT * FROM library_images WHERE review_type = 'Book'") 
or die(mysql_error()); 

while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table

         echo "<img src=\"images/books/$row[image]\"  align=\"left\">";
         echo "<h3>$row[name] <em>by</em> $row[author]</h3>";
         echo "<p align=\"justify\">$row[review_text]</p>";
         echo "<p> </p>";
}

Link to comment
https://forums.phpfreaks.com/topic/129164-solved-while-loop-layout-question/
Share on other sites

I'd do it like this:

 

// Retrieve all the data from the table
$result = mysql_query("SELECT * FROM library_images WHERE review_type = 'Book'")
or die(mysql_error());

$i=0;

while($row = mysql_fetch_array( $result )) {
   // Print out the contents of each row into a table

if ($i % 2)
{         
         echo "<img src=\"images/books/$row[image]\"  align=\"left\">";
}
else
{
         echo "<img src=\"images/books/$row[image]\"  align=\"right\">";
}
         echo "<h3>$row[name] <em>by</em> $row[author]</h3>";
         echo "<p align=\"justify\">$row[review_text]</p>";
         echo "<p> </p>";
$i++;
   }

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.