• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 115
  • Last Modified:

Create a loop within a while loop

Hi,
    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)) {  


        $x++;
 
  
?>


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

	<div>


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


</div>

</div>

Open in new window


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

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

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.

Cheers
0
damianb123
Asked:
damianb123
1 Solution
 
Brian TaoSenior Business Solutions ConsultantCommented:
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?
0
 
Ray PaseurCommented:
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.
0
 
damianb123Author Commented:
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': 
        $('#bookexp').css('width','720px');
        $('#bookexp').removeClass('bg1').addClass('bg2');
        $('#arrivalexp').css('width','0px');
        $('#clinicalexp').css('width','0px');
        $('#followupexp').css('width','0px');

break; 

case '720px':
        $('#bookexp').css('width','180px');
        $('#bookexp').removeClass('bg2').addClass('bg1');
        $('#arrivalexp').css('width','180px');
        $('#clinicalexp').css('width','180px');
        $('#followupexp').css('width','180px');
break; 

default: 

}

});

});

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.

Thanks
ee-img1.PNG
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Jim RiddlesPrepress/OMS SpecialistCommented:
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
    $i++;
    //whatever code you require here
?>

Open in new window


Let me know if that makes sense.
0
 
Chris StanyonCommented:
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
0
 
damianb123Author Commented:
Perfect, just what I needed.

Thanks.
0
 
Ray PaseurCommented:
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!
http://getbootstrap.com/
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now