heavensgate15
04-18-2010, 03:18 AM
Please examine my code and read my question after at the bottom.
index.php
<html>
<head>
<title> Pagination </title>
<link type="text/css" rel="stylesheet" href="pagedesign.css" />
</head>
<body>
<?php
include("dbconnect.php");
include("insertRecord.php");
// determine number of rows from the table records
$num_retrieved_rows = mysql_num_rows(mysql_query("select * from records"));
// number of rows to be displayed per page
$rows_per_page = 5;
// number of pages
$num_pages = ceil($num_retrieved_rows / $rows_per_page);
// get the current page
if(!(isset($_GET['page'])))
$page = 1;
else
$page = $_GET['page'];
// get the start of record to display in page
$start = ($page * $rows_per_page) - $rows_per_page;
// select all 5 records given the page number
$query_string = "select * from records order by lastname limit $start , $rows_per_page";
$page_data = mysql_query($query_string);
// display the 5 records
while($data = mysql_fetch_array($page_data))
{
$firstname = $data['firstname'];
$lastname = $data['lastname'];
$age = $data['age'];
echo "Firstname: " . $firstname . "<br />";
echo "Lastname: " . $lastname . "<br />";
echo "Age: " . $age . "<br /><br />";
}
// set the previous page
if($page > 1)
{
$prev = $page - 1;
echo "<a href='" . $_SERVER['PHP_SELF'] . "?page=" . $prev . "'> Previous </a>";
}
else
echo "Previous ";
// set the number page
for($i = 1 ; $i <= $num_pages ; $i++)
{
if($i != $page)
echo "<a href='" . $_SERVER['PHP_SELF'] . "?page=" . $i . "'> " . $i . "</a>";
else
echo "<a href='" . $_SERVER['PHP_SELF'] . "?page=" . $i . "'> <b> " . $i . "</b> </a>";
}
// set the next page
if($page < $num_pages)
{
$next = $page + 1;
echo "<a href='" . $_SERVER['PHP_SELF'] . "?page=" . $next . "'> Next </a>";
}
else
echo " Next";
?>
</body>
</html>
dbconnect.php
<?php
$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("pagination",$con) or die(mysql_error());
?>
insertRecord.php
<?php
if(isset($_POST['submit']))
{
include("dbconnect.php");
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
mysql_query("insert into records values('$firstname','$lastname','$age')");
}
?>
<form method="post" action="">
<label for="firstname"> Firstname: </label>
<input type="text" name="firstname" id="firstname" maxlength="30" />
<br />
<label for="lastname"> Lastname: </label>
<input type="text" name="lastname" id="lastname" maxlength="30" />
<br />
<label for="age"> Age: </label>
<input type="text" name="age" id="age" maxlength="3" />
<br />
<input type="submit" name="submit" value="insert record" />
<br />
</form>
This is not an error problem actually. I'm just curious about the output. Well, the output appeared as demanded but not what I've expected.
As you can see, I just included the insertRecord.php in the index.php. Inside the insertRecord.php is my code in inserting new record. The action of the form in insertRecord.php is empty (action="") so it will reload itself. But why is it, every time I inserted a new record, the index.php is also reloaded?
My expectation if I inserted a new record:
* A new record will be inserted in my database
* I still need to refresh or click another number (pagination) to see the newly added record
Actual output:
* A new record is inserted in the database
* The page reload immediately after inserting a new record, thus, I don't have to refresh the page so that I can see the newly added record.
As you can see, It appears to be ok coz the result is what I really wanted. But the flow of the program is still bugging me. I want to know why index.php reloaded even though I only reloaded the insertRecord.php :confused:
index.php
<html>
<head>
<title> Pagination </title>
<link type="text/css" rel="stylesheet" href="pagedesign.css" />
</head>
<body>
<?php
include("dbconnect.php");
include("insertRecord.php");
// determine number of rows from the table records
$num_retrieved_rows = mysql_num_rows(mysql_query("select * from records"));
// number of rows to be displayed per page
$rows_per_page = 5;
// number of pages
$num_pages = ceil($num_retrieved_rows / $rows_per_page);
// get the current page
if(!(isset($_GET['page'])))
$page = 1;
else
$page = $_GET['page'];
// get the start of record to display in page
$start = ($page * $rows_per_page) - $rows_per_page;
// select all 5 records given the page number
$query_string = "select * from records order by lastname limit $start , $rows_per_page";
$page_data = mysql_query($query_string);
// display the 5 records
while($data = mysql_fetch_array($page_data))
{
$firstname = $data['firstname'];
$lastname = $data['lastname'];
$age = $data['age'];
echo "Firstname: " . $firstname . "<br />";
echo "Lastname: " . $lastname . "<br />";
echo "Age: " . $age . "<br /><br />";
}
// set the previous page
if($page > 1)
{
$prev = $page - 1;
echo "<a href='" . $_SERVER['PHP_SELF'] . "?page=" . $prev . "'> Previous </a>";
}
else
echo "Previous ";
// set the number page
for($i = 1 ; $i <= $num_pages ; $i++)
{
if($i != $page)
echo "<a href='" . $_SERVER['PHP_SELF'] . "?page=" . $i . "'> " . $i . "</a>";
else
echo "<a href='" . $_SERVER['PHP_SELF'] . "?page=" . $i . "'> <b> " . $i . "</b> </a>";
}
// set the next page
if($page < $num_pages)
{
$next = $page + 1;
echo "<a href='" . $_SERVER['PHP_SELF'] . "?page=" . $next . "'> Next </a>";
}
else
echo " Next";
?>
</body>
</html>
dbconnect.php
<?php
$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("pagination",$con) or die(mysql_error());
?>
insertRecord.php
<?php
if(isset($_POST['submit']))
{
include("dbconnect.php");
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
mysql_query("insert into records values('$firstname','$lastname','$age')");
}
?>
<form method="post" action="">
<label for="firstname"> Firstname: </label>
<input type="text" name="firstname" id="firstname" maxlength="30" />
<br />
<label for="lastname"> Lastname: </label>
<input type="text" name="lastname" id="lastname" maxlength="30" />
<br />
<label for="age"> Age: </label>
<input type="text" name="age" id="age" maxlength="3" />
<br />
<input type="submit" name="submit" value="insert record" />
<br />
</form>
This is not an error problem actually. I'm just curious about the output. Well, the output appeared as demanded but not what I've expected.
As you can see, I just included the insertRecord.php in the index.php. Inside the insertRecord.php is my code in inserting new record. The action of the form in insertRecord.php is empty (action="") so it will reload itself. But why is it, every time I inserted a new record, the index.php is also reloaded?
My expectation if I inserted a new record:
* A new record will be inserted in my database
* I still need to refresh or click another number (pagination) to see the newly added record
Actual output:
* A new record is inserted in the database
* The page reload immediately after inserting a new record, thus, I don't have to refresh the page so that I can see the newly added record.
As you can see, It appears to be ok coz the result is what I really wanted. But the flow of the program is still bugging me. I want to know why index.php reloaded even though I only reloaded the insertRecord.php :confused: