Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

display one row at a time

Status
Not open for further replies.

scitech

Technical User
Jan 6, 2005
40
GB
What I want to do is display the results of a query one at a time with a "button click" to move/display to the next row. i have been trying to use mysql_fetch_array, with a break to the loop. I have found many next/previous methods too complicated for me to understand so cannot adapt.
Is there a method for "a" row from a query?
 
To be able to retrieve one particular record from a db, that record needs to be uniquely identified. Every table I created has a field called "id" or "ind" or something similar which set to autoincrement. When I need to do something to a particular record I use that field if I know it.

Here is one simple way of implementing what you're asking for:
Code:
<?
session_start();
$q = 'select * from yourtable';
if (isset($_SESSION['prev_id'])) $q .= " where id > '" . $_SESSION['prev_id'] . "'"; // are we getting the next record?
$q .= ' limit 1'; // just get 1 record
$rs = @mysql_query($q) or die('Problem with query: ' . $q . '<br>' . mysql_error());
if (@mysql_num_rows() == 1) {
    $_SESSION['prev_id'] = $rw['id']; // store the current record's id
    $tmp = array();
    $rw = @mysql_fetch_assoc($rs);
    $tmp[] = '<form action="" method="post">';
    foreach($rw as $k=>$v)
        if ($k != 'id') echo $k . ': ' . $v . '<br>';
    $tmp[] = '<input type="submit" name="submit" value="Next">';
    $tmp[] = '</form>';
    echo implode("\n",$tmp)."\n"; // just my way of displaying HTML with a "\n" at the end of each line.
}
else {
//
// no more records to display, do somethng
//
}

Please note I haven't checked this code for errors.

Ken
 
Hi Ken
Can you explain what the sesson_start() is all about? I've seen it around.
Also
I found some code that nearly works, except that the Next and prvious links created don't work,could you have a look and try to see why, I tryed putting in the whole path that's no help.It does not update the page!

Code:
<?PHP
include 'config.php';           // All database details will be included here 

$page_name = 'php_paging.php'; //  If you use this code with a different page ( or file ) name then change this 

if(!isset($start))
	{                         // This variable is set to zero for the first page
	$start = 0;
	}

$eu = ($start - 0); 
$limit = 1;                                 // No of records to be shown per page.
$this_one = $eu + $limit; 
$back = $eu - $limit; 
$next = $eu + $limit; 

/////////////// WE have to find out the number of records in our table. We will use this to break the pages///////
$query2 = "SELECT * FROM $table_name";
$result2 = mysql_query($query2) or die(mysql_error());
$nume = mysql_num_rows($result2);
/////// The variable nume above will store the total number of records in the table////
////////////// Now let us start executing the query with variables $eu and $limit  set at the top of the page///////////
$query=" SELECT * FROM $table_name  limit $eu, $limit ";
$result=mysql_query($query);
//////////////// Now we will display the returned records inside the rows of the table/////////
while($noticia = mysql_fetch_array($result))
{
echo "$noticia[FirstName]"." "."$noticia[SurName]"; 
echo "<br>"; 
echo "$noticia[Postcode]"." "."<br>"; 
}

////////////////////////////// End of displaying the table with records ////////////////////////
/////////////// Start the buttom links with Prev and next link with page numbers /////////////////
echo "<table align = 'center' width='50%'><tr><td  align='left' width='30%'>";
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0)
	{ 
	print "<a href='$page_name?start=$back'><font face='Verdana' size='2'>PREV</font></a>"; 
	} 

echo "</td><td  align='right' width='30%'>";
///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this_one < $nume)
	{ 
	print "<a href='$page_name?start = $next'>NEXT</a>";
	} 
	echo "</td></tr></table>";


?>
 
Sessions allow you to pass information between different pages without using the URL line like the example you found.

The reason the example doesn't work that it assumes that register_globals is set to ON. This was the case up to about 2 years ago. When set to ON it can be a security problem.

To fix this, replace these lines:
Code:
if(!isset($start))
    {                         // This variable is set to zero for the first page
    $start = 0;
    }
with
Code:
$start = (isset($_GET['start']))?$_GET['start']:0;

Ken
 
Hi Ken
Thanks for the fix , it works of course
I have another problem where do you put the limit statment in a query, I have found some examples and it goes at the end, but I have a query like this
Code:
$query1 = "SELECT * FROM Addressbook WHERE ".implode(" AND ", $queryItem)  or die(mysql_error());

$result1 = mysql_query($query1) or die(mysql_error());
and I cannot find any way to put a limit in where it does not error The Limit works fine with a simple query
Code:
$query1 = "SELECT * FROM Addressbook limit $a,$b"  or die(mysql_error());

$result1 = mysql_query($query1) or die(mysql_error());
thanks Charlie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top