0

我正在尝试使用数组对我的标题进行分类。但似乎我已经碰壁了。我的数组是数组中的一个数组,第一个数组保存 group_id,子数组包含有关标题的所有信息。我正在尝试的是在对应于 group_id 的表中返回数组。目前有第 1 组和第 2 组,但将来我会添加更多组。我被卡住的部分是我不能或者我不知道如何选择组的 id 及其在数组中的内容,以便它将显示在正确的表中。我的项目基于 Open cart。这是我到目前为止的代码:这是视图:

     foreach($informations as $information)
       {
            echo '<table border="1"><tr><td>';   
            print_r($information);
            echo '</table></tr></td>';  
       }

这是带有数组的控制器函数:

$information_total = $this->model_catalog_information->getTotalInformations();
$results = $this->model_catalog_information->getInformations($data);
foreach ($results as $result)
 {
   $action = array();
   $action[] = array(
        'text' => $this->language->get('text_edit'),
        'href' => $this->url->link('catalog/information/update', 'token=' . $this->session->data['token'] . '&information_id=' . $result['information_id'] . $url, 'SSL')
    );
   $this->data['informations'][] = array(  $result['groupe_id'] => array(
          'information_id' => $result['information_id'],
          'title'          => $result['title'],
          'master'         => $result['master'],
          'sort_order'     => $result['sort_order'],   
          'selected'       => isset($this->request->post['selected']) && in_array($result['information_id'], $this->request->post['selected']),
          'action'         => $action                               
    ));
     }

Tnx 提前寻求帮助。抱歉,如果我解释得不够清楚,因为英语不是我的第一语言 :) 如果有什么我解释得不够清楚,请问我会回答。

这就是数组输出的内容:

Array
(
    [1] => Array
        (
            [information_id] => 12
            [title] => ABOUT USfdsfds
            [master] => AND OTHERSfdsfds
            [sort_order] => 0
            [selected] => 
            [action] => Array
                (
                    [0] => Array
                        (
                            [text] => Edit
                            [href] => http://localhost/opencart/admin/index.php?route=catalog/information/update&token=be54d82da3beb8855eb80bc49a4b28a4&information_id=12
                        )
                     )
                  )
                )

Array
(
    [1] => Array
        (
            [information_id] => 17
            [title] => dsfsdfds
            [master] => fdsfdsf
            [sort_order] => 0
            [selected] => 
            [action] => Array
                (
                    [0] => Array
                        (
                            [text] => Edit
                            [href] => http://localhost/opencart/admin/index.php?route=catalog/information/update&token=be54d82da3beb8855eb80bc49a4b28a4&information_id=17
                        )
                      )
                   )
                )

Array
(
    [1] => Array
        (
            [information_id] => 14
            [title] => eeee
            [master] => eeeee
            [sort_order] => 0
            [selected] => 
            [action] => Array
                (
                    [0] => Array
                        (
                            [text] => Edit
                            [href] => http://localhost/opencart/admin/index.php?route=catalog/information/update&token=be54d82da3beb8855eb80bc49a4b28a4&information_id=14
                        )
                      )
                    )
                  )

Array
(
    [2] => Array
        (
            [information_id] => 15
            [title] => fffffffffffff
            [master] => fffffffffffffff
            [sort_order] => 0
            [selected] => 
            [action] => Array
                (
                    [0] => Array
                        (
                            [text] => Edit
                            [href] => http://localhost/opencart/admin/index.php?route=catalog/information/update&token=be54d82da3beb8855eb80bc49a4b28a4&information_id=15
                        )
                      )
                    )
                  )

Array
(
    [2] => Array
        (
            [information_id] => 13
            [title] => Hello 
            [master] => Hello Again
            [sort_order] => 0
            [selected] => 
            [action] => Array
                (
                    [0] => Array
                        (
                            [text] => Edit
                            [href] => http://localhost/opencart/admin/index.php?route=catalog/information/update&token=be54d82da3beb8855eb80bc49a4b28a4&information_id=13
                        )
                      )
                   )
                 )

Array
(
    [2] => Array
        (
            [information_id] => 16
            [title] => ssssssssssssssss
            [master] => ssssssssssssssss
            [sort_order] => 0
            [selected] => 
            [action] => Array
                (
                    [0] => Array
                        (
                            [text] => Edit
                            [href] => http://localhost/opencart/admin/index.php?route=catalog/information/update&token=be54d82da3beb8855eb80bc49a4b28a4&information_id=16
                        )
                      )
                    )

我希望表格看起来像这样:

+-------+------------+--------+
| Title | Sort order | Action |
+-------+------------+--------+
4

3 回答 3

0

所以你在寻找这样的东西 -

echo '<table border="1">';
echo '<tr><th>Title</th><th>Sort order</th><th>Action</th></tr>';

foreach($informations as $information){
        echo '<tr><td>'.$information['title'].'</td>';
        echo '<td>'.$information['sort_order'].'</td>';
        echo '<td>'.$information['action'][0]['text'].'</td></tr>';  
}
echo '</table>';
于 2013-07-12T07:43:33.710 回答
0

我更改了您的代码的某些部分以便能够运行它。

运行这个 php 代码并查看结果。这可能会帮助您:

<?php

//The Data:

$theselected=12;
$results=array(
        array(
            'groupe_id'=>1,
            'information_id'=>12,
            'title'=> 'abc',
            'master'=> 'm_abc',
            'sort_order'=>0
        ),
        array(
            'groupe_id'=>1,
            'information_id'=>17,
            'title'=> 'efg',
            'master'=> 'm_efg',
            'sort_order'=>0
        ),
        array(
            'groupe_id'=>2,
            'information_id'=>14,
            'title'=> 'abcdd',
            'master'=> 'm_abcdd',
            'sort_order'=>0
        )
    );


// The Controller:


$data['informations'] = array();
foreach ($results as $result)
{
    $action = array();
    $action = array(
        'text' => 'the_text',
        'href' => 'the_href'
    );
    if ( !isset($data['informations'][$result['groupe_id']]) )
        $data['informations'][$result['groupe_id']] = array();
    array_push(
        $data['informations'][$result['groupe_id']],
        array(
        'information_id' => $result['information_id'],
        'title'          => $result['title'],
        'master'         => $result['master'],
        'sort_order'     => $result['sort_order'],   
        'selected'       => '',
        'action'         => $action                               
        )
    );
}
?>


<?php // The View: ?>


<?php foreach($data['informations'] as $group_id => $information): ?>
    <h3>Group: <?php echo $group_id ?></h3>
    <table border="1">
    <thead><tr>
    <th>Information ID</th><th>title</th><th>master</th><th>sort order</th><th>Action</th>
    </tr></thead>
    <?php foreach ($information as $item): ?>
        <tr>
            <td><?php echo $item['information_id'] ?></td>
            <td><?php echo $item['title'] ?></td>
            <td><?php echo $item['master'] ?></td>
            <td><?php echo $item['sort_order'] ?></td>
            <td><?php echo $item['action']['text'] ?><br><?php echo $item['action']['href'] ?></td>
        </tr>
    <?php endforeach ?>
    </table>
<?php endforeach ?>
于 2013-07-12T07:44:59.260 回答
0

我希望这会奏效。

<?php foreach($informations as $group_id=>$information_array) : ?>
  <?php  foreach($information_array as $information) : ?>
        <tr><td> <?php echo $information['title']' ?></td>
        <td><?php echo $information['sort_order'] ?></td>
        <td>
         <?php  foreach($information['action'] as $action) : ?>
                 <a href="<?php echo $action['href'];?>?id=<?php echo $information['information_id']?>"><?php echo $action['text']?></a> &nbsp
          <?php endforeach ?> 
        </td>
  <?php endforeach ?>    
 <?php endforeach ?>
于 2013-07-12T11:10:24.237 回答