SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Dec 2000
    Location
    BOSTON MA
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, i know i saw this in the forums somewhere before but i can't seem to find it. if you can locate it let me know or you can try to answer the question. here it is.

    i have x number of records in a table that get displayed on a listings page with $limit1 and $limit2 as variables.
    like this :

    Code:
    $sqlquery = "SELECT * FROM $table LIMIT $limit1, $limit2";
    everything works fine. i used the examples from this thread :

    http://www.sitepointforums.com/showt...threadid=16004

    it's just that i can't figure out how to have individual links along the bottom of the page according to the number of pages. for examplem if there were 66 records in the table and i wanted to show 25 per page. that would mean 2 pages would have 25 records and the 3rd would have 16, equaling 3 pages total.

    what i want to do is put links to pages 1 - 2 - 3 along the bottom so that the user can jump from page to page w/o having to go to the previous or next page.

    so far i have this but it's definitely not working :

    Code:
    $limit2 = "25";
    
    $result = mysql_query("SELECT * FROM $table");
    $count = (mysql_num_rows($result));
    
    
    $pages = ceil($count / $limit2);
    
    $i= 0;
    
    while ($count > $i){
    	printf('<center><a href="%s?limit1=%s">%s - </a></center>', $PHP_SELF, $limit1 + $limit2, $pages);
    
    $i++;
    }
    this just gives me 66 identical links and does not increment until the next page.
    instead of this :
    3 -
    3 -
    3 -
    .....(66 times)
    i need this :


    1 - 2 - 3
    any help or ideas would be appreciated.
    thanks in advance.
    . . . chris

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This function should get you going:

    Code:
    function get_nav($offset, $limit, $totalnum) {
    	global $PHP_SELF;
    	if ($totalnum > $limit) {
    		// calculate number of pages needing links
    		$pages = intval($totalnum/$limit);
    
    		// $pages now contains int of pages needed unless there is a remainder from division
    		if ($totalnum%$limit) $pages++;
    
    			if (($offset + $limit) > $totalnum) {
    				$lastnum = $totalnum;
    				}
    			else {
    				$lastnum = ($offset + $limit);
    				}		
    			$return .= sprintf('%s%s<tr><td>Page </td>', $this->table_start("", 0, 4, 0), "\n");
    			for ($i=1; $i <= $pages; $i++) {  // loop thru
    				$newoffset=$limit*($i-1);
    				if ($newoffset != $offset) {
    					$return .= sprintf('<td><a href="%s?offset=%s">%s</a></td>%s', $PHP_SELF, $newoffset, $i, "\n");
    					}
    				else {
    					$return .= sprintf('<td>%s</td>%s', $i,"\n");
    					}
    				}
    			$return .= sprintf('</tr>%s', $this->table_end());
    			}
    		else {
    			$return = "";
    			}
    		return $return;
    	}

    Sample Usage


    Code:
    if (!isset($limit1)) $limit1 = 0;
    $limit2 = 25;
    $result1 = mysql_query("SELECT count(*) FROM $table");
    $totalcount = mysql_result($result1, 0);
    
    $result2 = mysql_query("SELECT * FROM $table LIMIT $limit1, $limit2");
    
    while($row= mysql_fetch_array($result2)) {
    //process results
    }
    
    print get_nav($limit1,$limit2,$totalnum);
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Addict
    Join Date
    Dec 2000
    Location
    BOSTON MA
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, i know it's been a few days.
    i've been working on adapting this script into mine and i think i almost have it working but i keep getting this error :

    Code:
    Fatal error: Call to a member function on a non-object in /www/bostoncreative/php-bin/listingcurrent3.php on line 68
    line 68 in the code is this :

    Code:
    $return .= sprintf('%s%s<tr><td>Page </td>', $this->table_start("", 0, 4, 0), "\n");
    any thoughts on how to get around this?
    . . . chris

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just take the $this-> off it is from a class that function used to be a part of.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Addict
    Join Date
    Dec 2000
    Location
    BOSTON MA
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok now i get this :

    Code:
    Fatal error: Call to undefined function: table_end() in /www/bostoncreative/php-bin/listingcurrent3.php on line 87
    if it's just s function that creates a table couldn't i just do something like this instead?

    Code:
    $return .= sprintf(\'<table width=\"600\" height=\"30\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td>Page </td>\')
    ** disregard the slashes.....i had to use them tbe able to submit the code **
    . . . chris

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure, my code is merely there to provide a basis for you to expand on. There were two or three functions that should have been included on the page for that to work right.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Addict
    Join Date
    Dec 2000
    Location
    BOSTON MA
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    gotcha......see i'm learning.

    just one last thing.

    can i call the values produced by the variables in the function. like $pages and $lastnum etc without having to recreate them outside the function?

    thanks freddy
    . . . chris


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
  •