3 Replies - 597 Views - Last Post: 05 October 2012 - 06:36 AM Rate Topic: -----

#1 arber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-October 12

how can i use a button to display my sql data

Posted 05 October 2012 - 05:44 AM

hello and thank you for the time! i have this php page i need to use a button to make change the rows displayed in it.
at this moment it dispaly the last 5 rows.
 
<?php 

$link = mysql_connect("localhost", "root","");
mysql_select_db("test", $link);
$query = "select * from post order by id DESC LIMIT 0, 5 ";
$results = mysql_query($query, $link) or die ("the code didn't worked!".mysql_error());
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){

echo $row['title']. "<img src=/1/" . $row['location']. " width=580px > " .  "<br /><br /><br />";


}
}
?>




i need a button "next" and "preview" so the rows displayed can change.
for ex. if i click on the button the code will change to this:
<?php 

$link = mysql_connect("localhost", "root","");
mysql_select_db("test", $link);
$query = "select * from post order by id DESC LIMIT 5, 5 ";
$results = mysql_query($query, $link) or die ("the code didn't worked!".mysql_error());
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){

echo $row['title']. "<img src=/1/" . $row['location']. " width=580px > " .  "<br /><br /><br />";


}
}
?>




Is This A Good Question/Topic? 0
  • +

Replies To: how can i use a button to display my sql data

#2 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: how can i use a button to display my sql data

Posted 05 October 2012 - 06:05 AM

Hi,

You are talking about pagination. You could do this by keeping track of what page you are on, this is often done via the query string. Then limit your query based on the page you are on and the number of records per page. Something like this:

$dsn = 'mysql:dbname=mydb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
//instantiate a PDO object
$db = new PDO($dsn, $user, $password);

//if no page is set default to page 1
$page = (isset($_GET['pg']) ? $_GET['pg'] : 1;
//records per page
$page_size = 5;
//see how many records we need to skip
$skip = ($page - 1) * $page_size;
$sql = "select * from post order by id DESC LIMIT ?, ?"
$stmt =  $db->prepare($sql);
$stmt->execute(array($skip,$page_size));
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    //do stuff with the records
}


Note that I use PDO instead of the mysql_* functions, you should stay away from them as they are deprecated. Use PDO and prepared statements instead. Dormilich has written a great tutorial as an introduction to PDO. Hope this helps :)

This post has been edited by Jstall: 05 October 2012 - 06:06 AM

Was This Post Helpful? 2
  • +
  • -

#3 arber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-October 12

Re: how can i use a button to display my sql data

Posted 05 October 2012 - 06:32 AM

ok but i think in this way the problem is when in pag 70 i will not have more data what happens than?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,329
  • Joined: 08-August 08

Re: how can i use a button to display my sql data

Posted 05 October 2012 - 06:36 AM

What happens when you try it?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1