简体   繁体   中英

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:

  <?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? (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:

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. 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 ($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.

Instead, you could edit the original array, using $k => $v to access the key and value inside the foreach loop:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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