简体   繁体   English

为codeigniter表中的每一行设置href链接

[英]set href link for each row in codeigniter table

Let's think that we have a db and a model function that retrieve a query result like this: 假设我们有一个数据库和一个模型函数来检索查询结果,如下所示:

public function lista_ordini ($idcliente)
{
     $this->db->select('ordini.num_fattura, misure.cosplay, ordini.data_richiesta,
                   ordini.anticipo, ordini.data_consegna, 
                   ordini.saldo_totale, ordini.stato');

      $this->db->join('misure', "ordini.id_cliente = $idcliente");

      $query = $this->db->get('ordini'); 

      if ($query && $query->num_rows() > 0){
             return $query;
        }else{
             echo "errore generazione query ordini";
           }
       }
}//This is missing in your original, I'm not sure if it's a typo

Using codeigniter's table helper to generate a table: 使用codeigniter的表助手来生成表:

  <?php echo $this->table->generate($query); ?>

I can generate the table...and this is ok and works... but, What if I want that every rows has a specific field (or the entire row if it's simpler) with href link? 我可以生成表...这可以正常工作...但是,如果我希望每行都具有带有href链接的特定字段(或者整行,如果更简单的话)呢? (this link will pass thorough GET method a variable for generate another query, but I don't figure out yet how that will work, this is just an hobby project) I try this: (此链接将通过彻底的GET方法传递一个用于生成另一个查询的变量,但是我还不知道它如何工作,这只是一个业余项目),我尝试了以下方法:

public function lista_ordini ($idcliente) 
{ 
    $this->db->select('ordini.num_fattura, misure.cosplay, ordini.data_richiesta, 
                       ordini.anticipo, ordini.data_consegna, ordini.saldo_totale, 
                        ordini.stato');   
    $this->db->join('misure', "ordini.id_cliente = $idcliente");  

    $query = $this->db->get('ordini');    

    if ($query && $query->num_rows() > 0) {
        $rows = $query->row();

        foreach ($rows as &$row) {
            $row['num_fattura'] = '<a href="segment_3">'.$row['num_fattura'].'</a>';
        }
        return $rows;   
     }else{ 
         echo "errore generazione query ordini"; 
     } 
}

I'm pretty sure that foreach loop is the way, but I can't understand how , cause I have so much errors that I cannot understand...to make this simply, my code doesn't work, and I think that I need some semantic advice. 我很确定foreach循环就是这样,但是我不明白怎么做 ,因为我有太多错误,我无法理解...简而言之,我的代码无法正常工作,而且我认为需要一些语义建议。 thank you in advace 谢谢你

I know what you're trying to do, but I dont understand how you are trying to do it. 我知道您要做什么,但是我不明白您要如何做。 Here's How I would do it: 这是我的处理方式:

//controller method
public function show_data()
{
     $this->load->library('table');
     $data['table_data'] = $this->some_model->get_the_data();

     $this->load->view('some_view_that_will_contain_my_table', $data);
 }

Now in my view, I'd generate the table: 现在,在我看来,我将生成表:

<!doctype html>
 ...
 <?if(!empty($table_data)):?>

     <? $this->table->set_heading('column_1', 'column_2');?>
     <?foreach($able_data as $table_row):
         $this->table->add_row(
             array('data'=>'<a href="/">'.$table_row['column_1'].'</a>.'),
             array('data'=>'<a href="/">'.$table_row['column_2'].'</a>.'),
         );
      endforeach;?>

     <?= $this->table->generate();?>
 <?endif;?> //or display an empty table 

There is a logical problem in the foreach loop here: 这里的foreach循环中存在一个逻辑问题:

foreach ($rows as &$row) {
  $row['num_fattura'] = '<a href="segment_3">'.$row['num_fattura'].'</a>';
}
return $rows; 

Each time the loop runs you are changing the value of $row, but when it completes you are returning $rows, which is unchanged. 每次循环运行时,您都在更改$ row的值,但是当循环完成时,您将返回$ rows,该值不变。

Instead, you could edit the original array, using $k => $v to access the key and value inside the foreach loop: 相反,您可以编辑原始数组,使用$k => $v访问foreach循环内的键和值:

foreach ($rows as $k => $v) {
  $rows[$k]['num_fattura'] = '<a href="segment_3">'.$rows[$k]['num_fattura'].'</a>';
}
return $rows;

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

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