Jump to content

[SOLVED] limiting loop question


dennismonsewicz

Recommended Posts

I have been doing a lot of studying up on loops here lately, hence the reason for all of my loop questions, and I am stuck on a particular script I am trying to finish.

 

code:

while($results = mysql_fetch_assoc($structure)) {
	echo '<td>';
	      echo $results['Field'];
	echo '</td>';

$array[] = $results['Field'];
}

 

How would I go about limiting the number of fields that show up?

 

Here is the SQL query I am using to pull the fields:

$tableName = mysql_real_escape_string($_GET['tablename']);
$structure = mysql_query("DESCRIBE " . $tableName)or die(mysql_error());

Link to comment
https://forums.phpfreaks.com/topic/175256-solved-limiting-loop-question/
Share on other sites

$limit = 10;
for ($x = 0;$x < $limit; $x++) {
  $results = mysql_fetch_assoc($structure);
      echo '<td>';
            echo $results['Field'];
      echo '</td>';
                              
   $array[] = $results['Field'];
}

better to keep a counter and use break

http://us2.php.net/manual/en/control-structures.break.php

This will exit the current level of the loop

 

$i=0;

while($result=mysql_fecth....){

  $i++;

  if($i==10)

  break;

}

 

While you are learning take a look at continue as well

http://us2.php.net/manual/en/control-structures.continue.php

So...how is that better?  Way I see it, your method is technically less efficient, as it runs 2 conditions every iteration.

 

Your method will result in undesireable results.

If the number of rows you get back from the table is less than 10 you are echoing empty cells and adding Nulls into your array.

 

Besides the OP said he was trying to learn, why not point him to useful constructs such as break and continue, since for loops are a dime a dozen

 

So...how is that better?  Way I see it, your method is technically less efficient, as it runs 2 conditions every iteration.

 

Your method will result in undesireable results.

If the number of rows you get back from the table is less than 10 you are echoing empty cells and adding Nulls into your array.

 

Umm..the same can be said about your method.

 

Umm..the same can be said about your method.

 

eh?

 

<?php
$structure = array(1,2,3,4,false); //imitating the data from mysql_fetch_assoc
/* http://us2.php.net/manual/en/function.mysql-fetch-assoc.php */

$limit = 10;
for ($x = 0;$x < $limit; $x++) {
  $array[] = $structure[$x];
}
var_dump($array);

/* ================= */

unset($array);
$i=0;
while($result=$structure[$i]){
$array[] = $result;
$i++;
if($i==10)
	break;
}
var_dump($array);
?>

ok so I am trying the second opinion on this loop stuff and here is my code:

 

$i = 0;
							  while($results = mysql_fetch_assoc($structure)) {
								$i++
								if($i == 10) {
									 echo '<td>';
								            echo $results['Field'];
								      echo '</td>';

								   $array[] = $results['Field'];
									break;
								}
							}

 

I now get a blank page :(

Hi

 

You want to put

echo '<td>';
echo $results['Field'];
echo '</td>';
$array[] = $results['Field'];

 

Before your if statment, you only need the break; in there. This will exit your while loop when 10 is reached

if($i == 10) { 
   break;
}

 

ok so I am trying the second opinion on this loop stuff and here is my code:

 

$i = 0;
							  while($results = mysql_fetch_assoc($structure)) {
								$i++
								if($i == 10) {
									 echo '<td>';
								            echo $results['Field'];
								      echo '</td>';

								   $array[] = $results['Field'];
									break;
								}
							}

 

I now get a blank page :(

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.