OK I hope this isn't too specific. I have a database driven CMS that a coworker uses with many categories in it. Here's how it echoes some products we have now:
$offers = get_offers('category1','none','compare');
foreach ($offers as $row) {
$offername = $row['name'];
$offerlogo = $row['logo'];
$offera=$row['detailA'];
$offerb=$row['detailB'];
$offerc=$row['detailC'];
echo "you can have $offername, it's logo looks like <img src='$offerlogo'>" it's characteristics are $offera, offerb, offerc, etc";}
This works fine. My the problem is I want to get offera, offerb and offerc from another category, category 2. I tried going like this:
$offers = get_offers('category1','none','compare');
foreach ($offers as $row) {
$offername = $row['name'];
$offerlogo = $row['logo'];
$offers = get_offers('category2','none','compare');
$offera=$row['detailA'];
$offerb=$row['detailB'];
$offerc=$row['detailC'];
echo "you can have $offername, it's logo looks like <img src='$offerlogo'>" it's characteristics are $offera, offerb, offerc, etc";}
But of course that doesn't开发者_如何学C work. I don't want my coworker to have to go through the CMS and copy all the information over, is there a way to make this work?
Assuming the ordering of the results from both calls to get_offers
matches up, I believe this might work for you:
$offers['cat1'] = get_offers('category1', 'none', 'compare');
$offers['cat2'] = get_offers('category2', 'none', 'compare');
$numberOfOffers = count($offers['cat1']);
for ($i = 0; $i < $numberOfOffers; $i++)
{
$offername = $offers['cat1'][$i]['name'];
$offerlogo = $offers['cat1'][$i]['logo'];
$offera = $offers['cat2'][$i]['detailA'];
$offerb = $offers['cat2'][$i]['detailB'];
$offerc = $offers['cat2'][$i]['detailC'];
echo "you can have $offername, its logo looks like <img src='$offerlogo'> its characteristics are $offera, $offerb, $offerc, etc\n";
}
I agree with ngm. If the categories match up then you could bring the results from the different call with the following:
$offersCat1 = get_offers('category1','none','compare');
$offersCat2 = get_offers('category2','none','compare');
foreach ($offers as $key=>$row) {
$offera=$offersCat2[$key]['detailA'];
$offerb=$offersCat2[$key]['detailB'];
$offerc=$offersCat2[$key]['detailC'];
echo "you can have {$row['name']}, it's logo looks like <img src='{$row['logo']}'> it's characteristics are $offera, offerb, offerc, etc";
}
This example will do the same thing, but you keep it in the foreach loop. By using the $key=>$row you are able to access the key of the array. I also took the liberty to echo your variables directly rather than putting them in variables. If you put {} around an array variable (or class variable) you can use them within an echo, print or <<<. This makes my life easier.
Example: echo "This variable {$variable['test']}";
精彩评论