简体   繁体   English

使用此助手的正确方法是什么?

[英]What is the correct way of using this helper?

I am building a website wit CodeIgniter and I wrote a helper which has a function which returns data based on what is pulled from a database, and I'm not sure how I should lay this out to keep it in good MVC. 我正在使用CodeIgniter构建一个网站,并编写了一个帮助程序,该帮助程序具有根据从数据库中提取的内容返回数据的功能,并且不确定如何将其保留在良好的MVC中。

The Controller: 控制器:

    $char = $this->uri->segment(4);
    $q = $this->kal_db_model->get_char($this->session->userdata('uid'), $char);
    $q_row = $q->row();

    $data['items'] = $this->kal_db_model->get_items($q_row->PID);
    $data['page'] = 'control_view_char';

The Model: 该模型:

function get_items($pid)
{
$kal_db = $this->load->database('kal_db', TRUE); 
$sql = "
        SELECT i.*, n.Name, n.Grade, n.type, p.Name AS PrefixName, m.Name AS MixName
        FROM dbo.Item as i
        INNER JOIN dbo.ItemName as n
        ON i.[Index] = n.[Index]
        INNER JOIN dbo.PrefixName as p
        ON i.[Prefix] = p.[Prefix]
        INNER JOIN dbo.MixName as m
        ON i.[Info] = m.[Info]
        WHERE i.PID = '". $pid ."'
        ";         
$query = $kal_db->query($sql);
return $query;
}

The View: 风景:

<?php if ($items->num_rows() > 0)
          {
             foreach ($items->result() as $item): ?>
          <tr class="odd">
            <td><img src="<?php echo base_url(); ?>/assets/items/<?php echo $item->Index; ?>.bmp" alt="" /></td>
            <?php if($item->type == 1)
                  {
                    echo '<td>G' . $item->Grade . '<span style="color:blue">' . $item->PrefixName . '</span>&nbsp;<span style="color:red">' . $item->XAttack . '/' . $item->UpgrLevel . '/' . $item->XHit . '</span>&nbsp;' .$item->Name; if($item->Info > 50000){ echo '&nbsp;<span style="color:green">' . $item->MixName . '</span>'; } echo '</td>';
                  }
                  elseif($item->type == 2)
                  {
                    echo '<td>G' . $item->Grade . '<span style="color:blue">' . $item->PrefixName . '</span>&nbsp;<span style="color:red">' . $item->XDefense . '/' . $item->XDodge . '</span>&nbsp;' .$item->Name . '</td>';
                  }
                  elseif($item->type == 3)
                  {
                    echo '<td>' .$item->Name . '</td>';
                  }
                  elseif($item->type == 4)
                  {
                    echo '<td><span style="color:blue">G' . $item->Info . '</span>&nbsp;' .$item->Name . '</td>';
                  }

            ?>
          </tr>
        <?php endforeach;
            } ?> 

Simply put, I would like to not display the value of $item->Name in the view, but rather something returned from the helper. 简而言之,我不想在视图中显示$item->Name的值,而是从帮助程序返回的内容。

the helper function looks like this: 辅助函数如下所示:

parse_item($Index, $Prefix, $Info)

and returns something like this: 并返回如下内容:

Array ( [name] => Short Iron Sword [prefix] => The King, GuhBalHan's [mix] => Shadow )

I can get $Index as $item->Index , $Prefix as $item->Prefix and $Info as $item->Info 我可以获得$Index作为$item->Index$Prefix作为$item->Prefix$Info作为$item->Info

I'm not really sure how to do this, as I know its bad practise to load a helper to a view and have alot of logic there obviously, but I need to call this function for each item which is returned, as it will provide the $item->Name as $data['name'] , $item->Prefix as $data['prefix'] and $item->MixName as $data['mix'] 我不太确定该怎么做,因为我知道将助手加载到视图并在其中显然有很多逻辑的做法很糟糕,但是我需要为返回的每个项目调用此函数,因为它将提供$item->Name$data['name']$item->Prefix$data['prefix']$item->MixName$data['mix']

I just can't seem to find a way of doing it without calling the function within the foreach statement. 如果没有在foreach语句中调用函数,我似乎无法找到一种实现方法。 Am I missing something? 我想念什么吗?

Thoughts/suggestions? 有想法/建议吗?

Thanks in advance. 提前致谢。

--------------Part solved --------------零件已解决

This is partially solved, I'm putting this in the model, but it is just returning the first row, 17 times. 这已部分解决,我将其放入模型中,但它仅返回第一行17次。 I don't understand why $q is just the first row, shouldn't it be an array? 我不明白为什么$ q只是第一行,难道不是数组吗?

$query = $kal_db->get('Item');
$q = $query->row();
$i = 1;
foreach($q as $row)
{  
  $item[$i]['IID'] = $q->IID;
  $i = $i + 1;
} 
return $item;

I'm not sure I understand, but this thing 我不确定我是否明白,但是这件事

I'm not really sure how to do this, as I know its bad practise to load a helper to a view and have alot of logic there obviously, but I need to call this function for each item which is returned, as it will provide the $item->Name as $data['name'], $item->Prefix as $data['prefix'] and $item->MixName as $data['mix'] 我不太确定该怎么做,因为我知道将助手加载到视图并在其中显然有很多逻辑的做法很糟糕,但是我需要为返回的每个项目调用此函数,因为它将提供$ item->名称为$ data ['name'],$ item->前缀为$ data ['prefix'],$ item-> MixName为$ data ['mix']

does not require an helper, since PHP provides multidimensional arrays. 不需要帮助程序,因为PHP提供了多维数组。 You could just: 您可以:

  1. Use the loop in some model in order to return an array $items to the controller. 在某些模型中使用循环,以便将数组$items返回到控制器。 The $items array will be like array(1 => array('name' => 'foo', 'prefix' => 'bar', 'mixname' => 'foobar') ; $items数组将类似于array(1 => array('name' => 'foo', 'prefix' => 'bar', 'mixname' => 'foobar') ;

  2. Pass that data to the view as $data['items'] ; 将该数据作为$data['items']传递给视图;

  3. Loop trough the items in the view. 循环浏览视图中的项目。 Pretend you need to display every name: foreach ($items as $item) { echo $item['name']; } 假装您需要显示每个名称: foreach ($items as $item) { echo $item['name']; } foreach ($items as $item) { echo $item['name']; }

Just to clarify: in the model's loop you'll do something like this inside the loop that grabs data: 需要澄清的是:在模型的循环中,您将在捕获数据的循环内执行以下操作:

$item[$i]['name'] = $name;
$item[$i]['prefix'] = $prefix;
$item[$i]['mixname'] = $mixname;

(Where $i is the number of iterations). (其中$i是迭代次数)。

Also, the fact that you're doing queries in a loop kinda suggests that you should take a look for the SQL IN operator. 另外,您实际上是在循环中进行查询,这表明您应该看一下SQL IN运算符。

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

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