SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 28
  1. #1
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a question. I'm trying to set up next/previous links on my page however they can't be set up where the number just increases/decreases by one because they are listed in date order, not ID order.

    For example:
    http://www.sohh.com/thewire/index.php

    Underneath the query, you'll see a long list of articles. They are listed from most recent to least recent. If you scroll down a bit you'll notice that the numbers skip and jump in certain places.

    Basically, what I'm trying to do is make some next/previous links on the inside pages - click on the links. So if the user is reading the article they can move around without having to go back to the main page all the time. The next/previous buttons would be based upon the query printed at the top of http://www.sohh.com/thewire/index.php. This query ensures that the reader is supposed to see the articles.

    I managed to do it in Slow Fusion, but I want to do it in PHP - as we're sick of Slow Fusion.

    Any ideas?
    <///////~

    www.sohh.com

  2. #2
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoa!

    Not even a reply from freddiedoesphp? This is not looking good.

    Seems like I need to find a way to get all of the contentID's into an array and then tell the system to move up or down one slot in the array. Does this sound about right?
    <///////~

    www.sohh.com

  3. #3
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ideblunt,

    What's keeping you from making use of a LIMIT constraint in your SQL query to divide your set of results into pages? It doesn't matter if they're ordered by ID, alphabetically, or by date; LIMIT let's you say "give me the first n rows in the result set, beginning with the m'th row.

    LIMIT is covered with examples in my PHP/MySQL tutorial:

    http://www.webmasterbase.com/article...d=228&pid=1039
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  4. #4
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I read that article before. But I didn't get it.
    <///////~

    www.sohh.com

  5. #5
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try my post in this thread on for size:

    http://www.sitepointforums.com/showt...4645#post34645
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  6. #6
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know how to use the LIMIT function in SQL but that doesn't apply to what I'm trying to do. If you feel that it does, please explain.

    What I'm trying to accomplish is the following:

    goto: http://www.sohh.com/thewire/

    Let's say we go to article #826 - Eminem Now Faces Lawsuit.

    The next article would be #1084 and the previous article would be #825.
    Last edited by ibeblunt; Feb 7, 2001 at 12:34.
    <///////~

    www.sohh.com

  7. #7
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I believe it does apply. Here is your query:

    Code:
    SELECT tbl_content.contentID, tbl_content.contentheadline
    FROM tbl_content
    WHERE tbl_content.contentsubsectionID = 1
    AND tbl_content.contentapproved = 1
    AND tbl_content.contentlive = 1
    AND tbl_content.contentgolive > 2001-02-07
    AND tbl_content.contentgodown > 2001-02-07
    ORDER BY tbl_content.contentgolive DESC, tbl_content.contentID DESC
    You're ordering the items by their 'go live' date. Fair enough. The following query will list the 10 most recent articles:

    Code:
    SELECT tbl_content.contentID, tbl_content.contentheadline
    FROM tbl_content
    WHERE tbl_content.contentsubsectionID = 1
    AND tbl_content.contentapproved = 1
    AND tbl_content.contentlive = 1
    AND tbl_content.contentgolive > 2001-02-07
    AND tbl_content.contentgodown > 2001-02-07
    ORDER BY tbl_content.contentgolive DESC, tbl_content.contentID DESC
    LIMIT 0, 10
    The following query will list the next 10 articles:

    Code:
    SELECT tbl_content.contentID, tbl_content.contentheadline
    FROM tbl_content
    WHERE tbl_content.contentsubsectionID = 1
    AND tbl_content.contentapproved = 1
    AND tbl_content.contentlive = 1
    AND tbl_content.contentgolive > 2001-02-07
    AND tbl_content.contentgodown > 2001-02-07
    ORDER BY tbl_content.contentgolive DESC, tbl_content.contentID DESC
    LIMIT 10, 10
    And for articles 21-30 (by "go live" date):

    Code:
    SELECT tbl_content.contentID, tbl_content.contentheadline
    FROM tbl_content
    WHERE tbl_content.contentsubsectionID = 1
    AND tbl_content.contentapproved = 1
    AND tbl_content.contentlive = 1
    AND tbl_content.contentgolive > 2001-02-07
    AND tbl_content.contentgodown > 2001-02-07
    ORDER BY tbl_content.contentgolive DESC, tbl_content.contentID DESC
    LIMIT 20, 10
    Your task is to generalise this using PHP code. Use a PHP variable to keep track of the first item on the current listing, then make your "next" link pass the variable + 10 (or however many items you're displaying per page), and your "previous" link should pass the variable - 10. All that's left is to detect the first page (the variable equals zero) and the last page (the variable + 10 is greater than the number of items available) to omit the next/previous links when appropriate.

    Let me know if you still need further detail.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  8. #8
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, I see what I'm not explaining (as usual)

    Basically, the code would go on the page with the actual article not on the list with all the articles. My bad.

    Like:
    http://www.sohh.com/thewire/read.php?contentID=826

    This page would say at the bottom:

    &laquo; previous page | next page &raquo;

    So then the previous page link would point to:
    http://www.sohh.com/thewire/read.php?contentID=825

    and the next page link would point to:
    http://www.sohh.com/thewire/read.php?contentID=1084

    Once again - mybad.
    <///////~

    www.sohh.com

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would take this approach, try sending the next and previous ids along from the main page Using the mysql_result() function we can loop through the results and get the id for the current record, the one before it and the one after it, by either adding or subtracting 1 from the current result pointer

    $result = mysql_query("SELECT tbl_content.contentID, tbl_content.contentheadline
    FROM tbl_content
    WHERE tbl_content.contentsubsectionID = 1
    AND tbl_content.contentapproved = 1
    AND tbl_content.contentlive = 1
    AND tbl_content.contentgolive > 2001-02-07
    AND tbl_content.contentgodown > 2001-02-07
    ORDER BY tbl_content.contentgolive DESC, tbl_content.contentID DESC");

    $count = mysql_num_rows($result);
    for($i=0;$i<$count;$i++) {

    //If we are on the first record there will be no previous id
    if ($i == 0) {
    printf('<a href="%s?contentID=%s&nextID=%s">%s</a><br>', $PHP_SELF, mysql_result($result, $i, 0), mysql_result($result, $i + 1, 0), mysql_result($result, $i, 1) );
    }
    //If we are on the last record there will be no next id
    elseif ($i == mysql_num_rows($result) - 1) {
    printf('<a href="%s?contentID=%s&prevID=%s">%s</a><br>', $PHP_SELF, mysql_result($result, $i, 0), mysql_result($result, $i - 1, 0), mysql_result($result, $i, 1));
    }

    //Or else there will be both
    else {
    printf('<a href="%s?contentID=%s&nextID=%s&prevID=%s">%s</a><br>', $PHP_SELF, mysql_result($result, $i, 0), mysql_result($result, $i + 1, 0), mysql_result($result, $i - 1, 0), mysql_result($result, $i, 1));
    }
    }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoa this works...but what if the user is on the page, reading the article? How would the nextID and prevID get passed?
    <///////~

    www.sohh.com

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doh! Let me look at it again
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  12. #12
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay so I changed my approach a bit, now it does have to query each time the page is loaded but it doesn't loop through all the resutls, it just grabs the rows that it needs.

    <?
    if (!isset($place)) $place = 0;
    $db = mysql_connect("localhost", "");
    mysql_select_db("");
    $result = mysql_query("select * from tablename");
    $count = mysql_num_rows($result);


    if ($place != 0) {
    printf('<a href="%s?place=%s">&lt;&lt;</a>&nbsp;&nbsp;&nbsp;', $PHP_SELF, $place - 1);
    }
    printf('Current = %s&nbsp;&nbsp;&nbsp;', mysql_result($result, $place, 0));

    if ($place != $count - 1) {
    printf('<a href="%s?place=%s">&gt;&gt;</a>&nbsp;&nbsp;&nbsp;', $PHP_SELF, $place + 1);
    }

    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  13. #13
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did it. Seems to work...

    http://www.sohh.com/thewire/test2.php?contentID=88

    I've gotta change the place variable to be contentID and see if it's working right. You got AIM Freddie?

    Steven
    <///////~

    www.sohh.com

  14. #14
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    aim - LouieSimps
    ICQ as seen below
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  15. #15
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Freddy, I'm a bit lost...First, I'm going to assume that $place represents the ID of the article that we are at.

    Ok, I get the top part...

    if (!isset($place)) $place = 0;
    $db = mysql_connect("localhost", "");
    mysql_select_db("");
    $result = mysql_query("select * from tablename");
    $count = mysql_num_rows($result);

    [If the variable $place is not set, the set the $place to 0. Then we connect to the database...Do the query...and find out how many rows are returned from the query.]

    Here's where I'm lost at...

    if ($place != 0) {
    printf('<a href="%s?place=%s">&lt;&lt;</a> ', $PHP_SELF, $place - 1);
    }

    [if $place does not equal zero print the link for back button.]

    printf('Current = %s ', mysql_result($result, $place, 0));

    [tell us what the current ID is...]

    if ($place != $count - 1) {
    printf('<a href="%s?place=%s">&gt;&gt;</a> ', $PHP_SELF, $place + 1);
    }

    [if $place does not equal the count minus 1 print the link for back the next button.]

    Are we still working from the array, or are we doing something totally different?
    <///////~

    www.sohh.com

  16. #16
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh yeah sorry, now I am using a counter to keep track of what I want use in mysql_result() function. Remember the part about mysql_result($result, 0, 0) would grab the first result from the first field. Well I am saying grab the first result of the record which $place holds. When it starts its at 0 so there is no previous link and when $count hits the number of records returned - 1 you know there is no next link.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  17. #17
    SitePoint Addict
    Join Date
    Dec 2000
    Location
    BOSTON MA
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i've learned so much from reading this post alone and i just wanted to thank everyone who replied to the question.

    i've been reading "PHP fast and easy web development" for the couple of months and i'm a little dissapointed that it didn't offer more.

    so, i'm wondering if a really great book can be recommended that would cover things like the differences between echo and print and explanations of the functions and expressions rather than just examples.

    would i want a book covering mysql in addition to one that would cover php?

    either way, thanks again
    . . . chris

  18. #18
    SitePoint Zealot moshe_be's Avatar
    Join Date
    Dec 2000
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I got better idea, instead of looping the entire database each time, just use simple select that checks the date.

    Example: The current article date (in unix timestamp) is 230817.

    $NextLink = mysql_query('SELECT articleID from articles WHERE UNIX_TIMESTAMP(articleDate) > ' . $curdate . ' LIMIT 1');

    $PreviousLink = mysql_query('SELECT articleID from articles WHERE UNIX_TIMESTAMP(articleDate) < ' . $curdate . ' LIMIT 1');

    That will give you the ID's for next and previous articles. Unless you got two articles at the exact same date/time it will work.

  19. #19
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For my site, I put up between 3 - 6 articles per day. So that wouldn't work well.

    To date, I'm still stuck doing this. So if anyone including those who've attempted to help in the past, can help me figure this out, thanks.

    Here what I've got so far.

    I got a query statement that list all of the article ID's into one array.

    $query_listIDs = "SELECT tbl_content.contentID, tbl_content.contentheadline FROM tbl_content WHERE tbl_content.contentsubsectionID = 1 AND tbl_content.contentapproved = 1 AND tbl_content.contentlive = 1 AND tbl_content.contentgolive > $todayis AND tbl_content.contentgodown > $todayis ORDER BY tbl_content.contentgolive DESC, tbl_content.contentID DESC";

    $result_listIDs = mysql_query($query_listIDs);

    while($row_listIDs = mysql_fetch_array($result_listIDs)) {
    $data_listIDs[] = array("contentID" => $row_listIDs["contentID"]);
    }

    Now, when I go to the page (http://www.sohh.com/thewire/read.php?contentID=97) I'm passing the contentID a value of 97, which gets the 97th article. How do I tell the internal pointer of the array that I'm on article 97?

    Holla Back!
    <///////~

    www.sohh.com

  20. #20
    SitePoint Zealot moshe_be's Avatar
    Join Date
    Dec 2000
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by ibeblunt
    [B]For my site, I put up between 3 - 6 articles per day. So that wouldn't work well.
    Why not? the time stamp even includes seconds, so unless you post two articles on the same second, the next and previous links will be accurate.

  21. #21
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll try it. I just hate converting UNIXTIMESTAMPS.
    <///////~

    www.sohh.com

  22. #22
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just realized. How does it know what article ID it's on?
    <///////~

    www.sohh.com

  23. #23
    SitePoint Zealot moshe_be's Avatar
    Join Date
    Dec 2000
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't have to convert them, you are getting the ID of the next and previous article

  24. #24
    SitePoint Zealot moshe_be's Avatar
    Join Date
    Dec 2000
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by ibeblunt
    Just realized. How does it know what article ID it's on?
    What it does is sorting the articles by Date and time of posting then get only one.. thus you are getting the one after the current article and one before it.

  25. #25
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I meant I have to convert my dates and times to UnixTimeStamps. As of now I'm using just regular dates (ie 2001/03/23). Any more ideas?
    <///////~

    www.sohh.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •