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

Link to comment
Share on other sites

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;
}        
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.