ok - i am super stumped. i'm usally never the type to post questions but i'm having serious troubles navigating through records backwards. ( ORDER BY DESC )
the problem i'm having is with the 'previous' link.
i've tried numerous methods and can not figure out a simple method to achieve this.
if anyone has any code examples they could pass on to me it would be much appreciated.
i will post my code but it is part of a larger application so it may not make much sense.
keep in mind that the following is a function called with the 'guestbook id' and previsous 'guestbook' id.
right now the code looks very sloppy, because i've been hacking at it to make it work properly. it's about the 5th time i've tried rewriting it and i am just utterly stumped.
Any help or links are super appreciated!!
<?
##################################################################################################
function DB_MakeGuestBook2($G_ID, $G_LASTID) {
global $DB_CON, $DB_CON_CONNECT, $APP_DATA;
mysql_select_db($DB_CON['DATABASE'], $DB_CON_CONNECT);
$H_ID_SQL=mysql_query("SELECT * FROM guestbook ORDER BY id DESC LIMIT 1");
$H_ID_ARRAY=mysql_fetch_array($H_ID_SQL);
$H_ID=$H_ID_ARRAY['id'];
$L_ID_SQL=mysql_query("SELECT * FROM guestbook ORDER BY id ASC LIMIT 1");
$L_ID_ARRAY=mysql_fetch_array($L_ID_SQL);
$L_ID=$L_ID_ARRAY['id'];
if ($G_ID==0) {
$GB_SQL=mysql_query("SELECT * FROM guestbook WHERE id > $G_ID ORDER BY id DESC LIMIT 10");
} else {
$GB_SQL=mysql_query("SELECT * FROM guestbook WHERE id < $G_ID ORDER BY id DESC LIMIT 10");
}
$T_ID_FLAG=0;
while ($T_GUEST=mysql_fetch_array($GB_SQL)) {
if ($T_GUEST['guest_site']) {
$L_TEST=DB_VerifyURL($T_GUEST['guest_url']);
if ($L_TEST==1) {
$T_LINK= '- <a href="' . $T_GUEST['guest_url'] . '" target="_blank">' . $T_GUEST['guest_site'] . '</a>';
} else {
//verify site fail
$T_LINK='';
}
}
if ($T_ID_FLAG==0) {
$T_ID_FLAG++;
$F_ID=$T_GUEST['id'];
}
$T_DATE_A = explode(".", $T_GUEST['guest_date']);
$T_TIME_A = explode(".", $T_GUEST['guest_time']);
$T_DATE=$T_DATE_A[0] . '.' . $T_DATE_A[1] . '.' . $T_DATE_A[2];
$T_TIME=$T_TIME_A[0] . ':' . $T_TIME_A[1];
$T_GBP.='<tr>
<td height="20"><div align="center" class="style13">
<div align="left" class="style17"><strong><img src="images/arrow04.gif" width="6" height="9"> Posted By:</strong> ' . $T_GUEST['id'] . ' :: ' . $T_GUEST['guest_name'] . '<span class="style16"> ' . $T_LINK . ' </span></div>
</div></td>
</tr>
<tr>
<td height="20"><span class="style17"><strong><img src="images/arrow02.gif" width="6" height="9"> Date:</strong> ' . $T_DATE . ' @ ' . $T_TIME .'<span class="style16"></span></span></td>
</tr>
<tr>
<td height="20"><span class="style17"><strong><strong><img src="images/arrow02.gif" width="6" height="9"> </strong>Message:</strong> ' . $T_GUEST['guest_msg'] . ' <span class="style16"> </span></span></td>
</tr>
<tr>
<td height="2" background="images/interlace-tile-001.gif"><img src="images/8x8tranz.gif" width="2" height="2"><img src="images/8x8tranz.gif" width="2" height="2"></td>
</tr>';
$T_LAST_ID=$T_GUEST['id'];
$T_LINK='';
$T_GUEST['guest_site']='';
$G_COUNT++;
}
if ($G_COUNT < 10) {
$T_NID='none';
} else {
$T_NID=$T_LAST_ID;
$F_ID++;
$N_LINK='<a href="guestbook.php?book=' . $T_NID . '&action=next&id=' . $F_ID . '">Next Page</a> > ';
}
$G_BOOK['code'] = '<table width="98%" border="0" cellspacing="0" cellpadding="4">' . $T_GBP . '</table>';
$T_GBP='';
$G_BOOK['last_id'] =$T_LAST_ID;
$G_BOOK['next_id'] = $F_ID - 1;
$G_BOOK['book_count']=$G_COUNT;
if ($G_ID >= $H_ID) {
$G_BOOK['hide_previous']=1;
}
if ($G_LASTID==0) {
$G_LASTID=$H_ID;
}
$G_BOOK['page_cursor']=$G_LASTID;
return $G_BOOK;
$G_BOOK['code']='';
}
##################################################################################################
?>
and the page itself that does the link and code output...
<?
require_once('includes/database.php');
$T_HEADER=DB_GeneratePageHeader();
if (is_numeric($last_id) && $action=='back') {
$T_GUEST=DB_MakeGuestBook2($book, $last_id);
} else {
if ($book==0) {
$T_GUEST=DB_MakeGuestBook2(0, 0);
} else {
if (is_numeric($book) && is_numeric($last_id)) {
$T_GUEST=DB_MakeGuestBook2($book, $last_id);
} else {
$T_GUEST=DB_MakeGuestBook2(0, 0);
}
}
}
if ($last_id) {
if ($T_GUEST['page_cursor'] > 0) {
$TP_ID=$T_GUEST['page_cursor'] + 1;
$P_LINK = '< <a href="guestbook.php?book=' . $TP_ID . '&last_id=' . $T_GUEST['last_id'] . '&action=back">Previous Page</a>';
}
if ($T_GUEST['hide_previous']==1) {
$P_LINK='';
}
if ($T_GUEST['book_count'] ==10) {
$N_LINK='<a href="guestbook.php?book=' . $T_GUEST['last_id'] . '&last_id=' . $T_GUEST['next_id'] . '">Next Page</a> > ';
}
}
if ($book==0) {
$N_LINK='<a href="guestbook.php?book=' . $T_GUEST['last_id'] . '&last_id=' . $T_GUEST['page_cursor'] . '">Next Page</a> > ';
}
if ($P_LINK && $N_LINK) {
$P_LINK.= ' - ';
}
?>