pagination

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
misteryoji
Forum Newbie
Posts: 3
Joined: Fri Nov 04, 2011 11:01 am

pagination

Post by misteryoji »

I have a pagination script and i have run into a small annoying error. When first visiting my page, i encounter an error in red that states "undefined index: page in....". I have about 5 of these, shown below. However, once i click on a page link, it disappears. How do i set it so the error doesnt show at all? Also, what is the best way to do form evaluation? I want to make sure there are no numbers in the name, email in correct format, and no blank fields for name, email or comment. Normally id use an if statement and recreate the form with the appropriate error message nest to the text area. Is there an easier way to do this? Any and all help is greatly appreciated. Thanks.

Code: Select all

<?php
mysql_connect("localhost","*****","*******");
mysql_select_db("********");


if (isset($_POST['name'])) {
$name=$_POST["name"];
$email=$_POST["email"];
$comment=$_POST["comment"];
$sql="insert into guestbook(name,email,comment) 
values('".$name."','".$email."','".$comment."')";
mysql_query($sql,$connection) 
or exit("Sql Error".mysql_error());
mysql_close($connection);
}

$perpage = 5;
$lynx = $html = "";
$startat = $_REQUEST['page'] * $perpage;

$q = mysql_query("select count(timenter) from guestbook");
$row = mysql_fetch_array($q);
$pages = ($row[0] + $perpage - 1) / $perpage;

$q = mysql_query("select * from guestbook order by timenter desc limit $startat,$perpage");

while ($row = mysql_fetch_assoc($q)) {
        $text = strip_tags($row['comment']);
        $text = substr($text,0,300);
        $html .= "<dt>$row[name] - <a href=/mouth/$row[name]_.html target=pix>$row[timenter]</a></dt>";
        $html .= "<dd>$text ....<br><br></dd>";
        };

for ($k=0; $k<$pages; $k++) {
        if ($k != $_REQUEST['page']) {
         $lynx .= " <a href=test.php"."?page=$k>".($k+1)."</a>";
        } else {
         $lynx .= " <b>--".($k+1)."--</b>";
        }
}

?>
My form:

Code: Select all

<h1>Guestbook</h1>

<h2>Here are the entries you selected - page <?= $_REQUEST['page']+1 ?>:</h2><br>
<?= $html ?>
Please choose the next page you want to view:
<?= $lynx ?><br /><hr /> <br />


<form action="/~jatindj01/hw2/index.php?" method="post">

<table>
<tr>
	<td>Name (Required):</td><td width="180"><input type="text" name="name" /></td>
</tr> 
<tr>
    <td>E-Mail (will not be published, required):</td><td width="180"><input type="text" name="email" /></td>
</tr> 
</table>
    <textarea name="comment" rows="10" cols="75"></textarea>
<table><tr>
    <td align="center"> <input type="submit" name="submit" value="send" /></td> 
</tr></table>
</form>

 </div>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: pagination

Post by Celauran »

You're getting the undefined index notice (not error) because you're referencing $_REQUEST['page'] when it may not exist. Putting this at the top of the page should fix it.

Code: Select all

$startat = (isset($_REQUEST['page'])) ? (int) $_REQUEST['page'] : 1;
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: pagination

Post by Celauran »

misteryoji wrote:what is the best way to do form evaluation?
Filters, ctype functions, and escaping.
misteryoji
Forum Newbie
Posts: 3
Joined: Fri Nov 04, 2011 11:01 am

Re: pagination

Post by misteryoji »

thanks a lot. the validation worked. however im still encountering the notice even though i put that at the top. I also encountered another problem. On form submit, i get the same page but without the queried results and two notices at the top:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in.....
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in....

I took a screenshot. The right side is what i want it to look like, minus the red notice. The left is what happens when i try to submit the form
screenshot
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: pagination

Post by Celauran »

misteryoji wrote:thanks a lot. the validation worked. however im still encountering the notice even though i put that at the top.
Can you post the modified code? That seems odd.

You're getting the mysql error because your query is returning FALSE.
misteryoji
Forum Newbie
Posts: 3
Joined: Fri Nov 04, 2011 11:01 am

Re: pagination

Post by misteryoji »

Code: Select all

<?php
$host = "localhost";

$user = "******";

$password = "********";

$database = "********888";

$connection = mysql_connect($host,$user,$password) 

or die("Could not connect: ".mysql_error());

$connection1 = mysql_connect($host,$user,$password) 

or die("Could not connect: ".mysql_error());

mysql_select_db($database,$connection) 

or die("Error in selecting the database:".mysql_error());



mysql_connect("localhost","*****","*******");

mysql_select_db("jatindj01");





if (isset($_POST['name'])) {

$name=$_POST["name"];

$email=$_POST["email"];

$comment=$_POST["comment"];

$sql="insert into guestbook(name,email,comment) 

values('".$name."','".$email."','".$comment."')";

mysql_query($sql,$connection) 

or exit("Sql Error".mysql_error());

mysql_close($connection);

}



$perpage = 5;

$lynx = $html = "";


$startat = $_REQUEST['page'] * $perpage;



$q = mysql_query("select count(timenter) from guestbook");

$row = mysql_fetch_array($q);

$pages = ($row[0] + $perpage - 1) / $perpage;



$q = mysql_query("select * from guestbook order by timenter desc limit $startat,$perpage");



while ($row = mysql_fetch_assoc($q)) {

        $text = strip_tags($row['comment']);

        $text = substr($text,0,300);

        $html .= "<dt>$row[name] - $row[timenter]</dt>";

        $html .= "<dd>$text <br><br></dd>";
        $html .= "<hr />";

        };



for ($k=0; $k<$pages; $k++) {

        if ($k != $_REQUEST['page']) {

         $lynx .= " <a href=index.php"."?page=$k>".($k+1)."</a>";

        } else {

         $lynx .= " <b>--".($k+1)."--</b>";

        }

}



mysql_close($connection);

?>
Form:

Code: Select all

<h2>Here are the entries you selected - page <?= $_REQUEST['page']+1 ?>:</h2><br>

<?= $html ?>

Please choose the next page you want to view:

<?= $lynx ?><br /><hr /> <br />





<form action="index.php" method="post">



<table>

<tr>

	<td>Name (Required):</td><td width="180"><input type="text" name="name" /></td>

</tr> 

<tr>

    <td>E-Mail (will not be published, required):</td><td width="180"><input type="text" name="email" /></td>

</tr> 

</table>

    <textarea name="comment" rows="10" cols="75"></textarea>

<table><tr>

    <td align="center"> <input type="submit" name="submit" value="send" /></td> 

</tr></table>

</form>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: pagination

Post by Celauran »

misteryoji wrote:

Code: Select all

<?php
$host = "localhost";
$user = "******";
$password = "********";
$database = "********888";

$connection = mysql_connect($host,$user,$password) or die("Could not connect: ".mysql_error());

$connection1 = mysql_connect($host,$user,$password) or die("Could not connect: ".mysql_error());

mysql_select_db($database,$connection) or die("Error in selecting the database:".mysql_error());

mysql_connect("localhost","*****","*******");
mysql_select_db("jatindj01");
What's going on here?

You're still getting the undefined index 'page' notice because you didn't add the line I had posted earlier.
misteryoji wrote:

Code: Select all

if (isset($_POST['name'])) {
$name=$_POST["name"];
$email=$_POST["email"];
$comment=$_POST["comment"];
All of that block will also lead to undefined index notices for the same reason. You're referencing an array index that may not exist.
misteryoji wrote:

Code: Select all

$q = mysql_query("select * from guestbook order by timenter desc limit $startat,$perpage");
Try echoing the query. Then try running it manually.
misteryoji wrote:Form:

Code: Select all

<h2>Here are the entries you selected - page <?= $_REQUEST['page']+1 ?>:</h2><br>
Another undefined index here.
Post Reply