Three column layout keeps looping with the same data - php

I have some PHP code which pulls data from the database and places it in to a three column layout like below:
Column one column two column three
Now this works, the only problem is, the while loop keeps going and it repeats the same data over and over again. If i only have one entry in the database then this is repeated three times and i end up with one column of data. shown here :
http://www.gulfwarmemorial.co.uk/events.php
If I have three entries then the rows get repeated. I have tried various things but nothing seems to work.
$tableName="fundraising_event";
$targetpage = "events.php";
$limit = 6;
$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT *, SUBSTRING(event_details,1,200) AS event_details_short FROM $tableName LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}
$paginate.= "</div>";
}
?>
<?php while($row = mysql_fetch_array($result)){?>
<div class="row-fluid">
<article class="span4 mid">
<div class="img">
<img src="images/<?php echo $row['image_link']; ?>" alt="post4" />
<div class="overlay"></div>
</div>
<div class="info">
<p class="tags">
<?php echo $row['event_type']; ?>
<?php echo $row['location']; ?>
</p>
<h1><?php echo $row['event_title']; ?></h1>
<p class="details"> <?php echo $row['date']; ?> | <?php echo $row['posted_by']; ?></p>
<p class="text">
<?php echo $row['event_details_short']; ?>
</p>
</div>
</article>
<article class="span4 mid">
<div class="img">
<img src="images/<?php echo $row['image_link']; ?>" alt="post4" />
<div class="overlay"></div>
</div>
<div class="info">
<p class="tags">
<?php echo $row['event_type']; ?>
<?php echo $row['location']; ?>
</p>
<h1><?php echo $row['event_title']; ?></h1>
<p class="details"> <?php echo $row['date']; ?> | <?php echo $row['posted_by']; ?></p>
<p class="text">
<?php echo $row['event_details_short']; ?>
</p>
</div>
</article>
<article class="span4 mid">
<div class="img">
<img src="images/<?php echo $row['image_link']; ?>" alt="post4" />
<div class="overlay"></div>
</div>
<div class="info">
<p class="tags">
<?php echo $row['event_type']; ?>
<?php echo $row['location']; ?>
</p>
<h1><?php echo $row['event_title']; ?></h1>
<p class="details"> <?php echo $row['date']; ?> | <?php echo $row['posted_by']; ?></p>
<p class="text">
<?php echo $row['event_details_short']; ?>
</p>
</div>
</article>
</div>
<?php } ?>

The issue is that, in turn, you need to fill one event column with the current event from the database. So you need a 'selector for the next row to fill in the next event column.
You have a choice of filling each column in turn, Which is slightly more complex. Or filling the columns horizontally. Which this code attempts to do.
This is all untested code but hopefully shows the logic needed.
<?php $columnSelector = 0; // define a column selector ?>
<?php while($row = mysql_fetch_array($result)) :?>
<?php if ($columnSelector == 0): ?>
<div class="row-fluid">
<article class="span4 mid">
<div class="img">
<img src="images/<?php echo $row['image_link']; ?>" alt="post4" />
<div class="overlay"></div>
</div>
<div class="info">
<p class="tags">
<?php echo $row['event_type']; ?>
<?php echo $row['location']; ?>
</p>
<h1><?php echo $row['event_title']; ?></h1>
<p class="details"> <?php echo $row['date']; ?> | <?php echo $row['posted_by']; ?></p>
<p class="text">
<?php echo $row['event_details_short']; ?>
</p>
</div>
</article>
<?php elseif ($columnSelector == 1): ?>
<article class="span4 mid">
<div class="img">
<img src="images/<?php echo $row['image_link']; ?>" alt="post4" />
<div class="overlay"></div>
</div>
<div class="info">
<p class="tags">
<?php echo $row['event_type']; ?>
<?php echo $row['location']; ?>
</p>
<h1><?php echo $row['event_title']; ?></h1>
<p class="details"> <?php echo $row['date']; ?> | <?php echo $row['posted_by']; ?></p>
<p class="text">
<?php echo $row['event_details_short']; ?>
</p>
</div>
</article>
<?php elseif ($columnSelector == 2): ?>
<article class="span4 mid">
<div class="img">
<img src="images/<?php echo $row['image_link']; ?>" alt="post4" />
<div class="overlay"></div>
</div>
<div class="info">
<p class="tags">
<?php echo $row['event_type']; ?>
<?php echo $row['location']; ?>
</p>
<h1><?php echo $row['event_title']; ?></h1>
<p class="details"> <?php echo $row['date']; ?> | <?php echo $row['posted_by']; ?></p>
<p class="text">
<?php echo $row['event_details_short']; ?>
</p>
</div>
</article>
<?php endif; ?>
</div>
<?php $columnSelector++; // advance to the next row?>
<?php $columnSelector %= 3; // reset to zero every third event?>
<?php endwhile; ?>

Related

Pagination for search page

I want to make a pagination for search results. I can see the results in first page when I use the search box but I can't see the other results in other pages. What am I doing wrong?
I would be really glad if you could help me
Here' my search page codes.
Codes for pagination
<?php
if(isset($_GET["page"])) {
$page = $_GET["page"];
}else {
$page = "";
}
if($page == "" || $page == 1) {
$starter_post = 0;
} else {
$starter_post = ($page * 6) - 6;
}
$sql_query2 = "SELECT * FROM posts ";
$look_all_post = mysqli_query($conn, $sql_query2);
$all_post_count = mysqli_num_rows($look_all_post);
$page_number = ceil ($all_post_count / 6);
Codes for search results
if(isset($_POST["searchbtn"])) {
$search = $_POST["search"];
$query = "SELECT * FROM posts WHERE post_tags LIKE '%$search%' ORDER BY post_id DESC LIMIT $starter_post, 6";
$search_query = mysqli_query($conn, $query);
if(!$search_query) {
die("QUERY FAILED:". mysqli_error($conn));
}
$search_count = mysqli_num_rows($search_query);
if($search_count == 0) {
echo "<h3> No Result </h3>";
} else {
while ($row = mysqli_fetch_assoc($search_query)){
$post_id = $row["post_id"];
$post_date = $row["post_date"];
$date = strtotime($post_date);
$newdate = date("d/m/Y", $date);
$post_title = $row["post_title"];
$post_text = $row["post_text"];
$post_image = $row["post_image"];
?>
<div class="col-md-6">
<div class="blog">
<div class="blog-img ">
<img src="images/<?php echo $post_image; ?>" class="img-fluid" >
</div>
<div class="blog-content">
<ul class="blog-meta">
<li><i class="far fa-calendar-alt"></i><span class="writer"><?php
echo $newdate; ?></span></li>
</ul>
<h3><?php echo $post_title; ?></h3>
<p><?php echo $post_text; ?></p>
<div class="blog-content2 text-center">
</div>
</div>
</div>
</div>
<?php }
}
}
?>
Codes for pagination
</div>
<div class="row">
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center">
<li <?php if ($page == 1 or $page == "") {echo "class='page-item disabled'";} ?>>
<a class="page-link" href="search.php?page=<?php if ($page > 1) {echo $page - 1;} ?>">Previous</a>
</li>
<?php //Pagination Continue
for($i=1; $i<=$page_number; $i++) {
echo "<li class='page-item'><a class='page-link' href='search.php?page=$i'>{$i}</a></li>";
}
?>
<li <?php if ($page == $page_number) {echo "class='page-item disabled'";} ?>>
<a class="page-link" href="search.php?page=<?php if ($page == "") {echo $page = 2;} else if($page_number!=$page){echo $page+1;} else if($page_number==$page){echo $page;}?>">Next</a>
</li>
</ul>
</nav>
</div>

How to do I properly send variables using pagination?

I currently am having trouble using pagination. The first page is great, and the display on the second and third pages are correct, but the actual $_Get variables are not being carried over onto the second page and third page. Where would I put the actual $_Get Variables to go onto the next pages? Also I am using a form to pass the variables as a hidden input, that again work on the first page, but revert to empty on the second and third pages. I have read about decoupling and using sessions, but have tried implementing session variables at the top to no avail. How would I get the session to go to the second and third page. Thank you!
session_start();
include "dbhReal.inc.php";
include "header.php";
$timeSelected = $_GET['time'];
$dateSelected = $_GET['date'];
$ShownDate = date('M-d-y', strtotime("$dateSelected"));
$Null = '00:00:00';
<!--Form is below, left out the queries for space sake-->
echo '
<div class="col-sm-6">
<div class="card h-100">
<img class="card-img-top" src="../PhotoUploads/uploads/'.$userPic['Link1'].'" height="350" width="400" alt="anotherOne"/>
<div class="card-body">
<h5 class="card-title">'.$row['firstNameP'].' '.$row['lastNameP'].'</h5>
<p class="card-text">'.$row['briefDescription'].'</p>
<form action="settingTheSession.php" method="GET">
<input type="hidden" name="time" value='.$timeSelected.'>
<input type="hidden" name="date" value='.$dateSelected.'>
<input type="hidden" name="idNumber" value='.$boookingNumber.'>
<button type="submit" class="btn btn-primary" name="buttonBookprofileSearch">See Profile</button>
</form>
</div>
</div>
</div>';
<!--Pagnation-->
<ul class="pagination justify-content-center">
<li class="page-item" <?php if($page_no <= 1){ echo "class='page-item disabled'"; } ?>>
<a class='page-link' <?php if($page_no > 1){ echo "href='?page_no=$previous_page'"; } ?>>Previous</a>
</li>
<?php
if ($total_no_of_pages <= 10){
for ($counter = 1; $counter <= $total_no_of_pages; $counter++){
if ($counter == $page_no) {
echo "<li class='page-item active'><a class='page-link'>$counter</a></li>";
}else{
echo "<li class='page-item'><a class='page-link' href='?page_no=$counter'>$counter</a></li>";
}
}
}
elseif($total_no_of_pages > 10){
if($page_no <= 4) {
for ($counter = 1; $counter < 8; $counter++){
if ($counter == $page_no) {
echo "<li class='page-item active'><a class='page-link'>$counter</a></li>";
}else{
echo "<li class='page-item' ><a class='page-link' href='?page_no=$counter'>$counter</a></li>";
}
}
echo "<li class='page-item'><a>...</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=$second_last'>$second_last</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=$total_no_of_pages'>$total_no_of_pages</a></li>";
}
elseif($page_no > 4 && $page_no < $total_no_of_pages - 4) {
echo "<li class='page-item'><a class='page-link' href='?page_no=1'>1</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=2'>2</a></li>";
echo "<li class='page-item'><a class='page-link'>...</a></li>";
for ($counter = $page_no - $adjacents; $counter <= $page_no + $adjacents; $counter++) {
if ($counter == $page_no) {
echo "<li class='page-item active'><a class='page-link'counter</a></li>";
}else{
echo "<li class='page-item'><a class='page-link' href='?page_no=$counter'>$counter</a></li>";
}
}
echo "<li class='page-item'><a class='page-link'>...</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=$second_last'>$second_last</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=$total_no_of_pages'>$total_no_of_pages</a></li>";
}
else {
echo "<li class='page-item'><a class='page-link' href='?page_no=1'>1</a></li>";
echo "<li class='page-item'><a class='page-link' href='?page_no=2'>2</a></li>";
echo "<li class='page-item'><a class='page-link'>...</a></li>";
for ($counter = $total_no_of_pages - 6; $counter <= $total_no_of_pages; $counter++) {
if ($counter == $page_no) {
echo "<li class='page-item active'><a class='page-link'>$counter</a></li>";
}else{
echo "<li class='page-item'><a class='page-link' href='?page_no=$counter'>$counter</a></li>";
}
}
}
}
?>
<li class="page-item" <?php if($page_no >= $total_no_of_pages){ echo "class='disabled'"; } ?>>
<a class="page-link" <?php if($page_no < $total_no_of_pages) { echo "href='?page_no=$next_page'"; } ?>>Next</a>
</li>
<?php if($page_no < $total_no_of_pages){
echo "<li class='page-item'><a class='page-link' href='?page_no=$total_no_of_pages'>Last ››</a></li>";
} ?>
</ul>
<div class="text-center">
<strong>Page <?php echo $page_no." of ".$total_no_of_pages; ?></strong></div>
<br />
At first try not to reinvent the wheel. There are a lot of packages out there which deal with pagination: https://packagist.org/packages/voku/pagination?query=pagination
Secondly, considering your question, as PHP is server side you need to pass the needed data to every page/url you navigate.
So like the following example from your code:
echo "<li class='page-item'><a class='page-link' href='?page_no=1'>1</a></li>";
You will need the add the wanted arguments to the href query string. Like you are already doing with the 'page_no' parameter.
echo "<li class='page-item'><a class='page-link' href='?page_no=1&date=". $dateSelected ."'>1</a></li>";
In that manner you will have access to the wanted arguments on the next pages.

$_GET getting destroyed on Pagination

I am learning PHP. I am trying to use pagination in my one of page. If its first page, all is working fine but as soon as I press next page button its giving me error like
Notice: Undefined index: number1 in C:\xampp\htdocs\mycode\compare.php on line 6
Notice: Undefined index: number2 in C:\xampp\htdocs\mycode\compare.php on line 7
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\mycode\compare.php on line 18
I think its due to $_GET which I am using in line 6 and 7 but I do not know why its getting destroyed as soon as I try to go on next page. My full code is like below
<?php include("includes/header.php");
require("includes/function.php");
require("language/language.php");
$number1 = $_GET['number1'];
$number2 = $_GET['number2'];
$tableName="number_status";
$targetpage = "compare.php";
$limit = 10;
$sr=0;
$query = "SELECT COUNT(*) as num FROM $tableName WHERE number = $number1 OR number = $number2 ";
$total_pages = mysqli_fetch_array(mysqli_query($mysqli,$query));
$total_pages = $total_pages['num'];
$stages = 3;
$page=0;
if(isset($_GET['page'])){
$page = mysqli_real_escape_string($mysqli,$_GET['page']);
}
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
$quotes_qry="SELECT * FROM number_status WHERE number = $number1 OR number = $number2 ORDER BY id DESC LIMIT $start, $limit";
$result=mysqli_query($mysqli,$quotes_qry);
?>
<div class="row">
<div class="col-xs-12">
<div class="card mrg_bottom">
<div class="page_title_block">
<div class="col-md-5 col-xs-12">
<div class="page_title">Number Details</div>
</div>
</div>
<div class="clearfix"></div>
<div class="row mrg-top">
<div class="col-md-12">
<div class="col-md-12 col-sm-12">
<?php if(isset($_SESSION['msg'])){?>
<div class="alert alert-success alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<?php echo $client_lang[$_SESSION['msg']] ; ?></a> </div>
<?php unset($_SESSION['msg']);}?>
</div>
</div>
</div>
<div class="col-md-12 mrg-top">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>#SR</th>
<th>Number</th>
<th>Online Time</th>
<th>Offline Time</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
<?php
$i=0;
while($row=mysqli_fetch_array($result))
{
$number = $row['number'];
$start_time = "<span style='color:green;'>".$row['start_time']."</span>";
$end_time = "<span style='color:red'>".$row['end_time']."</span>";
$tmp = strtotime($row['end_time']) - strtotime($row['start_time']);
$duration = floor($tmp/3600) . ":" . floor($tmp/60) . ":" . ($tmp%60);
?>
<tr>
<td><?php echo ++$sr+$start;?></td>
<td><?php echo $number ?></td>
<td><?php echo nl2br($start_time);?></td>
<td><?php echo nl2br($end_time);?></td>
<td><?php echo nl2br ($duration); ?></td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
</div>
<div class="col-md-12 col-xs-12">
<div class="pagination_item_block">
<nav>
<?php if(!isset($_POST["quotes_search"])){ include("pagination.php");}?>
</nav>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php include("includes/footer.php");?>
My pagination.php is like this
<?php
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<ul class='pagination'>";
// Previous
if ($page > 1){
$paginate.= "<li><a href='$targetpage?page=$prev' aria-label='Previous'><span aria-hidden='true'>«</span></a></li>";
}else{
$paginate.= "<li><span aria-hidden='true'>«</span></li>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<li class='active'><span class='active'>$counter</span></li>";
}else{
$paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<li class='active'><span class='active'>$counter</span></li>";
}else{
$paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}
}
$paginate.= "<li>...</li>";
$paginate.= "<li><a href='$targetpage?page=$LastPagem1'>$LastPagem1</a></li>";
$paginate.= "<li><a href='$targetpage?page=$lastpage'>$lastpage</a></li>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<li><a href='$targetpage?page=1'>1</a></li>";
$paginate.= "<li><a href='$targetpage?page=2'>2</a></li>";
$paginate.= "<li>...</li>";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<li class='active'><span class='active'>$counter</span></li>";
}else{
$paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}
}
$paginate.= "<li>...</li>";
$paginate.= "<li><a href='$targetpage?page=$LastPagem1'>$LastPagem1</a></li>";
$paginate.= "<li><a href='$targetpage?page=$lastpage'>$lastpage</a></li>";
}
// End only hide early pages
else
{
$paginate.= "<li><a href='$targetpage?page=1'>1</a></li>";
$paginate.= "<li><a href='$targetpage?page=2'>2</a></li>";
$paginate.= "<li>...</li>";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<li class='active'><span class='active'>$counter</span></li>";
}else{
$paginate.= "<li><a href='$targetpage?page=$counter'>$counter</a></li>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<li><a href='$targetpage?page=$next' aria-label='Next'><span aria-hidden='true'>»</span></a></li>";
}else{
$paginate.= "<li><span aria-hidden='true'>»</span></li>";
}
$paginate.= "</ul>";
}
// pagination
echo $paginate;
?>
Let me know if someone can look and help me for get out of it.
Thanks
The 2 "notices" mean that the php page was called having omitted the "page" parameter in the query string (for this reason the $_GET['page'] causes an "undefined index" message). The last message means that the "mysqli_query($mysqli,$query)" has returned false, and you cannot use that result as argument to the "mysqli_fetch_array" (you should always check for errors when using DB functions). The query went wrong because $number1 and $number2 were empty because of the previous error.

While loop need to create new div every 5 elements

I've got a while loop that displays 50 logos. But what I need is another loop that creates a new div(.autogrid_wrapper .cte .block) every 5 images.
<?php
$cn = 1;
while($result->next()) {
if ($cn % 5 == 0) {
?>
<div class="autogrid_wrapper cte block">
<div class="inner">
<?php } ?>
<div class="ce_card autogrid-type_cte n5 one_fifth autogrid_mode_auto autogrid <?php echo $class2; ?> <?php echo $class; ?> block">
<div class="card_wrapper">
<a class="download_image" title="<?php echo $result->name; ?>"
<div class="ce_image attribute image">
<div class="ce_image block">
<figure class="image_container">
<img src="<?php echo $imageVar->path; ?>" onerror="this.onerror=null; this.src='files/Intershop/media/images/customers/<?php echo $rest; ?>.png'" title="<?php echo $entry->field('name')->value(); ?>" alt="<?php echo $entry->field('name')->value(); ?>" >
</figure>
</div>
</div>
</a>
</div>
</div>
<div class="clear autogrid_clear"></div>
<?php if ($cn % 5 == 0) { ?>
</div>
</div>
<?php
}
$cn++;
?>
<?php } ?>
I hope you guys can help me.
Ok , after ask you in comments I know what's your purpose.
You wanna print div(.autogrid_wrapper .cte .block) before 1st item and close this div after while walk through 5th item and so on.
$cn = 1;
while($result->next()) {
if($cn % 5 == 1) {
//div(.autogrid_wrapper .cte .block)
}
// HTML wraps image
if($cn % 5 == 0) {
//print the close tag of div(.autogrid_wrapper .cte .block)
}
$cn ++;
}
Embed this flow control in your code, I think this will work.
Try with this hope it's helps
<?php
$cn = 1;
while($result->next()) {
if ($cn % 5 == 0) { //check if number is divided by 5 like 5,10,15 etc
//new div'
}
YOUR HTML
if ($cn % 5 == 0) {
//close div'
}
$cn++;
} ?>

Using Pagination with Mysql Queries

I am unable to maintain a GET variable with this pagination script. Was hoping you all could help.
I am using a GET function so the user can choose which categories are displayed. I'm using a pagination script I found online and I am now confronted with this issue, when I choose "page 2" $dynCat no longer is parsed in the URL because the pagination script creates a new URL and uses GET also.
Could someone help me out with using pagination with a variable query? Thank you.
ERROR Notice: Undefined index: dynCat ----/dynCat.php on line 32
I understand why it is undefined, I just don't know how to integrate and maintain the user's Query (i.e. GET variable from URL) with the pagination script. Thanks again.
Page1 index.php (User Selects option)
<?php
//Generate and list Categories include "storescripts/connect_to_mysql.php";
$dynCat = "";
$data = mysql_query("SELECT category, id FROM products GROUP BY category") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
$listcategory = $info["category"];
$dynCat .=
'
<li>
<a href="dynCat.php?dynCat='.$listcategory.'" > '.$listcategory.' </a>
</li>
';
}
mysql_close();
?>
<?php echo $dynCat; ?>
Page2 dynCat.php (User Views Selection with Pagination)
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
//Generate and list Categories
include "storescripts/connect_to_mysql.php";
$dynGallery = "";
$dynCat = "";
$data = mysql_query("SELECT category, id FROM products GROUP BY category") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
$listcategory = $info["category"];
$dynCat .=
'
<li>
<a href="dynCat.php?dynCat='.$listcategory.'" > '.$listcategory.' </a>
</li>
';
}
mysql_close();
?>
<?php
//Query User Selection & Pagination
include('storescripts/connect_to_mysql.php');
$cat = mysql_escape_string($_GET['dynCat']);
$tableName="products";
$targetpage = "dynCat.php";
$limit = 3;
$query = "SELECT COUNT(*) as num FROM $tableName WHERE category = '$cat'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
$stages = 3;
$page = mysql_escape_string(isset($_GET['page'])) ? mysql_escape_string($_GET['page']) : 0;
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get category Data and Images
$query1 = "SELECT * FROM $tableName WHERE category = '$cat' ORDER BY views DESC LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev#gallery'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1#gallery'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage#gallery'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1#gallery'>1</a>";
$paginate.= "<a href='$targetpage?page=2#gallery'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1#gallery'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage#gallery'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1#gallery'>1</a>";
$paginate.= "<a href='$targetpage?page=2#gallery'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next#gallery'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}
$paginate.= "</div>";
}
mysql_close();
?>
<?php
$productCount = mysql_num_rows($result); // count the output amount
while($row = mysql_fetch_array($result))
{
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$category = $row["category"];
$subcategory = $row["subcategory"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynGallery .=
'
<div>
<li>
<a href="product.php?id=' . $id . '#gallery">
<img style="border:#FFF 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="180" height="255" border="1" style="opacity: 0;/></a>
<h4> <a name= ' . $id . ' id= ' . $id . ' value= ' . $id . ' href="product.php?id= ' . $id . '#gallery"> ' . $product_name . ' </a> </h4>
<p>'. $category .'</p>
<p>'. $subcategory .'</p>
<span> $' . $price . '</span>
</li>
</div>
';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<?php include ("../header.php");?>
<?php include ("../menu.php");?>
<a id="shop"></a>
<div class="body">
<div class="sidebar">
<div class="first">
<h2>Dora Designs</h2>
<!-- Dynamic Categories -->
<ul>
<?php echo $dynCat; ?>
</ul>
</div>
</div>
<div class="content">
<div class="figure">
<img src="/images/galleryholder.png" alt=""/>
</div>
<div class="products">
<div class="paging" align = "center">
<a id="gallery"></a>
<? echo $paginate;?>
<? echo '</br>'. $cat;?>
<? echo '</br>'. $total_pages.' Results'; ?>
</div>
<ul>
<?php echo $dynGallery; ?>
</ul>
I hate to dump this much code in the post but I am really at a loss and have never used Pagination before. Any help will be greatly appreciated.
change ever instance of
$targetpage?page=
to
$targetpage?dynCat=$cat&page=
you want 2 copies of $cat one for the db requires escaping with mysql_escape_string() the other for the url with urlencode()
replace the line
$cat = mysql_escape_string($_GET['dynCat']);
with
$cat=urldecode($_GET['dynCat']); //from the url, raw value to display
$cat_mysql = mysql_escape_string($cat); //use in mysql queries
$cat_url=urlencode($cat); //use in the url
then
$targetpage?dynCat=$cat&page=
will now be
$targetpage?dynCat=$cat_url&page=
Try adding dynCat=$cat to all of your links that look like this - <a href='$targetpage?page=...
<a href='$targetpage?dynCat=$capage=...
on a quick search there are approx. 14 of them to change.

Categories