Solved

Cycle through MySql database rows

Posted on 2006-07-17
6
406 Views
Last Modified: 2013-12-12
I am not totally new to programming, I have a year in VB.NET and want to learn some PHP. My goal is to convert a VB.NET program into something web based and I am thinking PHP is the way I want to go.

So I can currently connect to the MySQL database and output the data. Next step is to output the data into textboxes or similar. Getting the data in the textboxes, etc seems easy enough. What I need to know is how to cycle through the results, with a forward, back, first, last button. This is similar to a binding manager in VB.NET which cycles through your current dataset.

0
Comment
Question by:ParanoidOne
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:pritaeas
ID: 17126224
If you do not want to execute a query each time, you can store the results in a session, or create a class to do this (or any other caching method you can think of). Otherwise you need to store the index of the current record yourself, and on a button submit retrieve the next one with a new query.
0
 
LVL 7

Accepted Solution

by:
maUru earned 250 total points
ID: 17126710
for cycling through results i use a bit of simple code, something like:

$wheredata = " where price > 100 "; // example, change with your query data
$totalrecords = mysql_num_rows(mysql_query("select id from table $wheredata"));
$page = (!isset($_GET['page'])) ? 1 : $_GET['page'];
$rowsperpage = 20;
$totalpages = ceil($numrows/$rowsperpage);
if ($page > $totalpages) { echo 'wrong page'; exit; }
$offset = ($page - 1) * $rowsperpage;
$q = "select * from table $wheredata limit $offset, $rowsperpage";
$r = mysql_query($q);
while ($row = mysql_fetch_assoc($r)) {
  // echo list data eg:
  echo $row['ID'] . ' - ' . $row['price'] . "\n";
}
for ($i = 1; $i <= $totalpages; $i++) {
  if ($page != $i) {
    echo '<a href="' . $PHP_SELF . '?page=' . $i . '">' . $i . '</a>';
  }
  else {
    echo '<b>' . $i . '</b>';
  }
}


i just wrote this, its sure to have some errors, but you get the idea.
0
 
LVL 7

Expert Comment

by:maUru
ID: 17126731
sorry this line:

$totalpages = ceil($numrows/$rowsperpage);

should be this:

$totalpages = ceil($totalrecords/$rowsperpage);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ParanoidOne
ID: 17130057
I have tried your code and other people code and I get this a lot.

Notice: Undefined variable: PHP_SELF in c:\Inetpub\wwwroot\new\index.php on line 34

Why does my PHP_SELF variable never seem to work ??
0
 

Author Comment

by:ParanoidOne
ID: 17130074
By the way this code.

<?php
echo $PHP_SELF;
?>

Also produces

Notice: Undefined variable: PHP_SELF in c:\Inetpub\wwwroot\new\index2.php on line 2
0
 

Author Comment

by:ParanoidOne
ID: 17130101
Ok REGISTER_GLOBALS was off, set it to on and it is fixed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now