Jump to content

alternate results layout


adzie

Recommended Posts

Hello again folks,

 

hope somone can help, I want my db results to display in a different format ie odd left and even right side of page.  Any help appreciated.

 


<?php


$query  = "SELECT * FROM members WHERE group='1'";
$result = mysql_query($query) or die('Error.');
$number = mysql_numrows($result);
if ($number > 0) {
for ($i=0; $i<$number; $i++) {
$location = mysql_result($result,$i, "location");
$image = mysql_result($result,$i, "image");
?>


<div class="ac">
<h5><?php echo "$location"; ?></h5>

<a href="../index.php"><img border="0" src="img/<?php echo "$image"; ?>"></a>
</div>

<br /> 

<?php
}

}

?>

Link to comment
https://forums.phpfreaks.com/topic/166362-alternate-results-layout/
Share on other sites

I'm not sure that I understand you completely but you could simply put the results into a table.

 

Essentially start the <TR> before you start outputting the results

 

Have a counter that increments after each set of data is echoed

 

Have a <TD></TD> around each result

 

Have an if statement stating if the counter is odd then </TR><TR>

 

The logic is that you'll have a <TR>, two sets of <TD></TD> and then a </TR><TR> to start the next line.

what I was hoping to do for each row/result was to create a section so for the odd rows

 

<div class="ac">
<h5><?php echo "$location"; ?></h5>

<a href="../index.php"><img border="0" src="img/<?php echo "$image"; ?>"></a>
</div>

<br /> 

 

and for the even rows

 


<div class="ac2">
<h5><?php echo "$location"; ?></h5>

<a href="../index.php"><img border="0" src="img/<?php echo "$image"; ?>"></a>
</div>

<br /> 

this is merely a matter of using the modulus operator to check if the counter is even or odd on each iteration of the loop:

 

 for ($i=0; $i<$number; $i++) {
$location = mysql_result($result,$i, "location");
$image = mysql_result($result,$i, "image");
$div_class = ($i % 2 == 1) ? 'class_if_odd' : 'class_if_even';
?>


<div class="<?php echo $div_class; ?>">
<h5><?php echo "$location"; ?></h5>

<a href="../index.php"><img border="0" src="img/<?php echo "$image"; ?>"></a>
</div>

<br />

<?php
}

}

?>

 

change the two classes in the line i added to be whatever you want (here, most likely "ac" and "ac2" based on your messages). so you're aware, the modulus operator returns the remainder after dividing by the divisor. here we're using 2, so any even numbers will return 0 (because they're divisible by 2), and odd numbers will return 1.

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.