Solved

Create a loop within a while loop

Posted on 2015-02-04
7
94 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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 42

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

757 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

18 Experts available now in Live!

Get 1:1 Help Now