Jump to content

[SOLVED] foreach results seemingly incorrect


stephenlk

Recommended Posts

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>

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.