• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to dynamically link to a dynamic single db record view?

#1
[eluser]Unknown[/eluser]
Looked for this, but new to CI so not sure what to look for.

I have a controller which loads a view. The view shows a query listing of records.

How do I write a dynamic link for each record result in the list which then opens a single record view?

What is best practice in MVC?

#2
[eluser]thinkigniter[/eluser]
Idea 1.
In your controller create a function single($record_id = 1)
From the view file have a <a> link refering to this controller function.
Pass the index_id of the record to this function
and load up a second view with the single view option

Idea 2.
To save server load time create a javascript option that appends the <a> link with a <div> that has all the necessary view fields.

Good Coding

#3
[eluser]Unknown[/eluser]
Thank you for your reply and well wishes.

I kind of understand what you are saying in idea 1. It is bit unclear to me.

With the help of the documentation and the blog screencast tutorial I have been able to get the following so far. Which displays the correct single record in the "single view" when a link is clicked in the "category view":

Please keep in mind this is very much in development code, and for illustration purposes only. Do not use on a live site unless you know what you are doing.

In the "category view"

Unfortunately I can't get the php link I wrote to post properly, so please bear with me until I figure that out.

a link with "$row->id" in the href path so you get for example the result "www.yourdomain.com/path/to/2".

Where in "row->id" "id" the primary key of the table is. Although I would like to display the title of the article for pretty url's.

So you get something like this at its basic level:

Code:
&lt;?php foreach($query->result() as $row): ?&gt;
        
link here

&lt;?php endforeach; ?&gt;


In the "single view"

Code:
&lt;?php
    $article_id = $this->uri->segment(3, 0);
        $query = $this->db->query("SELECT * FROM `database_table`");

if ($query->num_rows() > 0)
{
   $row = $query->row($article_id-1);
  }
   ?&gt;

            <h2>&lt;?=$row->title?&gt;</h2>
            <p>&lt;?=$row->story?&gt;</p>

Please note that in this example the segment in the variable "$article_id" needs to equal the segment location in the link in the "category view".

The code above is excluding any modrewrites and routes.

#4
[eluser]thinkigniter[/eluser]
Sorry I can't give you an example the sites security strips out the code, whatever way I format it.

Thanks


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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