Page 1 of 1

Advance Prev/Next links

Posted: Mon Jul 28, 2003 6:48 am
by revillalon
I am trying to make a weblog script that will let me limit each page with only 3 entries per page. I programmed it to only count each date as one entry (meaning if der is one date with multiple times then it will count as one). I am having problem setting the previous link. the block of coding for the PREVIOUS link (the while statement, not the link itself) seems to be the thing that's messed up. if you wanna take on the challenge to figure out my problem... haha.. ud have to think very logically because i been trying to figure out this things for days.. literally! lol


btw.. when i output this coding... the first page comes out right.. but the second page goes really bad. it doesnt seem to organize it the way it was prorammed to be organized.

and one more thing.. i tried to comment it fully so you guys could follow along.. if u get stuck on the PREVIOUS link while statement.. skip it and come back to it and ull figure it out =D

take on the challenge guys/gurls! =D


Code: Select all

<?php
error_reporting(E_ALL);
include_once('inc/database.php');

    // check if $date is set. if set, that means that we're not viewing the starting page
    if( isset($_GET["date"]) ) {
        $date = $_GET["date"];
        $time = $_GET["time"];
    }else{ // $date is not set, meaning we're viewing the starting page and we need to set the $date and $time to the first entry
        $date = '2003-07-03';
        $time = '18:52:20';
    }



// this block of coding is to set the PREVIOUS 3 link with the correct date and time to start with
if( isset($_GET["new"]) ){
if( $_GET["new"] == 'yes' ){
            $rev_prev_date = '0';
            $rev_total_entries = '0';

            $rev_count = mysql_query("SELECT date,time FROM testing_entries ORDER BY date");
            while( $rev_entry = mysql_fetch_object($rev_count) ){
                if( $date == $rev_entry->date && $time == $rev_entry->time ) $start_prev_count = 'set';
                if( isset($start_prev_count) ){
                    if( $rev_prev_date != $rev_entry->date ) $rev_total_entries++;
                    if( $rev_total_entries == '4' ){
                        $rev_date = $rev_entry->date;
                        $rev_time = $rev_entry->time;
                    }
                }

                $rev_prev_date = $rev_entry->date;
                $rev_prev_time = $rev_entry->time;
            }

}
}
$new = 'no';





    // set the variable so that we can use it to compare later
    $prev_date = '0';
    $total_entries = '0';

    echo "<table width="380" cellspacing="0" cellpadding="0">";
    $count = mysql_query("SELECT date,time,message FROM testing_entries ORDER BY date DESC");
    while( $entry = mysql_fetch_object($count) ){

        // compare dates to get the date i want to start off with
        if( $prev_date != $entry->date ){

            // check if $start_count is set. if set then continue counting new entries by date (not time)
            if( isset($start_count) ) $total_entries++;

            // if the wanted date and time are the same, start the counting of new entries by date
            if( $date == $entry->date && $time == $entry->time ){

                // set the variable to let us know that we can start counting new dates now
                $start_count = 'set';
                $total_entries++;
            }
        }

        // check if the entry date is the next entry to start to include for the next page
        if( $total_entries == '4' ){

            // set $next_date and $next_time for the next page link
            $next_date = $entry->date;
            $next_time = $entry->time;
        }


        // start printing out the entries until it's reached limit of entries (by date) per page
        if( $total_entries <= '3' ){

            // check if $set is set. if set then continue printing out the rest of the remaining entries to be outputted
            if( isset($set) ){

                // if it's a new date entry, output it with a new DATE
                if( $prev_date != $entry->date ){
                    echo "<tr><td width="380" align="left" valign="top"><font class="content"><b>" .  $entry->date . "</b></font></td></tr>";
                    echo "<tr><td width="380" align="right" valign="top">";
                        echo "<table width="370" cellspacing="0" cellpadding="0">";
                        echo "<tr><td width="370" align="left" valign="top">";
                        echo "<font class="content">" . $entry->message . "<br></font>";
                        echo "<font class="content">" . $entry->time . "</font>";
                        echo "</td></tr>";
                        echo "</table><br>";
                } else { // not a new date entry, so continue with new entry with the same date
                    echo "<table width="370" cellspacing="0" cellpadding="0">";
                    echo "<tr><td width="370" align="left" valign="top">";
                    echo "<font class="content">" . $entry->message . "<br></font>";
                    echo "<font class="content">" . $entry->time . "</font>";
                    echo "</td></tr>";
                    echo "</table><br>";
                }
            }

            // if the wanted date and time are the same, start the outputting of entries by date (not time)
            if( $date == $entry->date && $time == $entry->time ){

                // set the variable to let us know that we can start outputting the rest of the entries to be outputted 
                $set = 'set';

                // if it's a new date entry, output it with a new DATE
                if( $prev_date != $entry->date ){
                    echo "<tr><td width="380" align="left" valign="top"><font class="content"><b>" .  $entry->date . "</b></font></td></tr>";
                    echo "<tr><td width="380" align="right" valign="top">";
                        echo "<table width="370" cellspacing="0" cellpadding="0">";
                        echo "<tr><td width="370" align="left" valign="top">";
                        echo "<font class="content">" . $entry->message . "<br></font>";
                        echo "<font class="content">" . $entry->time . "</font>";
                        echo "</td></tr>";
                        echo "</table><br>";
                } else { // not a new date entry, so continue with new entry with the same date
                    echo "<table width="370" cellspacing="0" cellpadding="0">";
                    echo "<tr><td width="370" align="left" valign="top">";
                    echo "<font class="content">" . $entry->message . "<br></font>";
                    echo "<font class="content">" . $entry->time . "</font>";
                    echo "</td></tr>";
                    echo "</table><br>";
                }
            }
        }


        //set the variables with the entry date and time that we were comparing
        $prev_date = $entry->date;
        $prev_time = $entry->time;
    }
    mysql_free_result($count);

echo "<a href="testing.php?date=$rev_date&time=$rev_time&new=yes" class="or_link">Previous 3</a>"; 
echo "<a href="testing.php?date=$next_date&time=$next_time&new=yes" class="or_link">Next 3</a>";


?>

Posted: Wed Jul 30, 2003 10:07 am
by jmarcv
On your formatting
Note that this section:

Code: Select all

<?php
              if( $prev_date != $entry->date ){ 
                    echo "<tr><td width="380" align="left" valign="top"><font class="content"><b>" .  $entry->date . "</b></font></td></tr>"; 
                    echo "<tr><td width="380" align="right" valign="top">"; 
?>


the last line you start a new line in the table, but when you get here

Code: Select all

<?php

       // if the wanted date and time are the same, start the outputting of entries by date (not time) 
?>
You next start another line before closing it. So, first off you need an html checker to look at your output.

Second, a suggestion. This code uses a bunch of overhead. All your WHERE clauses are implemented in code. Things will be much more efficient if you query for a subset of the data instead of the whole thing, especially if this is a weblog that is active, you will choke the system in NO time.

The problem is you want 3 days that are not necessarilly consecutive, so the 'between' command is pretty much out. But, consider this:

Code: Select all

<?php
$valid_dates=mysql_query("SELECT date FROM testing_entries WHERE date >='$date' ORDER BY date GROUP by date limit 0,3"); 
?>
This will return 3 records, just 3 dates, starting with the date in $date.

Then try this:

Code: Select all

<?php
while( list($sel_date) = mysql_fetch_object($valid_dates) ){ 
   $dates .= $sel_date.',';
}
?>
This will create a comma delim string, and then you can pull ONLY the records you want like so:

Code: Select all

<?php
 $count = mysql_query("SELECT date,time,message FROM testing_entries WHERE find_in_set(date,'$dates') ORDER BY date DESC"); 
?>
This way, you can eliminate all the code checking for dates, because you only have the info you want.