how to create a link field for table generated using table library

#1
[eluser]Unknown[/eluser]
Hi

I have created a table to show the query results using

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

then i create a table using

$this->table->generate($query);


but can anybody help me on this
how do i create a link corresponding to every row in the table to edit the table data

#2
[eluser]xwero[/eluser]
Between the query and the table generation you have to add an extra column with the links.

So instead of adding the query object directly to the table you have to use the result or result_array methods to alter the object or array.

#3
[eluser]tunelko[/eluser]
Hi to Every<body> Wink

I have an identical problem and i have solved that way, in model:

Code:
$query = "SELECT blah,blah...";

        $table = $this->db->query($query);
        $this->table->set_heading('h1', 'h2', 'h3');
  
     foreach ($table->result_array() as $row){            
            $row['field'] = anchor(site_url('dir/'. url_title($link) .'/'. $row['id']),$row['field']) ;
            // we generate an array to use in generate method.
            $a_row[]=$row;
        }

        $tmpl = array ( 'table_open'  => '<table border="0" cellpadding="2" cellspacing="1" class="table">' );        
        $this->table->set_template($tmpl);        
        $table_with_a_link =  $this->table->generate($a_row);

And you have a table with a field 'linkable'.
byes!

#4
[eluser]bigdaddysheikh[/eluser]
Hey,

I like the code, but I do not understand the foreach loop. The way I see this is that you are adding an extra object to the last array right?

#5
[eluser]xwero[/eluser]
The problem with tunelko's code could be the need for more fields than you need to create the table. To prevent this you can use the concat mysql function to build the url inside you query instead of doing it in php.

#6
[eluser]tunelko[/eluser]
[quote author="bigdaddysheikh" date="1213591735"]Hey,

I like the code, but I do not understand the foreach loop. The way I see this is that you are adding an extra object to the last array right?[/quote]


Hi,

How can you iterate through the table's results array if you don't use 'foreach'.?
Yes, you are right. I use an array to accumulate data.

#7
[eluser]tunelko[/eluser]
[quote author="xwero" date="1213611178"]The problem with tunelko's code could be the need for more fields than you need to create the table. To prevent this you can use the concat mysql function to build the url inside you query instead of doing it in php.[/quote]

More fields to create the table ? You use the fields that needs... no more.

Can you show an sql concat example and explain better ?

Thanks for the replys,
Greetings. !

#8
[eluser]xwero[/eluser]
Quote:More fields to create the table ? You use the fields that needs... no more.

Can you show an sql concat example and explain better ?

Thanks for the replys,
Greetings. !
In your example you use the id but most of times you are not going to create a column for the id field as it is most of the time a metadata field.

Based on your example the concat function would be
Code:
$this->db->select("id h1, field h2, CONCAT('".site_url()."/dir/". url_title($link) ."/',id) h3");
And then to create a link you have to do
Code:
$rows = $query->results_array();

foreach($rows as $key=>$row)
{
    $rows[$key]['h3'] = anchor($row['h3'],$row['h2']);
}
Using an alias for a field takes care of the headers and i reuse the array created by the query. Your code adds the link at the end of the row but what if you want it between the first and second column. I only have to move the h3 column where you would have to use functions to remove the last column and slip it in between the columns.
Bottom line it's best to do string manipulations in the sql statements and let php take care of adding the markup.

#9
[eluser]tunelko[/eluser]
You got it.
Interesting concat example. It's better this way.
thanks Wink


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.