Solved

Cycle through MySql database rows

Posted on 2006-07-17
6
405 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Router for PHP reqeusts 12 33
Wordpress Query 1 35
PHP loop not working 4 33
update field on focusout 15 24
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

762 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

19 Experts available now in Live!

Get 1:1 Help Now