好的,我希望这不是太具体。我有一个数据库驱动的CMS,同事在其中使用了许多类别。以下是它与我们现在拥有的产品相呼应的方式:
$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";}
这很好用。我的问题是我想从其他类别,类别2获得offera,offerb和offerc。我试过这样:
$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";}
但当然这不起作用。我不希望我的同事必须通过CMS并复制所有信息,有没有办法使这项工作?
答案 0 :(得分:0)
假设两次调用get_offers
的结果排序匹配,我相信这可能适合您:
$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";
}
答案 1 :(得分:0)
我同意ngm。如果类别匹配,那么您可以使用以下内容从不同的调用中获取结果:
$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";
}
此示例将执行相同的操作,但您将其保留在foreach循环中。通过使用$ key =&gt; $行,您可以访问数组的键。我也冒昧直接回应你的变量,而不是把它们放在变量中。如果将{}放在数组变量(或类变量)周围,可以在echo,print或&lt;&lt;&lt;中使用它们。这让我的生活更轻松。
示例:echo "This variable {$variable['test']}";