Help with Previous and Next links and Displaying Database information

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark ??;-\)

    Help with Previous and Next links and Displaying Database information

    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_da te, file_meeting_ti me,
    file_title, file_modified, file_section_id , sec_name, sec_id,
    sec_disp_archiv e
    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_erro r($link));
    $result_total = mysqli_query( $link, $query_total ) or
    die(mysqli_erro r($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_ob ject($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_ob ject($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='#CCCCC C'><td width='33%'>Mee ting Date</td><td
    width='33%'>Mee ting Time</td><td width='34%'>Fil e</td></tr>";
    $content .= "<tr><td>" . date("F jS,
    Y",strtotime($o bj->file_meeting_d ate)) . "</td><td>" . date("g:i
    A",strtotime($o bj->file_meeting_t ime)) . "</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='#CCCCC C'><td width='33%'>Mee ting Date</td><td
    width='33%'>Mee ting Time</td><td width='34%'>Fil e</td></tr>";
    //List archive items
    while($obj = mysqli_fetch_ob ject($result)){
    $content .= "<tr><td>" . date("F jS,
    Y",strtotime($o bj->file_meeting_d ate)) . "</td><td>" . date("g:i
    A",strtotime($o bj->file_meeting_t ime)) . "</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_it em)) {
    $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_ob ject($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='#CCCCC C'><td width='33%'>Mee ting Date</td><td
    width='33%'>Mee ting Time</td><td width='34%'>Fil e</td></tr>";
    $content .= "<tr><td>" . date("F jS,
    Y",strtotime($o bj->file_meeting_d ate)) . "</td><td>" . date("g:i
    A",strtotime($o bj->file_meeting_t ime)) . "</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($l ink);



  • Mark ??;-\)

    #2
    Re: Help with Previous and Next links and Displaying Database information

    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


    Comment

    Working...