Jump to content

how to get data from db page by page


Go to solution Solved by Ch0cu3r,

Recommended Posts

This is the code what tried.

Data is divided and displayd no.of pages too...

But, when i click 2link/page, its showng sorry no data found.

Help me...

 

<?php

$name = $_POST['name'];

$category = $_POST['category'];

$con = mysql_connect("localhost","zxzx","xzxz") or die("Unable to connect");

$db = mysql_select_db("test",$con) or die("Unable to select DB");

if(isset($_GET['page']))

{

$page = $_GET['page'];

}

else

{

$page = 1;

}

$sf = ($page-1) * 16;

$sql = " SELECT * FROM budget where NAME=\"$name\" LIMIT ".$sf.",16 ";

$rs = mysql_query($sql,$con);

if(mysql_num_rows($rs) == 0)

{

echo("<p class=\"no\">Sorry, NO Records Found..!</p>

<p class=\"no\">Please try with another Name.</p>

");

}

else

{

echo "<table id='main-table' cellspacing='0' cellpadding='0' class=\"table1\">

<tr class=\"rowh\">

<td>Date</td>

<td>Name</td>

<td colspan=\"2\"> ACTIONS </td>

</tr>";

while ($row=mysql_fetch_array($rs))

{

echo ("<tr><td class=\"rowr\">$row[DATE]</td>");

echo ("<td class=\"rowr\">$row[NAME]</td>");

echo ("<td ><a class=\"rowrl\" href=\"edit_form.php?number=$row[iD]\"> Edit </a></td>");

echo ("<td ><a class=\"rowrl\" href=\"delete.php?number=$row[iD]\" onclick=\"return confirm(' Do you really want to DELETE ?');\"> Delete </a></td></tr>");

}

}

</table>

</td>

</tr>

</table>

<?php

$sql1 = "SELECT COUNT(NAME) FROM BUDGET where NAME=\"$name\" ";

$rs1 = mysql_query($sql1,$con);

$row1 = mysql_fetch_row($rs1);

$total = $row1[0];

$tp = ceil($total/16);

for($i = 1; $i <= $tp; $i++)

{

echo "<a href='edit.php?page=".$i."'>".$i."</a> ";

}

?>

Link to comment
https://forums.phpfreaks.com/topic/282631-how-to-get-data-from-db-page-by-page/
Share on other sites

Your code relies on the $_POST['name'] value, this is coming from the form you submitted to edit.php. Now it will work fine on the first page as this value is present. But it is not when you click on any of the page links as this value is not being carried over. If you don't pass it back to edit.php $name will be empty and your sql query

$sql = " SELECT * FROM budget where NAME=\"$name\" LIMIT ".$sf.",16 ";
wont return any data and thus you get the sorry no data found message

 

To preserve the value  you'll need to pass the name within your page links to edit.php

echo "<a href='edit.php?name=".$name."&page=".$i."'>".$i."</a> ";
Now change $name = $_POST['name']; to

$name = isset($_POST['name']) ? $_POST['name'] : isset($_GET['name']) ? $_GET['name'] : '';
if(empty(trim($name)))
    echo 'Name required';
 

The above code will first check to see if the name has been submitted by _POST (from your form). If it is it'll set $name to that value. If _POST['name'] doesn't exits it'll see if it is in the url (values passed in the url is retrieved using _GET). If that value is found it'll set $name to $_GET['name']. Otherwise it'll set $name to emtpy. The if statement checks to make sure $name is not empty and if it is displays a message saying name required.

Edited by Ch0cu3r
  • Solution

Sorry

 

Change

$name = isset($_POST['name']) ? $_POST['name'] : isset($_GET['name']) ? $_GET['name'] : '';
if(empty(trim($name)))
    echo 'Name required';
to

$name = (isset($_POST['name']) ? $_POST['name'] : (isset($_GET['name']) ? $_GET['name'] : ''));
if(empty($name))
echo 'Name required';
Should work ok now? Edited by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.