Monday, 15 March 2010

php - Dynamic webpages: how to link to previous and next blog post -



php - Dynamic webpages: how to link to previous and next blog post -

this question has reply here:

how next/previous record in mysql? 15 answers

a couple of weeks ago started switch static dynamic webpages create life more easier. result looking improve each day still got issues didn't manage prepare myself.

the next thing want accomplish add together link previous , next blog post in 2 divs @ bottom of each page, based on "id".

the url each blog post consists of id , title: "domain.com/id/title"

example:

let's i'm reading blog post id = 2. how can link blog posts id = 1 (previous) , 3 (next)?

<?php (connect database) $id = str_replace ('-', ' ', $_get['id']); $sql = "select * `newstable` `id` = '$id'"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { ?> <div class="content-title"> <?php echo $row['title'];?> </div> <div class="content-text"> <?php echo $row['text'];?> </div> <div id="previous post"> ........... (here comes link previous post, need link right url means i'll need echo id , title of previous post) </div> <div id="next post"> ........... (here comes link next post, need link right url means i'll need echo id , title of next post) </div>

below simple method utilize require.

note code untested, , might errors. can clarify them in comments below or help of google.

however, hope understand required logic through this.

<?php function selectfromdatabase($querytorun){ // function facilitate querying of database. returns array. $result = $conn->query($querytorun); if ($result->num_rows > 0) { $record = $result->fetch_assoc(); homecoming $record; } } $pagelink = "http://$_server[http_host]$_server[request_uri]"; // page's url. $exploded = explode('/', $pagelink); // split url array delimited '/'s. $id = $exploded[3]; // id of content, based on sample url provided. $smallestid = selectfromdatabase("select min(id) `newstable` `id` = '$id'")['id']; // select smallest id database, ensure link existing content only. $largestid = selectfromdatabase("select max(id) `newstable` `id` = '$id'")['id']; // select greatest id database similar purpose. note doing not reap benefits if have gaps in ids of content. $previouspostid = (($id - 1) < $smallestid) ? $largestid : ($id - 1); // if ($id - 1) smaller smallest id, create previous post's id = largest id. else leave @ that. $nextpostid = (($id++) > $largestid) ? $smallestid : ($id++); // similar previous line, largest id. $previousposttitle = selectfromdatabase("select `title` `newstable` `id` = '$previouspostid'")['title']; // select required title. $nextposttitle = selectfromdatabase("select `title` `newstable` `id` = '$nextpostid'")['title']; // select required title. $sql = "select * `newstable` `id` = '$id'"; // code content in question. $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { ?> <div class="content-title"> <?php echo $row['title'];?> </div> <div class="content-text"> <?php echo $row['text'];?> </div> <?php } } ?> <div id="previouspost"> <a href="<?php echo "http://$_server[http_host]".$previouspostid."/".$previousposttitle; ?>">previous</a> <!-- putting url previous page. --> </div> <div id="nextpost"> <a href="<?php echo "http://$_server[http_host]".$nextpostid."/".$nextposttitle; ?>">next</a> <!-- putting url next page. --> </div>

php post hyperlink echo blogs

No comments:

Post a Comment