Create a loop within a while loop

Posted on 2015-02-04
Medium Priority
Last Modified: 2016-02-25
    I have a query which returns all the rows from  a mysql table, basically on every row I want to increment a number by 1, so on each row I would see:

$variable1, $variable2, $variable3 and so on.....

I have the while loop working fine, which is set up like this:

 while ($rows1 = mysql_fetch_array($query)) {  


<div style="width: 1000px; margin-left: auto; margin-right: auto;">


		<div style="float: left; width: 230px; height: 45px; font-size: 11pt; font-family:Arial; font-weight:bold; text-align: left;"><div style="margin-left:7px; margin-top:7px">test1</div>	

                <div style="height: 45px; float: left; width: 180px; font-family:Arial; color: #000; font-size: 12pt; font-weight:bold; text-align: left; overflow: hidden;">test2</div>

                <div style="height: 45px; float: left; width: 180px; font-family:Arial; color: #000; font-size: 12pt; font-weight:bold; text-align: left; overflow: hidden;">test3</div>

                <div style="height: 45px; float: left; width: 180px; font-family:Arial; color: #000; font-size: 12pt; font-weight:bold; text-align: left; overflow: hidden;">test4</div>

                <div style="height: 45px; float: left; width: 180px; font-family:Arial; color: #000; font-size: 12pt; font-weight:bold; text-align: left; overflow: hidden;">test5</div>



Open in new window

The above works fine, but if I add a loop within the loop i.e. something like this:

while ($i <= 10) { // Output values from 0 to 10
   $number = $1;

Open in new window

Then when the above is run, the $i is the same on ALL the lines pulled from the database, rather than each row being +1 of the previous.

What am I doing wrong?  Can anyone help, and point me in the right direction.

Question by:damianb123
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

by:Brian Tao
ID: 40590598
Can you provide your code in one piece?  so that we know:
What do you expect the $i to do?  
Where do you out put $i?
What is $x doing here?
LVL 111

Expert Comment

by:Ray Paseur
ID: 40590826
Can you please rephrase this question in plain, non-technical terms?  Nobody does anything like what you describe -- it's not a design pattern that is used in information technology.  I think if we could understand what your application needs to do (what it has for inputs and what it should produce for outputs) we might be able to suggest a practical approach.

Author Comment

ID: 40590845
Hi Brian/Ray,
    Sorry I should have explained in more detail.  Basically, I have a jquery which adjusts the width of a div, using a block of code like this:

$(document).ready(function() { 

$('#expand').click(function () { 

switch($('#bookexp').css('width') )


case '180px': 


case '720px':





Open in new window

This is great and adjusts the div accordingly, which looks like this:

<div id="bookexp" style="width=180px;"><div class="<?php echo $class1; ?>" style="float: left; width:60px; height: 40px;">Test</div>

Open in new window

class="<?php echo $class1; ?>", basically just changes the colour on the background.  

The issue I have is, this div is part of a while loop, so the ID is always the same -  id="bookexp", and you can't have 2 ID's the same - the the expand of the column does not work (the top part does)....

So I thought if I could add a loop to add 1 to the ID i.e. id="bookexp", id="bookexp1", id="bookexp2", inside the loop then this would work.  

I hope that helps.  I have added a screenshot of what it looks like currently when expanded.

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!


Accepted Solution

Jim Riddles earned 2000 total points
ID: 40591030
Not that I am condoning what you are doing, but I think I see what you are attempting to accomplish.

You can't have that while loop inside your main loop, because it will simply run the while loop until the condition is met, and your $i variable will always be 10 when the loop quits.

Simply add the $i=0 outside of your code that loops through the rows in your mysql database and increment it within that loop.

Something like this:

//set the id counter
$i = 0;
while ($rows1 = mysql_fetch_array($query)) {
    //increment the id counter
    //whatever code you require here

Open in new window

Let me know if that makes sense.
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40591374
Although you can't have more than one element with the same ID, you can have more than one element with the same class, so why not just switch to using a class instead of an ID, then you wouldn't need to worry about incrementing the ID.

You'll need to adjust your jQuery, but it'll make life a lot easier

Author Closing Comment

ID: 40591503
Perfect, just what I needed.

LVL 111

Expert Comment

by:Ray Paseur
ID: 40592680
CSS Id's must be unique.   CSS classes may address zero, one of more than one element.  You may find a better solution to this issue if you use Twitter Bootstrap, which is designed for responsive displays.  Amazingly easy to use!

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question