Click to See Complete Forum and Search --> : 20 results on page and link for the others


vinsa
09-17-2004, 02:18 PM
First I'm sorry for my English
Can someone help me. I want this script
to show 20 results of one page and
create links for the rest of the results
(the other pages to show 20 results too).

This is my script:



<?php

require ( 'top.php' );

@ $db = mysql_pconnect('localhost', 'base1', 'password');

if (!$db)
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}

mysql_select_db('base1');
$query = "select * from imoti where nomer > 0";
$result = mysql_query($query);

$num_results = mysql_num_rows($result);

echo '<table align="right" border="0" cellspacing="2" cellpadding="3" width="613">
<tr>
<td width="" bgcolor="#AFC1D8"><b>name</b></td>
<td width="" bgcolor="#AFC1D8"><b>age</b></td>
<td width="" bgcolor="#AFC1D8"><b>sex</b></td>
<td width="" bgcolor="#AFC1D8"><b>location</b></td>
<td width="" bgcolor="#AFC1D8"><b>email</b></td>
</tr>';

for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo '<tr><td width="">';
echo $row['name'];
echo '</font></td><td width="">';
echo $row['age'];
echo '</td><td width="">';
echo $row['sex'];
echo '</td><td width="">';
echo $row['location'];
echo '</td><td width="">';
echo $row['email'];
echo '</td></tr><tr bgcolor="#AFC1D8"><td bgcolor="#AFC1D8" colspan="6" height="1"></td></tr>';
}

?>
</table>

<?
require ( 'down.php' );
?>

ShrineDesigns
09-17-2004, 03:04 PM
try this:<?php
require('top.php');

$db = @mysql_pconnect('localhost', 'base1', 'password');

if(!$db)
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
$pg = (isset($_GET['pg']) && !empty($_GET['pg'])) ? $_GET['pg'] : 1;
mysql_select_db('base1');
// for mysql 4 and up use: SQL_CALC_FOUND_ROWS
$result = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM `imoti` WHERE `nomer` > 0 LIMIT ".($pg - 1) * 20.",20");
$count = mysql_query("SELECT FOUND_ROWS()");
$total = @mysql_result($count, 0);

if(@mysql_num_rows($result) > 0)
{
?>
<table border="0" cellspacing="2" cellpadding="3" width="613">
<tr align="left" bgcolor="#AFC1D8">
<th>name</th>
<th>age</th>
<th>sex</th>
<th>location</th>
<th>email</th>
</tr>
<?php
while(($row = mysql_fetch_array($result, MYSQL_ASSOC)) !== false)
{
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['age']; ?></td>
<td><?php echo $row['sex']; ?></td>
<td><?php echo $row['location']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
<tr>
<td bgcolor="#AFC1D8" colspan="6" height="1"></td>
</tr>
<?php
}
}
?>
</table>
<?php
if($total > 20)
{
for($i = 1; $i <= ceil($total / 20); $i++)
{
if($i == $_GET['pg'])
{
?>
<b><?php echo $i; ?></b>
<?php
}
else
{
?>
<a href="<?php echo "{$_SERVER['PHP_SELF']}?pg=$i"; ?>"><?php echo $i; ?></a>
<?php
}
}
}
require('down.php');
?>

jacen6678
09-17-2004, 03:19 PM
I would do the following:

1) get the total number of return matching rows...

$total_rows = mysql_query( "SELECT COUNT( * ) FROM table" );
1.1) you will use $total rows to generate the page menu
$num_pages = $total_rows / 20;
if( $num_pages % 20 )
$num_pages++;

for( $i = 1; $i <= $num_pages; $i++ )
echo "<a href=''>[".$i."]...

2) keep track of the page number (ie: I want this script to show 20 results of one page and... )
if( !$page_number )
$page_number = 1;

3) use $page_number to compute the number of the first and last rows to be displayed on the page.
$last_product_on_page = $page_number * 20;
$first_product_on_page = $last_product_on_page - 20;

4) limit my query return
$query = "select * from imoti where nomer > 0 LIMIT ".$first_product.", ".$last_product;

You might need to tweak the numbers... for example the query might need to be $query = "select * from imoti where nomer > 0 LIMIT ".$first_product.", ".( $last_product + 1 );

vinsa
09-17-2004, 03:23 PM
It show me this error massage:

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /web/d2/w/multioffice-1/multioffice-1.com/imoti/bbb.php on line 14

This is line 14:

$result = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM `imoti` WHERE `nomer` > 0 LIMIT ".($pg - 1) * 20.",20");

ShrineDesigns
09-17-2004, 03:26 PM
sorry about that, i fixed it

vinsa
09-17-2004, 03:37 PM
it show me the same error, my PHP version is 4.2

ShrineDesigns
09-17-2004, 03:40 PM
what version of mysql are you using???

vinsa
09-17-2004, 03:44 PM
the last

ShrineDesigns
09-17-2004, 04:02 PM
try this:<?php
require('top.php');

$db = @mysql_pconnect('localhost', 'base1', 'password');

if(!$db)
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
$pg = (isset($_GET['pg']) && !empty($_GET['pg'])) ? $_GET['pg'] : 1;
mysql_select_db('base1', $db);

$version = floatval(mysql_get_server_info($db));

$sql = "SELECT".(($version >= 4.0) ? " SQL_CALC_FOUND_ROWS" : "")." * FROM `imoti` WHERE `nomer` > 0 LIMIT ".($pg - 1) * 20.",20";
$result = mysql_query($sql, $db);

$sql = ($version >= 4.0) ? "SELECT FOUND_ROWS()" : preg_replace("/ LIMIT (\d+)(,*)(\s*)(\d*)/i", '', $sql);
$count = mysql_query($sql, $db);
$total = @mysql_num_rows($count, 0);
@mysql_free_result($count);

if(@mysql_num_rows($result) > 0)
{
?>
<table border="0" cellspacing="2" cellpadding="3" width="613">
<tr align="left" bgcolor="#AFC1D8">
<th>name</th>
<th>age</th>
<th>sex</th>
<th>location</th>
<th>email</th>
</tr>
<?php
while(($row = mysql_fetch_array($result, MYSQL_ASSOC)) !== false)
{
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['age']; ?></td>
<td><?php echo $row['sex']; ?></td>
<td><?php echo $row['location']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
<tr>
<td bgcolor="#AFC1D8" colspan="6" height="1"></td>
</tr>
<?php
}
}
?>
</table>
<?php
if($total > 20)
{
for($i = 1; $i <= ceil($total / 20); $i++)
{
if($i == $_GET['pg'])
{
?>
<b><?php echo $i; ?></b>
<?php
}
else
{
?>
<a href="<?php echo "{$_SERVER['PHP_SELF']}?pg=$i"; ?>"><?php echo $i; ?></a>
<?php
}
}
}
require('down.php');
?>

vinsa
09-17-2004, 04:10 PM
again the same error :( ok, thank you, don't lose your time again I'll try to write something like this.
Besht regards.

Paul Jr
09-17-2004, 04:17 PM
Try wrapping ($pg - 1) * 20 in parentheses -- the query would end up looking like this:

$sql = "SELECT".(($version >= 4.0) ? " SQL_CALC_FOUND_ROWS" : "")." * FROM `imoti` WHERE `nomer` > 0 LIMIT ".(($pg - 1) * 20).",20";

vinsa
09-17-2004, 04:31 PM
:rolleyes: now it work but not create links for the other pages with next results. Why? I want when results are > 20 to create links for another pages (and they with 20 results too)

Something like this:

1, 2, 3, ... all results