Log in

View Full Version : Pagination and insertion of new record problem



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:

djr33
04-18-2010, 03:31 AM
PHP generates HTML (and does other things on the server).

A page requests triggers this generation. Anything that happens then on the server occurs only on the server. The whole page (whole PHP setup) will be loaded each time.


In other words, PHP includes just grab the PHP code from another file and execute it as part of the other PHP file, nothing more. It lets you separate code, but then it's still like just using one page (to the end user).

So you end up with the page actually being entirely index.php even though there may (or may not) be code in some other page.

In short, the page you are looking at is what is in the address bar. The actual location of the code on the server does not matter, just where it is served from (the main PHP page including others).

PHP includes are just file organization strategies, not like iframes (in which HTML actually loads two separate pages into one window).

heavensgate15
04-18-2010, 04:42 AM
Gee, tnx... It's my new discovery about PHP. Bet, this kind of discovery is not on any php tutorial, so I'll keep it in my notes..

Let me confirm, if a file A which is part of file B via include statement, is reloaded, file B will also be reloaded because file A and file B is just like one file or one page for the end user?

hmmmm.. another thing, is $_POST['firstname'] or $_POST['lastname'] available in index.php when it reloaded?

djr33
04-18-2010, 05:16 AM
Effectively, they are NOT separate files. Don't think of them that way.

include() is a PHP function that transplants code from page B directly and seemlessly into page A as if you wrote it there directly. To the user, page B does not exist.

You are never loading, reloading or doing anything to page B. You are only using page A. The location of the actual text code is irrelevant-- PHP is serving a single HTML page.


Now, as for the issue of variables in one or the other, that's a little different-- for PHP there are actually two files, BUT they are processed as one.
So, yes, anything in A is available in B and vice versa. There's literally no difference if the two pages are executed together. Of course you could have a more complex include setup where page B may be included in other pages or may not be included at all (where it's run by itself) and that would be a totally different situation.

But in short, if two pages are combined through includes, don't think of them as different pages-- think of them as multiple parts of the same page. Literally the only difference is where the 'physical' code (text) is stored. That's it.



The main use of includes in PHP is to keep things organized and allow cross-referencing.

For example, if you have a login component of your website, it's normal to use that on all of your pages, at the top use include('login.php');, BUT remember that then FOR EACH INDIVIDUAL PAGE LOAD, this would be like it was written directly into the code. The only difference is that it can be used on many pages at once.

You can now imagine how powerful this is for things like templating and expanding the PHP base (with functions, data processing, whatever) and using it throughout a site.

On a smaller level, it's also nice to be able to include pages just to make the individual pages shorter. For example, you may have a page that normally displays A and in rare cases instead displays B. So here you could just have an if statement with an include and put all the "B" stuff into a separate file.

Generally includes are used to bridge separate components into a single framework. That is, most coders don't just randomly split single pages for now reason unless the code has VERY distinct parts. But every PHP designer will at some point use an include and probably lots of them for more complex things. The most common is when a single component is used in many places in a site... this is perhaps the most powerful/useful part of PHP (though there's lots of other good stuff too).



Directly responding to your question: yes, both of those variables will be available in both pages, because they are effectively the same page.