Jump to content

Pagination Problem


wolfcry

Recommended Posts

Hey all,

 

I wrote a script to preform the basic pagination needs but for some reaons I cannot get the data to prin t to the screen when I click on the paginated page number (EG page #2). Everything prints fine for page one, but nothing shows up for page to or any other pages.

 

Heres the bit of code:

<?php
session_start();
require("config.php");
$link = ft_db_connect();

// now retrieve ALL submissions in this location
$table_id = information;
$_SESSION['city'] = $_POST['city'];
$sl = $_SESSION['city'];
$city_location = $sl;

$result = mysql_query("SELECT COUNT(*) AS total_entries FROM data_{$table_id} WHERE col_3 = '$city_location'") or die("Uh Oh" . mysql_error()); 
$row = mysql_fetch_row($result); $total_entries = $row[0];
$entries_per_page = 5;
if(isset($_GET['page_number'])) { $page_number = $_GET['page_number']; } else { $page_number = 1; }
$total_pages = ceil($total_entries / $entries_per_page);
$offset = ($page_number - 1) * $entries_per_page;

$result = mysql_query("SELECT * FROM ft_form_{$form_id} WHERE col_3 = '$city_location' LIMIT $offset, $entries_per_page") or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
  echo "<table>";
  echo "<tr><td><b>Location Name:</b> {$row['col_2']}</td></tr>";
  echo "<tr><td><b>City / Town:</b> {$row['col_3']}</td></tr>";
  echo "<tr><td><b>State / Province:</b> {$row['col_4']}</td></tr>";
  echo "<tr><td><b>GPS Data:</b> {$row['col_5']}</td></tr>";
  echo "<tr><td><b>Description:</b> {$row['col_6']}</td></tr>";
  echo "</table>";

  echo "<hr size=\"1\" />"; // just for display purposes to separate the entries
}


for($i = 1; $i <= $total_pages; $i++) { if($i == $page_number) { print "$i "; } 
  else { print "<a href='search.php?page_number=$i'>$i</a>"; } 

}


if (isset($_POST["submit"]) AND mysql_num_rows($result) == 0) { 

  
echo "Sorry, there have been no submissions in this location.";
  
}


 

Now, you'll see $_SESSION['city'] = $_POST['city'] because I've been experimenting with trying to get this to work (it was originally $city = $_POST['city']  and the value is being pulled from posted data search from another page.

 

How do I store the original value of the search into a variable or into this code so the paginated data will show up on all pages?

 

I've been working on it for a bit now and it's really starting to drive me nuts lol.

 

Thanks in advance.

Link to comment
Share on other sites

 

Now, you'll see $_SESSION['city'] = $_POST['city'] because I've been experimenting with trying to get this to work (it was originally $city = $_POST['city']  and the value is being pulled from posted data search from another page.

 

How do I store the original value of the search into a variable or into this code so the paginated data will show up on all pages?

 

just change $_SESSION['city'] = ... to $city = $_REQUEST['city']; that will capture post and get methods. then add the city as a var to your pagination url like

<a href='search.php?city=$city&page_number=$i'>

 

that should fix the other problem, as when you click on a subsequent page past the first result, you redefine your city location based on the posted var (which is no longer posted, we are using get now..)

 

here, just use this...

 

<?php
session_start();
require("config.php");
$link = ft_db_connect();

// now retrieve ALL submissions in this location
$table_id = information;
$city_location = $_REQUEST['city'];

$result = mysql_query("SELECT COUNT(*) AS total_entries FROM data_{$table_id} WHERE col_3 = '$city_location'") or die("Uh Oh" . mysql_error()); 
$row = mysql_fetch_row($result); $total_entries = $row[0];
$entries_per_page = 5;
if(isset($_GET['page_number'])) { $page_number = $_GET['page_number']; } else { $page_number = 1; }
$total_pages = ceil($total_entries / $entries_per_page);
$offset = ($page_number - 1) * $entries_per_page;

$result = mysql_query("SELECT * FROM ft_form_{$form_id} WHERE col_3 = '$city_location' LIMIT $offset, $entries_per_page") or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
  echo "<table>";
  echo "<tr><td><b>Location Name:</b> {$row['col_2']}</td></tr>";
  echo "<tr><td><b>City / Town:</b> {$row['col_3']}</td></tr>";
  echo "<tr><td><b>State / Province:</b> {$row['col_4']}</td></tr>";
  echo "<tr><td><b>GPS Data:</b> {$row['col_5']}</td></tr>";
  echo "<tr><td><b>Description:</b> {$row['col_6']}</td></tr>";
  echo "</table>";

  echo "<hr size=\"1\" />"; // just for display purposes to separate the entries
}


for($i = 1; $i <= $total_pages; $i++) { if($i == $page_number) { print "$i "; } 
  else { print "<a href='search.php?city=$city_location&page_number=$i'>$i</a>"; } 

}


if (isset($_POST["submit"]) AND mysql_num_rows($result) == 0) { 

  
echo "Sorry, there have been no submissions in this location.";
  
}

Link to comment
Share on other sites

Thanks Benjamin its finally working. I tried using the $_REQUEST method before but was setting it up wrong.

 

I have another question if I could, I wish to allow visitors the chance to update a row if that particular row comes up empty.

 

I tried using the following code (I surrounded it in #'s for this post only. It's not actually in my code):

 

<?php
session_start();
require("config.php");
$link = ft_db_connect();

// now retrieve ALL submissions in this location
$table_id = information;
$city_location = $_REQUEST['city'];

$result = mysql_query("SELECT COUNT(*) AS total_entries FROM data_{$table_id} WHERE col_3 = '$city_location'") or die("Uh Oh" . mysql_error()); 
$row = mysql_fetch_row($result); $total_entries = $row[0];
$entries_per_page = 5;
if(isset($_GET['page_number'])) { $page_number = $_GET['page_number']; } else { $page_number = 1; }
$total_pages = ceil($total_entries / $entries_per_page);
$offset = ($page_number - 1) * $entries_per_page;

$result = mysql_query("SELECT * FROM ft_form_{$form_id} WHERE col_3 = '$city_location' LIMIT $offset, $entries_per_page") or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
  echo "<table>";
  echo "<tr><td><b>Location Name:</b> {$row['col_2']}</td></tr>";
  echo "<tr><td><b>City / Town:</b> {$row['col_3']}</td></tr>";
  echo "<tr><td><b>State / Province:</b> {$row['col_4']}</td></tr>";
  echo "<tr><td><b>GPS Data:</b> {$row['col_5']}</td></tr>";
  echo "<tr><td><b>Description:</b> {$row['col_6']}</td></tr>";
  echo "</table>";

##################### NEW CODE #######################
if ($row ['col_6'] == "") {

echo "<form action='$_SERVER[php_SELF]' method='post'>";
echo "<input type='text' name='description'>";
echo "<input type='submit' name='submit' value='Submit Information'>";
echo "</form>";

$description = $_POST['description'];

$query="UPDATE ft_form_{$form_id} SET description='$description'";
mysql_query($query);
mysql_close();

}
################## END NEW CODE ##########################

echo "<hr size=\"1\" />"; // just for display purposes to separate the entries

}


for($i = 1; $i <= $total_pages; $i++) { if($i == $page_number) { print "$i "; } 
  else { print "<a href='search.php?city=$city_location&page_number=$i'>$i</a>"; } 

}


if (isset($_POST["submit"]) AND mysql_num_rows($result) == 0) { 

  
echo "Sorry, there have been no submissions in this location.";
  
}

 

The function works as in it shows up if that particular field is empty but I need two things from this:

 

1. To find any empty field not just the one I have as shown above if possible.

2. To actually be able to update the table if that information is submitted.

 

Seems I'm hitting a brick wall again. Only thing that happens when new information is submitted is the default "Sorry, there have been no submissions in this location." message is shown. I know I'm missing something simple I just don't know what.

 

Again, thanks for all of your help guys.

Link to comment
Share on other sites

Sorry for reposting, but I wasn't able to modify my original post.

 

For this section of code:

$description = $_POST['description'];

$query="UPDATE ft_form_{$form_id} SET description='$description'";
mysql_query($query);
mysql_close();

 

I'm actually using this:

if (isset($_POST[submit])) {
$form_id = 24;
$id = $row["submission_id"];
$new_description = $_POST["description"];

$query = "UPDATE `ft_form_{$form_id} SET `description` = '{$new_description}' WHERE submission_id = '{$id}'";
mysql_query($query);

}

Link to comment
Share on other sites

Actually, let me reword this.

 

I know the syntax for modifying a database. What I want to know is, which one is better to use for the visitor side if I created a function that only shows a form based on an empty field, would it be UPDATE or INSERT?

 

What I'm trying to accomplish is, after everything prints (which it does) if there is a field that is empty (say for example:

  echo "<tr><td><b>GPS Data:</b> {$row['col_5']}</td></tr>";

), I am trying to successfully allow a user to enter that data and update the database with the submitted code.

 

Hope that makes sense. I just need a fresh pair of eyes to help me distinguish where I'm going wrong.

 

Thanks

 

 

Link to comment
Share on other sites

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.