Solved

Create a loop within a while loop

Posted on 2015-02-04
7
98 Views
Last Modified: 2016-02-25
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
Comment
Question by:damianb123
[X]
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
7 Comments
 
LVL 9

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?
0
 
LVL 110

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

Author Comment

by:damianb123
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': 
        $('#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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 5

Accepted Solution

by:
Jim Riddles earned 500 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
    $i++;
    //whatever code you require here
?>

Open in new window


Let me know if that makes sense.
0
 
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
0
 

Author Closing Comment

by:damianb123
ID: 40591503
Perfect, just what I needed.

Thanks.
0
 
LVL 110

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

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 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