Solved

Cycle through MySql database rows

Posted on 2006-07-17
6
407 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 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