PDA

View Full Version : pagination page code error


sonny
07-31-2009, 04:01 AM
I followed a how to, on pagination but cant get this to work
can someone familiar with this, please have a look I followed
everything correctly I thought, the table is named "visits"
I am querying results from.

But I get this ERROR
Parse error: parse error, unexpected T_STRING, expecting ',' or ';' line 46

Thanks
Sonny


<?

require "connect.php";

if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}

$query = "SELECT count(*) FROM visits";
$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

$rows_per_page = 15;
$lastpage = ceil($numrows/$rows_per_page);

$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
} // if
if ($pageno < 1) {
$pageno = 1;
}

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

[/code]

$query = "SELECT * FROM Visits $limit";
$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);

//Start My Results
while($row = mysql_fetch_assoc($result))
{
$id=$row['id'];
$ip=$row['ip'];
$country=$row['country'];


if($lip == $ip){print "";}
echo "<tr><td>$ip</td><td>$country</td></tr>
}
//End My Results

if ($pageno == 1) {
echo " FIRST PREV ";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
$prevpage = $pageno-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
}

if ($pageno == $lastpage) {
echo " NEXT LAST ";
} else {
$nextpage = $pageno+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
}

?>

MattF
07-31-2009, 04:05 AM
You're missing the closing quotes and semi-colon on this line:


echo "<tr><td>$ip</td><td>$country</td></tr>

sonny
07-31-2009, 04:12 AM
You're missing the closing quotes and semi-colon on this line:


echo "<tr><td>$ip</td><td>$country</td></tr>


you would figure I would see something as simple as a " and ;

now I get this I am pulling my hair out
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource line 12

MattF
07-31-2009, 04:18 AM
Remove the $db bit from this line.


$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);

sonny
07-31-2009, 04:47 AM
Remove the $db bit from this line.


$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);


I thought that was part of the paginnation code $db

I changed that line in both spots to

$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);


Now
Fatal error: SQL line 10

Does this code look right to you in general I followed a how to exactly

Thanks
Ed

MattF
07-31-2009, 06:08 AM
Which howto did you use?

sonny
07-31-2009, 06:33 AM
Which howto did you use?

Off the top of my head I don't remember it was from this mourning the
good news is I finally just got it to work its a nice feature to have

Ill post the page in a little bit I am formatting the look

This is what I used
$result = mysql_query($query) or die(mysql_error());

instead of this
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);

Talk about a crash course today, I am getting a bit better lately, of course
with help from you as well.

Sonny

sonny
07-31-2009, 08:49 PM
This is for all those out there with very limited PHP skills like myself
but have too much data to display on one page.

Below is a working example of how I accomplished spreading a query
over mutiable pages, just add your query and format your display
text output, and you should be good to go.

Hope this helps someone like me.
Sonny

also maybe some experianced people will drop by and show us different
ways of displaying page links, like viewing 20 of 200 etc and build on this.



<?

//full path to your db, username and pass file etc
require "/home/content/html/connect.php";

?>
<HTML>
<HEAD>

<title>Your Results</title>

</HEAD>

<BODY>
<div align=center><TABLE width=600 border=0>
<tr><td><center><b><h3>Your Results</h3><br></center></td></tr>

<?

if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}

$query = "SELECT count(*) FROM visits";
$result = mysql_query($query) or die(mysql_error());
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

$rows_per_page = 10;// How many result do you want to show per page?
$lastpage = ceil($numrows/$rows_per_page);

$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
}
if ($pageno < 1) {
$pageno = 1;
}

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

//---------------------------------------
// Start of your query and display area

//Do your query (example)
$query = "SELECT * FROM visits ORDER BY id DESC $limit";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result))
{
$ip=$row['ip'];
$city=$row['city'];
$region=$row['region'];
$country=$row['country'];

$result1 = mysql_query("SELECT 1 FROM visits where ip='$ip'");
$num_rows = mysql_num_rows($result1);

//Display results (example)
echo " <tr><td>$ num_rows $ip $city $region $country</td></tr> ";

// End of your query and display area Thats it!
//---------------------------------------

//this is your muti page links at the bottom of page, in case you want to modify them

echo "<tr><td><center>";
if ($pageno == 1) {
echo " FIRST PREV ";
} else {
echo "<a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
$prevpage = $pageno-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
}

if ($pageno == $lastpage) {
echo " NEXT LAST ";
} else {
$nextpage = $pageno+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
}

?>
</center></td></tr></TABLE>
</div>

</body>
</html>