?
Solved

Cycle through MySql database rows

Posted on 2006-07-17
6
Medium Priority
?
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

777 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