Sam
Sam

Reputation: 7058

While loop into 2 different divs?

I've got an about page, and two HTML columns and I want to while loop the contents of my mysql query into BOTH columns, even though they are defined as two different divs. What I was thinking of doing is somehow dividing the amount of rows in the mysql database by 2, and then showing half and half, but I'm not sure how to do this (specifically doing the half and half mysql queries)

Could someone please either show me how I can halve a mysql database into two parts, or come up with a better solution to my problem? Thankyou :).

EDIT:

<?php $djs_all_db = mysql_query("SELECT * FROM djs")
        or die(mysql_error());      
        while($djs_all = mysql_fetch_array( $djs_all_db )) {
        echo    "
        <div class="row">
        <!--It does it once here-->
        <div class=\"column column-2\">
                <img src=\"images/about/" . $djs['username'] . "-profile.png\" alt=\"Profile\" class=\"profile-image\"/>

                <p class=\"float-left\"><strong>" . $djs['realname'] . "</strong></p>
                <p class=\"float-right\"><a href=\"#\" title=\"\">" . $djs['twitterusername'] . "</a></p>

                <div class=\"clear\"></div>

                <p>" . $djs['biography'] . "</p>

            </div>
        <!--but now for column 2?-->
            <div class=\"column column-3\">
                <img src=\"images/about/profile.png\" alt=\"Profile\" class=\"profile-image\"/>

                <p class=\"float-left\"><strong>Edward Smith, Developer</strong></p>
                <p class=\"float-right\"><a href=\"#\" title=\"\">Twitter</a></p>

                <div class=\"clear\"></div>

                <p>In tellus arcu, luctus sed vulputate ut, dictum sed nisi. Suspendisse commodo, enim sed mollis cursus, urna quam laoreet velit, vel sollicitudin arcu augue at quam.</p>
            </div>

        </div> <!--/.row-->" ?>

Upvotes: 0

Views: 2851

Answers (3)

Ian P
Ian P

Reputation: 12993

As this reads to me, you're wanting to concatenate the contents of two separate columns and multiple rows in a table into two divs?

Try this:

while ($result = mysql_fetch_assoc($db_result)
{
  $div1 .= $result['column1'];
  $div2 .= $result['column2'];
}

echo "<div class='div1'>".$div1."</div>";
echo "<div class='div2'>".$div2."</div>";

Hope I read the question correctly....

Good luck.

Upvotes: 0

Michał Pękała
Michał Pękała

Reputation: 2389

You can append every odd to 1st column and every even to 2nd column variables and then flush them into the HTML.

See my pseudocode:

while(there_are_more_rows) {
  if(row_num_is_odd) {
    $first_column += $result_row;
  } else {
    $second_column += $result_row;
  }
}
print "<div='1st-column'>" . $first_column . "</div>";
print "<div='2nd-column'>" . $second_column . "</div>";

Upvotes: 2

cypher
cypher

Reputation: 6992

Try something like this:

$i=0;
//1rst div:
        while((count($mysql_results)/2) < $i) 
        {
            echo $mysql_results[$i]; 
            i++;
        }
//2nd div: 
        while((count($mysql_results)/2 >= $i) 
        {
            echo $mysql_results[$i]; 
            i++;
        }

Upvotes: 0

Related Questions