Make a pagination for cards php - php

I have these cards with data that comes from a database. This is the reason it is in a while loop.
Now my problem is that I don't have a clue how to add pagination to this. If some of you guys have any suggestions or answers please let me know.
$start_from = ($page - 1)*$record_per_page;
$query = "SELECT organisation_name, organisation_logo, organisation_url FROM `organisations`";
$result = mysqli_query($conn, $query);
if ($result ->num_rows > 0) {
echo '<div id="myItems">';
echo '<div class="row">';
while ($row = $result-> fetch_assoc()) {
echo '
<div class="col-md-6">
<div class="card rounded-lg mt-3">
<div class="card-body p-0 shadow">
<div class="row">
<div class="col-md-4 p-0">
<img src="../img/';
if ($row['organisation_logo'] == '') {
echo 'stock/no-image-icon.png"';
} else {
echo 'uploads/'.$row['organisation_logo'].'"';
}
echo 'class="border p-3" style=" width: 150px; height: 150px;">
</div>
<div class="col-md-8">
<h3 class="card-title mt-3">'.$row["organisation_name"].' <a class="text-dark" href="http://'.$row["organisation_url"].'" target="_blank"><i class="fas fa-external-link-alt" style="font-size: 18px;"></i></a></h3>
<p>Current active surveys: <b>0</b></p>
<b>View <i class="fas fa-arrow-circle-right"></i></b>
</div>
</div>
</div>
</div>
</div>';
}
echo '</div>';
}
else {
echo 'nothing here';
}

You need to use LIMIT and OFFSET functionality in your SQL query. Limit determines how many elements you want to show on one site. Offset is point from where you want to get your items. So, for example – if You want 5 items, and you are on page 3, you have to add LIMIT 5 OFFSET 10. You calculate offset with (page_number-1) * limit equation. If you want to count how many pages you have, you must get total pages number, and use something like $pages = ceil($total / $limit). There are a lot of examples on stackoverflow.
I think this one can be very helpful – Simple PHP Pagination script
You also don't need to do all the calculation on your own, there are plany of pagination library for PHP, check look for them.
Hope it helps you a little.

Related

Footer is being affected by bootstrap c;ass

I am currently working on the store page for my website and it seems that I have come across a problem. I have created a search function for my website. In the search.php page, however, where items are displayed according to the search, a bootstrap class seems to be interfering with my footer. When I remove the class the footer fits fine in the page but the product cards get messed up. When I add the class back in my footer shrinks and distorts. What can I do in order for that not to happen?
Here is my code:
search.php:
while($row = $stmt->fetch()){
$stock = $row['stock'];
if ($stock > 0){
echo '
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3">
<form method="post" action="view_product.php?action=add&id='.$row["ID"].'">
<div class="products">
<a href="view_product.php?product='.$row["ID"].'">
<img src="'.$row["image"].'" class="product-img">
</a>';
echo' <div class="product-txt" style="padding: 5px;">
<h4 style="line-height: 10px;">
<a href="view_product.php?product='.$row['ID'].'">
<b>'.$row['name'].'</b>
</a>
<br>
<h4>';
if($stock > 0){
echo ''.$row["price"].'';}
else{
echo ''.$row['outofstock'].'';
}
echo' </h4>
</h4>
</div>
</div>
</form>
</div>
</body>
';
}
}
include 'footer.php';
The "col-xs-12 col-sm-6 ...." class is interfering with my footer. What can I do in order ti fix this. Thanks in advance!
Here are your bugs:
echo' </h4>
</h4>
</div>
</div>
</form>
</div>
</body>
';
}
}
include 'footer.php';
First - double closing </h4>, you cannot have <h4> inside <h4>, but the real problem is including text of footer after closing </body>.
This happens sometimes, but you'd better use a validator w3c
update
That's how it should go
while($row = $stmt->fetch()){
$stock = $row['stock'];
if ($stock > 0){ echo
"<div class=\"col-xs-12 col-sm-6 col-md-4 col-lg-3\">\r\n
<form method=\"post\" action=\"view_product.php?action=add&id=$row['ID']\">
<div class=\"products\">\r\n
<img src=\"$row['image']\" class=\"product-img\"></div>\r\n
<div class=\"product-txt\" style=\"padding: 5px\">\r\n
<h4 style=\"line-height: 10px\">
<b>$row['name']</b>
$row['price']
</h4></div></form></div>\r\n";}
else{ echo "<p>$row['outofstock']</p>\r\n";}}
include './footer.php';
echo'</body></html>';
The point is, you have to control your loops and HTML tags - what is open must be closed in time. If you get lost, take a look at the source code in your browser. And use the validator w3c like the best designers do.

How to auto count previous 6 months annoucement record in SQL put in the separate box?

I have facing big problem to auto count previous 6 months annoucement record in SQL put in the separate box. Is using loop to count the annoucement record? Anyone can guide me or give me an example to do it? If can, better using my code to edit and let me refer. Thanks a lot.
Below is my coding:
<?php
$sql_select = 'SELECT * FROM announcement where id = 20' ; //This I try to test id = 20 announcment to call out data
$query_select = db_conn_select($sql_select);
foreach ($query_select as $rs_select) {
$title = $rs_select['title'];
$date = $rs_select['posted_date'];
$contents = $rs_select['contents'];
}
?>
<div class="row">
<div class="col-md-12">
<div class="box box-success box-solid">
<div class="box-header with-border" style="text-align: center;" >
<h3 class="box-title" ><?php echo $title ?> (<?php echo $date ?>)</h3>
<div class="box-tools pull-right">
<button type="button" class="btn btn-default btn-xs collapse-box" data-toggle="collapse" data-target="#collapseExample1" aria-expanded="false" aria-controls="collapseExample"><i class="fa fa-minus"></i></button>
</div>
</div>
<div id="collapseExample1" class="collapse" style="overflow: auto; text-align: center;">
<?php echo $contents ?>
</div>
</div>
</div>
</div>
Below is my database information (I want get the posted_date previous 6 months annoucement record to show):
My coding output show like the below:
Actually I want the output like below show auto count previous 6 months announcements put in the separate box :
There are two problems:
1) Your query will only return one record. You need to change it so it fetches a set of records from the last 6 months.
The general pattern for finding the last 6 months is easy to find online
yourdatecolumn >= CURDATE() - INTERVAL 6 MONTH
(credit to this answer specifically in this case, but there are dozens of examples available). N.B. You didn't specify, but I'm assuming your DBMS is MySQL, since that's most commonly used with PHP. If you have a different DBMS, you can find an equivalent answer online with a few seconds of searching.
2) you're ending your loop too soon. The current code is ok for one record, but will fail when there is more than one. It will loop over all the records, but then keep assigning their values the same variables, so after the loop finishes you end up with just the last record's values inside those variables. And you are not using a loop to create the HTML multiple times.
To fix that, just bring the HTML inside the loop. Here's the final version:
<?php
$sql_select = 'SELECT * FROM announcement where posted_date >= CURDATE() - INTERVAL 6 MONTH';
$query_select = db_conn_select($sql_select);
foreach ($query_select as $rs_select) {
$title = $rs_select['title'];
$date = $rs_select['posted_date'];
$contents = $rs_select['contents'];
?>
<div class="row">
<div class="col-md-12">
<div class="box box-success box-solid">
<div class="box-header with-border" style="text-align: center;">
<h3 class="box-title">
<?php echo $title ?> (
<?php echo $date ?>)</h3>
<div class="box-tools pull-right">
<button type="button" class="btn btn-default btn-xs collapse-box" data-toggle="collapse" data-target="#collapseExample1" aria-expanded="false" aria-controls="collapseExample"><i class="fa fa-minus"></i></button>
</div>
</div>
<div id="collapseExample1" class="collapse" style="overflow: auto; text-align: center;">
<?php echo $contents ?>
</div>
</div>
</div>
</div>
<?php
}
?>
That way it will create the same HTML repeatedly for each record, and use the current values of your variables within each version.

Add a unique style class to each mySQL entry on page?

I have a php function to retrieve the three most recent entries to a table that holds news highlights. Here is my function:
function getHighlights(){
return $this->query("
SELECT *
FROM `highlights`
ORDER BY `inserted` DESC
LIMIT 3
");
}
These highlights are then placed on my homepage via a foreach loop. Here is my code:
<?php foreach($highlights as $a){ ?>
<div class="col-md-6 highlight">
<div class="highlightItem">
<img class="highlight-backdrop" src="<?=$a->backdrop;?>">
<p class="highlight-title"><?=$a->title;?></p>
</div>
</div>
<?php } ?>
I'd like for my homepage to ALWAYS be in this format:
News Highlight Format. However, the only reason it's in that format right now is because the images have predefined sizes that fit nicely together.
I want to be able to reference the most recent news highlight and set that image to always display as 300 x 210. I also want to reference the next two highlights and set them to always display as 300 x 100.
What's the best course of action for this?
<?php foreach($highlights as $index => $a){ ?>
if($index == 0){
<div class="col-md-6 highlight">
<div class="highlightItem">
<img width="300" height="210" class="highlight-backdrop" src="<?=$a->backdrop;?>">
<p class="highlight-title"><?=$a->title;?></p>
</div>
</div>
}else{
<div class="col-md-6 highlight">
<div class="highlightItem">
<img width="300" height="100" class="highlight-backdrop" src="<?=$a->backdrop;?>">
<p class="highlight-title"><?=$a->title;?></p>
</div>
</div>
}
<?php } ?>
try to use that code

Php search function displaying null

Hi guys so i am just using learning php for the first time and building my own site etc to try it out. I have a database of recipes. For each recipe it has a list of ingredients. Each recipe will have different amounts. So one will have 5, the other can have 3 etc. The problem with my code is. If someone searches for a recipe and they find it, it will return the ingredients but sometimes if it has more than the divs i put there it will give some null values back. Also i understand about the sql injections etc and the bad practice but i am just playing about with it first. I want to get it to work and then fix that part later :)
PHP:
<div class="panel panel-default">
<div class="panel-heading"><b>' . htmlentities($rN, ENT_QUOTES) . '</b></div>
</a>
}
?>
Now i am pretty sure in my white loop i am suppose to do an if statement after the div tags and say if the value == null then dont display but i have tried and nothing has worked so any help on this matter would be great
Thanks
Your HTML for ingredients seems be repeating, so you can resolve the empty <div> issue and short your code using a for loop and a if condition:
$output .= '<div class="panel panel-default">
(...)
<h3 class="media-heading">INGREDIENTS:</h3>';
for( $i=1; $i < 7; $i++ )
{
if( ${"rI$i"} )
{
$output .= '<div class="food-graph">
<span class="food-graph-title">' . htmlentities(${"rI$i"}, ENT_QUOTES) . '</span>
</div>';
}
}
$output .= ' (...) ';
If encountering null values is a problem, this will only print out the ingredients that your row contains.
while($row = mysql_fetch_array($query)) {
$rN = $row['recipeName'];
$i=1;
$recipes = '';
if (isset($row['recipe_ing'.$i]) {
while(isset($row['recipe_ing'.$i]) {
$value = htmlentities($row['recipe_ing'.$i], ENT_QUOTES);
$recipes .= <<< EOT
<div class="food-graph">
<span class="food-graph-title">$value</span>
</div>
EOT;
$i++;
}
$output .= <<< EOT
<div class="panel panel-default">
<div class="panel-heading"><b>' . htmlentities($rN, ENT_QUOTES) . '</b></div>
<div class="panel-body">
<div class="pull-left col-xs-12 col-sm-4">
<a href="#">
<img class="img-thumbnail img-responsive" src="Image/green.jpg">
</a>
<a class="btn btn-success btn-block btnrec" href="#">View Recipe</a>
</div>
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="media-body">
<h3 class="media-heading">INGREDIENTS:</h3>
$recipes
</div>
</div>
</div>
</div>
<div class="panel-footer">Rating</div>
</div>
EOT;
}
}
EDIT: Also, i'm using Heredocs to set the strings. the lines with EOT; must have no whitespace or code in front of it. it must be at the start of the line. also no code may be on the same line behind it. Else, your documents will become a huge string.

MySQL Calculate Percentage of Total Donations

I have a donation site and a little bit stuck on finding what percent of the Donation Goal that the Current Donations are at?
In my MySQLi database I have two columns - donation_goal (numerical eg. 500) and current_raised (numerical eg. 20.00).
What I need to do is find and display how much in percentage has been raised currently out of the donation goal. So if £10 was donated and the goal was £100 it would display 10%. But it would do this each time the page was loaded.
I display each page with the code below and need to find a way to add this feature to this code below:
<?php
$sql = "SELECT * FROM pages ORDER BY RAND() LIMIT 3";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
$page_name = $row['page_name'];
$count = strlen($page_name);
if($count < 27){
$space = '<br> <br/>';
} else {
$space = '';
}
$page_dp = $row['page_dp'];
$donation_goal = $row['donation_goal'];
$current_raised = $row['current_raised'];
?>
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="pageimg/<? echo $page_dp; ?>" style="width:348px;height:233px;" alt="Page Image">
<div class="caption">
<h3><? echo $page_name; ?> <? echo $space; ?></h3>
<div class="progress">
<div class="progress-bar progress-bar-success progress-bar-striped" role="progressbar" aria-valuenow="10" aria-valuemin="0" aria-valuemax="100" style="width: 10%">10%
<span class="sr-only">10% Complete (success)</span>
</div>
</div>
<p><b>£<? echo $donation_goal; ?>.00</b> Target | <b>£<? echo $current_raised; ?></b> Raised</p>
Here is a little info about this current topic, please help us raise more than we can! We need your help!
<p>Donate Now Details</p>
</div>
</div>
</div>
<?
}
?>
I have tried adding this code to the mix, but receiving errors so am pretty sure I have no idea how to add it correctly:
$sql = "SELECT * FROM pages, COUNT( current_raised ) AS test, concat(round(( current_raised/donation_goal * 100 ),2),'%') AS percentage ORDER BY id DESC LIMIT 3";
This just gave out errors. Any ideas of how to do this efficiently with short code?
If you would like to calculate the percentage of the dontation from the goal you could do this directly in your query:
SELECT page_dp,
donation_goal,
current_raised,
ROUND(current_raised/donation_goal*100) percentage
FROM pages
ORDER BY RAND()
LIMIT 3
Then you only need to output the percentage within your loop:
<span class="sr-only"><?php print $row['percentage']; ?>% Complete (success)</span>
Is this what you want?
SELECT p.*, pc.cr / donation_goal
FROM pages p CROSS JOIN
(SELECT SUM(current_raised) as cr FROM pages) pc
ORDER BY RAND()
LIMIT 3;

Categories