簡體   English   中英

為codeigniter表中的每一行設置href鏈接

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

假設我們有一個數據庫和一個模型函數來檢索查詢結果,如下所示:

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

使用codeigniter的表助手來生成表:

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

我可以生成表...這可以正常工作...但是,如果我希望每行都具有帶有href鏈接的特定字段(或者整行,如果更簡單的話)呢? (此鏈接將通過徹底的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"; 
     } 
}

我很確定foreach循環就是這樣,但是我不明白怎么做 ,因為我有太多錯誤,我無法理解...簡而言之,我的代碼無法正常工作,而且我認為需要一些語義建議。 謝謝你

我知道您要做什么,但是我不明白您要如何做。 這是我的處理方式:

//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);
 }

現在,在我看來,我將生成表:

<!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 

這里的foreach循環中存在一個邏輯問題:

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

每次循環運行時,您都在更改$ row的值,但是當循環完成時,您將返回$ rows,該值不變。

相反,您可以編輯原始數組,使用$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