ID badge - Find services - Online Schooling - Layouts Myspace - Download Anime

PDA

View Full Version : Help with foreach and case returning


glasglow
Mar 25th 2009, 2:34 am
I am using this and when I am echoing there, it's coming from an array and the results are not even. For example if A goes with 1, I am getting B with 1 and c with 2 etc.. The results are uneven which I think is because of the case. Is there anyway that I can even them out on the return?



foreach ($values as $key=>$val)
{

switch ($val['tag'])
{
case 'DetailPageURL':
$DetailPageURL = $val['value'];
echo '<img border="0" src="'.$MediumImage.'" height="100">';
break;
case 'ItemAttributes':
$inItemAttributes = ($val['type'] == 'open');
echo '<h1><a href='.$DetailPageURL.'>'.$itemAttributes['Title'].'</a></h1><br>'.$itemAttributes['FormattedPrice'].'<hr>';
break;
case 'MediumImage':
$inMediumImage = ($val['type'] == 'open');
break;
case 'EditorialReview':
$inEditorialReview = ($val['type'] == 'open');
break;
case 'Content':
if ($inEditorialReview) $EditorialReview = $val['value'];
break;
case 'URL':
if ($inMediumImage) $MediumImage = $val['value'];
break;
default:
if ($inItemAttributes)
{
$itemAttributes[$val['tag']] = $val['value'];
}
}
}
//$Title = $itemAttributes['Title'];

//echo "<h1><a href=\"$DetailPageURL\">$Title</a></h1>";

//echo "<img src=\"$MediumImage\" align=\"right\">";

//echo $EditorialReview;
}

centralb
Mar 25th 2009, 3:46 am
Can you clarify with an example what you mean by this?

For example if A goes with 1, I am getting B with 1 and c with 2 etc.. The results are uneven which I think is because of the case.

From a quick glance: You might want to clear some or all of the contents of $itemAtributes before the switch block. Each iteration of foreach is re-using $itemAttributes that was changed in previous iterations. If this isn't intentional, start there.

glasglow
Mar 25th 2009, 7:20 am
Actually that is an array.

$inItemAttributes = false;
$inMediumImage = false;
$inEditorialReview = false;
$itemAttributes = array();
foreach ($values as $key=>$val)
{

switch ($val['tag'])
{
case 'DetailPageURL':
$DetailPageURL = $val['value'];
echo '<img border="0" src="'.$MediumImage.'" height="100">';
break;
case 'ItemAttributes':
$inItemAttributes = ($val['type'] == 'open');
echo '<h1><a href='.$DetailPageURL.'>'.$itemAttributes['Title'].'</a></h1><br>'.$itemAttributes['FormattedPrice'].'<hr>';
break;
case 'MediumImage':
$inMediumImage = ($val['type'] == 'open');
break;
case 'EditorialReview':
$inEditorialReview = ($val['type'] == 'open');
break;
case 'Content':
if ($inEditorialReview) $EditorialReview = $val['value'];
break;
case 'URL':
if ($inMediumImage) $MediumImage = $val['value'];
break;
default:
if ($inItemAttributes)
{
$itemAttributes[$val['tag']] = $val['value'];
}
}
}

centralb
Mar 25th 2009, 7:40 am
Actually that is an array.

Yes, from the usage in your sample code, it was clear that $itemAttributes is an array there. However, without further details, the recommendation is the same:

From a quick glance: You might want to clear some or all of the contents of $itemAttributes before the switch block. Each iteration of foreach is re-using $itemAttributes that was changed in previous iterations. If this isn't intentional, start there.


Please carefully read and provide additional detail if your problem persists. Good luck!

glasglow
Mar 25th 2009, 8:27 am
Yes, from the usage in your sample code, it was clear that $itemAttributes is an array there. However, without further details, the recommendation is the same:



Please carefully read and provide additional detail if your problem persists. Good luck!

I see I see.. Thanks for the help.