简体   繁体   English

PHP foreach覆盖结果Joomla 2.5

[英]PHP foreach overwriting results Joomla 2.5

I Everyone I am currently creating a component for Joomla 2.5 that uses jvectormap, and for some reason i am getting all of the continents and countries in a json but only the latest added firms and not the older ones... i think it may be to do with my foreach statement... But not really sure in all honesty: 我每个人目前正在为使用jvectormap的Joomla 2.5创建一个组件,由于某种原因,我将所有大洲和国家/地区都放在json中,但仅添加了最新添加的公司,而不是较旧的公司...我认为可能是与我的foreach语句有关...但实际上并不能完全确定:

my model file: 我的模型文件:

public function getLand()
{
$land_id = JRequest::getInt('id');

$db = $this->getDbo();
$query = $db->getQuery(true);

$query->select('*, a.continent_name,continent_facts');
$query->from('#__vectormap_countries AS t')
        ->join('LEFT', '#__vectormap_continents AS a USING(continent_id)')
        ->where('t.published = 1');

$db->setQuery($query);

$rows = $db->loadObjectList();

return $rows;
}

public function getFirm(){
    $firm_id = JRequest::getInt('id');

    $db = $this->getDbo();
    $query = $db->getQuery(true);

    $query->select('*');
    $query->from('#__vectormap_firms AS t')
            ->where('t.published = 1');

    $db->setQuery($query);

    $firms = $db->loadObjectList();

    return $firms;
}

and then the part that is echo'ing the array my view.json.php: 然后回显数组的部分是我的view.json.php:

public function display($tpl = null)
{
$land = $this->get('Land');
$firm = $this->get('Firm');
$response = array();

foreach ($land as $row) {
        $response[$row->country_code] = array(
                    'path' => $row->country_svgpath,
                    'name' => $row->country_name,
                    'continent' => $row->continent_name,
                    'fact' => $row->continent_facts
        );
};

foreach ($firm as $firms) {
        $response['firms'] = array (
            'firm_name' => $firms->firm_companyname,
            'firm_latitude' => $firms->firm_latitude,
            'firm_longitude' => $firms->firm_longitude
            );
        };
echo json_encode($response);
}

and that returns everything in the first foreach but in the second foreach it only returns the latest firm not all of them.. 并且在第一个foreach中返回所有内容,但是在第二个foreach中,它仅返回最新的公司,而不是全部。

Please don't hesitate to ask if further detail is required.. Any Help Greatly Apreciated. 请毫不犹豫地询问是否需要更多详细信息。.非常感谢。 Thank you :) 谢谢 :)

Change the second loop to: 将第二个循环更改为:

$response['firms'] = array();
foreach ($firm as $firms) {
        $response['firms'][] = array (
            'firm_name' => $firms->firm_companyname,
            'firm_latitude' => $firms->firm_latitude,
            'firm_longitude' => $firms->firm_longitude
            );
        };

With this, $response['firms'] will be an indexed array listing all the firms. 这样, $response['firms']将成为列出所有公司的索引数组。

The key in the second foreach is fixed, as $response['firms'] , so later runs will over-write earlier ones. 第二个foreach中的键是固定的,如$response['firms'] ,因此以后的运行将覆盖以前的运行。 You can try something like this: 您可以尝试如下操作:

foreach ($firm as $firms) {
    $response[$firms->firm_companyname] = array (
        'firm_latitude' => $firms->firm_latitude,
        'firm_longitude' => $firms->firm_longitude
        );
    };

Which will give you the company name as a key - you need something that's going to change on each run of the loop. 这将为您提供公司名称作为关键字-您需要在每次循环运行中进行更改。 You could also have an incrementing integer: 您还可以有一个递增的整数:

$firmcount = 0;
foreach ($firm as $firms) {
    $response['firms' . $firmcount++] = array (
        'firm_name' => $firms->firm_companyname,
        'firm_latitude' => $firms->firm_latitude,
        'firm_longitude' => $firms->firm_longitude
        );
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM