stephenlk Posted May 31, 2009 Share Posted May 31, 2009 In the fieldset (but not restricted to) the code is different than before it was put through foreach, also it is outputting six items when the array only has 4. What am I doing incorrect? <?php $result = mysql_query("SELECT * FROM module WHERE page_id='".$currentpage." ORDER BY order ASC'") or die(mysql_error()); $dbarray = mysql_fetch_array($result); echo $dbarray['title']; foreach ($dbarray as $array){ if ($array['fieldset'] == 1) { echo "<fieldset><legend>".$array['title']."</legend>"; echo "content"; echo "</fieldset>"; } } ?> results in this HTML (reformatted so it's easier to read(previously on one line)) Address <fieldset> <legend> 1 </legend> content </fieldset> <fieldset> <legend> 1 </legend> content </fieldset> <fieldset> <legend> 1 </legend> content </fieldset> <fieldset> <legend> 1 </legend> content </fieldset> <fieldset> <legend> 1 </legend> content </fieldset> <fieldset> <legend> 1 </legend> content </fieldset> nonreformatted Address<fieldset><legend>1</legend>content</fieldset><fieldset><legend>1</legend>content</fieldset><fieldset><legend>1</legend>content</fieldset><fieldset><legend>1</legend>content</fieldset><fieldset><legend>1</legend>content</fieldset><fieldset><legend>1</legend>content</fieldset> if I comment out the less necessary code (like this) <?php $result = mysql_query("SELECT * FROM module WHERE page_id='".$currentpage." ORDER BY order ASC'") or die(mysql_error()); $dbarray = mysql_fetch_array($result); echo $dbarray['title']; foreach ($dbarray as $array){ //if ($array['fieldset'] == 1) { echo "<fieldset><legend>".$array['title']."</legend>"; echo "content"; echo "</fieldset>"; //} } ?> i get Address<fieldset><legend>5</legend>content</fieldset><fieldset><legend>5</legend>content</fieldset><fieldset><legend>3</legend>content</fieldset><fieldset><legend>3</legend>content</fieldset><fieldset><legend>1</legend>content</fieldset><fieldset><legend>1</legend>content</fieldset><fieldset><legend>A</legend>content</fieldset><fieldset><legend>A</legend>content</fieldset><fieldset><legend>0</legend>content</fieldset><fieldset><legend>0</legend>content</fieldset><fieldset><legend>0</legend>content</fieldset><fieldset><legend>0</legend>content</fieldset><fieldset><legend>3</legend>content</fieldset><fieldset><legend>3</legend>content</fieldset><fieldset><legend>1</legend>content</fieldset><fieldset><legend>1</legend>content</fieldset><fieldset><legend>1</legend>content</fieldset><fieldset><legend>1</legend>content</fieldset> Quote Link to comment https://forums.phpfreaks.com/topic/160323-solved-foreach-results-seemingly-incorrect/ Share on other sites More sharing options...
Philip Posted May 31, 2009 Share Posted May 31, 2009 Well, you're not running $dbarray through a loop to get each result. That is of course, unless you wanted just one result. above the foreach, put: echo count($dbarray); Quote Link to comment https://forums.phpfreaks.com/topic/160323-solved-foreach-results-seemingly-incorrect/#findComment-846039 Share on other sites More sharing options...
stephenlk Posted May 31, 2009 Author Share Posted May 31, 2009 The first echo is just to demonstrate the values are different (before being put through the foreach vs. after) When I counted I got 18, which is odd as I'm only calling 4 rows from the database. Quote Link to comment https://forums.phpfreaks.com/topic/160323-solved-foreach-results-seemingly-incorrect/#findComment-846042 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.