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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.