Forum Moderators: coopster

Message Too Old, No Replies

New Pagination Query....

Question about the listing data from DB

         

woldie

11:19 am on Jul 6, 2004 (gmt 0)

10+ Year Member



Okay,

I've got it to work! Which I'm well pleased about.

BUT.... what it's doing at the moment is that I have 8 records in the DB in total, and what it does is that it doesn't print out the first record but the next four, where it should print the first five.

Then when you click on the 2nd page link it prints out 2 records where it should print out 3 records.

So essentially its missing one record on each new page.

Any ideas on how to overcome this problem.

Thanks in advance

:o)

Code:

<?
//Results per page
$num_result = "5";

function calclink()
{
static $x;
$x=$x+1;
echo "$x";
}

function calclink2()
{
static $z;
$z=$z+1;
echo "$z";
}

//CHECK TO SEE IF $cur_page is set
if($cur_page==""){$curpage="0";}

//DBINFO HERE
$query="select aid,title,firstname,lastname,injury_type,applieddate from accident LIMIT ";
$res = $num_result*$cur_page;
$query .= $res.','. ' ' .$num_result;

echo $query;

$result=mysql_query($query);

//Get Number of rows
$num_rows = mysql_num_rows($result);


$data=mysql_fetch_row($result);

//might be something to do with is?
$total_num_rows=$data[0];

while (list($DBaid,$DBtitle,$DBfirstname,$DBlastname,$DBinjury_type,$DBdate)=mysql_fetch_row($result))
{
?>
<tr>
<td class="webbtd"><a href="acc-edit.php?aid=<? echo $DBaid?>"><? echo $DBtitle. ' ' .$DBfirstname. ' ' .$DBlastname?></a></td>
<td class="webbtd"><? echo $DBinjury_type?></td>
<td class="webbtd"><? echo $DBdate?></td>
<td class="webbtd" align="center"><input type=checkbox name=del_cust[] value=<? echo $DBaid?>></td>
</tr>
<?
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="submit" value="update"></td>
</tr>
</table>
<?
//do some math to see how many pages are needed
$total_pages = intval($data[0]/$num_result);

echo '<div align=center>';
echo "<a href=\"index.php?cur_page=0\">Pg ";
calclink();
echo"</a>&nbsp; ¦ ";

//See if there is more then one page needed
if(!$total_pages>="$num_result")
{
//If more then one page is needed, use for loop to create links
for($page=0; $page<=$total_pages; $page++)
{
echo "<a href=\"index.php?cur_page=";
calclink2();
echo"\">Pg ";
calclink();
echo"</a>&nbsp; ¦ ";
}
}
echo '</div>';
?>

woldie

4:24 pm on Jul 6, 2004 (gmt 0)

10+ Year Member



Right,

Almost there!

I've changed the code slightly now, however I've set the num result variable to 5, so it should have 3 links at the bottom but I get 2.

So it displays 2 pages with 5 records on each but there is no link for the rest of the records.

Any suggestion is much appreciated.

Thanks

<?
//Results per page
$num_result = "5";

function isstatic()
{
static $x;
$x=$x+1;
echo "$x";
}

function isstatic2()
{
static $z;
$z=$z+1;
echo "$z";
}

//CHECK TO SEE IF $cur_page is set
if($cur_page==""){$curpage="0";}

//DBINFO HERE
$query="select aid,title,firstname,lastname,injury_type,applieddate from accident";
#$res = $num_result*$cur_page;
#$query .= $res.','. ' ' .$num_result;

$result=mysql_query($query);

//Get Number of rows
$num_rows = mysql_num_rows($result);

$data=mysql_fetch_row($result);
$total_num_rows=$data[0];

echo "Total Num Rows: $total_num_rows";
$query = "SELECT aid,title,firstname,lastname,injury_type,applieddate FROM accident LIMIT ";
$res= $num_result*$cur_page;
$query.=$res.','. ' ' .$num_result;

echo $query;

$r=mysql_query($query);

while ($row=mysql_fetch_array($r))
{
?>
<tr>
<td class="webbtd"><a href="acc-edit.php?aid=<? echo $row[0]?>"><? echo $row[1]. ' ' .$row[2]. ' ' .$row[3]?></a></td>
<td class="webbtd"><? echo $row[4]?></td>
<td class="webbtd"><? echo $row[5]?></td>
<td class="webbtd" align="center"><input type=checkbox name=del_cust[] value=<? echo $row[0]?>></td>
</tr>
<?
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="submit" value="update"></td>
</tr>
</table>
<?
//do some math to see how many pages are needed
$total_pages = intval($data[0]/$num_result);
#$total_pages = intval($num_rows[0] / $max_results);

echo '<div align=center>';
echo "<a href=\"index.php?cur_page=0\">Pg ";
isstatic();
echo"</a>&nbsp; ¦ ";

//See if there is more then one page needed
if(!$total_pages>="$num_result")
{
//If more then one page is needed, use for loop to create links
for($page=0; $page<=$total_pages; $page++)
{
echo 'hello';
echo "<a href=\"index.php?cur_page=";
isstatic2();
echo"\">Pg ";
isstatic();
echo"</a>&nbsp; ¦ ";
}
}
echo '</div>';
?>

coopster

5:24 pm on Jul 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You really should turn error_reporting() [php.net] on. It will help you troubleshoot [webmasterworld.com] some of your issues, for example, your
$cur_page
variable is also used as
$curpage
(without the underscore), probably not what you are expecting.

coopster

5:39 pm on Jul 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Other than that, I think you have a couple of other logic issues:
Change this: 
$total_pages = intval($data[0]/$num_result);
to:
$total_pages = intval($num_rows/$num_result);
//
Change this:
if(!$total_pages>="$num_result")
to:
if(!($total_pages >= $num_result))

And you won't need these lines...
$data=mysql_fetch_row($result); 
$total_num_rows=$data[0];
// if you put this line in, you'll see why this was not working:
print '<pre>'; print_r($data); exit('</pre>');
echo "Total Num Rows: $total_num_rows";

woldie

7:57 am on Jul 7, 2004 (gmt 0)

10+ Year Member



Thanks Coopster for responding back.

However I'm still getting the same results where there should be 3 links at the bottom because I have 12 records in the DB.

I've tried those changes, and this line of code, I get a division error.

$total_pages = intval($num_rows / $num_result);

I've turned the error reporting, but no joy...

I'm missing something here, but can't put a finger on it, it should get a 3rd link to appear.

I've also done those changes in your last thread but I only get one link. So I'm back to square 1 again.

Many Thanks

<?
//Results per page
$num_result = "5";

function isstatic()
{
static $x;
$x=$x+1;
echo "$x";
}

function isstatic2()
{
static $z;
$z=$z+1;
echo "$z";
}

//CHECK TO SEE IF $cur_page is set
if($cur_page==""){$cur_page="0";}

//DBINFO HERE
$query="select aid,title,firstname,lastname,injury_type,applieddate from accident";
$result=mysql_query($query);

//Get Number of rows
$num_rows = mysql_num_rows($result);

$data=mysql_fetch_row($result);
$total_num_rows=$data[0];
//print '<pre>'; print_r($data); exit('</pre>'); echo "Total Num Rows: $total_num_rows";

echo "Total Num Rows: $num_rows";
$query = "SELECT aid,title,firstname,lastname,injury_type,applieddate FROM accident LIMIT ";
$res= $num_result*$cur_page;
$query.=$res.','. ' ' .$num_result;

echo $query;

$r=mysql_query($query);

while ($row=mysql_fetch_array($r))
{
?>
<tr>
<td class="webbtd"><a href="acc-edit.php?aid=<? echo $row[0]?>"><? echo $row[1]. ' ' .$row[2]. ' ' .$row[3]?></a></td>
<td class="webbtd"><? echo $row[4]?></td>
<td class="webbtd"><? echo $row[5]?></td>
<td class="webbtd" align="center"><input type=checkbox name=del_cust[] value=<? echo $row[0]?>></td>
</tr>
<?
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="submit" value="update"></td>
</tr>
</table>
<?
//do some math to see how many pages are needed
$total_pages = intval($data[0]/$num_result);
#$total_pages = intval($num_rows / $num_result);

echo '<div align=center>';
echo "<a href=\"index.php?cur_page=0\">Pg ";
isstatic();
echo"</a>&nbsp; ¦ ";

//See if there is more then one page needed
if(!$total_pages>=$num_result)
{
//If more then one page is needed, use for loop to create links
for($page=0; $page<=$total_pages; $page++)
{
echo "<a href=\"index.php?cur_page=";
isstatic2();
echo"\">Pg ";
isstatic();
echo"</a>&nbsp; ¦ ";
}
}

error_reporting(E_ALL);
echo '</div>';
?>

</form>