Jump to content

dumb, newb question :)


ccbayer

Recommended Posts

hi guys,

 

i'm learning php and teaching myself some stuff.

i have a blog in the works and while i've figured a lot of it out on my own here is one thing i need some help with.

 

basically the blog is entered into a table with 5 or so fields, one of which is the primary key "blog_id."  each blog gets assigned one.

 

there are two ways to view my blog. 1 is a list of all of them printed out. another is a "permalink" style where ou just view one entry. my issue is with the previous and next buttons i use on this "view one entry" (we'll call it "viewblog.php" page).

 

my first way of doing this was like so:

 

//commented out for privacy - connect to database
//QUERY DATABASE
$q = "SELECT blog_id FROM blog_entries ORDER BY blog_id DESC limit 1";
$result = mysql_query($q);
while ($row = mysql_fetch_array($result)){
//retrieve query string
$latest_entry = $row['blog_id'];
}

$prev = $blogid - 1;
$next = $blogid + 1;
?>
<div align="right">
<?php
if ($blogid == 1){
print '<span class="deadlink">Prev</span> | <a href="viewblog.php?blogid='.$next.'" title="Next">Next</a>';
}
elseif ($blogid == $latest_entry){
print '<a href="viewblog.php?blogid='.$prev.'" title="Prev">Prev</a>  |  <span class="deadlink">Next</span>';
}else{
	print '<a href="viewblog.php?blogid='.$prev.'" title="prev">Prev</a>  |  <a href="viewblog.php?blogid='.$next.'" title="Next">Next</a>';
}
?>
</div>

 

so this logically works and is fine and dandy except for the fact that I have hardcoded the "first" entry.  as you all probably know, if i enterr in a few test blogs, then delete them, the primary key doesn't "reset" to accomodate the number of entries in the table...it represents the total number of rows that have been put in altogether- deleted or not.  My question is, is there a better way of doing this? i was thinking i was clever for thinking this up and i'm glad it worked but obviously i'm going to have to think of a different way. any suggestions?

 

 

ps. i get "$blogid" from the query string.

 

Link to comment
https://forums.phpfreaks.com/topic/37872-dumb-newb-question/
Share on other sites

you could put all the bogid's into an array then use the previous and next as you do now but on an array key not the blog id

<?php
//commented out for privacy - connect to database
//QUERY DATABASE
$idArray = array();
$count = 0;
$q = "SELECT blog_id FROM blog_entries ORDER BY blog_id ";
$result = mysql_query($q);
while ($row = mysql_fetch_array($result))
{
    $idArray[$count] =  $row['blog_id'];
    $count ++
}
$latest_entry = count($idArray)-1;
$prev = prev($idArray);
$next = next($idArray);
?>

Link to comment
https://forums.phpfreaks.com/topic/37872-dumb-newb-question/#findComment-181328
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.