开发者

Making anchor load page containing data from referenced row in DB

开发者 https://www.devze.com 2023-01-02 09:15 出处:网络
I\'m trying to learn the code igniter library and object oriented PHP in general and have a question.

I'm trying to learn the code igniter library and object oriented PHP in general and have a question.

I've gotten as far as making a page which loads all of the rows from my database and in there, I'm echoing an anchor tag which is a link to the following structure.

echo anchor("videos/video/$row->video_id", $row->video_title);

So, I have a class called Videos which extends the controller, within that class there is index and video, which is being called correctly (when you click on the video title, it sends you to videos/video/5 for example, 5 being the primary key of the table I'm working with.

So basically all I'm trying to do is pass that 5 back to the controller, and then have the 开发者_高级运维particular video page output the particular rows data from the videos table. My function in my controller for video looks like this:

function video()
{
    $data['main_content'] = 'video';
    $data['video_title'] = 'test';
    $this->load->view('includes/template', $data);      
}

So ya, basically test should be instead of test, a returned value of a query which says get in the table "videos", the row with the video_id of "5", and make $data['video_title'] equal to value of video_title in database...

Should have this figured out by now but don't, any help would be appreciated!


I don't know if I'm too late but maybe this can solve your problem... put this in your video() function

data[$query] = $this->db->query("SELECT * FROM videos WHERE video_id = 5");

and then that in your video_view file...

if ($query->num_rows() > 0)
{
   $row = $query->row_array(); 

   echo $row['title'];
   echo $row['something'];
   echo $row['somethingElse'];
}

this is a good resource: http://codeigniter.com/user_guide/database/index.html

hope that helps...

and please someone edit the question because it's too hard to read...


What you need is to understand how the URI Class works

Basically:

$default_url_args = array('video');
$url_args = $this->uri->uri_to_assoc(3,$default_url_args);
$video_UID = $url_args['video'];

and then something like

$the_video = $this->videos_model->get_video_by_UID($video_UID);


You could use the URI Class, or you can do the following:

function video($video_id)
{
  $data['main_content'] = $this->videoprovider->get_video( $video_id );
  $data['video_title'] = 'test';
  $this->load->view('includes/template', $data);
} 

In other words, with functions inside classes that extend Controller, you can add parameters to those functions and CI will automatically pass in the URI items in order to those parameters.

function generic_function_in_controller($item1, $item2, ...)
{
  // would receive as: http://example.com/controller/generic_function_in_controller/item1/item2
}
0

精彩评论

暂无评论...
验证码 换一张
取 消