I'm struggling with pagination with 2 get variables - 1 of which being the page number.
My second $_GET variable I have is $_GET['cat'] which is getting the category of products I want to filter on.
Everything works with the pagination DISPLAY, number of pages etc., however, if I am filtered on a particular category, the first page of results displays fine, but when i try to get to the next page or any page number, nothing is displayed product wise.
Any help would be appreciated, please see code below.
<?php include("header.php");
echo '<div id="content_header"><center><h1> • Products • </h1></center></div>';
echo '<div id="products_left">
Show:<br />
<ul style="list-style-type:none; margin:0; padding:0;">
<li>Wedding Stationery</li>
<li>Frames</li>
<li>Nappy Cakes</li>
<li>Miscellaneous</li>
<li>All</li>
</ul>
</div>';
echo '<div id="clear"></div>';
echo '<div style="padding-left:150px; margin-top:-70px">';
$connect = mysqli_connect("localhost","root","") or die("Failed to connect to database");
mysqli_select_db($connect,"magical_moments") or die("Unable to find database");
if(empty($_GET['cat'])){
$query = mysqli_query($connect,"SELECT * FROM products");
} else {
$cat = $_GET['cat'];
$query = mysqli_query($connect,"SELECT * FROM products WHERE type='$cat'");
};
$products = mysqli_fetch_array($query);
$start = 0;
$limit = 9;
if(isset($_GET['page']))
{
$pid=$_GET['page'];
$start=($pid-1)*$limit;
}
else{
$pid=1;
}
//Fetch from database first 10 items which is its limit. For that when page open you can see first 10 items.
if(empty($_GET['cat'])){
$query = mysqli_query($connect,"SELECT * FROM products LIMIT $start, $limit");
} else {
$cat = $_GET['cat'];
$query = mysqli_query($connect,"SELECT * FROM products WHERE type='$cat' LIMIT $start, $limit");
};
?>
<ul>
<?php
//print 10 items
while($result=mysqli_fetch_array($query))
{
echo '<div id="display_product">
<table>
<tr>
<td><div id="product_image_holder" style="background-image:url('.$result[5].')"></div></td>
</tr>
<tr>
<td>'.$result[1].'</td>
</tr>
<tr>
<td>'.$result[3].'</td>
</tr>
<tr>
<td>£ '.$result[4].'</td>
</tr>
</table>
</div>';
}
echo '<div id="clear"></div>';
?>
</ul>
<?php
//fetch all the data from database.
echo '<div id="pagination_div">';
if(empty($_GET['cat'])) {
$rows=mysqli_num_rows(mysqli_query($connect,"SELECT * FROM products"));
//calculate total page number for the given table in the database
$total=ceil($rows/$limit);
if($pid>1)
{
//Go to previous page to show previous 10 items. If its in page 1 then it is inactive
echo "<a href='?page=".($pid-1)."' class='previous_button'>PREVIOUS</a>";
}
if($pid!=$total)
{
////Go to previous page to show next 10 items.
echo "<a href='?page=".($pid+1)."' class='next_button'>NEXT</a>";
}
//show all the page link with page number. When click on these numbers go to particular page.
for($i=1;$i<=$total;$i++)
{
if($i==$pid) { echo "<li class='current'>".$i."</li>"; }
else { echo "<li class='pagination'><a href='?page=".$i."'>".$i."</a></li>"; }
}
echo '</div>';
} else {
$rows=mysqli_num_rows(mysqli_query($connect,"SELECT * FROM products WHERE type='$cat'"));
//calculate total page number for the given table in the database
$total=ceil($rows/$limit);
// cat is set - pagination for cat + page
if($pid>1)
{
//Go to previous page to show previous 10 items. If its in page 1 then it is inactive
echo "<a href=".$_SERVER['REQUEST_URI']."?page=".($pid-1)." class='previous_button'>PREVIOUS</a>";
}
if($pid!=$total)
{
////Go to previous page to show next 10 items.
echo "<a href=".$_SERVER['REQUEST_URI']."?page=".($pid+1)." class='next_button'>NEXT</a>";
}
//show all the page link with page number. When click on these numbers go to particular page.
for($i=1;$i<=$total;$i++)
{
if($i==$pid) { echo "<li class='current'>".$i."</li>"; }
else { echo "<li class='pagination'>".$i."</li>"; }
}
echo '</div>';
};
?>
</ul>
</div>
<?php include("footer.php"); ?>
Well, you define the page parameter. But the category parameter is lost once clicked on a link because it's simply not there.
You've got this:
echo "<li class='pagination'>".$i."</li>";
You lack the parameter cat. Here's an example:
echo '<li class="pagination">'.$i.'</li>';
Related
I have a top_menu and a almenu named table. I want a simple dropdown menu by the menu id-s. The problem with this code, that it echo-s out the "Információk" menuitem 3 times, and puts into them the 3 menuitem from the almenu table.
The code does now: (It puts out 3 times the Információk menuitem from the top_menu table as new menuitems in the navbar)
Informáciok (Alapadatok), Informáciok (Kollégium), Informáciok (Osztályok)
And not: Informáciok (Alapadatok, Kollégium, Osztályok) (This is what i want)
<?php
$menu_sql =
"
SELECT
top_menu.top_menu_nev,
top_menu.top_menu_seo,
top_menu.top_menu_dropdown,
almenu.almenu_nev,
almenu.almenu_seo
FROM top_menu LEFT JOIN almenu ON top_menu.top_menu_id = almenu.almenu_parent
WHERE top_menu.menu_status = 1 AND top_menu.menu_position = 1
ORDER BY top_menu.top_menu_sorrend ASC
";
$get_menu = mysqli_query($kapcs, $menu_sql) or die(mysqli_error($kapcs));
while($top_menu = mysqli_fetch_assoc($get_menu))
{
if($top_menu['top_menu_dropdown'] == 0 )
{
echo '<li>'.$top_menu['top_menu_nev'].'</li>';
}
else
{
echo '<li class="dropdown">';
echo ''.$top_menu['top_menu_nev'].'';
echo '<ul class="dropdown-menu">';
echo '<li>'.$top_menu['almenu_nev'].'</li>';
echo '</ul>';
echo '</li>';
}
}
?>
The Problem is that you loop thru the whole nav over and over again.
The following code lets you generate new li items:
echo '<li class="dropdown">';
echo ''.$top_menu['top_menu_nev'].'';
echo '<ul class="dropdown-menu">';
$get_menu = mysqli_query($kapcs, $menu_sql) or die(mysqli_error($kapcs));
while($top_menu = mysqli_fetch_assoc($get_menu))
{
if($top_menu['top_menu_dropdown'] == 0 )
{
echo '<li>'.$top_menu['top_menu_nev'].'</li>';
}
else
{
echo '<li>'.$top_menu['almenu_nev'].'</li>';
}
}
echo '</ul>';
echo '</li>';
Here Is my question: What I am wanting To do is Take Results from a mysql table and turn them into a menu and a drop down menu
HERE IS A QUICK EXAMPLE:
if you see in my mysql table i have page_name and parent, So the example is:
page_name and if i have row 1 the page_name is 'Home' now it's parent is 'none' right but on id number 39 the page_name is 'Contact Us' and the Parent Is 'Far Far Away 123' so if the parent is equal to 'none' then it will show at the top of the menu not the drop down if it has a parent it will show under that parent like this:
Home | the ben page | The Brock Page | Far Far Away 123 | dsfk
Contact Us
You see Contact Us is under Far Far Away Because the parent Is Far Far Away 123
here is my table:
Here is my code That I am trying but it is not working for some reason:
<ul>
<?php
$sql = "SELECT * FROM pages ORDER by item_order";
$result = mysqli_query($db, $sql);
confirm_query($result);
while ($links = mysqli_fetch_assoc($result)) {
if($links['parent'] !== "none") {
?>
<li id = "<?php echo $links['id']; ?>"><a href="
<?php
echo "page.php?id=" . $links['id'] . "\" title=\"" . $links['page_title'] . "\"";
?>>
<?php
echo $links['page_name'];
?>
</a>
<?php
if($links['parent'] !== "none") {
$child = "";
$sql = "SELECT * FROM pages";
$result = mysqli_query($db, $sql);
while($row = mysqli_fetch_assoc($result)) {
if($row['parent'] !== "none") {
$child = $row['page_name'];
}
}
echo "<ul id=\"sub_menu\" class=\"sub_navagation" . $links['id'] . "\">";
echo "<li>";
echo $child;
echo "<li>";
echo "</ul>";
}
?>
</li>
<?php
}
}
?>
</ul>
CSS:
#sub_menu {
display: none;
}
#sub_menu:hover {
display: block;
}
Ok if as you can see i have the parent row in the MYSQL table and on id number 39 i want the 'Far Far Away123' to be the parent of Contact Us and i want to show it when i hover over 'Far Far Away123'
My suggestion is to build out an array of all the results. Then run through that array (instead of multiple database queries).
I added a function build_dropdown() that will take the page name and run through the array of pages to see if there are any items with a parent matching. If so, we make an array of those items and run through them to build the dropdown menu. If not, it does nothing and moves on to the next menu item.
<?php
function build_dropdown ($parent, $pages){
foreach($pages as $page){
if($page['parent'] == $parent){
$items = $page;
} // END if
} // END foreach
if(is_array($items)){ // If a sub
echo '<ul id="sub_menu" class="sub_navagation'. $item['id'] .'">';
foreach($items as $item){
echo '<li>'.$item['name'].'<li>';
} // END foreach
echo '</ul>';
} // END if
}
$sql = "SELECT * FROM pages ORDER by item_order";
$result = mysqli_query($db, $sql);
confirm_query($result);
while ($row = mysqli_fetch_assoc($result)) {
$pages[] = $row; // Add each row to $pages array to use later
}
foreach($pages as $key => $page){
if($page['parent'] == 'none'){ ?>
<li id = "<?php echo $page['id']; ?>">
<a href="page.php?id=<?php echo $page['id']; ?>" title="<?php echo $page['page_title']; ?>">
<?php echo $page['page_name']; ?>
</a>
<?php
build_dropdown($page['page_name'], $pages); // If there are child items then build them out
?>
</li>
<?php
} // END if
} // END foreach
?>
I suggest you will need to JOIN your table to basically query it again to get the parent value, and add that to your markup.
SELECT *
FROM Pages
LEFT JOIN Pages p2 on page_name = p2.parent
(note: the syntax above may not be right, but I wanted to give you an idea of where I would start).
i want my script to add a closing div tag after every fourth entrie of the db. i tried something like this:
<div class="row">
$ergebnis = $mysqli->query("SELECT name FROM pages Where city = '1';");
while($zeile = $ergebnis->fetch_array()) {
echo "<div class=\"col-sm-4 col-md-3\">
echo "<h3>".$zeile['name']."</h3>";
..
echo "</div>";
$i=0;
i++;
if ($i == 4){
echo "</div>";}
}
?>
would be great if you can help me here. thx
1) Get names of all cities
2) Initiate an indexing variable
3) Ieterate through the loop
4) Open <div class="row"> when $i==0 (Very First time) OR $i%4==0 (When fifth entry is to be printed). (Remember you are using 0 based indexing i.e. initializing $i=0).
5) Close the div tag for <div class="row"> when fourth city name has been printed i.e. $i%3==0 (Remember you are using 0 based indexing i.e. initializing $i=0).
Here's the code:
<?php
$ergebnis = $mysqli->query("SELECT name FROM pages Where city = '1';");
$i=0;
while($zeile = $ergebnis->fetch_array()) {
if ($i == 0 || $i%4==0){ // <div class="row"> opens on first entry and every fifth entry
echo "<div class=\"row\">";
}
echo "<div class=\"col-sm-4 col-md-3\">";
echo "<h3>".$zeile['name']."</h3>";
/*
Rest of your code
*/
echo "</div>";
$i++;
if ($i % 3 == 0){
echo "</div>"; // <div class="row"> closes here on every fourth entry
}
}
?>
Try this code,
<div class="row">
$ergebnis = $mysqli->query("SELECT name FROM pages Where city = '1';");
$i=0;
while($zeile = $ergebnis->fetch_array()) {
echo "<div class=\"col-sm-4 col-md-3\">
echo "<h3>".$zeile['name']."</h3>";
..
echo "</div>";
$i++;
if ($i == 4){
echo "</div>";
$i = 0;
}
}
?>
I'm having trouble with my site.
I have a photographs table with id's. THis table is populated when a user uploads a file to the server, which is fine. I also have a forum where images can be posted, and the images are sent to the same photograph table. On the homepage there is a section where one of the photographs from this table shows up with Pagination links below it.
Now when someone posts to the forum and they do not include an image it breaks. That thread won't show up because it's looking for the photo_id of 0, which didn't exist. So I uploaded a dummy image, changed the id value to zero and tried
$sql = "SELECT * FROM photographs HAVING id != 0 ";
$sql .= "LIMIT {$per_page} ";
$sql .= "OFFSET {$pagination->offset()}";
Which worked, the image no longer shows up, but the pagination still thinks that there is a record there. It shows a link for that image and when a user clicks on it it breaks and removes a bunch of other links and it's a mess.
Is it possible to Select everything from the database but not the 0 record, or maybe remove the link from the pagination links? Here is the code:
//1. the current page number ($current_page)
$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
//2. records per page ($per_page)
$per_page = 1;
//3. total record count ($total_count)
$total_count = Photograph::count_all();
//Find all photos
//use Pagination instead
//$photos= Photograph::find_all();
$pagination = new Pagination($page, $per_page, $total_count);
//Instead of finding all records, just find the records
//for this page
$sql = "SELECT * FROM photographs HAVING id != 0 ";
$sql .= "LIMIT {$per_page} ";
$sql .= "OFFSET {$pagination->offset()}";
$photos = Photograph::find_by_sql($sql);
//Need to add ?page=$page to all links we want to
//maintain the current page(or store $page in $session)
?>
<div id="right">
<?php
foreach($photos as $photo):
?>
<h3 style="margin: 0;"></h3>
<div style="padding: 5px;">
<img src="<?php echo $photo->image_path(); ?>" width="200" alt="Photo Share Photo" />
<p align="center"><?php echo $photo->caption; ?></p>
</div>
<?php }
endforeach;
?>
<div id="pagination" style="clear: both;" align="center">
<?php
if($pagination->total_pages() > 1) {
if($pagination->has_previous_page()) {
echo "<a href=\"index.php?page=";
echo $pagination->previous_page();
echo "\">« Previous</a> ";
}
for($i=1; $i <= $pagination->total_pages(); $i++) {
if($i == $page) {
echo " <span class=\"selected\">{$i}</span> ";
} else {
echo " {$i} ";
}
}
if($pagination->has_next_page()) {
echo " <a href=\"index.php?page=";
echo $pagination->next_page();
echo "\">Next »</a> ";
}
}
?><br />
<div align="center">
Upload a new photograph
</div>
[EDIT]
Pagination code.
class Pagination {
public $current_page;
public $per_page;
public $total_count;
public function __construct($page=1, $per_page=20, $total_count=0){
$this->current_page = (int)$page;
$this->per_page = (int)$per_page;
$this->total_count = (int)$total_count;
}
public function offset() {
//Assuming 20 items per page:
//page 1 has an offset of 0 (1-1) * 20
// page 2 has an offset of 20 (2-1) * 20
//in other words, page 2 starts with item 21
return ($this->current_page - 1) * $this->per_page;
}
public function total_pages() {
return ceil($this->total_count/$this->per_page);
}
public function previous_page() {
return $this->current_page - 1;
}
public function next_page() {
return $this->current_page +1;
}
public function has_previous_page() {
return $this->previous_page() >= 1 ? true : false;
}
public function has_next_page() {
return $this->next_page() <= $this->total_pages() ? true : false;
}
}
I will look into the total_pages and get back to you if it works. I think I can just add WHERE id != 0 to the end of the count_all method. I don't think anything else uses it other than the photo share. I'll have to look into the code.
Thanks for your help with this.
DC
I simply added:
public static function count_all() {
global $database;
$sql = "SELECT COUNT(*) FROM ".self::$table_name." WHERE `id` **!= 0 AND `show` != 0";**
$result_set = $database->query($sql);
$row = $database->fetch_array($result_set);
return array_shift($row);
}
I added this to the photograph object. Now users can select whether or not that want their image to show in the photo sharing section, and if they don't upload an image with their post then it is automatically assigned the ID of 0. If this isn't clear and someone needs help with it, let me know.
As G-Nugget mentioned. NULL is better suited for this.
Your query also looks odd. The usual syntax is:
SELECT * FROM photographs WHERE id != 0
Having is used to apply filtering on GROUP BY queries.
Edit:
It also looks like you are using $total_count from pagination here:
$total_count = Photograph::count_all();
<snip>
for($i=1; $i <= $pagination->total_pages(); $i++) {
if($i == $page) {
echo " <span class=\"selected\">{$i}</span> ";
} else {
echo " {$i} ";
}
This will show a link for more pages than exist since $total_count includes the 0 record.
I have a section in my footer from a script, that I am trying to customize and that displays the website categories.
Currently the results were limited to 9 results per column and continuing onto x amounts of columns. I need to be able to stop at 2 columns.
Here is the code I have
<div class="left">
<h3 style="padding-top:15px;"><?php echo BROWSE_CATEGORIES; ?></h3>
<?php $footer_category= $this->home_model->get_category(); ?>
<?php if($footer_category) { $ftr_cnt=1; ?>
<ul>
<?php foreach($footer_category as $cat) {
echo '<li>'.anchor('search/category/'.$cat->project_category_id,substr($cat->project_category_name,0,30)).'</li>';
if($ftr_cnt>9) { $ftr_cnt=1; echo "</ul><ul>"; }
$ftr_cnt++;
}
} ?>
</ul>
</div>
Use a break when you've reached 9 * 2, where 2 is the amount of columns you want.
$ftr_cnt = 1;
foreach($footer_category as $cat)
{
echo '<li>'.anchor('search/category/'.$cat->project_category_id,substr($cat->project_category_name,0,30)).'</li>';
if($ftr_cnt == 9)
{
echo "</ul><ul>";
}
$ftr_cnt++;
if($ftr_cnt >= (9 * 2))
{
echo "</ul>";
break;
}
}