Reputation: 2773
I need to generate an url link for a view and display data from database into this view. My main view is Artists/Singers and it has a table of singers. Each singer should be a link which when I press leads me to his Artist page with his albums, biography and songs.
I need to do this in Code Igniter and of course I can read the docs but will need a week or so and I have only 2 days.
Here is my code:
$this->load->database();
$artists = $this->db->query('SELECT a.name, a.date, c.name FROM artist as a, country as c WHERE a.country_id=c.id');
echo "<tr><td>Artist</td><td>Year</td><td>Country</td></tr>";
foreach ($artists->result() as $row)
{
//echo $config['base_url'];
echo "<tr>";
echo "<td><a href=\"/Artist.php\">" . $row->name . "</a></td>";
echo "<td>" . $row->date . "</td>";
echo "<td>" . $row->name . "</td>";
echo "</tr>";
}
echo "</table>";
Upvotes: 0
Views: 812
Reputation:
I would do some thing like this
$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');
Model
Example_model.php
<?php
class Example_model extends CI_Model {
public function getArtist() {
$artists = $this->db->query('SELECT a.artist_id,a.name, a.date, c.name FROM artist as a, country as c WHERE a.country_id=c.id');
return $artists->result();
}
}
Controller Example
<?php
class Example extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('example_model');
}
public function index() {
$data['artist'] = $this->example_model->getArtist();
$this->load->view('example_view', $data);
}
}
Then on view
<?php foreach($artist as $row) {?>
<tr>
<td><?php echo anchor('artist/' . $row->artist_id, $row->name, array('target' => '_blank'));?></td>";
<td>$row->date</td>
<td>$row->name</td>
</tr>
<?php }?>
Then on routes
$route['artist/(:any)'] = "controller/function/$1";
Upvotes: 1
Reputation: 71
I think you will need to create a route like,
$route['artist/(:any)'] = "controller/load_artist_page/$1";
and in your href tag,
base_url('artist/'.$parameter);
and in the function get the id and load that view,
function load_artist_page($artist_id){
//do processing and load view.
}
Encrypt the id before passing it.
Upvotes: 1
Reputation: 101
find the primary key of the table artist, for expample id or artist_id and add to query
$artists = $this->db->query('SELECT a.artist_id,a.name, a.date, c.name FROM artist as a, country as c WHERE a.country_id=c.id');
and in your code
foreach ($artists->result() as $row)
{
//echo $config['base_url'];
echo "<tr>";
echo "<td><a href=\"/Artist.php?aid=" . $row->artist_id . "\">" . $row->name . "</a></td>";
echo "<td>" . $row->date . "</td>";
echo "<td>" . $row->name . "</td>";
echo "</tr>";
}
echo "</table>";
and in controler Artist get data by $_GET['aid']
Upvotes: 1