Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Cycle through MySql database rows

Posted on 2006-07-17
6
Medium Priority
?
415 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
Technology Partners: 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

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!

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 dynamically set the form action using jQuery.
Suggested Courses

636 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