Data not show If multiple 3 data - php

i'm already create page schools and ads
This page show like this
school 1 - school 2 - school 3
ads1
school 4 - school 5 - school 6
ads2
school 7 - school 8 - school 9
ads default << when looping ads finished
but i have i problem when i search 1 school
this school not show, i know this school not show because result query not multipe 3 data. if my query result 1 or 2 data. this page not show this result because this result not multiple 3 data.
This is my website : http://www.tingali.com/pencarian.html
you can search and type "MA AL KARIMIYAH"
This is my script :
pagging_page3.php
<?php
include('connection.php');
$limit = 24;
$adjacent = 3;
if(isset($_REQUEST['actionfunction']) && $_REQUEST['actionfunction']!=''){
$actionfunction = $_REQUEST['actionfunction'];
call_user_func($actionfunction,$_REQUEST,$con,$limit,$adjacent);
}
function showData($data,$con,$limit,$adjacent){
$page = $data['page'];
$dt = $_POST['dt'];
if($page==1){
$start = 0;
}
else{
$start = ($page-1)*$limit;
}
$sqlo = "select * from schools where showschool='YES' ".$dt." and typeschool='school' order by paid,urut asc";
$sql = stripslashes($sqlo);
$rows = $con->query($sql);
$rows = $rows->num_rows;
$sqlo = "select * from schools where showschool='YES' ".$dt." and typeschool='school' order by paid,urut asc limit $start,$limit";
$sql = stripslashes($sqlo);
$data = $con->query($sql);
$str='';
$i=0;
$k=0;
$m =1;
$rumus = $page * ($limit / $adjacent); // bntr
$data_web = mysqli_query($con,"SELECT * from web");
$wew = ($page - 1) * 8;
$row2 = mysqli_fetch_object($data_web);
while($row = $data->fetch_array(MYSQLI_ASSOC)){
// This Data Ads
if($i % 3 == 0)
{
$data_ads = mysqli_query($con,"SELECT * from ads limit $wew,1");
$rowads = mysqli_fetch_object($data_ads);
$str = is_null($rowads->link) ? "<a href='#'><img src='images/ads/default/default.jpg' alt=''></a>":"<a href='".$rowads->link."'><img src='images/ads/default/".$rowads->file."' alt=''></a>";
$wew++;
}
// This Data School
$jd = $row['link'];
$data_sl = mysqli_query($con,"SELECT count(kode) as counter FROM `school_counter` where kode ='".$row['kode']."' and visit between '".$row2->range_start."' AND '".$row2->range_end."' ");
$sl = mysqli_fetch_object($data_sl);
$str.=" <li class='col-lg-4 col-md-4 col-sm-4 col-xs-4'>
<div class='product_item'>
<div>
<a href='".$jd."'>
<img height='228px' width='224px' src='images/sekolah/logo/".$row['logo']."' class='c_image_1' alt=''>
</a>
</div>
<div class='product_info'>
<a href='".$jd."'><h4>".$row['name_school']."</h4></a>
<div class='clearfix'>
<font size='2' color='blue'>Dilihat : ".$sl->counter." </font></br>
<i class='fa fa-home'></i> : ".$row['addres']."<br/>
</div>
</div>
</div>
</li>
";
$i++;
if ($i%3 == 0) {
echo $str;
}
}
?>
Help me thank's

Try to change
$adjacent = 3;
to
$adjacent = 1;

Related

How to show 1 result per page in PHP

I have three restaurants in my database(MySQL) just for testing and i tried to create some pagination code that would show me one restaurant per page.
So i have implemented some code but my problem is that every page displays me all the restaurants again and again.
<?php
$page = 1;
if(isset($_GET['page']))
{
$page = $_GET['page'];
}
$query=mysqli_query($con,"SELECT * FROM restaurants LIMIT 10". ($page * 10 - 10));
$result = mysqli_query($con,"SELECT COUNT(*) as c FROM restaurants");
$row = mysqli_fetch_assoc($result);
$numberOfPages = $row['c'] / 1;
for ($i = 1; $i <= $numberOfPages; $i++)
{
echo '<a style="margin-left:10px;" href="allrestaurants.php?page='. $i . '">' . $i . '</a>';
}
if (!empty($query))
{
if(mysqli_num_rows($query)>0)
{
while ($row=mysqli_fetch_assoc($query))
{
$resId=isset($row['id'])?$row['id']:''; ?>
<div class="col-md-4 p-t-30">
<!-- Block1 -->
<div class="blo1">
<?php
$q="SELECT * FROM restaurant_images JOIN restaurants ON restaurant_images.resaurant_id=restaurants.id AND restaurants.id='$resId'";
$queryRestaunt_image=mysqli_query($con,$q);
if (!empty($queryRestaunt_image))
{
if(mysqli_num_rows($queryRestaunt_image)>0)
{
$count=0;
while ($image=mysqli_fetch_assoc($queryRestaunt_image))
{
$count++;
?>
<a href="restaurantdetails.php?id=<?=$resId?>">
<div class="wrap-pic-blo1 bo-rad-10 hov-img-zoom" >
<div style="background:url('./images/restaurants/<?=isset($image['image'])?$image['image']:'intro-01.jpg'?>') no-repeat center; background-size: contain;height:230px; background-color: black">
</div>
</div>
</a>
<?php
if($count==1)break;
}
}
}
?>
I expect that that i have only one result per page.
You need to change your SQL query to properly use LIMIT clause:
SELECT * FROM restaurants LIMIT <page offset>, <number of restaurants per page>;
Example:
$query = mysqli_query($con, "SELECT * FROM restaurants LIMIT " . ($page - 1) . ", 1");
$query=mysqli_query($con,"SELECT * FROM restaurants LIMIT 10". ($page * 10 - 10));
You can limit to ONE, changing here:
$query=mysqli_query($con,"SELECT * FROM restaurants LIMIT 1". ($page * 10 - 10));
Then you would have to check your logic about the pagination system... if 10 in ($page * 10 - 10) the 10 is used because the default pagination would be 10 results, then you would have to change the 10 for 1 in every line...

Dropdown in pagination

I want to add dropdown in pagination so that if there is more then 10 pages it will be stored in dropdown so that any one can select any page from it.
Here is my php code which show this output:
1 2 3 4 5 6 7 8 9 10 11........... 100
$sql = "SELECT * FROM `new_data`";
$rs_result = mysqli_query($con,$sql);
$total_records = mysqli_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='View.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo " <a href='View.php?page=".$i."'>".$i."</a> ";
};
echo " <a href='View.php?page=$total_pages'>".'>|'."</a> ";//Goto last page
Output i want :
1 2 3 4 5 6 7 8 9 10 dropdown(contains all pages 11 to 100)
$sql = "SELECT * FROM `new_data`";
$rs_result = mysqli_query($con,$sql);
$total_records = mysqli_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='View.php?page=1'>".'|<'."</a> "; // Goto 1st page
$array = [];
for ($i=1; $i<=$total_pages; $i++) {
$array[$i] = "<a href='View.php?page=".$i."'>".$i."</a>";
};
echo " <a href='View.php?page=$total_pages'>".'>|'."</a> ";//Goto last page
Add all the page informations in array and then later populate the dropdown with an array I have changed the for loop code only ... you just popultae your dropdown with the $array;
I done it with php using your php code. Here is my code:
$sql = "SELECT * FROM `new_data`";
$rs_result = mysqli_query($con,$sql); //run the query
$total_records = mysqli_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='View.php?page=1'>".'FIRST <<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++)
{
if($total_pages<=10)
{
echo " <a style='color:#333;' href='View.php?page=".$i."'>".$i."</a> ";
}
else
{
for ($i=1; $i<=10; $i++)
{
echo " <a style='color:#333;' href="View.php?page=".$i."'>".$i."</a> ";
};
echo "<select class='mySelectBox' onchange='location = this.options[this.selectedIndex].value;'>";
for ($i=11; $i<=$total_pages; $i++)
{
echo "<option value= View.php?page=".$i.">".$i."</option>";
};
echo "</select>";
}
};
echo " <a href='View.php?page=$total_pages'>".'........>> LAST'."</a> "; // Goto last page

Optimize a MYSQL query while loop output with a for loop combination PHP

I am trying to output each row from a mysql query slightly differently in terms of styling, however I want to try to avoid having to use offsets and multiple queries.
Current code:
$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 1";
$query = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($query)){
$title = $row["title"];
// output first news item with a style
echo '<div class="style-1">'.$title.'</div>';
}
$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 1 OFFSET 1";
$query = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($query)){
$title = $row["title"];
// output second news item with different style
echo '<div class="style-2">'.$title.'</div>';
}
I would thus like to avoid having 2 (or more) queries simple because I want to use different css classes for each while row, something like this:
$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 10";
$query = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($query)){
$title = $row["title"];
} // end while
$i = 1;
for($i; $i<2; $i++){ // output first row item with first style
echo '<div class="style-1">'.$title.'</div>';
} // end for loop 1
for($i; $i<3; $i++){ // output first row item with second style
echo '<div class="style-2">'.$title.'</div>';
} // end for loop 2
for($i; $i<4; $i++){ // output third row item with third style
echo '<div class="style-3">'.$title.'</div>';
} // end for loop 3
...
Desired Output:
<div class="style-1">News Headline Title 1</div>
<div class="style-2">News Headline Title 2</div>
<div class="style-3">News Headline Title 3</div>
...
I'm assuming you want 3 different styles, and here's how I would accomplish this:
$sql = "SELECT * FROM news ORDER BY id DESC LIMIT 10";
$query = mysqli_query($connection, $sql);
$i = 1;
while($row = mysqli_fetch_array($query)){
$title = $row["title"];
if($i<2){
$styleNumber = 1;
}
else if($i<3){
$styleNumber = 2;
}
else if($i<4){
$styleNumber = 3;
}
echo '<div class="style-'.$styleNumber.'">'.$title.'</div>';
$i++;
}
// end while
Output:
Title 1 (Style 1)
Title 2 (Style 2)
Title 3 (Style 3)
Title 4 (Style 3)
This is not an answer. Just a comment/improvement of #JonTan answer since it was chosen as correct one.
My guess is that OP need to loop styles for every 3 records, but not to set style only for 3 first returned.
That means we can change condition from if($i<3){, if($i<2){ to something like:
$i = 1;
while($row = mysqli_fetch_array($query)){
$title = $row["title"];
if($i % 3 == 0){
$styleNumber = 3;
} else if($i % 2 == 0){
$styleNumber = 2;
} else {
$styleNumber = 1;
}
echo '<div class="style-'.$styleNumber.'">'.$title.'</div>';
$i++;
}

Limiting the number of pagination pages

I have one page where I echo 10 items from MySql DB and has pagination for next page. The problem is that there are 1000+ rows in database.. so you can guess how many pages are in pagination links. How can I limit their numbers for example to show
1 2 3 4 5 ... N
when user change to page 2
2 3 4 5 6 ... N
and so on?
Or something else.. the main goal is just to hide every single page number.
<?php
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 10;
$result = $pdo->prepare("SELECT t.* FROM images t order by randorder ASC LIMIT $start_from, 10");
$result->execute();
for($i=0; $row = $result->fetch(); $i++)
{
echo 'echo data';
}
echo '<div class="pagination" >';
$result = $pdo->prepare("SELECT COUNT(image_id) FROM images");
$result->execute();
$row = $result->fetch();
$total_records = $row[0];
$total_pages = ceil($total_records / 10);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='?page=".$i."'";
if($page==$i)
{
echo "class=active";
}
echo ">";
echo "".$i."</a> ";
};
echo '</div>';
?>

Problem with PHP Pagination

I am having a problem with my code ( sorry its a lot, but its the only way I knew to show you, its basically just a select statement from a table) that shows a page link but doesnt change the page results. Basically I set it for example at 1 result per page but it shows all the results but still shows a link at the top to go to the next page. The next page just shows the same. I'm a PHP beginner so any help would be greatly appreciated.
Thanks!
<?php
if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1;
$max_results = 1;
$from = (($page * $max_results) - $max_results);
REQUIRE('config.php');
$q = mysql_real_escape_string(ucfirst(trim($_REQUEST['q'])));
$result = mysql_query("SELECT * FROM gj WHERE name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q' ORDER by name") or trigger_error(mysql_error());
$rows = mysql_num_rows($result);
if($rows == 0){
}
echo " <div id='title'>Search for "$q"<div class='righttitle'>$rows business";if($rows > 1){echo "es";}elseif($rows == "0"){echo "es";}echo" found";
echo"<div id='pagenumbers'>";
// (1) get the total number of results for your query
// modify this to match the total results for the main query
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM gj where name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q'"),0);
// (2) Calculate total number of pages. Round up using ceil()
$total_pages = ceil($total_results / $max_results);
if($total_results > $max_results)
{
// (3) build Previous link
if($page > 1)
{
$prev = ($page - 1);
echo "<< Prev ";
}
// (4) display page numbers
for($i = 1; $i <= $total_pages; $i++)
{
if($page == $i)
{
echo $i . " ";
}
else
{
echo "$i ";
}
}
// (5) build Next Link
if($page < $total_pages)
{
$next = ($page + 1);
echo "Next >>";
}
}
echo"</div></div></div>";
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
$name=$row['name'];
$phone=$row['phone'];
$website=$row['website'];
$city=$row['city'];
$address=$row['address1'];
$zipcode=$row['zipcode'];
$sponsored = $row['sponsored'];
$addressmap = preg_replace('/\s/', '+',$address);
$citymap = preg_replace('/\s/', '+',$city);
//Start While Loop
echo"
<div id='listing'>
<div id='mainlisting'>";
echo"
<div class='name'>
<a href='./more.php?id=$id' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>$name</a> <div class='right'>$phone</div>
</div>
<div class='other'>
$address, $city, CO $zipcode
|<a target='_blank' href='http://maps.google.com/maps? f=q&source=s_q&hl=en&geocode=&q=$addressmap,+$city+CO&&&ie=UTF8&hq=&hnear=$address,+$city,+Colorado+$zipcode&safe=active&&&t=h&z=14&iwloc=A&output=embed' rel='lyteframe' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"' title='$name' rev='width: 500px; height: 500px; scrolling: no;'> See Map</a><br/>
<a href='#' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>";if($website != null){ echo "<a target='_blank' href='$website' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>Website</a> |";}echo" <a href='#' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>More Info</a>
</div>
</div>
</div><!--/LISTING-->";
}
Michael, I tried what you did but I might have done it wrong...Here is my code before the where statement
<?php
$page = 1; $total_pages = 9; $record_start = ($page * $total_pages) - $total_pages;
REQUIRE('config.php');
$q = mysql_real_escape_string(ucfirst(trim($_REQUEST['q'])));
$result = mysql_query("SELECT * FROM gj WHERE name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q' ORDER by name LIMIT 0,9") or trigger_error(mysql_error());
$rows = mysql_num_rows($result);
if($rows == 0){
}
echo " <div id='title'>Search for "$q"<div class='righttitle'>$rows business";if($rows > 1){echo "es";}elseif($rows == "0"){echo "es";}echo" found";
echo"<div id='pagenumbers'>";
// (1) get the total number of results for your query
// modify this to match the total results for the main query
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM gj where name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q '"),0);
// (2) Calculate total number of pages. Round up using ceil()
$alltotal_pages = ceil($total_results / $max_results);
if($total_results > $max_results)
{
// (3) build Previous link
if($page > 1)
{
$prev = ($page - 1);
echo "<< Prev ";
}
// (4) display page numbers
for($i = 1; $i <= $alltotal_pages; $i++)
{
if($page == $i)
{
echo $i . " ";
}
else
{
echo "$i ";
}
}
// (5) build Next Link
if($page < $alltotal_pages)
{
$next = ($page + 1);
echo "Next >>";
}
}
echo"";
What you need to do is add something like:
$page = 1;
$results_per_page = 10;
$record_start = ($page *
$results_per_page) -
$results_per_page;
$result = mysql_query("SELECT * FROM
gj WHERE name LIKE '%$q%' OR cat1 LIKE
'%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE
'%$q' ORDER by name LIMIT
$record_start,$results_per_page") or
trigger_error(mysql_error());
The problem is that you always fetch the same result set and then just output all of it, no matter which page is currently "active". If you want to paginate, you'll probably want to use something like a LIMIT clause in your SQL query (e.g. LIMIT 20,10 to return 10 records starting from offset 20 (zero-based, i.e. record number 21)).
http://www.apnacode.com/php/simple-php-pagination/
Check Above Link for Code, May it help you

Categories