PHP Insert an ad div after every 5 rows results - php

How could I go about inserting a div after every fifth row on result page?
I have a script that displays 15 rows from a database in every pagination.
This my entire script
<?php
$sql = "SELECT COUNT(id) FROM table";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);
// Here we have the total row count
$rows = $row[0];
$page_rows = 15;
$last = ceil($rows/$page_rows);
if($last < 1){
$last = 1;
}
$pagenum = 1;
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
if ($pagenum < 1) {
$pagenum = 1;
}
else if ($pagenum > $last) {
$pagenum = $last;
}
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$sql = "SELECT id, fname, image_text, address, contact, price, image FROM table WHERE fname LIKE '%".$search."%' $limit";
$query = mysqli_query($db_conx, $sql);
$textline1 = "table (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= '<td><a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> </td> ';
for($i = $pagenum-3; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= '<td><a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> </td> ';
}
}
}
$paginationCtrls .= ''.$pagenum.' ';
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= '<td><a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a></td> ';
if($i >= $pagenum+3){
break;
}
}
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' <td> <a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a></td> ';
}
}
$list = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
?>
<div class="light-purple column">
<table style="height:100px; border-bottom:#F88 solid 2px;" width="100%" border="0">
<tr>
<td>
<img src="img/<?php echo $row['image']; ?>" alt="No Image">
</td>
<td width="70%">
<div class="detail_text" style="font-weight:bold;"><input type="hidden" value="<?php echo $row['id']; ?>"/>
<?php echo $row['fname']; ?>
</div>
<div class="detail_text " ><?php echo $row['image_text']; ?></div>
<div style="color: #999; font-weight: bold;"><?php echo $row['address']; ?></div>
<div class="detail_text" style="font-weight:bold; color: #606;">
<table width="95%" border="0">
<tr>
<td width="51%"><?php echo $row['contact']; ?></td>
<td width="49%"><?php echo $row['price']; ?></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
<?php }
?>
I want to able to put an ad div in every 5 rows something like this "
<div class="ad"></div>
<row> content </row>
<row> content </row>
<row> content </row>
<row> content </row>
<row> content </row>
<div class="ad"></div>
<row> content </row>
<row> content </row>
<row> content </row>
<row> content </row>
<row> content </row>
<div class="ad"></div>
Can anyone assist me as to how could I set up the PHP script that will allow me to count the rows returned and after every certain number of rows , insert an ad script? Or is there a way to do this within rows itself?

You can use modulus operator,
$count = 1;
while{
if($count % 5 == 0)
echo '<div class="add"></div>';
$count++;
}

Just add a count variable to count till 5 and then reset to zero inside your loop.
$count=0;
while{
echo "<row> content </row>";
$count++;
if($count>=5)
{
echo "<div class="ad"></div>";
$count=0;
}
}

Related

multiple checkboxes with paging in php without using javascript and jquery

Good day, I have an HTML table and I use paging on it so that only a certain amount of items is shown. The problem is that I need to have multiple selections with checkboxes and that works for a single page but I need that to work between pages. So for example on page 1 you choose 3 items and in the next page you choose 5 items and when GET happens I need to have all those items in one place so that I can store them in a variable.
<?php
include("connect.php"); //database connection file
$limit = 7;
if ( isset($_GET['page']) ) {
$page_no = $_GET['page'];
} else {
$page_no = 1;
}
$start_from = ($page_no-1)*$limit;
$sql = "SELECT * FROM emp_info LIMIT $start_from,$limit ";
$result = mysqli_query($conn , $sql);
?>
<form method="GET" action="project.php?name=<?php echo
$data['name']; ?>">
<div class="container">
<h2>employee information:</h2>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>EmpId</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
$info = "SELECT * FROM emp_info LIMIT $start_from,$limit ";
//query to select the data from database
$query = mysqli_query ($conn , $info);
while ( $data = mysqli_fetch_assoc ($query) )
{ //query to fetch the data
$_SESSION['emp_name']=$data['name'];
?> <tr>
<td><?php echo $data['emp_id'];?></td>
<td>
<a href="project.php?id=<?php echo $data['emp_id'];?>&name=<?php echo $data['name']; ?>">
<input type="checkbox" name="check_list[]" value="<?php echo $data['name'];?>">
</a> <?php echo $data['name'];?>
</td>
<td><?php echo $data['email'];?></td>
</tr>
<?php }
?>
</tbody>
</table>
<ul class="pagination">
<?php
$sql = "SELECT COUNT(*) FROM emp_info";
$result = mysqli_query($conn , $sql);
$row = mysqli_fetch_row($result);
$total_records = $row[0];
// Number of pages required.
$total_pages = ceil($total_records /
$limit);
$pagLink = "";
for ( $i = 1; $i <= $total_pages; $i++) {
if ( $i == $page_no) {
$pagLink .= "<p>Pages:</p><li class='active'><a href='datatable.php?id=" . $data['emp_id'] .
"&page=" . $i ."'>". $i ."</a></li>";
} else {
$pagLink .= "<li><a href='datatable.php?page=". $i ."'>". $i ."</a></li>";
}
};
echo $pagLink;
?>
</ul>
</div>
<button type="submit" formaction="project.php"
name="select_proj">Select Project</button>
<button type="submit"
formaction="addnewproj.php" name="add_proj">Add New
Project</button>
</form>
</body>
</html>
I recommend reseaching abit of Javascript and more specificly aJax to solve this issue.
You need somewhere to store the information that has been selected in order to use it somewhere else.

how to create pagination with next and previous button?

code:
<table>
<tr style="background: white url(bg.gif) repeat-x left top;color: #fff;">
<th>College Id</th>
<th>College Name</th>
<th>Website</th>
<th>Field</th>
<th>City</th>
<th>Action</th>
</tr>
<?php
$per_page=10;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else {
$page=1;
}
$start_from = ($page-1) * $per_page;
$sql="select * from all_colleges LIMIT $start_from, $per_page";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['college_id']; ?></td>
<td><?php echo $row['college_name']; ?></td>
<td><?php echo $row['website']; ?></td>
<td><?php echo $row['field']; ?></td>
<td><?php echo $row['city']; ?></td>
<td>
<a class='view' title='view' href='view.php?id=<?php echo $row['college_id']; ?>'>
<img src='gridview/view.png' alt='view' />
</a>
<a class='update' title='Update' href='update.php?id=<?php echo $row['college_id']; ?>'>
<img src='gridview/update.png' alt='Update' />
</a>
<a class='delete' title='delete' href='delete.php?ad_id=<?php echo $row['college_id']; ?>'>
<img src='gridview/delete.png' alt='delete' />
</a>
</td>
</tr>
<?php
}
?>
</table>
<div style="padding: 10px;">
<?php
$query = "select * from all_colleges";
$result = mysqli_query($link, $query);
$total_records = mysqli_num_rows($result);
$total_pages = ceil($total_records / $per_page);
echo "<center><a href='admin.php?page=1' style='padding:5px;'>".'previous'."</a>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='admin.php?page=".$i."'>".$i."</a>";
}
echo "<a href='admin.php?page=$total_pages'>".'next'."</a></center>";
?>
</div>
In this code I want to create a pagination with next and previous button here this code is working properly but it show diffrent pagination i.e. if the number of results is 200 then it will display like
previous 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 next
but I want like
previous 1 2 3 4 5 ....8 9 next
How can I do this please help ?
Thank You
You can check each iteration of your loop if you want to display the number.
In the following code, only:
the first two pages
the last two pages
the current page
each two pages to the left and to the right of the current page
are displayed.
$skipped = false;
for ($i = 1; $i <= $total_pages; $i++) {
if ($i < 3 || $total_pages- $i < 3 || abs($page - $i) < 3) {
if ($skipped)
echo '<span> ... </span>';
$skipped = false;
echo "<a href='admin.php?page=" . $i . "'>" . $i . "</a>";
} else {
$skipped = true;
}
}
If you have a lot of pages, the big loop is unnecessary.
Instead, you could use three different loops:
$done = [];
for ($i = 1; $i < 3; $i++) {
$done[$i] = true;
//echo
}
if ($page > 3)
echo '<span> ... </span>';
for ($i = $page - 2; $i < $page + 3; $i++) {
if (isset($done[$i])
continue;
$done[$i] = true;
//echo
}
if ($page < $total_pages - 3)
echo '<span> ... </span>';
for ($i = $total_pages- 2; $i < $total_pages; $i++) {
if (isset($done[$i])
continue;
$done[$i] = true;
//echo
}
To fix your two other buttons (previous and next), link to $page - 1 and$page + 1 instead of 1 and $total_pages.

MySQL PHP Search result with pagination [duplicate]

This question already has an answer here:
PHP Pagination removes search term and reverts back to displaying all results, fix?
(1 answer)
Closed 6 years ago.
Creating a new project using php: retrieving data from mysql database using a search function (search.php) - added pagination to the search result on results page (find.php). Question is, when the first search is displayed, it shows the correct data; however, when i click on 2nd page (pagination button), it populates all the data again - not the term i searched for: Please help!
search.php
<form action="find.php" method="post">
<input name="search" type="search" autofocus><input type="submit" name="button">
</form>
find.php
<?php
include_once("config.php");
if(isset($_POST['button'])){
$search=$_POST['search'];
}
$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit ");
$row = mysql_num_rows($result);
$page_rows = 3;
$last = ceil($row/$page_rows);
if($last < 1){
$last = 1;
}
$pagenum = 1;
if(isset($_GET['page'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['page']);
}
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit ");
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
$paginationCtrls = '';
$self = $_SERVER['PHP_SELF'];
if($last != 1){
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= 'Previous';
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= ''.$i.'';
}
}
}
$paginationCtrls .= '<span class="current">'.$pagenum.'</span>';
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= ''.$i.'';
if($i >= $pagenum+4){
break;
}
}
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= 'Next ';
}
}
?>
<table border='1' cellpadding='10' style="border-collapse:collapse;">
<tr>
<th>id</th>
<th>name</th>
<th>address</th>
<th>phone</th>
<th>email</th>
<th>website</th>
<th>locations</th>
<th>info</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['website']; ?></td>
<td><?php echo $row['locations']; ?></td>
<td><?php echo $row['info']; ?></td>
</tr>
<?php
}
?>
You must have to set search term in pagination links. Try this.
<form action="find.php" method="post">
<input name="search" type="search" autofocus><input type="submit" name="button">
</form>
<?php
include_once("config.php");
if(isset($_POST['button'])){
$search=$_POST['search'];
}
$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit ");
$row = mysql_num_rows($result);
$page_rows = 3;
$last = ceil($row/$page_rows);
if($last < 1){
$last = 1;
}
$pagenum = 1;
if(isset($_GET['page'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['page']);
}
// get search tearm from url
if(isset($_GET['search'])){
$search=$_GET['search'];
}
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit ");
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
$paginationCtrls = '';
$self = $_SERVER['PHP_SELF'];
// you must have to set search tearm in pagination links
if($last != 1){
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= 'Previous';
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= ''.$i.'';
}
}
}
$paginationCtrls .= '<span class="current">'.$pagenum.'</span>';
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= ''.$i.'';
if($i >= $pagenum+4){
break;
}
}
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= 'Next ';
}
}
?>
<table border='1' cellpadding='10' style="border-collapse:collapse;">
<tr>
<th>id</th>
<th>name</th>
<th>address</th>
<th>phone</th>
<th>email</th>
<th>website</th>
<th>locations</th>
<th>info</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['website']; ?></td>
<td><?php echo $row['locations']; ?></td>
<td><?php echo $row['info']; ?></td>
</tr>
<?php
}
?>

Pagination only returning first page values

For the past 2 days I've been looking for a pagination script of sorts, and I found this one which I'm starting to implement on my website.
The code is the following:
<?php
if (!(isset($pagenum)))
{
$pagenum = 1;
}
$data = mysqli_query($lig,"SELECT * FROM requisicao") or die("data");
$rows = mysqli_num_rows($data);
$page_rows = 5;
$last = ceil($rows/$page_rows);
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$data_p = mysqli_query($lig,"SELECT * FROM requisicao $max") or die("data_p");
while($info = mysqli_fetch_assoc( $data_p ))
{
?>
<tr>
<td><?php print $info['id_requisicao']; ?> </td>
<td><?php print $info['username']; ?> </td>
<td><?php print $info['nome_servico']; ?> </td>
<td><?php print $info['data_requisicao']; ?> </td>
<td><?php print $info['estado_requisicao']; ?> </td>
<td><?php print $info['prioridade']; ?> </td>
<td><?php print $info['total']; ?> </td>
<td align="center">+</td>
<tr>
<?php
}
?>
</tbody>
</table>
<?php
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-Início</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Anterior</a> ";
}
echo " ---- ";
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Seguinte -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Última ->></a> ";
}
?>
I'm getting the first 5 rows out of it, but when I press Seguinte (which is Next) it changes the link (TO PAGE 2) but it doesn't change the values that get outputed. Basically, it always shows page 1.
I've also looked into several similar questions but I haven't found solutions that fix my problem.
Thanks in advance, and my apologies if this is a simple problem and I'm just overlooking things :s
The problem is that $pagenum will be initialized as 1, anyway.
Instead of:
if (!(isset($pagenum)))
{
$pagenum = 1;
}
Try writing this:
if (!isset($_REQUEST['pagenum']) || !is_int($_REQUEST['pagenum']))
{
$pagenum = 1;
} else {
$pagenum = (int)$_REQUEST['pagenum'];
}
You may find some more info handling HTTP Request variables here

PHP - MySQL query with Pagination

How would I go about making a pagination script for this MySQL & PHP query.
if (isset($_GET['c'])) {
$c = $_GET['c'];
}
$query = mysql_query("SELECT * FROM Categories WHERE category = '$c' ");
WHILE($datarows = mysql_fetch_array($query)):
$id = $datarows['id'];
$category = $datarows['category'];
$code = $datarows['code'];
endwhile;
$query2 = mysql_query("SELECT * FROM Games WHERE category = '$code' ");
WHILE($datarows_cat = mysql_fetch_array($query2)):
$title = $datarows_cat['title'];
$description = $datarows_cat['description'];
$imgurl = $datarows_cat['image_name'];
$category = $datarows_cat['category'];
$views = $datarows_cat['view_count'];
$pagename = $datarows_cat['pagename'];
$featured = $datarows_cat['featured'];
if ($featured =="1") {$f = "<img src='http://my-site.com/images/star.png' width='13px' title='Featured Game' /> Featured"; } else {$f = "";}
if(is_int($views/2)) {
$views = $views / 2;
} else { $views = $views / 2 + .5; }
if (strlen($description) > 95) {
$desc= substr($description,0,95);
$desmod = "$desc...<br/>- Read More";
}
else {$desmod = "$description";}
echo "$f - $title - $desmod<br/>";
endwhile;
And when I visit http://my-site.com/categories/Action for instance, The code looks up that category in my category table, then once it gets the unique code for that category, It runs another query to find all games in another table with that category code. Currently, however, I have 200+ games loading for a single category which causes a great amount of loading time.
Thanks for your help!
First of all find out how many games are there for a specific category
change the line
$query2 = mysql_query("SELECT * FROM Games WHERE category = '$code' ");
to
$sql="SELECT * FROM Games WHERE category = '$code' ";
$query_count=mysql_query($sql);
Add following after it
$per_page =30;//define how many games for a page
$count = mysql_num_rows($query_count);
$pages = ceil($count/$per_page);
if($_GET['page']==""){
$page="1";
}else{
$page=$_GET['page'];
}
$start = ($page - 1) * $per_page;
$sql = $sql." LIMIT $start,$per_page";
$query2=mysql_query($sql);
Then display the numbers of pages where you want
<ul id="pagination">
<?php
//Show page links
for ($i = 1; $i <= $pages; $i++)
{?>
<li id="<?php echo $i;?>"><?php echo $i;?></li>
<?php
}
?>
</ul>
Use CSS for pagination this will do the trick
//database connation
<?php
$conn = new mysqli("localhost", "root", "","database_name");
?>
<!DOCTYPE html>
<html>enter code here
<head>
<title>View Student Details</title>
<h1 align="center"> Student Details </h1>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="bootstrap/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<div class="row">
<div class="col-sm-2"></div>
<div class="col-sm-8">
<form>
<table class="table table-striped">
<tr>
<th>Sr.No.</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Class</th>
<th>Gender</th>
<th>Birth of Date</th>
<th>Contact No.</th>
<th>Action</th>
</tr>
<?php
$count=0;
if(isset($_GET['page_count']))
{
$count=1;
$page_count=$_GET['page_count']-1;
$count=$page_count*10;
}
$q="SELECT * from student_detail LIMIT $count,10";
$result=$conn->query($q);
$j=0;
while($data=$result->fetch_array())
{ $j=$j+1;
?>
<tr>
<td><?php echo $j ?></td>
<td><?php echo $data['std_id'] ?></td>
<td><?php echo $data['std_name'] ?></td>
<td><?php echo $data['std_class'] ?></td>
<td><?php echo $data['gender'] ?></td>
<td><?php echo $data['bod'] ?></td>
<td><?php echo $data['contact'] ?></td>
<td>
<div class="row">
<div class="col-sm-12">
Delete
Update
</div>
</div>
</td>
</tr>
<?php } ?>
</table>
<ul class="pagination">
<?php
$q="SELECT count(std_id) from student_detail";
$result=$conn->query($q);
$data=$result->fetch_array();
$total=$data[0];
$total_page=ceil($total/10);
if($total_page>1)
{
for($i=1;$i<=$total_page;$i++)
{
?>
<li class="active"><?php echo $i; ?></li>
<?php
}
}
?>
</ul>
</form>
<div class="col-sm-2"></div>
</div>
</div>
</body>
</html>
Pagiantion, it is working simple and easy
<?php
$sql = "SELECT COUNT(id) FROM contact_info";
$rs_result = $conn->query($sql);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
echo $total_records;
$previous = 1;
$total_pages = ceil($total_records / $limit);
$next = $_GET["page"] + 1;
$previous = $_GET["page"] - 1;
$pagLink = "<span>";
if($previous ==0)
{
$prev = "<a href='javascript:void(0);' >Previous</a>";
}
else
{
$prev = "<a href='http://homeacresfinefurniture.com/all-queries.php?page=".$previous."' style='color:black;'>Previous</a>";
};
echo $prev;
"</span><div class='pagination'>";
for ($i=1; $i<=$total_pages; $i++)
{
$pagLink .= "<a href='http://homeacresfinefurniture.com/all-queries.php?page=".$i."'>".$i."</a>";
};
echo $pagLink;
$nex = "<span><a href='http://homeacresfinefurniture.com/all-queries.php?page=".$next."' style='color:black;'>Next</a></span>";
echo $nex;
";
</div>";
?>
Get all data from database.
$limit = 1;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * $limit;
$sql = "SELECT * FROM contact_info ORDER BY id desc LIMIT $start_from , $limit";
$page = 1;
$limit = 10;
$offset = ($limit * $page) - $limit;
$query = mysqli_query(
$connect,
"SELECT * FROM Games WHERE category = '$code' limit $limit offset $offset"
);

Categories