Welcome Guest, Not a member yet? Register   Sign In
Building next/prev links
#1

[eluser]derekt[/eluser]
I'm building a photo gallery and would like the descriptive image file names to be in the URLs, like "pretty-sunset" in this link

http://www.mysite.com/collection/3/photo/pretty-sunset

I'm having a really hard time figuring out the best way to build the "next" and "previous" links on the photo view page, while keeping the URLs in this format. Is there a slick way to do this in CI? Any sage advice is highly appreciated; I've had great experiences with this framework so far and want to continue using it!
#2

[eluser]xwero[/eluser]
I think you have to get the names of the previous and next picture of the gallery to build the links. If the name returns as an empty string you remove that link.
#3

[eluser]derekt[/eluser]
Thanks xwero; I agree. I'm trying to do <3 db hits on each page if possible, so I'm kinda wondering if I need to pull the filename column of my db into a numeric array in the controller, then increment or decrement the array index value to retrieve the appropriate filename. It just really seems like there should be a neater way to do this...
#4

[eluser]Sarfaraz Momin[/eluser]
Well I do have a similar implementation on few of my sites. The code goes as follows.

model
Code:
function get_prevnext_content($catid,$id,$prevnext){
    $this->db->select('content_id, content_name');
    $this->db->where('content_trash', 0);
    $this->db->where('subcategory_id', $catid);
    if($prevnext == 'prev'){
        $this->db->where('content_id <', $id);
        $this->db->orderby("content_id", "desc");
    }else {
        $this->db->where('content_id >', $id);
        $this->db->orderby("content_id", "asc");
    }
    $this->db->limit(1, 0);
    $result = $this->db->get('content');
    return $result->result();
}

the code for controller
Code:
$prevdata = $this->MyModel->get_prevnext_content($pull_content[0]->subcategory_id,$pull_content[0]->content_id,'prev');
        $nextdata = $this->MyModel->get_prevnext_content($pull_content[0]->subcategory_id,$pull_content[0]->content_id,'next');
        $prevlink = "<div style=\"float:left;width:380px;padding-left:10px\" align=\"left\"></div>";
        $nextlink = "<div style=\"float:left;width:380px;padding-left:10px\" align=\"left\"></div>";
        if($prevdata){
            $prevlink = "<div style=\"float:left;width:380px;padding-left:10px\" align=\"left\"><a title=\"".$prevdata[0]->content_name."\" href=\"".str_replace(" ","-",strtolower($prevdata[0]->content_name)).".html\" class=\"prevnext\">Previous Ecard</a></div>";
        }
        if($nextdata){
            $nextlink = "<div style=\"float:left;width:380px;padding-right:10px\" align=\"right\"><a title=\"".$nextdata[0]->content_name."\" href=\"".str_replace(" ","-",strtolower($nextdata[0]->content_name)).".html\" class=\"prevnext\">Next Ecard</a></div>";
        }

I hope this make some sense. Well this is quite simple. You get the id of the content and for next you just check the next id and for prev you just check prev id but just keep in mind to order the content properly or the results would not be good.

Good Day !!!
#5

[eluser]derekt[/eluser]
The help is most appreciated! This community seems very cool- I like!
#6

[eluser]derekt[/eluser]
Sarfaraz, could you let me know how you're calling this from the view??

Thanks again!
#7

[eluser]Sarfaraz Momin[/eluser]
Well I am not calling it from the view. I have my own template system where once the prev/ next links are generated it just replaces the tags {prevnext} for me with the relevant links. What you can do is

controller
Code:
$prevdata = $this->MyModel->get_prevnext_content($pull_content[0]->subcategory_id,$pull_content[0]->content_id,'prev');
        $nextdata = $this->MyModel->get_prevnext_content($pull_content[0]->subcategory_id,$pull_content[0]->content_id,'next');
        $data['prevlink'] = "<div style=\"float:left;width:380px;padding-left:10px\" align=\"left\"></div>";
        $data['nextlink'] = "<div style=\"float:left;width:380px;padding-left:10px\" align=\"left\"></div>";
        if($prevdata){
            $data['prevlink'] = "<div style=\"float:left;width:380px;padding-left:10px\" align=\"left\"><a title=\"".$prevdata[0]->content_name."\" href=\"".str_replace(" ","-",strtolower($prevdata[0]->content_name)).".html\" class=\"prevnext\">Previous Ecard</a></div>";
        }
        if($nextdata){
            $data['nextlink'] = "<div style=\"float:left;width:380px;padding-right:10px\" align=\"right\"><a title=\"".$nextdata[0]->content_name."\" href=\"".str_replace(" ","-",strtolower($nextdata[0]->content_name)).".html\" class=\"prevnext\">Next Ecard</a></div>";
        }
$this->load->view('viewpage', $data);

viewpage.php
Code:
&lt;?= $prevlink; ?&gt;
&lt;?= $nextlink; ?&gt;

I hope it makes sense.

Good Day !!!
#8

[eluser]derekt[/eluser]
Stellar- it works like a charm. Thank you very much!
-derekt




Theme © iAndrew 2016 - Forum software by © MyBB