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

  • Home
  • SEARCH
  • 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 6108411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:17:37+00:00 2026-05-23T14:17:37+00:00

I am new to PHP , I hope someone can help me. I have

  • 0

I am new to PHP , I hope someone can help me. I have a table which contains “id” , “img” , “link” and “desc” in mysql database . I want it to echo all out into something like this :

    <div id="featured">
    <a target='_blank' href='link'><img src='image link1' title='description'/></a>
    <a target='_blank' href='link'><img src='image link2' title='description'/></a>
    <a target='_blank' href='link'><img src='image link3' title='description'/></a>
    <a target='_blank' href='link'><img src='image link4' title='description'/></a>
    </div>
    <div id="cap">
    <span class='desc' id='idnumber'>Description1</span>
    <span class='desc' id='idnumber'>Description2</span>
    <span class='desc' id='idnumber'>Description3</span>
    <span class='desc' id='idnumber'>Description4</span>
    </div>

PHP CODE:

<?php
  require_once "./dbconfig.php";
  opendb();
  $sql = "SELECT * FROM images ORDER BY id DESC"; 
  $query = querydb($sql);
?>
<div id="featured"> 
<?php
  while($row = mysql_fetch_array( $query )){
    echo "<a target='_blank' href='$row[link]'><img src='$row[img]' title='$row[desc]'/></a>";
  }
?>
</div>
<div id="cap">
<?php
  while($row = mysql_fetch_array( $query )){
    echo "<span class='desc' id='$row[id]'>$row[desc]</span>";
  }
closedb();
?>
</div>

Can WHILE being used twice or i am wrong? when i run this code , the second while loop is not working , the spans are not showing at all.

Please help.

  • 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-05-23T14:17:38+00:00Added an answer on May 23, 2026 at 2:17 pm

    The first loop consumes all data from mysql already, so there is nothing left in the second loop. You can store the rows data in the meanwhile however and then re-use that store.

    <div id="featured"> 
    <?php
    $rows = array();
    while($row = mysql_fetch_array( $query )){
      $rows[] = $row;
      echo "<a target='_blank' href='$row[link]'><img src='$row[img]' title='$row[desc]'/></a>";
    }
    ?>
    </div>
    <div id="cap">
    <?php
    foreach($rows as $row){
      echo "<span class='desc' id='$row[id]'>$row[desc]</span>";
    }
    closedb();
    ?>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am new to PHP and hope someone can help me. Sometimes the
I hope someone can help. The following piece of php code (see below) is
I have been trying to figure this out for days. Hope someone can help.
I hope there is a php genius here somewhere who can help me with
Hi Hope someone can help me. My .net app that connects (via tcp) to
I have a folder of jpegs, and a MySQL database that I hope to
so I hope someone would help me. My first page is leave_app.php . In
I really hope someone here can help out with this. I'm using Magento 1.6.1.0
I'm very new to RoR, moving from PHP. I have what I hope is
I hope someone here can help me.. I am currently having a small issue

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.