Skip to main content

Database & Paging

******************* Db.php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'mydatabase';
$conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($dbname);
?>
****************************** Paging *****************************



<html>

<head>

<title>Implementing Paging with next and prev</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>



<body>

<?php

include 'library/config.php';

include 'library/opendb.php';



// how many rows to show per page

$rowsPerPage = 3;



// by default we show first page

$pageNum = 1;



// if $_GET['page'] defined, use it as page number

if(isset($_GET['page']))

{

$pageNum = $_GET['page'];

}



// counting the offset

$offset = ($pageNum - 1) * $rowsPerPage;



$query = "SELECT val FROM randoms LIMIT $offset, $rowsPerPage";

$result = mysql_query($query) or die('Error, query failed');



// print the random numbers

while($row = mysql_fetch_array($result))

{

echo $row['val'] . '<br>';

}

echo '<br>';



// how many rows we have in database

$query = "SELECT COUNT(val) AS numrows FROM randoms";

$result = mysql_query($query) or die('Error, query failed');

$row = mysql_fetch_array($result, MYSQL_ASSOC);

$numrows = $row['numrows'];



// how many pages we have when using paging?

$maxPage = ceil($numrows/$rowsPerPage);



// print the link to access each page

$self = $_SERVER['PHP_SELF'];

$nav = '';

for($page = 1; $page <= $maxPage; $page++)

{

if ($page == $pageNum)

{

$nav .= " $page "; // no need to create a link to current page

}

else

{

$nav .= " <a href=\"$self?page=$page\">$page</a> ";

}

}



// creating previous and next link

// plus the link to go straight to

// the first and last page



if ($pageNum > 1)

{

$page = $pageNum - 1;

$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";



$first = " <a href=\"$self?page=1\">[First Page]</a> ";

}

else

{

$prev = '&nbsp;'; // we're on page one, don't print previous link

$first = '&nbsp;'; // nor the first page link

}



if ($pageNum < $maxPage)

{

$page = $pageNum + 1;

$next = " <a href=\"$self?page=$page\">[Next]</a> ";



$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";

}

else

{

$next = '&nbsp;'; // we're on the last page, don't print next link

$last = '&nbsp;'; // nor the last page link

}



// print the navigation link

echo $first . $prev . $nav . $next . $last;



// and close the database connection

include 'library/closedb.php';

?>

</body>

</html>

 

Comments

Popular posts from this blog

PHP Interview Questions & Answers

Web Development PHP Difference between echo and print? void echo ( string $arg1 [, string $...] ) Outputs all parameters. echo() is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo() (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo(), the parameters must not be enclosed within parentheses. echo() also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. This short syntax only works with the short_open_tag configuration setting enabled. I have foo. print --- out put a string print behaves like a function and you can return values and echo can't Prin can be used as a part of more complex operations What is the use of ob_start in php? ob_start — Turn on output buffering According to PHP manual* Description bool ob_start ( [callback $outpu...

Shopping Cart Help

*******************Important URL for Coding *********************** http://de.tikiwiki.org/xref-BRANCH-1-9/nav.html?lang/ca/language.php.source.html ******************************************************** Creating the Database Let's assume that we're running a website that sells Sony Playstation 2 games. We'll need one table to store the details of each product, and another table to store the contents of each user's shopping cart, so that these details can persist over multiple sessions. Fire up the MySQL console application and create a database named cart. Populate the database with two tables: items and cart, using this code: create database cart; create table items ( itemId int auto_increment not null, itemName varchar(50), itemDesc varchar(250), itemPrice decimal(4,2), primary key(itemId), unique id(itemId) ); create table cart ( cartId int auto_increment not null, cookieId varchar(50), itemId int, qty int, primary key(cartId), unique id(cartId) ); The first table...

Curl Example & fsockopen() Example

***************************** CURL Example **************************** $senthost = "http://s4.myvaluefirst.com/psms/servlet/psms.Eservice2?data=".$xmlcode."&action=send"; //echo "Path:".$path." "; //print $path." "; $user_agent=$_SERVER['HTTP_USER_AGENT']; $cookies = 'cookies'; $ch = curl_init($senthost); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); $line = curl_exec($ch); $error = curl_error($ch); echo (($error!= "")? ('Error: ' . $error) : $line); curl_close($ch); ******************************** Getting with fsockopen() function $senthost = "http://localhost/loginvalidate.php?username=demo&pass=123"; $host="127.0.0.1"; $ht...