By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,467 Members | 1,308 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,467 IT Pros & Developers. It's quick & easy.

Help with Previous and Next links and Displaying Database information

P: n/a
I would like to display a listing of files on a web page as follows:

If there is only one file: display the section name and then display the
current file.

If there is more than one file (for the first page): display the section
name, the current file and a few archive files.

If there is more than a page full (for each additional page): display the
section name, and the next set of archive files.

This is the code I have been working with (it's a bit ugly, suggestions are
welcome). For some reason the second page of listings skips the first item.
1st page lists items 0 through 4, second page skips item 5 and starts
displaying item 6.

Thanks,

-Mark

function display_table($sec_id, $num_item){
GLOBAL $link;
$updir = "../uploads/"; //maybe this should be a GLOBAL variable???
if (isset($_GET['start'])){$start = $_GET['start'];}else{$start = 0;}
//GLOBAL $start;
//query DB
$query_total = "SELECT file_id
FROM files
WHERE file_section_id =" . $sec_id ;
$query = "SELECT file_id, file_meeting_date, file_meeting_time,
file_title, file_modified, file_section_id, sec_name, sec_id,
sec_disp_archive
FROM files f, section s
WHERE f.file_section_id =" . $sec_id . " AND f.file_section_id = s.sec_id
ORDER BY file_modified DESC LIMIT " . $start . "," . $num_item;
$result = mysqli_query( $link, $query ) or die(mysqli_error($link));
$result_total = mysqli_query( $link, $query_total ) or
die(mysqli_error($link));
$num_rows = mysqli_num_rows($result_total);
//print $query;
//create hyperlinks and display table
//display current file
/* print "START: " . $start . "NUMBER: " .$num_item . " ROWS: " .
$num_rows;*/
/*$obj = mysqli_fetch_object($result);*/
if ($num_rows > 1){ //Display current and archive files
//only display current table if start is not set (not on page 1)
$obj = mysqli_fetch_object($result);//Call DB
$content = "<div id ='MainContent'><p><b>" . $obj->sec_name .
"</b></p><table width='100%' border='1'>";
if (!isset($_GET['start'])||$_GET['start']==0){
//for 1st item display file info
$content.= "<tr><td colspan='3' align='center'>Current</td></tr>";
$content.= "<tr bgcolor='#CCCCCC'><td width='33%'>Meeting Date</td><td
width='33%'>Meeting Time</td><td width='34%'>File</td></tr>";
$content .= "<tr><td>" . date("F jS,
Y",strtotime($obj->file_meeting_date)) . "</td><td>" . date("g:i
A",strtotime($obj->file_meeting_time)) . "</td><td><a href='" . $updir .
$obj->file_title . "'>" . "View Current File" . "</td><tr>";
$content .="</table>";
print "START: " . $start . "NUMBER: " .$num_item . " ROWS: " . $num_rows;
//DEBUG
}
//Create seperate table for archive items
$content.= "<table width='100%' border='1'>";
$content.="<tr><td colspan='3' align='center'>Archive</td></tr>";
$content.="<tr bgcolor='#CCCCCC'><td width='33%'>Meeting Date</td><td
width='33%'>Meeting Time</td><td width='34%'>File</td></tr>";
//List archive items
while($obj = mysqli_fetch_object($result)){
$content .= "<tr><td>" . date("F jS,
Y",strtotime($obj->file_meeting_date)) . "</td><td>" . date("g:i
A",strtotime($obj->file_meeting_time)) . "</td><td><a href='" . $updir .
$obj->file_title . "'>" . "View Archived File" . "</td><tr>";
}
$content.= "</table>";
print "*START: " . $start . " *NUMBER: " .$num_item . " *ROWS: " .
$num_rows;
//create navigation
//If there are more files to display, display a next link (number of files
is greater than start # + # per page)
if ($num_rows > ($start+$num_item)) {
$content.="<a href = '" . $_SERVER['PHP_SELF'] . "?start=" . ($start +
$num_item) . "&sec_id=" . $sec_id . "&num_item=" . $num_item . "'>
Next</a>";
}
//If there are previous files to display, show previous link (start# is
greater than 0)
if ($start > 0) {
$content.="<a href = '" . $_SERVER['PHP_SELF'] . "?start=" .
($start - $num_item) . "&sec_id=" . $sec_id . "&num_item=" . $num_item .
"'>Back</a> |";
}
} else {//Display only current file
//Call DB
$obj = mysqli_fetch_object($result);
$content = "<div id ='MainContent'><p><b>" . $obj->sec_name .
"</b></p><table width='100%' border='1'>";
//display file info
$content.= "<tr><td colspan='3' align='center'>Current</td></tr>";
$content.= "<tr bgcolor='#CCCCCC'><td width='33%'>Meeting Date</td><td
width='33%'>Meeting Time</td><td width='34%'>File</td></tr>";
$content .= "<tr><td>" . date("F jS,
Y",strtotime($obj->file_meeting_date)) . "</td><td>" . date("g:i
A",strtotime($obj->file_meeting_time)) . "</td><td><a href='" . $updir .
$obj->file_title . "'>" . "View Current File" . "</td><tr>";
$content .="</table>";
print "START: " . $start . "NUMBER: " .$num_item . " ROWS: " . $num_rows;
//DEBUG
}
$content.= "</form>";
print $content;
print "START: " . $start . "NUMBER: " .$num_item . " ROWS: " . $num_rows;
mysqli_close($link);

Oct 25 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I was able to get this to work by creating a seperate SQL query to get the
section name and then saving that as a variable and calling the variable
later in the code. Now I have a new question: Is there a better way to do
this? I currently use 3 different queries to create the page and wonder if
this is OK or if this would be considered bad coding.

-Mark
Oct 25 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.