dqvzfp6468
dqvzfp6468
2011-01-21 06:03
浏览 31
已采纳

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

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.

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.

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

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']

I just can't seem to find a way of doing it without calling the function within the foreach statement. 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. I don't understand why $q is just the first row, shouldn't it be an array?

$query = $kal_db->get('Item');
$q = $query->row();
$i = 1;
foreach($q as $row)
{  
  $item[$i]['IID'] = $q->IID;
  $i = $i + 1;
} 
return $item;
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtup3446
    dtup3446 2011-01-21 06:18
    已采纳

    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']

    does not require an helper, since PHP provides multidimensional arrays. You could just:

    1. Use the loop in some model in order to return an array $items to the controller. The $items array will be like array(1 => array('name' => 'foo', 'prefix' => 'bar', 'mixname' => 'foobar');

    2. Pass that data to the view as $data['items'];

    3. Loop trough the items in the view. Pretend you need to display every 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).

    Also, the fact that you're doing queries in a loop kinda suggests that you should take a look for the SQL IN operator.

    点赞 评论

相关推荐