Tuesday, June 9, 2009

Create Dynamic Web Pages With Php

Dynamic web pages are an efficient way to present data to the user. The web designer needs only to create the initial page that splits the data into segments and add data to the database from which the web page draws its content. This tutorial will cover create a web page that draws out data from a database, create a script that divides the data so that one record will exist per page and create the dynamic links to each dynamic web page.


Instructions


Initial Web Page


1. Type the following code to create a standard XHTML document, adding the title, "Dynamic Webpages" between the tags, as shown:



"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


Dynamic Webpages


Save the file as "dynamicwebpages.php" and upload it to your server.


2. Type the following code above the DOCTYPE statement to connect to your database. Substitute your information with the capital letters:



$dbh=mysql_connect ("SERVERNAME", "USERNAME", "PASSWORD")


or die ('I cannot connect to the database because: ' . mysql_error());


mysql_select_db ("DATABASENAME");


?>


3. Type the beginning and ending PHP tags between the tags:



?>


4. Type the following between the tags:


$query = mysql_query("SELECT * FROM table ORDER BY id");


while($r=mysql_fetch_array($query)) {


extract($r);


echo $variables;


}


Substitute "table" with the name of the table that you are drawing out information. Arrange the column names to display the data in a manner of your choosing, substituting "$variables" with your column names, plus a dollar sign. Sort the query by the id field (the column that counts the number of records contained in the table).


5. Save and upload your document. The page will render all the data in the database in one large column.


Paginating the Data


6. Type the following code between the second

$rowsPerPage = 1;


$pageNum = 1;


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


{


$pageNum = $_GET['page'];


}


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


Establish a variable called "$rowsPerPage" and set it equal to one. This variable will be used to limit the number of records in the database to one per page. Establish another variable, "$pageNum" and set it equal to one. Create an "if" statement to create each page number. Establish another variable called "$offset" to help modify the "select" query's number of records per page.


7. Add the following limit to the "select" query, after the "ORDER BY id" statement, as shown:


LIMIT $offset, $rowsPerPage


8. Type a "count" query to count the number of records in the database that conform to the "select" query. Assign the results of the "count" query to the variable "$numrows". Establish another variable, "$maxPage" and divide the value of "$numrows" by "$rowsPerPage". Round the results up to the nearest integer by enclosing the divisor with the "ceil()" function.








// how many rows we have in database


$query = "SELECT COUNT(columnname) AS numrows FROM table ORDER BY id ";


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


$row = mysql_fetch_array($result, MYSQL_ASSOC);


$numrows = $row['numrows'];


// how many pages we have when using paging?


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


9. Create the link to each page. Establish the variable "$self" and set it equal to the current page by invoking the superglobal variable and its parameter $_SERVER['PHP_SELF']. Establish a blank variable, called "$nav". Create a "for" loop setting the variable "$page" equal to one. State that "$page must be less than or equal to the variable "$maxPage". Then set page to automatically count with the double plus signs. Create an "if/else" statement where if the value of "$page" is equal to the value of the variable "$pageNum", then the variable "$nav" will equal the value of the variable "$page". Otherwise, the variable "$nav" will equal the dynamic link.


// print the link to access each page


$self = $_SERVER['PHP_SELF'];


$nav = '';


for($page = 1; $page 1)


{


$page = $pageNum - 1;


$prev = " [Prev] ";


$first = " [First Page] ";


}


else


{


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


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


}


if ($pageNum < $maxPage)


{


$page = $pageNum + 1;


$next = " [Next] ";


$last = " [Last Page] ";


}


else


{


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


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


}


11. Print the navigation links and close the connection to the database:








// Print the navigation links


echo $nav . "


";


echo $next . " " . $prev . "


";


echo $first . " " . $last;


// Close the connection to the database


mysql_close();


Explanation:


The navigation links connect the original page to the dynamic pages created by dividing the records in the database table, or rows, into pages. Then, all that remains is creating dynamic links to each page and printing them on the screen.

Tags: each page, number records, Type following, another variable, between tags, Establish another