...

View Full Version : limit with php script



0810
12-02-2002, 10:17 PM
hi how are you doing?

What I am doing is that my database is so big so that I would like to show my results 5 each. Let's say I have 20 pictures in my database. Then I would like to show up 5 pictures in each page. It will be 4 pages. First page 5 pictures then when I click Next link,it goes to Next page which is 2page(6-10pic).3page(11-15 pic) and 4page(16-20pic).

I tried my scritp but it doesn't work at all

Here is script. my php is 4.23



<?php
if(!isset($_POST['searchtype']) || !isset($_POST['searchterm']))
{
echo"you have not entered search details. Please go back and try again.";
exit;
}
else
{
$searchtype = $_POST["searchtype"];
$searchterm = $_POST["searchterm"];
}
trim($searchterm);

$dbconnect = mysql_connect("localhost", "itohideo_hide", "0810");
if (!$dbconnect) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}

if (! mysql_select_db("itohideo_japan") ) {
echo( "<p>Unable to locate the host " .
"database at this time.</p>" );
exit();
}

$PHP_SELF = $_SERVER["SCRIPT_NAME"];


if($searchterm==""){

exit("You should specify your request<a href=\"lookforgirl.html\">Go Back</a>");

}

else{
$sql = "SELECT * FROM girls WHERE " . $searchtype . " LIKE '%" . $searchterm . "%'";
$result = mysql_query($sql);
$num_results = mysql_num_rows($result);
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else {
$page = 1;
}

if($page>10)
{
$page = 10;

}
print($page."Page<br><br>\n");
echo("<p>itohideo.com found: " . $num_results . "</p>");
for($i=1; $i<$num_results; $i++)
{
$row=mysql_fetch_array($result);

echo"<table border=0 cellpadding=5 cellspacing=3 width=50% align=center>";
echo"<tr>";
echo"<td width=70% align=right>";
echo "No.".($page-1)*10+$i.".Name: ";
echo htmlspecialchars( stripslashes($row["name"]));
echo"</td>";
echo"<td valign=top rowspan=3 align=right>";
echo"<img src=\"$row[images]\" width=180 height=160>";
echo"</td>";
echo"</tr>";
echo"<tr>";
echo"<td width=70% align=right>";
echo"Country: ";
echo htmlspecialchars( stripslashes($row["country"]));
echo"</td>";
echo"<tr>";
echo"<td width=70% align=right>";
echo"Age:";
echo htmlspecialchars( stripslashes($row["age"]));
echo"<td>";
echo "</tr>";

echo"<table>";
}
if($page != 1)
{
print("<a href=\"$PHP_SELF?page=" . ($page -1) . "\">Privious Page</a>");


}
print(" ");
if($page != 10){

print("<a href=\"$PHP_SELF?page=" . ($page + 1) . "\">Next Page</a>");

}

echo ("<br>\n");
}
?>

mat
12-03-2002, 08:37 AM
well here is an example of a 'limit'. Limiting the amount of results that the SQL pulls out of the database to whatever number you specify, in this case 5 $page would be the current page number.




$perPage = 5;
$offset = ($page-1)*$perPage;

$sql = "SELECT age, image, name
FROM people
WHERE age > 17 LIMIT $offset,$perPage";

$result = mysql_query($sql);

0810
12-03-2002, 09:01 PM
I fixed this script but not working. I need your help where is wrong.

I have 14 database, so that it comes out 1page 4datas.

I set 10 pages in this script. Is this working?

10 pages equals to 100datas(10pages)but I have 14 datas in my database.

Here is my script



<?php
if(!isset($_POST['searchtype']) || !isset($_POST['searchterm']))
{
echo"you have not entered search details. Please go back and try again.";
exit;
}
else
{
$searchtype = $_POST["searchtype"];
$searchterm = $_POST["searchterm"];
}
trim($searchterm);

$dbconnect = mysql_connect("localhost", "itohideo_hide", "0810");
if (!$dbconnect) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}

if (! mysql_select_db("itohideo_japan") ) {
echo( "<p>Unable to locate the host " .
"database at this time.</p>" );
exit();
}



if($searchterm==""){

exit("You should specify your request<a href=\"lookforgirl.html\">Go Back</a>");

}

else{

$PHP_SELF = $_SERVER["SCRIPT_NAME"];

if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else {
$page = 1;
}

if($page>10)
{
$page = 10;

}
$perPage = 10;
$offset = ($page-1)*$perPage;

$sql = "SELECT * FROM girls WHERE " . $searchtype . " LIKE '%" . $searchterm . "%' limit $offset, $perPage";
$result = mysql_query($sql);
$num_results = mysql_num_rows($result);
print($page."Page<br><br>\n");
echo("<p>itohideo.com found: " . $num_results . "</p>");
for($i=0; $i<$num_results; $i++)
{
$row=mysql_fetch_array($result);

echo"<table border=0 cellpadding=5 cellspacing=3 width=50% align=center>";
echo"<tr>";
echo"<td width=70% align=right>";
echo "<strong>".($i+1).".Name: ";
echo htmlspecialchars( stripslashes($row["name"]-1) * 10 + $i );
echo"</td>";
echo"<td valign=top rowspan=3 align=right>";
echo"<img src=\"($row[images] -1)*10+$i\" width=180 height=160>";
echo"</td>";
echo"</tr>";
echo"<tr>";
echo"<td width=70% align=right>";
echo"Country: ";
echo htmlspecialchars( stripslashes($row["country"]-1) * 10 + $i );
echo"</td>";
echo"<tr>";
echo"<td width=70% align=right>";
echo"Age:";
echo htmlspecialchars( stripslashes($row["age"]-1)*10+$i);
echo"<td>";
echo "</tr>";

echo"<table>";

}



if($page != 1)
{
print("<a href=\"$PHP_SELF?page=" . ($page -1) . "\">Privious Page</a>");


}
print(" ");
if($page != 10){

print("<a href=\"$PHP_SELF?page=" . ($page + 1) . "\">Next Page</a>");




}



?>

0810
12-04-2002, 03:34 AM
Does anybody help me this script?

Thanks a lot

Wichetael
12-04-2002, 01:17 PM
You do of course need to specify the actual values in the limit clause, check out the documentation here (http://www.mysql.com/doc/en/SELECT.html)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum