Link to home
Start Free TrialLog in
Avatar of babak62
babak62

asked on

page pagination

Hi;
I have a search page which I am looking for an easy pagination for it since I am new with pho and can not do complex things yet.  In this page $row is an array and holds all the information retrieved from the database.  The Media_ID is what I need to pass to a page called media_page.php in URL.  Again appreciate all the help and the best to you guys out there.
<?php     
      echo   "<p class=' SubtitletextlightercolorCenter'>Your search '$query_text' returned " . "<i>" .$numrows. "</i>" .  " results.</p>\n";
    mysql_data_seek ($result, 0);    
    print "<table width=\"1000\\ border='1'\" align=\"center\">";
    while($row = mysql_fetch_array ($result))
    {
      echo " 
          <tr>
            <td class='textlinkTitle'>			
             <a href=media_page.php?media_id=$row[Media_ID]>$row[Media_Name]</a>	  
            </td>
          </tr>";

      echo " 
          <tr>
            <td class='textlinkDescription'> 
              ".$row['Description']."
            </td>
          </tr>
          <tr class= 'textlinkURL'>
            <td >
              ".$row['URL']."<br /><br /><hr SIZE=1 width='800' />
            </td>
          </tr>";
        }       
    print "</table>";      
  ?>

Open in new window

Avatar of frankcheong
frankcheong

It would be much more easier and productive to use any rapid development framework to implement any solution. PHP Cup Cake may be an ideal choice for you. Since the development after deploying php cup cake would be much more easier.

The site URL has been included below for your information.
http://cakephp.org/
Avatar of Loganathan Natarajan
You have to find out OFFSET, No of pages etc., Just have a look at this simple tutorial

http://www.weberdev.com/get_example-4092.html
Avatar of babak62

ASKER

is this the part you are refering to?

}
if($viewall == '') {
      if($numrows > $per_page) {
            echo '<br />';
            $page_pad = 2;
            $min_page = max(1, $cur_page - $page_pad);
            $max_page = min($total_pages, $cur_page + $page_pad);
            $show_prev = $cur_page> 1 ? $cur_page - 1 : false;
            $show_next = $cur_page <$total_pages ? $cur_page + 1 : false;
              if ($show_prev) {
                  echo "<a href=\"?p=1\">&laquo; First</a> ";
                  echo "<a href=\"?p=$show_prev\">&lt; Prev</a> ";
            }
            for ($page = $min_page; $page <= $max_page; $page++) {
                  if($cur_page<>$page)
                        echo "<a href=\"?p=$page\">$page</a> ";
                     else
                           echo "$page ";
               }
               if ($show_next) {
                     echo "<a href=\"?p=$show_next\">Next &gt;</a> ";
                     echo "<a href=\"?p=$total_pages\">Last &raquo;</a>";
               }
            echo ' &nbsp; <a href="?all=true">View All</a><br />';
      }
}
yes, .. also you need to write SQL query to retrieve those values..?
if you post your complete code, let me modify

did you go through the other links, perfect Paging
Avatar of babak62

ASKER

I have the db connection and already printing the records so that part is done.  In the table as you see $row array contains all the fields and Media_ID is what we are looking for so you think you can helpl me with these infoormation or you need the sql part of the code too and again thanks for helping me.
post complete code??
Avatar of babak62

ASKER

This is the DB part which generates the query.  Please let me know if you need anyother information.
<?php
  $MySQLPassword = "";  
  $HostName = "127.0.0.1";  
  $UserName = "root";
  $Database = "babak_iran79";
  mysql_connect($HostName,$UserName,$MySQLPassword) or die("ERROR: Could not connect to database!");
  mysql_set_charset('utf8');
  mysql_select_db($Database) or die("cannot select db");
  
  /* if order is not set, or it is not in the allowed
  * list, then set it to a default value. Otherwise, 
  * set it to what was passed in. */
  
  if (!isset ($_GET['order']))  
  {
    $order = 'Media_ID';
  }
  else 
  {
    $order = urldecode($_GET['order']);
  }
  
  if (isset($_GET['keyword'])) 
  {
    if(!$_GET['keyword']) 
    {
            die('<p>Please enter a search term.</p>');
      }     

    // Database Query Settings //
    /* Change these to match your database table, the fields you want displayed and the fields to run the query against. */
    $tables = 'media_owner_info';
    $return_fields = 'Media_ID Media_Name Media_Name_Farsi URL Description Description_Farsi';
    $check_fields = 'Media_ID Media_Name Media_Name_Farsi URL Description Description_Farsi';

    // Get the keyword from the search form.
    $query_text = urldecode($_GET['keyword']);
  
    // Sanitize Data Input
    $clean_query_text = ($query_text);

    // Call the bq_simple function and store the //
    // resulting SQL Query in $newquery variable //
    $newquery=bq_simple ($return_fields, $tables, $check_fields, $clean_query_text);
    $newquery = $newquery . " ORDER BY $order;";
    
        //echo $newquery;
    // sql data query construction //
    $result = mysql_query($newquery) or die(mysql_error());

    /* make sure data was retrieved */
    $numrows = mysql_num_rows($result);
    if ($numrows == 0) 
    {
      echo "<H4>No data to display!</H4>";
      exit;
	}
	}
    ?>

Open in new window

Avatar of babak62

ASKER

the code are 3 files already posted 2 of them and here is the third one.
<?php /* * * * * * * * * * * * * *  F U N C T I O N S * * * * * * * * * * * */

     function limitWords( $string, $number ) {

          $words = explode( " ", $string );
          
          $limitedWords = ( $number < count($words) ) ? array_slice( $words, 0, $number ) : $words;

          return implode(" ", $limitedWords );
     }




	function cleanQuery($string)
	{

	  $string = trim($string);
	  $string = strip_tags($string); // remove any html/javascript.

	  if(get_magic_quotes_gpc())  // prevents duplicate backslashes
	  {
	    $string = stripslashes($string);
	  }
	  if (phpversion() >= '4.3.0')
	  {
	    $string = mysql_real_escape_string($string);
	  }
	  else
	  {
	    $string = mysql_escape_string($string);
	  }
	  
	  return $string;
	}

#bq_simple ($query);
#
# Internal function, used to support "+keyword -keyword" syntax in addition
#   to "and keyword not keyword" syntax.
#
function bq_handle_shorthand($text) {
	$text = preg_replace("/ \+/", " and ", $text);
	$text = preg_replace("/ -/", " not ", $text);
	return $text;
}

#
# Internal function, used to keep quoted text together when building
#  the query.  i.e.... [fish and chips and "chipped ham"] syntax
#
# It essentially replaces " " with "~~~~" as long as we aren't within
# a set of quotes, in which case " " is retained.  The string is then
# split on "~~~~~" with the surviving spaces intact.
#
function bq_explode_respect_quotes($line) {
        $quote_level = 0;	#keep track if we are in or out of quote-space
        $buffer = "";

        for ($a = 0; $a < strlen($line); $a++) {
                if ($line[$a] == "\"") {
                        $quote_level++;
                        if ($quote_level == 2) { $quote_level = 0; }
                }
                else {
                        if ($line[$a] == " " and $quote_level == 0) {
                                $buffer = $buffer . "~~~~";   #Hackish magic key
                        }
                        else {
                                $buffer = $buffer . $line[$a];
                        }
                }

        }

	$buffer = str_replace("\\", "", $buffer);

        $array = explode("~~~~", $buffer);
        return $array;
}

#
# Internal function, used to apply a single keyword against an
# arbitrary number of fields in the database in the same fashion.
#
# Works via replacing whitespace rather than interation
#
function bq_make_subquery($fields, $word, $mode) {

	if ($mode == "not") {
		$back = " LIKE '%$word%'))";
	}
	else {
		$back = " LIKE '%$word%')";
	}

	if ($mode == "not") {
		$front = "(NOT (";
		$glue = " LIKE '%$word%' OR ";
	}
	else {
		$front = "(";
		$glue = " LIKE '%$word%' OR ";
	}

	$text = str_replace(" ", $glue, $fields);
	$text = $front . $text . $back;

	return $text;
}

#
#  Generates the "WHERE" portion of a query, iterating over every keyphrase in the
#  given search string.  Is safe to link (e.g. with AND) with the output of repeated
#  calls
#

function bq_make_query($fields, $text) {
	#
	# We can't trust the user to give us a specific case
	#
	#$fields = strtolower($fields);
	$text = mysql_real_escape_string($text); //$text = strtolower($text);

	#
	# Support +keyword -keyword
	#
	$text = bq_handle_shorthand($text);

	#
	# Split, but respect quotation
	#
	$wordarray = bq_explode_respect_quotes($text);

	$buffer = "";
	$output = "";

	#
	# work through each word (or "quoted phrase") in the text and build the
	#   outer shell of the query, filling the insides via bq_make_subquery
	#
	# "or" is assumed if neither "and" nor "not" is specified
	#
	for ($i = 0; $i<count($wordarray); $i++) {
		$word = $wordarray[$i];

		if ($word == "and" or $word == "or" or $word == "not" and $i > 0) {
			if ($word == "not") {
				#
				# $i++ kicks us to the actual keyword that the 'not' is working against, etc
				#
				$i++;
				if ($i == 1) {   #invalid sql syntax to prefix the first check with and/or/not
					$buffer = bq_make_subquery($fields, $wordarray[$i], "not");
				}
				else {
					$buffer = " AND " . bq_make_subquery($fields, $wordarray[$i], "not");
				}
			}
			else {
				if ($word == "or") {
					$i++;
					if ($i == 1) {
						$buffer = bq_make_subquery($fields, $wordarray[$i], "");
					}
					else {

						$buffer = " OR " . bq_make_subquery($fields, $wordarray[$i], "");
					}
				}
				else {
					if ($word == "and") {
						$i++;
						if ($i == 1) {
							$buffer = bq_make_subquery($fields, $wordarray[$i], "");
						}
						else {

							$buffer = " AND " . bq_make_subquery($fields, $wordarray[$i], "");
						}
					}
				}
			}
		}
		else {
			if ($i == 0) {  # 0 instead of 1 here because there was no conditional word to skip and no $i++;
				$buffer = bq_make_subquery($fields, $wordarray[$i], "");
			}
			else {
				$buffer = " OR " . bq_make_subquery($fields, $wordarray[$i], "");
			}
		}
		$output = $output . $buffer;
	}
	return $output;
}


#
# Generates a simple boolean query.  Use bq_make_query directly if you need to
#   add further levels of complexity (the output of bq_make_query can be and/or/etc'd
#   with any other output of bq_make_query)
#
#
#  $return_fields == author content
#      "blah1 blah2 blah3" --> "SELECT blah1, blah2, blah3...."
#
#  $tables == The tables you want the query to... query.
#      "t1 t2 t3" --> "FROM t1, t2
#

function bq_simple ($return_fields, $tables, $check_fields, $query_text) {

	#
	# Convert from space deliniated to comma deliniated for the query
	#
	$return_fields = str_replace(" ", ", ", $return_fields);
	$tables = str_replace(" ", ", ", $tables);

	#
	# build the query itself
	#
	
	$query = "SELECT $return_fields FROM $tables WHERE ";
	$query = $query . bq_make_query($check_fields, $query_text);

	#
	# Uncomment to debug
	#

	return $query;
}
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial