2013-04-06 34 views
2

我不明白我的代码有什么问题。我在Joomla下模拟一个网站! 2.5.9和VirtueMart 2.0.20b。从PHP中的数组获取密钥Foreach语句

我'上目录页tplname/html/com_virtuemart/category/default.php产品页面tplname/html/com_virtuemart/productdetails/default.php工作。

在这两个页面上,我需要显示以%计的税:即:20%的税。

对于产品页面是:

$product->prices['Tax']; 

对于分类页面是:

<?php foreach ($products as $product):?> 
    <li> 
     <?php echo $product->prices['Tax'];?> 
    </li> 
<?php endforeach;?> 

在这两个做一个var_dump()它给了我这样的输出:

array 
    204 => // <- Remember this number as FIRST ARRAY 
    array 
     0 => string 'TAXNAME_TITLE' (length=12) 
     1 => string '10.0000' (length=7) // <- I need this value! 
     2 => string '+%' (length=2) 
     3 => string '1' (length=1) 
     4 => string '47' (length=2) 
     5 => string '' (length=0) 
     6 => string '1' (length=1) 
     7 => string '204' (length=3) 

要获得我需要的价值,我这样做:

$productPriceTaxUnit = reset($product->prices['Tax']); 
$productPriceTaxUnit = $productPriceTaxUnit['1']; 
$productPriceTaxUnit = JText::_('COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX') . ' ' .number_format($productPriceTaxUnit, '2', '.', '') . '%'; 
echo $productPriceTaxUnit; 

它工作正常,但一个类别页面,FIRST ARRAY等于204上gaves我一个错误,但如果它等于1那么一切都很好。

有人看到错误的一步,我在这里做?

在此先感谢

====== ** 1 **修订========

这里是一个SQL调试:

SELECT SQL_CALC_FOUND_ROWS l.`virtuemart_product_id` 
    FROM `ar9hu_virtuemart_products_sk_sk` as l JOIN `ar9hu_virtuemart_products` AS p using (`virtuemart_product_id`) 
    LEFT JOIN `ar9hu_virtuemart_product_categories` as pc 
    ON p.`virtuemart_product_id` = `pc`.`virtuemart_product_id` 
    LEFT JOIN `ar9hu_virtuemart_categories_sk_sk` as c 
    ON c.`virtuemart_category_id` = `pc`.`virtuemart_category_id` 
    LEFT JOIN `ar9hu_virtuemart_product_shoppergroups` 
    ON p.`virtuemart_product_id` = `ar9hu_virtuemart_product_shoppergroups`.`virtuemart_product_id` 
    LEFT 
    OUTER JOIN `ar9hu_virtuemart_shoppergroups` as s 
    ON s.`virtuemart_shoppergroup_id` = `ar9hu_virtuemart_product_shoppergroups`.`virtuemart_shoppergroup_id` 
    WHERE (p.`published`="1" 
    AND `pc`.`virtuemart_category_id` = 9 
    AND `pc`.`virtuemart_category_id` > 0 
    AND (s.`virtuemart_shoppergroup_id`= "1" OR s.`virtuemart_shoppergroup_id` IS NULL)) 
    group by p.`virtuemart_product_id` 
    ORDER BY `p`.product_sku ASC 
    LIMIT 0, 10 

== ==== ** 2日修订版** ========

我这是在分类页面上得到的错误:

Notice: Trying to get property of non-object in D:\wamp\www\baranik\templates\baranik\html\com_virtuemart\category\default.php on line 119 
Call Stack 
# Time Memory Function Location 
1 0.0002 652096 {main}() ..\index.php:0 
2 0.0714 9131016 JSite->dispatch() ..\index.php:42 
3 0.0749 9542784 JComponentHelper::renderComponent() ..\application.php:197 
4 0.0785 9691392 JComponentHelper::executeComponent() ..\helper.php:351 
5 0.0789 9787480 require_once('D:\wamp\www\baranik\components\com_virtuemart\virtuemart.php') ..\helper.php:383 
6 0.0980 12819808 JController->execute() ..\virtuemart.php:99 
7 0.0980 12819888 VirtueMartControllerCategory->display() ..\controller.php:761 
8 0.0980 12821880 JController->display() ..\category.php:60 
9 0.1006 13209152 VirtuemartViewCategory->display() ..\controller.php:722 
10 0.2021 19796592 JView->display() ..\view.html.php:244 
11 0.2021 19796592 JView->loadTemplate() ..\view.php:205 
12 0.2031 19910088 include('D:\wamp\www\baranik\templates\baranik\html\com_virtuemart\category\default.php') ..\view.php:649 
+0

我不知道为什么,这有一个downvote。这似乎是一个有效的问题,提问者在其背后进行了有效的研究。 +1 – seanbreeden 2013-04-06 13:41:21

+1

你会得到什么错误? – Barmar 2013-04-06 13:59:05

+0

@Barmar - 我已经更新了一个问题,请检出第二次更新 – aspirinemaga 2013-04-06 18:34:23

回答

1

我不确定我是否正确理解你的问题,但我可以回答你的问题的标题。

“获取从阵列PHP foreach语句中的一个关键”

你可以通过遍历数组,并使用获得的密钥和值:

foreach($array as $key => $value){ 
    echo "$key has a value of $value<br>"; 
}