[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 395
  • Last Modified:

How to combine my while and for loop??

I have these two loops that I need to combine:

for($i = 0; $i <= 15; $i++) {
 while($rows = mysql_fetch_array($result)) {

When I have the for loop before the while loop it does nothing, and when I put it after the while loop it has my function output the variables 15 times each!

I just want it two output the first 15 variables of $rows.  Anyone know how to do this?
0
FairyBusiness
Asked:
FairyBusiness
  • 6
  • 3
1 Solution
 
Greg AlexanderLead DeveloperCommented:
You could do it like this
$i = 1;
while($rows = mysql_fetch_array($result)) {
	if($i == 15){
		return exit;	
	}else{
		//echo your data	
	}
$i++;
}

Open in new window


or you can limit 15 in your query maybe?
0
 
FairyBusinessAuthor Commented:
Well what you have works for my echo statements (which I added just for testing)  but it doesnt work on my $output variables. . .

http://auroriella.com/edit_product.php?edit=bracelets&item=b3
$output = "";
		$i = 1;
			while($rows = mysql_fetch_array($result)) {
				if($i == 15){
					return exit;
				}
				else {
				$output .= "<input type=\"checkbox\" name=\"materials[]\" value=\"" . $rows['id'] . "\" ";
				for($x = 0; $x <= count($materials)-1; $x++) {
					if($materials[$x] == $rows['id'] && !empty($rows['type'])) {
						$output .= "checked ";
					}
				}
			$output .=  "/>" . $rows['type'] . "<br />";
			echo "lala";
			}
			echo "hello";
		$i++;
		}

Open in new window

0
 
FairyBusinessAuthor Commented:
Its weird. . . if I change all of my $output .=  to echo then it works!  do  you know why??
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
FairyBusinessAuthor Commented:
Nevermind I got the $outputs to work.

The next part is to output the other half of the array.  I thought to do it the same way, just pick up where I left off:

	$whole = mysql_num_rows($result);
	$half = $whole/2;
	if(isset($_GET['edit'])) {
		$output = "";
		$i = 1;
		while($rows = mysql_fetch_array($result)) {
			if($i <= $half){
				$output .= "<input type=\"checkbox\" name=\"materials[]\" value=\"" . $rows['id'] . "\" ";
				for($x = 0; $x <= count($materials)-1; $x++) {
					if($materials[$x] == $rows['id'] && !empty($rows['type'])) {
						$output .= "checked ";
					}
				}
			$output .=  "/>" . $rows['type'] . "<br />";
			}
		$i++;
		}
		$b = $half;
		while($rows = mysql_fetch_array($result)) {
			if($b <= $whole){
				echo "<input type=\"checkbox\" name=\"materials[]\" value=\"" . $rows['id'] . "\" ";
				for($x = 0; $x <= count($materials)-1; $x++) {
					if($materials[$x] == $rows['id'] && !empty($rows['type'])) {
						$output .= "checked ";
					}
				}
			echo  "/>" . $rows['type'] . "<br />";
			}
		$b++;
		}

Open in new window


but its not doing anything.  Any suggestions??
0
 
FairyBusinessAuthor Commented:
I got it to work!! Thanks for your help again!
0
 
Greg AlexanderLead DeveloperCommented:
May I ask why not loop through all of them at once?
0
 
Greg AlexanderLead DeveloperCommented:
NP, thanks for the points
0
 
FairyBusinessAuthor Commented:
I wanted the first half to appear in a div and the rest to appear in another div.  Both divs have the same class though:  (all in the materials div)

http://auroriella.com/edit_product.php?edit=bracelets&item=b3

It may still need some fine tuning though :P
0
 
Mohamed AbowardaSoftware EngineerCommented:
for($i = 0; $i <= 15; $i++) {
 while($rows = mysql_fetch_array($result)) {

Open in new window


The above code means that PHP will get the sql query results 15 times! well, it's actually wrong!

What you could do is as the following:
$i = 1;
while($rows = mysql_fetch_array($result)) {
	if($i <= 15){
		// Code here to output first 15 row...
	} else {
		// Code here to output the other rows...
	}
$i++;
}

Open in new window

0
 
FairyBusinessAuthor Commented:
Instead of having two divs like I wanted I have one for each variable!  
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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