Solved

Create a loop within a while loop

Posted on 2015-02-04
7
95 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
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 108

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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 4

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 108

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now