dennismonsewicz Posted September 23, 2009 Share Posted September 23, 2009 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()); Quote Link to comment https://forums.phpfreaks.com/topic/175256-solved-limiting-loop-question/ Share on other sites More sharing options...
.josh Posted September 23, 2009 Share Posted September 23, 2009 $limit = 10; for ($x = 0;$x < $limit; $x++) { $results = mysql_fetch_assoc($structure); echo '<td>'; echo $results['Field']; echo '</td>'; $array[] = $results['Field']; } Quote Link to comment https://forums.phpfreaks.com/topic/175256-solved-limiting-loop-question/#findComment-923680 Share on other sites More sharing options...
dennismonsewicz Posted September 23, 2009 Author Share Posted September 23, 2009 sweet Thanks!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/175256-solved-limiting-loop-question/#findComment-923683 Share on other sites More sharing options...
knsito Posted September 23, 2009 Share Posted September 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175256-solved-limiting-loop-question/#findComment-923686 Share on other sites More sharing options...
.josh Posted September 23, 2009 Share Posted September 23, 2009 So...how is that better? Way I see it, your method is technically less efficient, as it runs 2 conditions every iteration. Quote Link to comment https://forums.phpfreaks.com/topic/175256-solved-limiting-loop-question/#findComment-923699 Share on other sites More sharing options...
knsito Posted September 23, 2009 Share Posted September 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175256-solved-limiting-loop-question/#findComment-923713 Share on other sites More sharing options...
.josh Posted September 23, 2009 Share Posted September 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175256-solved-limiting-loop-question/#findComment-923721 Share on other sites More sharing options...
knsito Posted September 23, 2009 Share Posted September 23, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/175256-solved-limiting-loop-question/#findComment-923731 Share on other sites More sharing options...
dennismonsewicz Posted September 23, 2009 Author Share Posted September 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175256-solved-limiting-loop-question/#findComment-923740 Share on other sites More sharing options...
knsito Posted September 23, 2009 Share Posted September 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175256-solved-limiting-loop-question/#findComment-923741 Share on other sites More sharing options...
dennismonsewicz Posted September 23, 2009 Author Share Posted September 23, 2009 sweet! it works now! I also had forgot to place a semicolon after the $i++ but thanks for EVERYONE'S help!!! Quote Link to comment https://forums.phpfreaks.com/topic/175256-solved-limiting-loop-question/#findComment-923749 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.