Jump to content

Recommended Posts

Im having trouble with paging, when im on the first page, the Previous displays, when im on the last page it still says Next when its meant to be Last.

 

Any other way of structuring?

 

This is how its structured:

 

<?php

$rss = new lastRSS; 
$searchx = $_GET["search"];
$search = $_GET["search"];
$start = $_GET["page"];
$replace = " "; $with = "+";
$search = str_replace($replace, $with, $search);
$title = "+"; $title2 = " ";
$titlefinal = str_replace($title, $title2, $s);
$perpage =  "1";	
$next = $start + $perpage;
$past = $start - $perpage;

?>

 

This is how I'd call it:

 

<a href="search.php?search=<? echo $search ?>&page=<? echo $past ?>" class="style2">
		Previous</a> | 
<a href="search.php?search=<? echo $search ?>&page=<? echo $next ?>" class="style2">
			Next</a>

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/
Share on other sites

Figure out how many pages there are. If the current page number equals the last page number, then the "Next" link shouldn't be shown. The first page is 1, so if that is the current page number the "Previous" link shouldn't be shown.

 

Can you provide some code to do this?, I dont get how I can define if the page numbers are the same.

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904022
Share on other sites

Of course I could, but I'll leave it to you as an exercise. It's quite simple logic. I'm sure you can work it out if you try, and you'll benefit more by doing it yourself than having people feed you the answers :)

 

Well i got a rough idea:

 

$past and $next are always numbers, so before the $past and $next are echo'd I could use regex to check the if the $past and $next numbers are the same, then do an str_replace to remove.

 

But im new at regex, so I'll do some studying.

 

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904027
Share on other sites

No, it's much simpler. Suppose we have a variable $numPages and a variable $currentPage. To determine if we should show the "Next" link we'll do:

if ($currentPage < $numPages) {
// display next
}

 

Almost similarly with the "previous" link:

if ($currentPage > 1) { // because 1 is the first page
// show previous
}

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904030
Share on other sites

So it would be like:

 

<?php

$rss = new lastRSS; 
$searchx = $_GET["search"];
$search = $_GET["search"];
$start = $_GET["page"];
$replace = " "; $with = "+";
$search = str_replace($replace, $with, $search);
$title = "+"; $title2 = " ";
$titlefinal = str_replace($title, $title2, $s);
$perpage =  "1";   
$next = $start + $perpage;
$past = $start - $perpage;

if ($currentPage < $numPages) {
echo "<a href=\"search.php?search=$search&page=$next\">Next</a>\n";

}

if ($currentPage > 1) { // because 1 is the first page
echo "<a href=\"search.php?search=$search&page=$previous\">Previous</a>\n";
}

?>

 

But what would the $currenPage variable equal too...

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904033
Share on other sites

I see, I've tried this but it dont work:

 

<?php

$rss = new lastRSS; 
$searchx = $_GET["search"];
$search = $_GET["search"];
$start = $_GET["page"];
$replace = " "; $with = "+";
$search = str_replace($replace, $with, $search);
$title = "+"; $title2 = " ";
$titlefinal = str_replace($title, $title2, $s);
$perpage =  "1";   
$next = $start + $perpage;
$past = $start - $perpage;

if ($start < $perpage) {
echo "<a href=\"search.php?search=$search&page=$next\">Next</a>\n";

}

if ($start > 1) { // because 1 is the first page
echo "<a href=\"search.php?search=$search&page=$previous\">Previous</a>\n";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904045
Share on other sites

Try learning from this =)

 

<?php

//if the page is not set, set it to 1
if(!$_GET['page'])
$_GET['page']=1;

//ser the variable start to the current page.
$start = $_GET['page'];

$perpage = 1;   
$next = $start + $perpage;
$past = $start - $perpage;

//If the current page is less than or equal to 1 ($perpage) 
if ($start <= $perpage) {
//Echo the current page, and add a "next" button.
echo "Page #$start <br/>";
echo "<a href=\"?page=$next\">Next</a>";
}
//If the current page is bigger than one
if ($start > 1) { // because 1 is the first page
//Echo the current page, and add a "next" and "previous" button.
echo "Page #$start <br/>";
echo "<a href=\"?page=$past\">Previous</a> |";
echo " <a href=\"?page=$next\">Next</a> ";
}

?>

 

Best wishes

//AngelicS

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904051
Share on other sites

@ AngelicS

 

Thanks, that works great, but it can't determine the Last. It carries on and on, even though the search results dont have that many pages (they just repeat the same page)

 

And a minor change:

 

replace: ?page=

 

with: search.php?search=$search&page=

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904059
Share on other sites

You'll have to find a way to find out how many pages there are, and then do the following.

 

Create a variable that contains the maximum number of pages.

 

For example:

$max_pages=10 //In this case, but you might want to check by looking in a database or something.

 

Then change the following code:

if ($start > 1 && <= $max_pages) { // because 1 is the first page
//Echo the current page, and add a "next" and "previous" button.
echo "Page #$start <br/>";
echo "<a href=\"?page=$past\">Previous</a> |";
echo " <a href=\"?page=$next\">Next</a> ";
}

 

And add the following code:

if ($start <= $max_pages)
{
echo "<a href=\"?page=$past\">Previous</a>";
}

 

 

And about the search thing, you just have to add it =)

 

Best wishes

//AngelicS

 

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904089
Share on other sites

Thanks everyone, heres the whole code, so you further understand the structure:

 

Search.php

<? $search = $_GET["search"]; ?>

<?php 

class lastRSS { 
    // ------------------------------------------------------------------- 
    // Public properties 
    // ------------------------------------------------------------------- 
    var $default_cp = 'UTF-8'; 
    var $CDATA = 'nochange'; 
    var $cp = ''; 
    var $items_limit = 0; 
    var $stripHTML = false; 
    var $date_format = ''; 

    // ------------------------------------------------------------------- 
    // Private variables 
    // ------------------------------------------------------------------- 
    var $channeltags = array ('title', 'link', 'description', 'language', 'copyright', 'managingEditor', 'webMaster', 'lastBuildDate', 'rating', 'docs'); 
    var $itemtags = array('title', 'link', 'description', 'author', 'category', 'comments', 'enclosure', 'guid', 'pubDate', 'source', 'thumbnail', 'Listings'); 
    var $imagetags = array('title', 'url', 'link', 'width', 'height'); 
    var $textinputtags = array('title', 'description', 'name', 'link'); 

    // ------------------------------------------------------------------- 
    // Parse RSS file and returns associative array. 
    // ------------------------------------------------------------------- 
    function Get ($rss_url) { 
        // If CACHE ENABLED 
        if ($this->cache_dir != '') { 
            $cache_file = $this->cache_dir . '/rsscache_' . md5($rss_url); 
            $timedif = @(time() - filemtime($cache_file)); 
            if ($timedif < $this->cache_time) { 
                // cached file is fresh enough, return cached array 
                $result = unserialize(join('', file($cache_file))); 
                // set 'cached' to 1 only if cached file is correct 
                if ($result) $result['cached'] = 1; 
            } else { 
                // cached file is too old, create new 
                $result = $this->Parse($rss_url); 
                $serialized = serialize($result); 
                if ($f = @fopen($cache_file, 'w')) { 
                    fwrite ($f, $serialized, strlen($serialized)); 
                    fclose($f); 
                } 
                if ($result) $result['cached'] = 0; 
            } 
        } 
        // If CACHE DISABLED >> load and parse the file directly 
        else { 
            $result = $this->Parse($rss_url); 
            if ($result) $result['cached'] = 0; 
        } 
        // return result 
        return $result; 
    } 
     
    // ------------------------------------------------------------------- 
    // Modification of preg_match(); return trimed field with index 1 
    // from 'classic' preg_match() array output 
    // ------------------------------------------------------------------- 
    function my_preg_match ($pattern, $subject) { 
        // start regullar expression 
        preg_match($pattern, $subject, $out); 

        // if there is some result... process it and return it 
        if(isset($out[1])) { 
            // Process CDATA (if present) 
            if ($this->CDATA == 'content') { // Get CDATA content (without CDATA tag) 
                $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>'')); 
            } elseif ($this->CDATA == 'strip') { // Strip CDATA 
                $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>'')); 
            } 

            // If code page is set convert character encoding to required 
            if ($this->cp != '') 
                //$out[1] = $this->MyConvertEncoding($this->rsscp, $this->cp, $out[1]); 
                $out[1] = iconv($this->rsscp, $this->cp.'//TRANSLIT', $out[1]); 
            // Return result 
            return trim($out[1]); 
        } else { 
        // if there is NO result, return empty string 
            return ''; 
        } 
    } 

    // ------------------------------------------------------------------- 
    // Replace HTML entities &something; by real characters 
    // ------------------------------------------------------------------- 
    function unhtmlentities ($string) { 
        // Get HTML entities table 
        $trans_tbl = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES); 
        // Flip keys<==>values 
        $trans_tbl = array_flip ($trans_tbl); 
        // Add support for ' entity (missing in HTML_ENTITIES) 
        $trans_tbl += array(''' => "'"); 
	$trans_tbl += array(''' => ","); 
        // Replace entities by values 
        return strtr ($string, $trans_tbl); 
    } 

    // ------------------------------------------------------------------- 
    // Parse() is private method used by Get() to load and parse RSS file. 
    // Don't use Parse() in your scripts - use Get($rss_file) instead. 
    // ------------------------------------------------------------------- 
    function Parse ($rss_url) { 
        // Open and load RSS file 
        if ($f = @fopen($rss_url, 'r')) { 
            $rss_content = ''; 
            while (!feof($f)) { 
                $rss_content .= fgets($f, 4096); 
            } 
            fclose($f); 

            // Parse document encoding 
            $result['encoding'] = $this->my_preg_match("'encoding=[\'\"](.*?)[\'\"]'si", $rss_content); 
            // if document codepage is specified, use it 
            if ($result['encoding'] != '') 
                { $this->rsscp = $result['encoding']; } // This is used in my_preg_match() 
            // otherwise use the default codepage 
            else 
                { $this->rsscp = $this->default_cp; } // This is used in my_preg_match() 

            // Parse CHANNEL info 
            preg_match("'<channel.*?>(.*?)</channel>'si", $rss_content, $out_channel); 
            foreach($this->channeltags as $channeltag) 
            { 
                $temp = $this->my_preg_match("'<$channeltag.*?>(.*?)</$channeltag>'si", $out_channel[1]); 
                if ($temp != '') $result[$channeltag] = $temp; // Set only if not empty 
            } 
            // If date_format is specified and lastBuildDate is valid 
            if ($this->date_format != '' && ($timestamp = strtotime($result['lastBuildDate'])) !==-1) { 
                        // convert lastBuildDate to specified date format 
                        $result['lastBuildDate'] = date($this->date_format, $timestamp); 
            } 

            // Parse TEXTINPUT info 
            preg_match("'<textinput(|[^>]*[^/])>(.*?)</textinput>'si", $rss_content, $out_textinfo); 
                // This a little strange regexp means: 
                // Look for tag <textinput> with or without any attributes, but skip truncated version <textinput /> (it's not beggining tag) 
            if (isset($out_textinfo[2])) { 
                foreach($this->textinputtags as $textinputtag) { 
                    $temp = $this->my_preg_match("'<$textinputtag.*?>(.*?)</$textinputtag>'si", $out_textinfo[2]); 
                    if ($temp != '') $result['textinput_'.$textinputtag] = $temp; // Set only if not empty 
                } 
            } 
            // Parse IMAGE info 
            preg_match("'<image.*?>(.*?)</image>'si", $rss_content, $out_imageinfo); 
            if (isset($out_imageinfo[1])) { 
                foreach($this->imagetags as $imagetag) { 
                    $temp = $this->my_preg_match("'<$imagetag.*?>(.*?)</$imagetag>'si", $out_imageinfo[1]); 
                    if ($temp != '') $result['image_'.$imagetag] = $temp; // Set only if not empty 
                } 
            } 
            // Parse ITEMS 
            preg_match_all("'<item(| .*?)>(.*?)</item>'si", $rss_content, $items); 
            $rss_items = $items[2]; 
            $i = 0; 
            $result['items'] = array(); // create array even if there are no items 
            foreach($rss_items as $rss_item) { 
                // If number of items is lower then limit: Parse one item 
                if ($i < $this->items_limit || $this->items_limit == 0) { 
                    foreach($this->itemtags as $itemtag) { 
                        $temp = $this->my_preg_match("'<$itemtag.*?>(.*?)</$itemtag>'si", $rss_item); 
                        if ($temp != '') $result['items'][$i][$itemtag] = $temp; // Set only if not empty 
                    } 
                    // Strip HTML tags and other bullshit from DESCRIPTION 
                    if ($this->stripHTML && $result['items'][$i]['description']) 
                        $result['items'][$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['description']))); 
                    // Strip HTML tags and other bullshit from TITLE 
                    if ($this->stripHTML && $result['items'][$i]['title']) 
                        $result['items'][$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['title']))); 
                    // If date_format is specified and pubDate is valid 
                    if ($this->date_format != '' && ($timestamp = strtotime($result['items'][$i]['pubDate'])) !==-1) { 
                        // convert pubDate to specified date format 
                        $result['items'][$i]['pubDate'] = date($this->date_format, $timestamp); 
                    } 
                    // Item counter 
                    $i++; 
                } 
            } 

            $result['items_count'] = $i; 
            return $result; 
        } 
        else // Error in opening return False 
        { 
            return False; 
        } 
    } 
} 



$rss = new lastRSS; 
$searchx = $_GET["search"];
$search = $_GET["search"];
$start = $_GET["page"];
$replace = " "; $with = "+";
$search = str_replace($replace, $with, $search);
$title = "+"; $title2 = " ";
$titlefinal = str_replace($title, $title2, $s);

//Paging
$perpage =  "1";	
$next = $start + $perpage;
$past = $start - $perpage;


?>

<form method="get" action="search.php">
<input name="search" type="text" class="input-box" />
<input name="submit" type="submit" class="button" value="Search" />


</form>

<?php
// Yahoo API

if ($rs = $rss->get("http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Adoc+$search&adult_ok=1&start=$start")) {

}

// Go through the list powered by the search engine listed and get the data from each <item>

    foreach($rs['items'] as $item) { 

// Get the title of result

   $title = $item['title'];

// Get the description of the result

   $description = $item['description'];

// Get the link eg amazon.com

   $urllink = $item['guid'];

//Include the layout which echos the result 
include "resulttemplate.php";

echo "\n"; 

} 





?>

<?php
// echo next and previous for paging
echo "<a href=\"search.php?search=$search&submit=Search&page=$past\">Previous</a> |";
echo "<a href=\"search.php?search=$search&submit=Search&page=$next\">Next</a><BR><BR>";

?>

 

I've added the modifications, except havent figured out a way to make it complete. I can't get the paging to say Last when its last page.

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904156
Share on other sites

Did you even try to read my last post?

Because the code you need is there.

 

Yes, I did, it worked great, but I can't figure out how I'd identify how many pages their is, since the data is coming externally from yahoo, and not a database. Theirfore each search result has a different number of pages. If you could point me to the right direction I'd really apreciate it.

 

If you save the above code as search.php and then access it you'll see the how it looks.

 

 

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904473
Share on other sites

This is the first sentence in the chapter on counting in my discrete mathematics book:

Techniques for counting are important in mathematics and in computer science, especially in the analysis of algorithms.

 

Now I'll guess what you're supposed to do in order to figure out how many pages there are.

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904475
Share on other sites

This is the first sentence in the chapter on counting in my discrete mathematics book:

Techniques for counting are important in mathematics and in computer science, especially in the analysis of algorithms.

 

Now I'll guess what you're supposed to do in order to figure out how many pages there are.

 

Mind sharing how you'd do it, so I can learn from it and mind telling me whats the book called?

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904477
Share on other sites

The book is called Discrete Mathematical Structures by Kolman, Busby and Ross, but that's beside the point. My point was that you figure out the number of pages by counting the number of items. You then just divide by the number of items you want per page and ceil that number.

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904480
Share on other sites

The book is called Discrete Mathematical Structures by Kolman, Busby and Ross, but that's beside the point. My point was that you figure out the number of pages by counting the number of items. You then just divide by the number of items you want per page and ceil that number.

 

Im trying to get 10 items per page. Im counting the items by $i++;.

 

So 10 divided by $i++ then ciel?

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904485
Share on other sites


$content_count = 1000;
$page = $_GET['page'];

$tpage = ceil( $content_count / 10 );
$startfrom = ( $page - 1 ) * 10;

    $prev = $page - 1;
    $next = $page + 1;
    $adjust = 9;
    
    if ( 1 < $page )
    {
        $page_links .= "<a href='".$prev."'><< Prev</a><a href='1'>1</a>....";
    }
    $k = $page - $adjust;
    for ( $page ; $k <= $page + $adjust; ++$k )
    {
        if (( 0 < $k ) && ( $k <= $tpage ) )
        {
            
            if(($page)==($k)){
			$page_links.="<span> ".$k." </span>";
			} else {
               $page_links .= "<a href='".$k."'>{$k}</a>";
                    		}
     	}
    }
    if ( $page < $tpage )
    {
    
        $page_links .= ".....<a href='".$tpage."'>".$tpage."</a><a href='".$next."'>Next >></a>";
        
    }

echo $page_links;

 

Your stuffed with a total ;)

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904488
Share on other sites


$content_count = 1000;
$page = $_GET['page'];

$tpage = ceil( $content_count / 10 );
$startfrom = ( $page - 1 ) * 10;

    $prev = $page - 1;
    $next = $page + 1;
    $adjust = 9;
    
    if ( 1 < $page )
    {
        $page_links .= "<a href='".$prev."'><< Prev</a><a href='1'>1</a>....";
    }
    $k = $page - $adjust;
    for ( $page ; $k <= $page + $adjust; ++$k )
    {
        if (( 0 < $k ) && ( $k <= $tpage ) )
        {
            
            if(($page)==($k)){
			$page_links.="<span> ".$k." </span>";
			} else {
               $page_links .= "<a href='".$k."'>{$k}</a>";
                    		}
     	}
    }
    if ( $page < $tpage )
    {
    
        $page_links .= ".....<a href='".$tpage."'>".$tpage."</a><a href='".$next."'>Next >></a>";
        
    }

echo $page_links;

 

Your stuffed with a total ;)

 

Thanks, but it goes to 100, some searches only need to 2 pages, not all have 100 pages, the ones that dont have 100 pages just loop continuously until they get to 100.

 

See:

 

<? $search = $_GET["search"]; ?>

<?php 

class lastRSS { 
    // ------------------------------------------------------------------- 
    // Public properties 
    // ------------------------------------------------------------------- 
    var $default_cp = 'UTF-8'; 
    var $CDATA = 'nochange'; 
    var $cp = ''; 
    var $items_limit = 0; 
    var $stripHTML = false; 
    var $date_format = ''; 

    // ------------------------------------------------------------------- 
    // Private variables 
    // ------------------------------------------------------------------- 
    var $channeltags = array ('title', 'link', 'description', 'language', 'copyright', 'managingEditor', 'webMaster', 'lastBuildDate', 'rating', 'docs'); 
    var $itemtags = array('title', 'link', 'description', 'author', 'category', 'comments', 'enclosure', 'guid', 'pubDate', 'source', 'thumbnail', 'Listings'); 
    var $imagetags = array('title', 'url', 'link', 'width', 'height'); 
    var $textinputtags = array('title', 'description', 'name', 'link'); 

    // ------------------------------------------------------------------- 
    // Parse RSS file and returns associative array. 
    // ------------------------------------------------------------------- 
    function Get ($rss_url) { 
        // If CACHE ENABLED 
        if ($this->cache_dir != '') { 
            $cache_file = $this->cache_dir . '/rsscache_' . md5($rss_url); 
            $timedif = @(time() - filemtime($cache_file)); 
            if ($timedif < $this->cache_time) { 
                // cached file is fresh enough, return cached array 
                $result = unserialize(join('', file($cache_file))); 
                // set 'cached' to 1 only if cached file is correct 
                if ($result) $result['cached'] = 1; 
            } else { 
                // cached file is too old, create new 
                $result = $this->Parse($rss_url); 
                $serialized = serialize($result); 
                if ($f = @fopen($cache_file, 'w')) { 
                    fwrite ($f, $serialized, strlen($serialized)); 
                    fclose($f); 
                } 
                if ($result) $result['cached'] = 0; 
            } 
        } 
        // If CACHE DISABLED >> load and parse the file directly 
        else { 
            $result = $this->Parse($rss_url); 
            if ($result) $result['cached'] = 0; 
        } 
        // return result 
        return $result; 
    } 
     
    // ------------------------------------------------------------------- 
    // Modification of preg_match(); return trimed field with index 1 
    // from 'classic' preg_match() array output 
    // ------------------------------------------------------------------- 
    function my_preg_match ($pattern, $subject) { 
        // start regullar expression 
        preg_match($pattern, $subject, $out); 

        // if there is some result... process it and return it 
        if(isset($out[1])) { 
            // Process CDATA (if present) 
            if ($this->CDATA == 'content') { // Get CDATA content (without CDATA tag) 
                $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>'')); 
            } elseif ($this->CDATA == 'strip') { // Strip CDATA 
                $out[1] = strtr($out[1], array('<![CDATA['=>'', ']]>'=>'')); 
            } 

            // If code page is set convert character encoding to required 
            if ($this->cp != '') 
                //$out[1] = $this->MyConvertEncoding($this->rsscp, $this->cp, $out[1]); 
                $out[1] = iconv($this->rsscp, $this->cp.'//TRANSLIT', $out[1]); 
            // Return result 
            return trim($out[1]); 
        } else { 
        // if there is NO result, return empty string 
            return ''; 
        } 
    } 

    // ------------------------------------------------------------------- 
    // Replace HTML entities &something; by real characters 
    // ------------------------------------------------------------------- 
    function unhtmlentities ($string) { 
        // Get HTML entities table 
        $trans_tbl = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES); 
        // Flip keys<==>values 
        $trans_tbl = array_flip ($trans_tbl); 
        // Add support for ' entity (missing in HTML_ENTITIES) 
        $trans_tbl += array(''' => "'"); 
      $trans_tbl += array(''' => ","); 
        // Replace entities by values 
        return strtr ($string, $trans_tbl); 
    } 

    // ------------------------------------------------------------------- 
    // Parse() is private method used by Get() to load and parse RSS file. 
    // Don't use Parse() in your scripts - use Get($rss_file) instead. 
    // ------------------------------------------------------------------- 
    function Parse ($rss_url) { 
        // Open and load RSS file 
        if ($f = @fopen($rss_url, 'r')) { 
            $rss_content = ''; 
            while (!feof($f)) { 
                $rss_content .= fgets($f, 4096); 
            } 
            fclose($f); 

            // Parse document encoding 
            $result['encoding'] = $this->my_preg_match("'encoding=[\'\"](.*?)[\'\"]'si", $rss_content); 
            // if document codepage is specified, use it 
            if ($result['encoding'] != '') 
                { $this->rsscp = $result['encoding']; } // This is used in my_preg_match() 
            // otherwise use the default codepage 
            else 
                { $this->rsscp = $this->default_cp; } // This is used in my_preg_match() 

            // Parse CHANNEL info 
            preg_match("'<channel.*?>(.*?)</channel>'si", $rss_content, $out_channel);
            foreach($this->channeltags as $channeltag)
            {
                $temp = $this->my_preg_match("'<$channeltag.*?>(.*?)</$channeltag>'si", $out_channel[1]);
                if ($temp != '') $result[$channeltag] = $temp; // Set only if not empty
            }
            // If date_format is specified and lastBuildDate is valid
            if ($this->date_format != '' && ($timestamp = strtotime($result['lastBuildDate'])) !==-1) {
                        // convert lastBuildDate to specified date format
                        $result['lastBuildDate'] = date($this->date_format, $timestamp);
            }

            // Parse TEXTINPUT info
            preg_match("'<textinput(|[^>]*[^/])>(.*?)</textinput>'si", $rss_content, $out_textinfo);
                // This a little strange regexp means:
                // Look for tag <textinput> with or without any attributes, but skip truncated version <textinput /> (it's not beggining tag)
            if (isset($out_textinfo[2])) {
                foreach($this->textinputtags as $textinputtag) {
                    $temp = $this->my_preg_match("'<$textinputtag.*?>(.*?)</$textinputtag>'si", $out_textinfo[2]);
                    if ($temp != '') $result['textinput_'.$textinputtag] = $temp; // Set only if not empty
                }
            }
            // Parse IMAGE info
            preg_match("'<image.*?>(.*?)</image>'si", $rss_content, $out_imageinfo);
            if (isset($out_imageinfo[1])) {
                foreach($this->imagetags as $imagetag) {
                    $temp = $this->my_preg_match("'<$imagetag.*?>(.*?)</$imagetag>'si", $out_imageinfo[1]);
                    if ($temp != '') $result['image_'.$imagetag] = $temp; // Set only if not empty
                }
            }
            // Parse ITEMS
            preg_match_all("'<item(| .*?)>(.*?)</item>'si", $rss_content, $items);
            $rss_items = $items[2];
            $i = 0;
            $result['items'] = array(); // create array even if there are no items
            foreach($rss_items as $rss_item) {
                // If number of items is lower then limit: Parse one item
                if ($i < $this->items_limit || $this->items_limit == 0) {
                    foreach($this->itemtags as $itemtag) {
                        $temp = $this->my_preg_match("'<$itemtag.*?>(.*?)</$itemtag>'si", $rss_item);
                        if ($temp != '') $result['items'][$i][$itemtag] = $temp; // Set only if not empty
                    }
                    // Strip HTML tags and other bullshit from DESCRIPTION
                    if ($this->stripHTML && $result['items'][$i]['description'])
                        $result['items'][$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['description'])));
                    // Strip HTML tags and other bullshit from TITLE
                    if ($this->stripHTML && $result['items'][$i]['title'])
                        $result['items'][$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['title'])));
                    // If date_format is specified and pubDate is valid
                    if ($this->date_format != '' && ($timestamp = strtotime($result['items'][$i]['pubDate'])) !==-1) {
                        // convert pubDate to specified date format
                        $result['items'][$i]['pubDate'] = date($this->date_format, $timestamp);
                    }
                    // Item counter
                    $i++;
                }
            }

            $result['items_count'] = $i;
            return $result;
        }
        else // Error in opening return False
        {
            return False;
        }
    }
}



$rss = new lastRSS;
$searchx = $_GET["search"];
$search = $_GET["search"];
$start = $_GET["page"];
$replace = " "; $with = "+";
$search = str_replace($replace, $with, $search);
$title = "+"; $title2 = " ";
$titlefinal = str_replace($title, $title2, $s);

//Paging
$perpage =  "1";   
$next = $start + $perpage;
$past = $start - $perpage;


?>

<form method="get" action="search.php">
<input name="search" type="text" class="input-box" />
<input name="submit" type="submit" class="button" value="Search" />


</form>

<?php
// Yahoo API

if ($rs = $rss->get("http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Adoc+$search&adult_ok=1&start=$start")) {

}

// Go through the list powered by the search engine listed and get the data from each <item>

    foreach($rs['items'] as $item) { 

   // Get the title of result

      $title = $item['title'];

   // Get the description of the result

      $description = $item['description'];

   // Get the link eg amazon.com

      $urllink = $item['guid'];

//Include the layout which echos the result 
include "resulttemplate.php";

echo "\n"; 

} 

   



?>

<?php
// echo next and previous for paging
echo "<a href=\"search.php?search=$search&submit=Search&page=$past\">Previous</a> |";
echo "<a href=\"search.php?search=$search&submit=Search&page=$next\">Next</a><BR><BR>";

?>

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904495
Share on other sites


$content_count = 1000;
$page = $_GET['page'];

$tpage = ceil( $content_count / 10 );
$startfrom = ( $page - 1 ) * 10;

    $prev = $page - 1;
    $next = $page + 1;
    $adjust = 9;
    
    if ( 1 < $page )
    {
        $page_links .= "<a href='".$prev."'><< Prev</a><a href='1'>1</a>....";
    }
    $k = $page - $adjust;
    for ( $page ; $k <= $page + $adjust; ++$k )
    {
        if (( 0 < $k ) && ( $k <= $tpage ) )
        {
            
            if(($page)==($k)){
			$page_links.="<span> ".$k." </span>";
			} else {
               $page_links .= "<a href='".$k."'>{$k}</a>";
                    		}
     	}
    }
    if ( $page < $tpage )
    {
    
        $page_links .= ".....<a href='".$tpage."'>".$tpage."</a><a href='".$next."'>Next >></a>";
        
    }

echo $page_links;

 

Your stuffed with a total ;)

 

Thanks, but it goes to 100, some searches only need to 2 pages, not all have 100 pages, the ones that dont have 100 pages just loop continuously until they get to 100.

 

 

Yeah what i meant to say was - Your stuffed without a total .

 

You need to be getting $content_count = ????????; which is the total amount of items from somewhere

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904509
Share on other sites

 

Yeah what i meant to say was - Your stuffed without a total .

 

You need to be getting $content_count = ????????; which is the total amount of items from somewhere

 

Danny mentioned to get the total, divide the number of items per page by the number of items in total

 

The following snippet, is for when the items/results get parsed:

 

          // Parse ITEMS
            preg_match_all("'<item(| .*?)>(.*?)</item>'si", $rss_content, $items);
            $rss_items = $items[2];
            $i = 0;
            $result['items'] = array(); // create array even if there are no items
            foreach($rss_items as $rss_item) {
                // If number of items is lower then limit: Parse one item
                if ($i < $this->items_limit || $this->items_limit == 0) {
                    foreach($this->itemtags as $itemtag) {
                        $temp = $this->my_preg_match("'<$itemtag.*?>(.*?)</$itemtag>'si", $rss_item);
                        if ($temp != '') $result['items'][$i][$itemtag] = $temp; // Set only if not empty
                    }
                    // Strip HTML tags and other bullshit from DESCRIPTION
                    if ($this->stripHTML && $result['items'][$i]['description'])
                        $result['items'][$i]['description'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['description'])));
                    // Strip HTML tags and other bullshit from TITLE
                    if ($this->stripHTML && $result['items'][$i]['title'])
                        $result['items'][$i]['title'] = strip_tags($this->unhtmlentities(strip_tags($result['items'][$i]['title'])));
                    // If date_format is specified and pubDate is valid
                    if ($this->date_format != '' && ($timestamp = strtotime($result['items'][$i]['pubDate'])) !==-1) {
                        // convert pubDate to specified date format
                        $result['items'][$i]['pubDate'] = date($this->date_format, $timestamp);
                    }
                    // Item counter
                    $i++;
                }
            }

            $result['items_count'] = $i;
            return $result;
        }

 

So the trouble is trying to get the total, because their is no default total or a way of getting a number, is their some way i can define the total from the above snippet? so i can get the pagination to work.

Link to comment
https://forums.phpfreaks.com/topic/171418-solved-paging/#findComment-904513
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.