Jump to content

trying to create a "next" link to cycle through data from a mysql query


Shawerma

Recommended Posts

I need to create a next button so people can cycle through data that will have to be queried and stored as the data can change. I have looked into the php next function and it looks like the way to go. Can someone post some code of how to get an array from a query and then utilize the next function with the data gathered from the query? or if there is a better way to achieve this that would be great too.

 

$query = ("Select id from table where status = 4");

$result = (mysql_query);

while ($lrow = mysql_fetch_array($result)){

      $id = $lrow;

}

 

$newid = next($id);

echo $newid;

 

every time i refresh it should give me the next id in the array from the query.

 

thanks,

 

D

Use this

 

 

<?php
function select_row($sql)
{
//echo $sql . "<br />";
if ($sql!="")
{
	$result = mysql_query($sql) or die("Error: ".mysql_errno().":- ".mysql_error());
	if ($result)
	{
		while($row = mysql_fetch_assoc($result))
			$data[] = $row;
	}
	return $data;
}
}

function pagingSlot($sql, $recperpage, $pagesetlimit, $page, $class, $getvars)
{
$rescnt=mysql_query($sql);
$totcnt=mysql_num_rows($rescnt);
   
	if (!$page)   
		$page = 1;
	$first=(($page-1)* $recperpage);

	$sql = $sql . " limit ".$first.",".$recperpage;
	$res = select_row($sql);
   
	$serial_no = ($page - 1) * $recperpage;
   
	$t = ($totcnt/$recperpage);
	$arr=split('[.]',$t);
	if ($arr[1])
		$totalpages=$arr[0]+1;
	else
		$totalpages=$arr[0];
   
	if ($totalpages > $pagesetlimit)
	{
		if ($page > 1)
			$pagesetstart = $page - 1;
		else
			$pagesetstart = $page;               
		   
		$pagesetend= ($pagesetstart-1) + $pagesetlimit;
		if ($pagesetend > $totalpages)
		{
			$pagesetend = $totalpages;
			$pagesetstart = $pagesetend - $pagesetlimit + 1;
		}
	}
	else
	{
		$pagesetstart = 1;
		$pagesetend = $totalpages;
	}
   
	$str = "";

	if ($page > 1)
	{
		$prev = $page - 1;
		$str.= "<a href='".$_SERVER['PHP_SELF']."?page=$prev".$getvars."'  class='".$class."'> << </a> | ";                                           
	}
	else
	{
		$str.= "<<  | ";   
	}


	for ($i=$pagesetstart; $i<=$pagesetend; $i++)
	{       
   
		if ($i <= $totalpages)
		{
			if (!$page)
				$page=1;
			if ($page==$i)
				$str.= '<font color=red>'.$i.'</font> ';
			else
				$str.= "<a href='".$_SERVER['PHP_SELF']."?page=$i".$getvars."'  class='".$class."'>".$i."</a> ";
		}                               
	}

	if ($page < $totalpages)
	{
		$next = $page + 1;
		$str.= " | <a href='".$_SERVER['PHP_SELF']."?page=$next".$getvars."'  class='".$class."'> >> </a> ";                                           
	}
	else
	{
		$str.= " |  >>  ";
	}

if ($totcnt == 0)
	$str = "";
   
$arr["records"]=$res;
$arr["link"]=$str;
$arr["serial"]=$serial_no;
return $arr;
}        
?>

 

 

Dood, thanks for the script! I am trying to figure it out but am having some problems. This code is a little beyond my knowledge. If you could insert some comments to help me figure it out that would help. I'll post again if I figure it out as well.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.