Jump to content

Breaking records into pages


Guest

Recommended Posts

Hi there,

 

I am trying to break my records into pages so that when there will be lots of records, they won't appear in the same page.

 

Right now, my code does not allow me that but I have all my subjects displayed in a list and each one has its own link. I don't have a lot of records so it's not really a problem. Here is the code:

 

<?php

$subject_set = get_all_subjects();

echo "<ul>";

while ($subject = mysql_fetch_array($subject_set)) {

echo "<li";

if ($subject["id"] == $sel_subject) {echo " class=\"selected\"";}

echo "><a href=\"edit_subject.php?subj=" . urlencode($subject["id"]) .

"\"> {$subject["menu_name"]}</a></li>" . "<br />";

}

echo "</ul>";

?>

 

I have found a code to break the records into pages an managed to make my records appear in the page and break poperly, but I have some issues. The first one is that it is no longer a list and they are not displayed nicely. The second is that I no longer have the link for each of them like before. Here is the code:

 

<?php

 

$perpage = 5;

 

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

$page = max(intval($_GET['page'] - 1), 0);

$page_original = intval($_GET['page']);

}

else {

$page = 0;

$page_original = 1;

}

 

$start_at = $page * $perpage;

$start_at_public = $start_at + 1;

 

$query = 'SELECT COUNT(*)

FROM `subjects`';

$result = mysql_query($query) or die('Error in query: ' . $query);

$total_entries = mysql_result($result, 0);

 

// You've asked for a page that is too high

if ($total_entries < $start_at_public) {

die('Not enough entries to go that high.');

}

 

$query2 = "SELECT `menu_name`

FROM `subjects`

ORDER BY `id` DESC

LIMIT $start_at, $perpage";

$result2 = mysql_query($query2);

 

while ($entries = mysql_fetch_object($result2)) {

print '<div>' . "\n";

print '<p>' . $entries->menu_name . '</p>' . "\n";

print '</div>' . "\n";

}

 

$end_value_this_page = min($start_at + $perpage, $total_entries);

 

if (($end_value_this_page == $total_entries) && ($start_at_public == $total_entries)) {

print '<p>Entry ' . $end_value_this_page . ' of ' . $total_entries . '</p>' . "\n";

}

 

else {

print '<p>Entries ' . ($start_at_public) . '-' . $end_value_this_page . ' of ' . $total_entries . '</p>' . "\n";

}

 

$total_pages = ceil($total_entries / $perpage);

 

if ($total_pages != 1) {

 

print '<hr />' . "\n";

 

print '<div class="paginator">' . "\n";

 

$next_link = '';

$previous_link = '';

$page_links = '';

 

 

for ($page_count = 1; $page_count <= $total_pages; ++$page_count) {

 

if ($page_original == $page_count) {

 

// We're on this page, so no link needed

$page_links .= $page_count;

 

// Get the "previous" link if there is more than one page and if this isn't the first page

if ($total_pages > 1 && $page_count != 1) {

$previous_link = '<a href="?page=' . ($page_count - 1) . '"><</a> | ';

}

 

if ($page_count != $total_pages) {

$next_link = ' <a href="?page=' . ($page_count + 1) . '">></a>';

}

}

 

else {

$page_links .= '<a href="?page=' . $page_count . '">' . $page_count . '</a>';

        }

 

if ($page_count != $total_pages) {

$page_links .= ' | ';

}

}

print $previous_link . $page_links . $next_link . '</div>';

}

?>

 

Thanks in advance if you can help me.

Link to comment
https://forums.phpfreaks.com/topic/177774-breaking-records-into-pages/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.