Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8953939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:12:20+00:00 2026-06-15T14:12:20+00:00

I have a div named as container and inside container I have two div

  • 0

I have a div named as container and inside container I have two div column1 and column2.Inside column1 and column2 I have to fetch record from database and have to show it here.My code look like this

  <div id="container" style="height:600px; width:600px; border:1px solid #990000">
 <?php $str=mysql_query("SELECT * FROM users") or die(mysql_error());
  while($rec=mysql_fetch_array($str){
 ?>
<div id="column1" style="height:200px; width:295px; border:1px solid #0000FF;float:left;">

  /*Here this record should be of id=1 */
 <img src="<?php echo $rec['pic'];?>" alt="No image" />
 <?php echo $rec['detail'];
</div>
<div id="column2" style="height:200px; width:295px; border:1px solid #0000FF;float:left;">

 /*Here this record should be of id=2 */
<img src="<?php echo $rec['pic'];?>" alt="No image" />
 <?php echo $rec['detail'];
</div>
 <?php } ?>

I want to have in column1 record of user having id=1 and in column2 id=2 and again in column1 id=3 and column2 id=4 and so on. Any help me in this looping?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:12:20+00:00Added an answer on June 15, 2026 at 2:12 pm

    You are using a deprecated extension, by the way. I highly recommend you use the newer MySQLi or PDO extensions to interface with your MySQL database in PHP for new development as this will make your job so much easier.

    The answer to what you want to do is simply to traverse over the result set in multiples of two, which can be achieved as demonstrated below with the PDO extension.

    <div id="container">
    <?php
    $pdo = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'mysqlusername', 'mysqlpassword');
    $stmt = $pdo->query('SELECT * FROM users');
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
    
    /* And now do your loop in pairs of two */
    
    foreach (array_chunk($result, 2) as $pair) {
        ?>
        <div id="1">
            <?php echo $pair[0]['detail']; ?>
        </div>
        <div id="2">
            <?php echo $pair[1]['detail']; ?>
        </div>
        <?php
    }
    ?>
    </div>
    

    The other option is to use a counter and flip it with each iteration then follow that by a condition to determine which div you want to output for this iteration of the loop.

    <div id="container">
    <?php
    $pdo = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'mysqlusername', 'mysqlpassword');
    $stmt = $pdo->query('SELECT * FROM users');
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
    
    /* And now do your loop in pairs of two */
    
    $div = 0;
    foreach ($result as $row) {
        $div ^= 1; // bitwise XOR
        if ($div) { // If it's odd show the first div
            ?>
            <div id="1">
                <?php echo $row['detail']; ?>
            </div>
            <?php
        } else {  // If it's even show the second div
            ?>
            <div id="2">
                <?php echo $row['detail']; ?>
            </div>
            <?php
        }
    }
    ?>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my html page i have two divs inside a container div. The two
Suppose I have 5 div classes named .content inside a div with an id
Situation: I have a check box and a div container inside another div container.
I have a content div and inside it there are two div's with background
I have Div named Container whose width is 100%; so its actual width in
I have got a div id event_container and two div classes inside that div
I have a div class named switch control which has a div class named
I have the following code: HTML <div id='foo'> <input type='checkbox' value='english' name='bar[english]' /> <input
I have the following code: <div><strong>Name: </strong><?php echo anchor('http://'.$link, $row->Name); ?></div> Which takes a
What's the best way to have the following? Name: [Textbox] Inside a div? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.