Two Pictures in one row in a large gallery - php

I have a Problem:
I coded a whole gallery with bootstrap and in this gallery are gaps. There are 100 pictures on 7 pages and instead of going down in one picture block it has gaps between it. This follows no obvious order.
Here is a picture:
So I wanted to take the whole col-md etc and picture container away and make my own columns. So that it works properly, but my solution from the internet doesn't work... I think it must be a problem with PHP that it is in a For loop and not in pure HTML like in the solutions. But I don't know how to solve it.
Here is the PHP code before:
<?php
//require_once("galerie-config.php");
if (!isset($_GET['pageindex'])) {
$_GET['pageindex'] = "1";
}....
<script type='text/javascript' src='../js/Bateaux/jquery/jquery.js'></script>
<script type='text/javascript' src='../js/Bateaux/jquery/jquery-migrate.min.js'></script>
<script type='text/javascript' src='../revslider/public/assets/js/jquery.themepunch.tools.min.js'></script>
<script type='text/javascript' src='../revslider/public/assets/js/jquery.themepunch.revolution.min.js'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var BateauxOptions = {"ajax_url":"../js/admin-ajax.html"};
/* ]]> */
</script>
<script type='text/javascript'>
/* <![CDATA[ */
var mejsL10n = {
"language": "en-US",
"strings": {
"Close": "Close",
"Fullscreen": "Fullscreen",
"Download File": "Download File",
"Download Video": "Download Video",
"Play\/Pause": "Play\/Pause",
"Mute Toggle": "Mute Toggle",
"None": "None",
"Turn off Fullscreen": "Turn off Fullscreen",
"Go Fullscreen": "Go Fullscreen",
"Unmute": "Unmute",
"Mute": "Mute",
"Captions\/Subtitles": "Captions\/Subtitles"
}
};
var _wpmejsSettings = {
"pluginPath": "../js\/mediaelement\/"
};
/* ]]> */
</script>
<script type='text/javascript' src='../js/Bateaux/hoverIntent.min.js'></script>
<script type='text/javascript' src='../js/Bateaux/jquery/ui/widget.min.js'></script>
<script type='text/javascript' src='../js/Bateaux/main-vendors.min.js'></script>
<script type='text/javascript' src='../js/Bateaux/main.min.js'></script>
<!-- INNER BANNER STARTS
========================================================================= -->
<div class="inner-banner" style="background-color:#fff;">
<div class="container">
<ol class="breadcrumb">
<li>Home</li>
<li>Referenzen</li>
<li class="active">Galerie</li>
</ol>
<h1>Galerie</h1>
</div>
</div>
<!-- /. INNER BANNER STARTS
========================================================================= -->
<!-- PORTFOLIO STARTS
========================================================================= -->
**<div class="container contents portfolio">
<div class="row no-gutter-6">
<?php
require_once("dbh.php");
$numPicturesPerPage = 20;
//$seiten = [0,1,2,3,4,5,6,7,8,9,10];
global $pageIndex;
$pageIndex = 1;
if (isset($_GET['pageindex'])) {
$pageIndex = $_GET['pageindex'];
}
$bildauswahl = $numPicturesPerPage * ($pageIndex -1);
$sql = "SELECT * FROM galeriebilder LIMIT $bildauswahl,$numPicturesPerPage";
$rows = $dbh->query($sql);
$imgPathNames = [];
$i = 0;
foreach ($rows as $row) {
//for ($i=0;$i<$numPicturesPerPage;$i++) {
$bildcode = $row['bildcode'];
//array_push($imgPathNames, $bildcode);
$i++;
echo "<!-- Picture Starts -->";
***echo "<div class='col-lg-6 col-md-6 col-sm-6'>";
echo "<div class='picture'>";
echo "<img id='aquarium-galerie-" . $i . "' src='../images/galerie/". "$bildcode" . "'" . " onclick='alert(\"" . $bildcode . "\")' class='img-responsive editable' alt='' />";
echo "</div>";
echo "</div>";
//}***
?>
<!-- Picture Ends -->
<?php
}
?>
</div>
<div class="row">
<!-- Picture Starts -->
<?php
global $numPages;
$sql ="SELECT count(bild_id) FROM galeriebilder";
$rows = $dbh->query($sql);
foreach ($rows as $row){
global $numPictures;
$numPictures = $row[0];
}
$biggerThan = $numPictures % $numPicturesPerPage;
$numPages = intval(floor($numPictures / $numPicturesPerPage));
if ($biggerThan > 0) {
$numPages++;
}
if (preg_match("/d+/", $pageIndex, $matches)) { $pageIndex = $matches[0]; } // Filtert die Zahl aus z.B. 2.php heraus
function navSeiten($aktuell,$gesamt) {
$pageIndex = $_GET['pageindex'];
$next = $aktuell + 1;
$previous = $aktuell - 1;
$ausgabe = '<ul class="pager">';
if ($aktuell <= 3 ) {
$seiten = array(1,2,3,4,5);
}else if ($aktuell == $gesamt -3 ) {
$seiten = array($aktuell -2 ,$aktuell-1 ,$aktuell , $aktuell +1 , $aktuell +2);
}else if ($aktuell >= $gesamt -3 ) {
$seiten = array($gesamt -4 , $gesamt -3, $gesamt -2 , $gesamt -1, $gesamt );
} else {
$seiten = array( $aktuell -2 ,$aktuell-1 ,$aktuell , $aktuell +1 , $aktuell +2);
}
//echo ($pageIndex);
?>
<li class="previous" style="<?php
if ($pageIndex == "1" ) { echo "display:none ";} ?>" >
<a href="galerie.php?pageindex=' . $previous . '" aria-label="Previous">
<span aria-hidden="true"></span>
</a>
</li>
<?php
foreach ($seiten as $seite) {
if ($seite == $pageIndex) {
$ausgabe .= '<li class="active">' . $seite . '</li>';
} else {
$ausgabe .='<li>' . $seite . '</li>';
}
}
?><li class="next" style="
<?php
/*$sql ="SELECT count(bild_id) FROM galeriebilder";
$rows = $dbh->query($sql);
foreach ($rows as $row) {
$numPictures = $row[0];
}*/
$numPicturesPerPage = 20;
$numPages = intval(floor($numPictures / $numPicturesPerPage));
if ($pageIndex == $numPages ) { echo "display:none"; }
?>" >
<a href="galerie.php?pageindex=' . $next . '" aria-label="Next">
<span aria-hidden="true"></span>
</a>
</li>
<?php
//echo $numPages;
echo $numPictures;
$ausgabe .= '</ul>';
return $ausgabe;
}
?>
<div class="col-lg-12 no-gutter-12 pagging">
<?php echo navSeiten($pageIndex,$numPages) ?> <!-- Dies ist der Funktionsaufruf der Funktion die die komplette Leiste der Seitenaufrufe-->
</div>
</div>
</div>
<!-- /. PORTFOLIO ENDS**
========================================================================= -->
<!-- FOOTER STARTS
========================================================================= -->
<footer class="parallax-1">
<!-- Social Media Starts -->
<div class="social-media transparent-black-bg" style="background-color:#000;">
<div class="container">
<div class="row">
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-6">
<div class="caption">Facebook</div>
</div>
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-6">
<div class="caption">Youtube</div>
</div>
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-6">
<div class="caption">Instagram</div>
</div>
<div class="col-lg-2 col-md-2 col-sm-4 col-xs-6">
<div class="caption">Google+</div>
</div>
</div>
</div>
</div>
<!-- Social Media Ends -->
<!-- Contact Info Starts -->
<div class="contact-info" style="background-color:#313131;">
<div class="container">
<div class="row">
<!-- Address Starts -->
<div class="col-lg-3 col-md-3 col-sm-3 address">
<div class="footer-logo"><img src="../images/Startseite/Logo-Footer.png" class="img-responsive" alt="Logo von Biological Aqua Dreams (eine Koralle)" style="margin-left:0;"></div>
<address>
<strong>KONRAD-HAUSSMANN-WEG 6<br>
D-73614 SCHORNDORF</strong>
<div class="phone">+49 176 6316 1653</div>
<div>INFO#BIOLOGICAL-AQUA-DREAMS.COM</div>
</address>
<div class="about">Sie haben Fragen? Kontaktieren Sie uns einfach telefonisch oder per Email.</div>
</div>
<!-- Address Ends -->
<!-- Blog Posts Starts -->
<div class="col-lg-4 col-md-4 col-sm-4 latest-posts">
<h1>ALLES AUF EINEN BLICK</h1>
<div class="post">
<h2>Unsere Leistungen </h2>
<div class="info"><span class="user">Erfahren Sie mehr über uns</span></div>
</div>
<div class="post">
<h2>Wartung und Reparatur</h2>
<div class="info"><span class="user">Wir sind für Sie da</span></div>
</div>
</div>
<!-- Blog Posts Ends -->
<!-- Get in Touch Starts -->
<div class="col-lg-5 col-md-5 col-sm-5 get-in-touch">
<h1>KONTAKT AUFNEHMEN</h1>
<form action='../process.php' method='post' name='ContactForm' id='ContactForm'>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email *">
</div>
<textarea rows="5" class="form-control" name="comment" placeholder="Nachricht *"></textarea>
<div id='message_post'></div>
<input class="btn btn-default" type='submit' value='SENDEN' name='submitf' id="submitf" style="outline:none;">
</form>
</div>
<!-- Get in Touch Ends -->
</div>
</div>
</div>
<!-- Contact Info Ends -->
<!-- Copyright Starts -->
<div class="copyright light-grey-bg">
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-8 col-sm-8">© 2018 BIOLOGICAL AQUA DREAMS |
DATENSCHUTZ | IMPRESSUM</div>
<div class="col-lg-4 col-md-4 col-sm-4 created-by">Created by Crew Ad</div>
</div>
</div>
</div>
<!-- Copyright Starts -->
</footer>
....
And here after my solution:
<div class="container contents portfolio">
<div class="row no-gutter-6">
<?php
require_once("dbh.php");
$numPicturesPerPage = 20;
//$seiten = [0,1,2,3,4,5,6,7,8,9,10];
global $pageIndex;
$pageIndex = 1;
if (isset($_GET['pageindex'])) {
$pageIndex = $_GET['pageindex'];
}
$bildauswahl = $numPicturesPerPage * ($pageIndex -1);
$sql = "SELECT * FROM galeriebilder LIMIT $bildauswahl,$numPicturesPerPage";
$rows = $dbh->query($sql);
$imgPathNames = [];
$i = 0;
foreach ($rows as $row) {
//for ($i=0;$i<$numPicturesPerPage;$i++) {
$bildcode = $row['bildcode'];
//array_push($imgPathNames, $bildcode);
$i++;
echo "<!-- Picture Starts -->";
//echo "<div class='col-lg-6 col-md-6 col-sm-6'>";
echo "<div class='picture'>";
echo "<figure><img id='aquarium-galerie-" . $i . "' src='../images/galerie/". "$bildcode" .
"'" . "width='750px' height='500px' onclick='alert(\"" . $bildcode .
"\")' class='img-responsive pic editable' alt='' /></figure>";
echo "</div>";
//echo "</div>";
//}
?>
<!-- Picture Ends -->
<?php
}
?>
</div>
<div class="row">
<!-- Picture Starts -->
<?php
global $numPages;
$sql ="SELECT count(bild_id) FROM galeriebilder";
$rows = $dbh->query($sql);
foreach ($rows as $row){
global $numPictures;
$numPictures = $row[0];
}
$biggerThan = $numPictures % $numPicturesPerPage;
$numPages = intval(floor($numPictures / $numPicturesPerPage));
if ($biggerThan > 0) {
$numPages++;
}
if (preg_match("/d+/", $pageIndex, $matches)) { $pageIndex = $matches[0]; } // Filtert die Zahl aus z.B. 2.php heraus
function navSeiten($aktuell,$gesamt) {
$pageIndex = $_GET['pageindex'];
$next = $aktuell + 1;
$previous = $aktuell - 1;
$ausgabe = '<ul class="pager">';
if ($aktuell <= 3 ) {
$seiten = array(1,2,3,4,5);
}else if ($aktuell == $gesamt -3 ) {
$seiten = array($aktuell -2 ,$aktuell-1 ,$aktuell , $aktuell +1 , $aktuell +2);
}else if ($aktuell >= $gesamt -3 ) {
$seiten = array($gesamt -4 , $gesamt -3, $gesamt -2 , $gesamt -1, $gesamt );
} else {
$seiten = array( $aktuell -2 ,$aktuell-1 ,$aktuell , $aktuell +1 , $aktuell +2);
}
//echo ($pageIndex);
?>
<li class="previous" style="<?php
if ($pageIndex == "1" ) { echo "display:none ";} ?>" >
<a href="galerie.php?pageindex=' . $previous . '" aria-label="Previous">
<span aria-hidden="true"></span>
</a>
</li>
<?php
foreach ($seiten as $seite) {
if ($seite == $pageIndex) {
$ausgabe .= '<li class="active">' . $seite . '</li>';
} else {
$ausgabe .='<li>' . $seite . '</li>';
}
}
?><li class="next" style="
<?php
/*$sql ="SELECT count(bild_id) FROM galeriebilder";
$rows = $dbh->query($sql);
foreach ($rows as $row) {
$numPictures = $row[0];
}*/
$numPicturesPerPage = 20;
$numPages = intval(floor($numPictures / $numPicturesPerPage));
if ($pageIndex == $numPages ) { echo "display:none"; }
?>" >
<a href="galerie.php?pageindex=' . $next . '" aria-label="Next">
<span aria-hidden="true"></span>
</a>
</li>
<?php
//echo $numPages;
echo $numPictures;
$ausgabe .= '</ul>';
return $ausgabe;
}
?>
<div class="col-lg-12 no-gutter-12 pagging">
<?php echo navSeiten($pageIndex,$numPages) ?> <!-- Dies ist der Funktionsaufruf der Funktion die die komplette Leiste der Seitenaufrufe-->
</div>
</div>
</div>
Here is the CSS Code:
.picture {
clear:both;
}
.pic {
//float: left;
width:50%;
display:block;
}
figure {
display: table-cell;
text-align: center;
}
Sorry that it's now a mix of all solutions I found on the internet. I didn't changed it already.
I hope you can help me.
With kindly greetings Lukas Stetter

Related

Holding PHP/AJAX filter selections during pagination

I have a page which allows you to filter results using an AJAX call which works fine, I have added pagination which work fine initially but as soon as you move to another page, the checkbox becomes unchecked and it just shows all results again. I assume this is because the page is reloading when it moves to page 2, is there a way of keep the filter setting set and continue to show the results from the filter AJAX. The pagination obvisouly works fine when no filter is selected but my brain just doesn't seem to be working and can't work this out.
Any help would be appreciated!
My code is below, I am also aware that currently my code is open to sql injection but just trying to get everything to work and then will go back through it:
<body>
<?php include("PHP/header.php"); ?>
<div class="container-fluid">
<div class="container" style="margin-top: 2%; text-align: center;">
<h1> Reviews</h1>
On This page you will find our reviews on music tech and software
<br/>
<br/>
<br/>
Filter Reviews:
<ul class="list-group">
<?php
$search = $conn->prepare("SELECT DISTINCT reviewcat FROM review_db ORDER BY reviewcat");
$search->execute();
while ($row = $search->fetch(PDO::FETCH_ASSOC)) {
?>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="<?=$row['reviewcat'];?>" id="reviewcat"> <?=$row['reviewcat']; ?>
</label>
</div>
</li>
<?php } ?>
</ul>
</div>
<br/><br/>
<div class="row-fluid ">
<h5 class="text-center" id="textChange"> All Reviews </h5>
<hr>
<div class="text-center">
<img src="Images/loader.gif" id="loader" width="100" style="display: none">
</div>
<?php
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$no_of_records_per_page = 8;
$offset = ($pageno-1) * $no_of_records_per_page;
// Prev + Next
$prev = $pageno - 1;
$next = $pageno + 1;
?>
<div id="result" class="card-deck card_group_style pt-4" >
<?php
$stmt = $conn->prepare("SELECT COUNT(*) FROM review_db");
$stmt->execute();
$total_rows = $stmt->fetchColumn();
$total_pages = ceil($total_rows / $no_of_records_per_page);
$result = $conn->prepare("SELECT * FROM review_db ORDER BY reviewsub DESC LIMIT $offset, $no_of_records_per_page ");
$result->execute();
?>
<?php while ($row = $result->fetch(PDO::FETCH_ASSOC)) {// Important line !!! Check summary get row on array .. ?>
<?php
$my_date = $row['reviewsub'];
$date = DATE("d/m/Y",strtotime($my_date));
?>
<div class="col-sm-6 col-lg-3 py-2">
<div class="card mb-4">
<img class="card-img-top card-images " src="Images/Reviews/<?php echo $row['reviewimage1'];?>" alt="<?php echo $row['reviewtitle'];?>" >
<div class="card-body">
<h5 class="card-title"><?php echo $row['reviewtitle'];?></h5>
<p class="card-text"><?php echo $row['reviewsynop'];?></p>
<a href="Reviews/review-content.php?id=<?php echo $row['id'];?>&reviewtitle=<?php echo $row['reviewtitle'];?>" class="btn btn-primary my-4" >Read More</a>
<div class="card-footer" style="padding: 1%;">
<small class="text-muted">Submitted: <?php echo $date; ?></small>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
<div class="container">
<!-- Pagination Controller -->
<?php
if($total_pages <= 1){
$hidepage = 'none';
}else{
$hidepage = 'flex';
}
?>
<ul class="pagination justify-content-center pagination-mb" style="display: <?php echo $hidepage; ?>">
<li><a class="page-link" href="?pageno=1">First</a></li>
<li class="page-item <?php if($pageno <= 1){ echo 'disabled'; } ?>">
<a class="page-link" href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".($pageno - 1); } ?>">Prev</a>
</li>
<?php for($i = 1; $i <= $total_pages; $i++ ): ?>
<li class="page-item <?php if($pageno == $i) {echo 'active'; } ?>">
<a class="page-link" href="?pageno=<?= $i; ?>"> <?= $i; ?> </a>
</li>
<?php endfor; ?>
<li class="page-item <?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
<a class="page-link" href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?pageno=".($pageno + 1); } ?>">Next</a>
</li>
<li><a class="page-link" href="?pageno=<?php echo $total_pages; ?>">Last</a></li>
</ul>
<!-- Pagination end -->
</div>
</div>
</div>
</div>
<?php include("PHP/footer.php"); ?>
</div>
</body>
<?php include("PHP/js.php"); ?>
<script>
$(document).ready(function(){
$('#link-review,#link-footer-review').addClass('active');
});
</script>
<script type="text/javascript">
$(document).ready(function(){
function get_filter_text(text_id){
var filterData = [];
$('#'+text_id+':checked').each(function(){
filterData.push($(this).val());
});
return filterData;
}
$(".product_check").click(function(){
if ($(this).prop('checked')) {
$("#loader").show();
var action = 'data';
var reviewcat = get_filter_text('reviewcat');
$.ajax({
method:'POST',
url:'reviewaction.php',
data:{action:action,reviewcat:reviewcat},
success:function(response){
$("#result").html(response);
$("#loader").hide();
$("#textChange").text("Filtered Reviews");
}
});
} else {
$("#loader").show();
var action = 'data';
var reviewcat = get_filter_text('reviewcat');
$.ajax({
method:'POST',
url:'reviewaction.php',
data:{action:action,reviewcat:reviewcat},
success:function(response){
$("#result").html(response);
$("#loader").hide();
$("#textChange").text("All Reviews");
}
});
}
});
});
</script>
reviewaction.php:
<?php
if(isset($_POST['action'])){
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$no_of_records_per_page = 8;
$offset = ($pageno-1) * $no_of_records_per_page;
// Prev + Next
$prev = $pageno - 1;
$next = $pageno + 1;
$checksql = "SELECT COUNT(*) FROM review_db WHERE reviewcat !=''";
$sql = "SELECT * FROM review_db WHERE reviewcat !=''";
if(isset($_POST['reviewcat'])){
$reviewcat = implode("','", $_POST['reviewcat']);
$checksql .="AND reviewcat IN('".$reviewcat."')";
$sql .="AND reviewcat IN('".$reviewcat."')";
}
$resultpag = $conn->prepare($checksql);
$resultpag->execute();
$total_rows = $resultpag->fetchColumn();
$total_pages = ceil($total_rows / $no_of_records_per_page);
$sql .="ORDER BY reviewsub DESC LIMIT $offset, $no_of_records_per_page ";
$result = $conn->prepare($sql);
$result->execute();
$output='';
if (count($result) > 0) {
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$my_date = $row['reviewsub'];
$date = DATE("d/m/Y",strtotime($my_date));
$output .= '
<div class="col-sm-6 col-lg-3 py-2">
<div class="card mb-4">
<img class="card-img-top card-images" src="Images/Reviews/'.$row['reviewimage1'].'" alt="'.$row['reviewtitle'].'" >
<div class="card-body">
<h5 class="card-title">'.$row['reviewtitle'].'</h5>
<p class="card-text">'.$row['reviewsynop'].'</p>
<a href="Reviews/review-content.php?id='.$row['id'].'&reviewtitle='.$row['reviewtitle'].'" class="btn btn-primary my-4" >Read More</a>
<div class="card-footer" style="padding: 1%;">
<small class="text-muted">Submitted: '.$date.'</small>
</div>
</div>
</div>
</div>
';
} //While Loop End
}else{
$output = "<h3>No Reviews Found!</h3>";
}
echo $output;
}
?>
You can do it a couple of ways. One is to add the filter to the GET URI parameters of each page link at the end of your filter function, and add code that marks the filters as selected/checked if the parameters exist in the URI before running the POST request. The other is to change the code so that pagination is done with the same POST request instead of actually navigating to a new URL.

How to make PHP Pagination is working in Mobile?

I have implement pagination in PHP.
It's works fine on Desktop and Laptop but but its not working on mobile devices.
member_list.php
<?php
include "../connection.php";
extract($_REQUEST);
$perPage = 50; // total records per page
// page
$conditionArr = array(); // array for condition
$condition = ""; // conditions
$pages = ""; // how many pages created
$type = "";
$company = "";
$message = "";
$rersArr = array();
$data = array();
$templeArr = array();
if ($sabhasadnumber != "") {
array_push($conditionArr, "sabhasad_number = '" . $sabhasadnumber . "'");
}
if ($Sakhe != "") {
array_push($conditionArr, "sakhe_id = '" . $Sakhe . "'");
}
if ($g != "") {
array_push($conditionArr, "gender LIKE '%" . $g . "%'");
}
$sql = "SELECT * FROM members WHERE village_id = '$id' and is_active = '1' and status = '1' ";
if (sizeOf($conditionArr) > 0) {
$condition = implode(" AND ", $conditionArr);
// echo $condition;
$sql .= " AND $condition";
}
$start = ($page - 1) * $perPage;
$sql1 = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$count = mysqli_num_rows($sql1);
$pages = ceil($count / $perPage);
$sql .= " ORDER BY members_id ASC";
$sql .= " limit $start,$perPage ";
$num_of_rows = mysqli_num_rows($sql1);
?>
<input type="hidden" name="total_page" id="total_page" value="<?php echo $pages; ?>">
<input type="hidden" name="current_page" class="current_page" id="current_page"
value="<?php echo $page; ?>">
<div class="member--items">
<div class="row gutter--15 AdjustRow">
<div class="row">
<?php
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
?>
<div class="col-md-3 col-xs-6 col-xxs-12" style="height:300px">
<div class="member--item ">
<?php
$imgurl = "";
if ($row["image"] != "") {
$imgurl = "../admin/uploads/" . $row["image"];
} else {
$imgurl = "../admin/uploads/default_profile.jpg";
}
?>
<div class="img img-circle">
<a href="sabhasad_family.php?id=<?php echo $row['sabhasad_number']; ?>" class="btn-link">
<img src="<?php echo $imgurl; ?>" alt=""
style="height: 150px; width: 150px;">
<!-- <img src="../admin/uploads/default_profile.jpg" alt=""-->
<!-- style="height: 150px; width: 150px;">-->
</a>
</div>
<div class="name">
<h3 class="h fs--12">
<p>
<a href="sabhasad_family.php?id=<?php echo $row['sabhasad_number']; ?>"> <?php echo $row['surname'] ." " ;
echo $row['name'] ." ";
echo $row['middle_name'] ." "; ?></a></p>
</h3>
</div>
<div class="activity">
<p>સભાસદ નંબર : <?php echo $row['sabhasad_number']; ?> </p>
</div>
</div>
</div>
<?php
}
}
else { ?>
<?php
}
?>
member_list.js
var page = 1;
var total_page = "";
function getresult(searches) {
$.ajax({
url: "process/member_list.php",
type: "post",
data: {
sabhasadnumber: $("#sabhasadnumber").val(),
id: $("#village_id").val(),
Sakhe: $("#Sakhe").val(),
g: $("#g").val(),
page: page
},
success: function (data) {
// alert(data);
if (data != "") {
if (searches === true) {
$("#mlist").html(data);
} else {
$("#mlist").append(data);
}
}
}
});
}
$(document).ready(function () {
getresult(false);
$(window).scroll(function () {
if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
if (parseInt($(".current_page:last").val()) < parseInt($("#total_page").val())) {
page = parseInt($(".current_page:last").val()) + 1;
// alert("scrool"+$(".current_page:last").val());
getresult(false);
}
}
});
$("#reset").click(function () {
$("#Sakhe").val("");
$("#sabhasadnumber").val("");
$("#g").val("");
page = 1;
getresult(true);
});
$("#shopsubmit").click(function () {
page = 1;
getresult(true);
});
$("#freset").click(function () {
$("#subcategory").val("");
page = 1;
getresult(true);
});
});
My front page code look like this
village.php
<?php
include 'connection.php';
include 'header.php';
$id = $_GET['id'];
$totalvillagemember = mysqli_num_rows(mysqli_query($conn, "select members_id from members WHERE village_id = $id AND is_active = '1' and status = '1' "));
$villagename = mysqli_query($conn, "SELECT name from village WHERE village_id = $id");
$row1 = mysqli_fetch_array($villagename);
?>
<body>
<!-- Preloader Start -->
<div id="preloader">
<div class="preloader--inner"></div>
</div>
<!-- Preloader End -->
<!-- Wrapper Start -->
<div class="wrapper">
<!-- Header Section Start -->
<!-- Header Section End -->
<!-- Page Header Start -->
<div class="page--header pt--60 pb--60 text-center" data-bg-img="img/page-header-img/bg.jpg" data-overlay="0.85">
<div class="container">
<div class="title">
<h2 class="h1 text-white">સભ્યો</h2>
</div>
<ul class="breadcrumb text-gray ff--primary">
<li>હોમ</li>
<li class="active"><span class="text-primary">સભ્યો</span></li>
</ul>
</div>
</div>
<!-- Page Header End -->
<!-- Page Wrapper Start -->
<section class="page--wrapper pt--80 pb--20">
<div class="container">
<div class="row">
<!-- Main Content Start -->
<div class="main--content col-md-8 pb--60" data-trigger="stickyScroll">
<div class="main--content-inner">
<!-- Filter Nav Start -->
<div class="filter--nav clearfix">
<div class="filter--link">
<h2 class="">ગામ : <?php echo $row1['name']; ?></h2>
<h2 class="">કુલ સભ્ય :<?php echo number_format($totalvillagemember) ?></h2>
</div>
</div>
<div id="mlist" name="mlist">
</div>
<!-- Page Count End -->
</div>
</div>
<!-- Main Content End -->
<!-- Main Sidebar Start -->
<div class="main--sidebar col-md-4 pb--60" data-trigger="stickyScroll">
<!-- Widget Start -->
<div class="widget">
<h2 class="h4 fw--700 widget--title">સભ્ય શોધો</h2>
<!-- Buddy Finder Widget Start -->
<div class="buddy-finder--widget">
<form name="village" id="village">
<div class="row">
<div class="col-xs-6 col-xxs-12">
<div class="form-group">
<label>
<span class="text-darker ff--primary fw--500">શોધી રહ્યો છુ</span>
<select class="form-control form-sm" name="g" id="g">
<option value=""> પસંદ કરો
</option>
<option value = "M">પુરુષ</option>
<option value = "F">સ્ત્રી</option>
</select>
</label>
</div>
</div>
<div class="col-xs-6 col-xxs-12">
<div class="form-group">
<label>
<span class="text-darker ff--primary fw--500">સભાસદ નંબર</span>
<input type="text" name="sabhasadnumber" id="sabhasadnumber"
class="form-control form-sm"
placeholder="સભાસદ નંબર">
</label>
</div>
</div>
<div class="col-xs-6 col-xxs-12">
<div class="form-group">
<label>
<input type="hidden" id="village_id" name="village_id"
value="<?php echo $id; ?>">
<span class="text-darker ff--primary fw--500">સાખે</span>
<select class="form-control form-sm" name="Sakhe" id="Sakhe">
<option value=""> સાખે પસંદ કરો
</option>
<?php
$query = mysqli_query($conn, "select * from sakhe where status = 1 order by sakhe_id asc");
while ($row = mysqli_fetch_array($query)) {
echo "<option value = " . $row['sakhe_id'] . ">" . $row['name'] . "</option>";
}
?>
</select>
</label>
</div>
</div>
<div class="col-xs-12">
<button type="button" onclick="getresult()" class="btn btn-primary" id="shopsubmit" name="shopsubmit">
સબમિટ કરો
</button>
<button type="button" onclick="getresult()" class="btn btn-primary"
id="reset" name="reset">
ફરીથી સેટ કરો
</button>
</div>
</div>
</form>
</div>
</div>
<!-- Widget End -->
</div>
<!-- Main Sidebar End -->
</div>
</div>
</section>
<!-- Page Wrapper End -->
<!-- Footer Section Start -->
<!-- Footer Widgets End -->
<!-- Footer Extra Start -->
<?php include 'footer.php'; ?>
<!-- Footer Section End -->
</div>
<!-- Wrapper End -->
<!-- Back To Top Button Start -->
<div id="backToTop">
<i class="fa fa-caret-up"></i>
</div>
<!-- Back To Top Button End -->
<!-- ==== Plugins Bundle ==== -->
<script src="js/plugins.min.js"></script>
<!-- ==== Color Switcher Plugin ==== -->
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<!-- ==== Main Script ==== -->
<script src="js/main.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<!--<script src="http://malsup.github.com/jquery.form.js"></script>-->
<script src="myjs/member_list.js"></script>
</body>
<!-- Mirrored from themelooks.us/demo/socifly/html/members.html by HTTrack Website Copier/3.x [XR&CO'2014], Mon, 30 Sep 2019 09:33:46 GMT -->
</html>
This pagination is not working only on Mobile devices.
Please help.

PHP performance drops when adding while output string to variable

First some general information.
My PHP runs approx 1000-1500 while loops depending on what the database returns.
Normal execution time is approx 0.3 sec. That's great :)
Now I need to load the PHP output on another page using AJAX and therefore I start adding the output to a $html variable in stead of just doing echo on it, so that it can be encoded to JSON format and send over AJAX.
I use the $html .= some_output for this. (this task must be done as many times as the while loop runs).
However after adding the $html .= in stead of echo the script is now taking +9 sec's to finish!!
That's clearly not optimal to the end user who is waiting for the AJAX call to return with some results.
I did some microtime on the code to be sure where the problem origins from - and no doubt it is the .= operator.
Any suggestions on how to minimize this?
EDIT: Here comes the code blocks.
First the one that loads flawlessly.
<?php
$time_start = microtime(true);
session_start();
include "../functions/sqlsrv_connect.php";
$skid_id = $_GET['skid_id'];
$vendor_id = $_GET['vendor_id'];
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<link rel="stylesheet" type="text/css" href="../stylesheets/fonts.css">
<link rel="stylesheet" type="text/css" href="../stylesheets/linkcontainer.css">
<script src="/js/jquery-1.11.3.min.js"></script>
</head>
<body>
<div id="parent" style="display: none;">
<?php
$sql_get_vdm_sections = "
SELECT
ml2.level2_descr,
ml2.level2_id
FROM
main_vdm_level2 AS ml2
JOIN
vdm_index AS vdm_i
ON
vdm_i.level2_id = ml2.level2_id
WHERE
vdm_i.skid_id = $skid_id
AND
vdm_i.vendor_id = $vendor_id
GROUP BY
ml2.level2_descr,
ml2.level2_id
ORDER BY
ml2.level2_id
";
$get_vdm_sections = sqlsrv_query($sqlsrv, $sql_get_vdm_sections);
while($vdm_section = sqlsrv_fetch_array($get_vdm_sections,SQLSRV_FETCH_ASSOC)){
$level2_id = $vdm_section['level2_id'];
$level2_descr = $vdm_section['level2_descr'];
?>
<div id="S<?php echo $level2_id;?>" class="section">
<div class="sectionhead">
<?php echo $level2_descr;?>
</div>
<div class="container_holder">
<div id="O<?php echo $level2_id;?>" class="obsolete_section">
<div class="obsolete_lineholder">
<div class="obsolete_header">
<div class="clmn_header">
<p class="small bold">Obsolete Documentation</p>
</div>
</div>
</div>
<div class="obsolete_lineholder">
<div class="obsolete_linkholder">
<a href="#" class="div" target="_blank">
<div class="obsolete_text">
</div>
</a>
</div>
</div>
<div class="obsolete_lineholder">
<div class="obsolete_linkholder">
<a href="#" class="div" target="_blank">
<div class="obsolete_text">
</div>
</a>
</div>
</div>
<div class="obsolete_lineholder">
<div class="obsolete_linkholder">
<a href="#" class="div" target="_blank">
<div class="obsolete_text">
</div>
</a>
</div>
</div>
</div>
<div class="active_cnt">
<div class="button_holder">
<div class="active_btn">
<p class="small">Active Documentation</p>
</div>
<div class="obsolete_btn">
<p class="small">View Obsolete Documentation</p>
</div>
</div>
<div class="lineholder">
<div class="linkholder">
<div class="clmn_header">
<p class="small bold">Chapters</p>
</div>
</div>
</div>
<div class="linkholder_cnt">
<?php
$file_verification = true;
$sql_get_section_content = "
SELECT
file_verification,
level3_head,
level3_descr,
level4_descr,
doc_no,
doc_place,
doc_denominator,
doc_type
FROM
vdm_index
WHERE
skid_id = $skid_id
AND
vendor_id = $vendor_id
AND
level2_id = $level2_id
AND
level4_descr IS NULL
";
$get_section_content = sqlsrv_query($sqlsrv, $sql_get_section_content);
$row_number = 1;
while ($section_content = sqlsrv_fetch_array($get_section_content,SQLSRV_FETCH_ASSOC)){
$level3_file_verification = $section_content['file_verification'];
$level3_head = $section_content['level3_head'];
$level3_descr = $section_content['level3_descr'];
$level3_doc_no = $section_content['doc_no'];
$level3_doc_place = $section_content['doc_place'];
$level3_doc_denominator = $section_content['doc_denominator'];
$level3_doc_type = $section_content['doc_type'];
$level3_width = 100;
$row_id = 'S'.$level2_id.'_R'.$row_number;
$sql_get_level4 = "
SELECT
file_verification,
level4_descr,
doc_no,
doc_place,
doc_denominator,
doc_type
FROM
vdm_index
WHERE
skid_id = $skid_id
AND
vendor_id = $vendor_id
AND
level2_id = $level2_id
AND
level3_descr = '$level3_descr'
AND
level4_descr IS NOT NULL
";
$get_level4 = sqlsrv_query($sqlsrv, $sql_get_level4);
$level4_array = array();
while ($level4 = sqlsrv_fetch_array($get_level4,SQLSRV_FETCH_ASSOC)){
array_push($level4_array, $level4);
}
if (!empty($level4_array)){
$level3_width -= 10;
$level4_active = true;
}
else {
$level4_active = false;
}
if ($file_verification != true){
$level3_width -= 10;
$level3_flag = true;
}
else {
$level3_flag = false;
}
?>
<div class="lineholder">
<a href="../proj_hist.pdf" class="div" target="_blank">
<div class="project">
<?php
if ($level3_doc_denominator != NULL){
?>
<svg x="0px" y="0px" width="45px" height="100%" viewBox="0 0 45 25" preserveAspectRatio="none">
<rect fill="#66FECB" width="27.1" height="25"></rect>
<polygon fill="#66FECB" points="45,12.5 27,0.000 27,25.000 "></polygon>
</svg>
<?php
}
?>
</div>
<div class="project_name">
<div class="project_text">
<?php echo $level3_doc_denominator;?>
</div>
</div>
</a>
<div class="linkholder" >
<div id="<?php echo $row_id;?>" class="top_row" >
<div class="projectstatus" style="background: #ffffff; width: 100%;">
</div>
<?php
if(!(empty($level3_head))&&empty($level3_descr)){
?>
<div class="link_text level3_head" style="float: left; width: 100%; white-space: nowrap;">
<?php echo $level3_head;?>
</div>
<?php
}
else {
?>
<a href="../functions/load_doc.php?doc_no=<?php echo $level3_doc_no.'&doc_place='.$level3_doc_place.'&doc_type='.$level3_doc_type.'&doc_denominator='.$level3_doc_denominator;?>" class="div" target="_blank">
<div class="link_text" style="float: left; width: <?php echo $level3_width;?>%; white-space: nowrap; overflow: hidden;">
<?php echo $level3_descr;?>
</div>
</a>
<?php
if($level3_flag == true){
?>
<div class="flag_this">
</div>
<?php
}
if ($level4_active == true){
?>
<div id="<?php echo $row_id;?>_EXPAND" class="expand">
<div class="expand_icon <?php echo $row_id;?>_EXPAND_icon">
</div>
</div>
<?php }
}
?>
</div>
<div class="clear">
</div>
<?php
if ($level4_active == true) {
?>
<div class="<?php echo $row_id;?>_level4">
<?php
foreach ($level4_array as $level4){
$level4_doc_no = $level4['doc_no'];
$level4_doc_place = $level4['doc_place'];
$level4_doc_type = $level4['doc_type'];
?>
<div id="<?php echo $row_id;?>_SL1" class="sub_row <?php echo $row_id;?>_EXPAND_sub">
<div class="projectstatus" style="background: #ffffff; width: 50%;">
</div>
<a href="../functions/load_doc.php?doc_no=<?php echo $level4_doc_no.'&doc_place='.$level4_doc_place.'&doc_type='.$level4_doc_type;?>" class="div" target="_blank">
<div class="sub_link_text">
<?php echo $level4['level4_descr'];?>
</div>
</a>
</div>
<div class="clear">
</div>
<?php
}
?>
</div>
<?php
}
?>
</div>
</div>
<?php
$row_number++;
}
?>
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
<div id="additional_box">
<div id="additional_info">
I hold additional information about this document!
</div>
<div id="close_additional" onclick="close_additional()">
</div>
</div>
<script>
var mouseX;
var mouseY;
$(document).mousemove( function(e) {
mouseX = e.pageX;
mouseY = e.pageY;
});
function additional() {
mouseY -= 5;
mouseX += 20;
$('#additional_box').css({'top':mouseY,'left':mouseX}).fadeIn('slow');
};
function close_additional() {
$('#additional_box').hide();
};
function reset_expansion_icon(){
var index;
var i = document.getElementsByClassName('expand_icon');
for (index =0; index < i.length; ++index) {
i[index].style.backgroundImage = "url('../img/expand.png')";
}
}
function reset_this_expansion_icon(me){
var index;
var e = $(me).children('.expand_icon')
for (index =0; index < e.length; ++index) {
e[index].style.backgroundImage = "url('../img/expand.png')";
}
}
$('.flag_this').click(function(){
additional();
});
$('.obsolete_btn').click(function() {
$('.section').hide();
var section = $(this).closest('.section');
$(section).show();
//var btn_parent = $(this).parent();
var container = $(this).closest('.container_holder');
var obsolete = $(container).children('.obsolete_section');
$(obsolete).fadeIn(500);
$('.sub_row').hide();
reset_expansion_icon();
});
$('.active_btn').click(function() {
$('.section').fadeIn(500);
$('.obsolete_section').hide();
$('.sub_row').hide();
reset_expansion_icon();
});
$('.expand').click(function() {
var parent = $(this).parent('.top_row');
var level4_class = $(parent).attr('id') + '_level4';
var sub_row_class = $(parent).attr('id') + '_EXPAND_sub';
var index;
var e = document.getElementsByClassName(sub_row_class);
var d = document.getElementsByClassName(level4_class);
for (index = 0; index < e.length; ++index) {
if(e[index].style.display == 'block'){
$(d[0]).slideUp('slow');
e[index].style.display = 'none';
}
else{
d[0].style.display = 'block';
$(e[index]).fadeIn('slow');
}
}
var expand_icon_class = $(parent).attr('id') + '_EXPAND_icon';
var i = document.getElementsByClassName(expand_icon_class);
if (e[0].style.display == 'block')
i[0].style.backgroundImage = "url('../img/condense.png')";
else
var me = $(this);
reset_this_expansion_icon(me);
});
$(window).load(function(){
$('#parent').fadeIn(300);
});
</script>
</body>
</html>
<?php
echo 'Total execution time in seconds: ' . (microtime(true) - $time_start);
?>
And the the .= version (html, head and body is removed as it will load on another page)
<?php
$time_start = microtime(true);
session_start();
include "sqlsrv_connect.php";
$skid_id = $_POST['skid_id'];
$vendor_id = $_POST['vendor_id'];
$html = "
<div id='parent'>
";
$sql_get_vdm_sections = "
SELECT
ml2.level2_descr,
ml2.level2_id
FROM
main_vdm_level2 AS ml2
JOIN
vdm_index AS vdm_i
ON
vdm_i.level2_id = ml2.level2_id
WHERE
vdm_i.skid_id = $skid_id
AND
vdm_i.vendor_id = $vendor_id
GROUP BY
ml2.level2_descr,
ml2.level2_id
ORDER BY
ml2.level2_id
";
$get_vdm_sections = sqlsrv_query($sqlsrv, $sql_get_vdm_sections);
while($vdm_section = sqlsrv_fetch_array($get_vdm_sections,SQLSRV_FETCH_ASSOC)){
$level2_id = $vdm_section['level2_id'];
$level2_descr = $vdm_section['level2_descr'];
$html .= "
<div id='S$level2' class='section'>
<div class='sectionhead'>
$level2_descr
</div>
<div class='container_holder'>
<div id='O$level2_id' class='obsolete_section'>
<div class='obsolete_lineholder'>
<div class='obsolete_header'>
<div class='clmn_header'>
<p class='small bold'>Obsolete Documentation</p>
</div>
</div>
</div>
<div class='obsolete_lineholder'>
<div class='obsolete_linkholder'>
<a href='#' class='div' target='_blank'>
<div class='obsolete_text'>
</div>
</a>
</div>
</div>
<div class='obsolete_lineholder'>
<div class='obsolete_linkholder'>
<a href='#' class='div' target='_blank'>
<div class='obsolete_text'>
</div>
</a>
</div>
</div>
<div class='obsolete_lineholder'>
<div class='obsolete_linkholder'>
<a href='#' class='div' target='_blank'>
<div class='obsolete_text'>
</div>
</a>
</div>
</div>
</div>
<div class='active_cnt'>
<div class='button_holder'>
<div class='active_btn'>
<p class='small'>Active Documentation</p>
</div>
<div class='obsolete_btn'>
<p class='small'>View Obsolete Documentation</p>
</div>
</div>
<div class='lineholder'>
<div class='linkholder'>
<div class='clmn_header'>
<p class='small bold'>Chapters</p>
</div>
</div>
</div>
<div class='linkholder_cnt'>
";
$file_verification = true;
$sql_get_section_content = "
SELECT
file_verification,
level3_head,
level3_descr,
level4_descr,
doc_no,
doc_place,
doc_denominator,
doc_type
FROM
vdm_index
WHERE
skid_id = $skid_id
AND
vendor_id = $vendor_id
AND
level2_id = $level2_id
AND
level4_descr IS NULL
";
$get_section_content = sqlsrv_query($sqlsrv, $sql_get_section_content);
$row_number = 1;
while ($section_content = sqlsrv_fetch_array($get_section_content,SQLSRV_FETCH_ASSOC)){
$level3_file_verification = $section_content['file_verification'];
$level3_head = $section_content['level3_head'];
$level3_descr = $section_content['level3_descr'];
$level3_doc_no = $section_content['doc_no'];
$level3_doc_place = $section_content['doc_place'];
$level3_doc_denominator = $section_content['doc_denominator'];
$level3_doc_type = $section_content['doc_type'];
$level3_width = 100;
$row_id = 'S'.$level2_id.'_R'.$row_number;
$sql_get_level4 = "
SELECT
file_verification,
level4_descr,
doc_no,
doc_place,
doc_denominator,
doc_type
FROM
vdm_index
WHERE
skid_id = $skid_id
AND
vendor_id = $vendor_id
AND
level2_id = $level2_id
AND
level3_descr = '$level3_descr'
AND
level4_descr IS NOT NULL
";
$get_level4 = sqlsrv_query($sqlsrv, $sql_get_level4);
$level4_array = array();
while ($level4 = sqlsrv_fetch_array($get_level4,SQLSRV_FETCH_ASSOC)){
array_push($level4_array, $level4);
}
if (!empty($level4_array)){
$level3_width -= 10;
$level4_active = true;
}
else {
$level4_active = false;
}
if ($file_verification != true){
$level3_width -= 10;
$level3_flag = true;
}
else {
$level3_flag = false;
}
$html .= "
<div class='lineholder'>
<a href='../proj_hist.pdf' class='div' target='_blank'>
<div class='project'>
";
if ($level3_doc_denominator != NULL){
$html .= "
<svg x='0px' y='0px' width='45px' height='100%' viewBox='0 0 45 25' preserveAspectRatio='none'>
<rect fill='#66FECB' width='27.1' height='25'></rect>
<polygon fill='#66FECB' points='45,12.5 27,0.000 27,25.000 '></polygon>
</svg>
";
}
$html .= "
</div>
<div class='project_name'>
<div class='project_text'>
$level3_doc_denominator
</div>
</div>
</a>
<div class='linkholder' >
<div id='$row_id' class='top_row' >
<div class='projectstatus' style='background: #ffffff; width: 100%;'>
</div>
";
if(!(empty($level3_head))&&empty($level3_descr)){
$html .= "
<div class='link_text level3_head' style='float: left; width: 100%; white-space: nowrap;'>
$level3_head
</div>
";
}
else {
$html .= "
<a href='../functions/load_doc.php?doc_no=$level3_doc_no&doc_place=$level3_doc_place&doc_type=$level3_doc_type&doc_denominator=$level3_doc_denominator' class='div' target='_blank'>
<div class='link_text' style='float: left; width: $level3_width%; white-space: nowrap; overflow: hidden;'>
$level3_descr
</div>
</a>
";
if($level3_flag == true){
$html .= "
<div class='flag_this'>
</div>
";
}
if ($level4_active == true){
$html .= "
<div id='$row_id_EXPAND' class='expand'>
<div class='expand_icon $row_id_EXPAND_icon'>
</div>
</div>
";
}
}
$html .= "
</div>
<div class='clear'>
</div>
";
if ($level4_active == true) {
$html .= "
<div class='$row_id_level4'>
";
foreach ($level4_array as $level4){
$level4_doc_no = $level4['doc_no'];
$level4_doc_place = $level4['doc_place'];
$level4_doc_type = $level4['doc_type'];
$level4_descr = $level4['level4_desr'];
$html .= "
<div id='$row_id_SL1' class='sub_row $row_id_EXPAND_sub'>
<div class='projectstatus' style='background: #ffffff; width: 50%;'>
</div>
<a href='../functions/load_doc.php?doc_no=$level4_doc_no&doc_place=$level4_doc_place&doc_type=$level4_doc_type' class='div' target='_blank'>
<div class='sub_link_text'>
$level4_descr
</div>
</a>
</div>
<div class='clear'>
</div>
";
}
$html .= "
</div>
";
}
$html .= "
</div>
</div>
";
$row_number++;
}
$html .= "
</div>
</div>
</div>
</div>
";
$time_end = microtime(true);
$total_time = $time_end - $time_start;
$html .= 'Total execution time in seconds: ' . $total_time;
}
$html .= "
</div>
<div id='additional_box'>
<div id='additional_info'>
I hold additional information about this document!
</div>
<div id='close_additional' onclick='close_additional()'>
</div>
</div>
";
$script = "
var mouseX;
var mouseY;
$(document).mousemove( function(e) {
mouseX = e.pageX;
mouseY = e.pageY;
});
function additional() {
mouseY -= 5;
mouseX += 20;
$('#additional_box').css({'top':mouseY,'left':mouseX}).fadeIn('slow');
};
function close_additional() {
$('#additional_box').hide();
};
function reset_expansion_icon(){
var index;
var i = document.getElementsByClassName('expand_icon');
for (index =0; index < i.length; ++index) {
i[index].style.backgroundImage = 'url('../img/expand.png')';
}
}
function reset_this_expansion_icon(me){
var index;
var e = $(me).children('.expand_icon')
for (index =0; index < e.length; ++index) {
e[index].style.backgroundImage = 'url('../img/expand.png')';
}
}
$('.flag_this').click(function(){
additional();
});
$('.obsolete_btn').click(function() {
$('.section').hide();
var section = $(this).closest('.section');
$(section).show();
//var btn_parent = $(this).parent();
var container = $(this).closest('.container_holder');
var obsolete = $(container).children('.obsolete_section');
$(obsolete).fadeIn(500);
$('.sub_row').hide();
reset_expansion_icon();
});
$('.active_btn').click(function() {
$('.section').fadeIn(500);
$('.obsolete_section').hide();
$('.sub_row').hide();
reset_expansion_icon();
});
$('.expand').click(function() {
var parent = $(this).parent('.top_row');
var level4_class = $(parent).attr('id') + '_level4';
var sub_row_class = $(parent).attr('id') + '_EXPAND_sub';
var index;
var e = document.getElementsByClassName(sub_row_class);
var d = document.getElementsByClassName(level4_class);
for (index = 0; index < e.length; ++index) {
if(e[index].style.display == 'block'){
$(d[0]).slideUp('slow');
e[index].style.display = 'none';
}
else{
d[0].style.display = 'block';
$(e[index]).fadeIn('slow');
}
}
var expand_icon_class = $(parent).attr('id') + '_EXPAND_icon';
var i = document.getElementsByClassName(expand_icon_class);
if (e[0].style.display == 'block')
i[0].style.backgroundImage = 'url('../img/condense.png')';
else
var me = $(this);
reset_this_expansion_icon(me);
});
$(window).load(function(){
$('#parent').fadeIn(300);
});
";
$json = array();
$json['html'] = $html;
$json['script'] = $script;
header('Content-Type: application/json');
echo json_encode( $json );
?>
First some basic benchmarking
Actually... looking a bit more into it, the concat operator should be faster than the (often more convenient) array method...
<?php
ini_set('memory_limit', '256M');
$start = microtime(true);
$html = array();
for ($i=0; $i < 900000; $i++) {
$html[] = "Line number $i\n";
}
$html_out = implode('', $html);
$time_spent = microtime(true) - $start;
printf("Array method: %ss\n", number_format($time_spent, 5));
printf("Array method md5: %s\n", md5($html_out));
unset($start);unset($html);unset($html_out);
$start = microtime(true);
$html = '';
for ($i=0; $i < 900000; $i++) {
$html .= "Line number $i\n";
}
$time_spent = microtime(true) - $start;
printf("Concat method: %ss\n", number_format($time_spent, 5));
printf("Concat method md5: %s\n", md5($html));
unset($start);unset($html);unset($html_out);
$start = microtime(true);
ob_start();
for ($i=0; $i < 900000; $i++) {
echo "Line number $i\n";
}
$html = ob_get_clean();
$time_spent = microtime(true) - $start;
printf("Output buffering method: %ss\n", number_format($time_spent, 5));
printf("Output buffering method md5: %s\n", md5($html));
Output:
Array method: 0.43333s
Array method md5: d3700cc66c04760d857e8bc9a986399c
Concat method: 0.21945s
Concat method md5: d3700cc66c04760d857e8bc9a986399c
Output buffering method: 0.22360s
Output buffering method md5: d3700cc66c04760d857e8bc9a986399c
...then trying to answer the question
Possible cause #1
I think the only reasonable explanation to your problem is lack of physical memory - when you do a plain echo (without output buffering), you don't keep much in memory - but if the strings you are concatenating are relatively long, then you may reach the limit of your physical memory when you store them in memory (no matter which method you use to do so)... so swapping could occur...?
Possible cause #2
On line 237 in the old code you have
<div id="<?php echo $row_id;?>_EXPAND" class="expand">
In the new code (on line 250) that becomes
echo "[...]<div id='$row_id_EXPAND' class='expand'>[...]"
Which means you are looking up a variable called $row_id_EXPAND - which I'm guessing doesn't exist, and therefore could cause a write operation to one or more error-logs for each iteration of the loop (and you have several of the same mistake elsewhere) This could potentially mean tens or even hundreds of thousands of lines in the error log per request === a lot of time spent writing all that data.
To avoid this kind of problem always encapsulate variables in {}, ie. make it:
echo "[...]<div id='{$row_id}_EXPAND' class='expand'>[...]"
Try ob_start() maybe.
Not actually verfied to be faster by myself but allows you to collect the results of echo from the buffer.
Example of using ob_start():
ob_start();
echo("Hello there!"); //would normally get printed to the screen/output to browser
$output = ob_get_clean();
None of the echo's will work as PHP will use a different string buffer whose results can be loaded into an array at the end. The ob_get_clean() will allow echo to start working again.
Hope this helps.

html output formatting php

I am trying to format some html output from my db using php and here's my problem:
How it should be formated:
...
<li>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-1</div>
<div class="some-class-1">ARRAY-ELEMENT-2</div>
<div class="some-class-1">ARRAY-ELEMENT-3</div>
<div class="some-class-2">ARRAY-ELEMENT-4</div>
</div>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-5</div>
<div class="some-class-1">ARRAY-ELEMENT-6</div>
<div class="some-class-1">ARRAY-ELEMENT-7</div>
<div class="some-class-2">ARRAY-ELEMENT-8</div>
</div>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-9</div>
<div class="some-class-1">ARRAY-ELEMENT-10</div>
<div class="some-class-1">ARRAY-ELEMENT-11</div>
<div class="some-class-2">ARRAY-ELEMENT-12</div>
</div>
</li>
<li>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-13</div>
<div class="some-class-1">ARRAY-ELEMENT-14</div>
<div class="some-class-1">ARRAY-ELEMENT-15</div>
<div class="some-class-2">ARRAY-ELEMENT-16</div>
</div>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-17</div>
<div class="some-class-1">ARRAY-ELEMENT-18</div>
<div class="some-class-1">ARRAY-ELEMENT-19</div>
<div class="some-class-2">ARRAY-ELEMENT-20</div>
</div>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-21</div>
<div class="some-class-1">ARRAY-ELEMENT-22</div>
<div class="some-class-1">ARRAY-ELEMENT-23</div>
<div class="some-class-2">ARRAY-ELEMENT-24</div>
</div>
</li>
... etc.
Data is held inside an array gathered from mysql db, so far I got to this:
$num_thumbs = 4; //Number od elemets in a row
$result = myQueryFunction("SELECT * FROM table_name ORDER BY Id ASC");
$num_rows = mysql_num_rows($result);
if (!empty($num_rows)) {
while ($row = mysql_fetch_array($result)) {
$thumbs_array[] = "<a href=\"" . $row[0] . "\"><img src=\"" . $row[1] . "\"";
}
$thumb_p = "<div>\n";
mysql_free_result($result);
$i = 1;
foreach ($thumbs_array as &$thumb_link) {
if ($i == $num_thumbs) {
$i = 1;
$thumb_p .= "<div class=\"some-class-2\">" . $thumb_link . "</div>";
$thumb_p .= "</div>\n<div class=\"row-wrapper\">\n";
} else {
$thumb_p .= "\t\n<div class=\"some-class-1\">" . $thumb_link . "</div>";
$i++;
}
}
$thumb_p .= "\n</div>\n";
}
print($thumb_p);
this covers only this part of code formatting:
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-13</div>
<div class="some-class-1">ARRAY-ELEMENT-14</div>
<div class="some-class-1">ARRAY-ELEMENT-15</div>
<div class="some-class-2">ARRAY-ELEMENT-16</div>
</div>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-17</div>
<div class="some-class-1">ARRAY-ELEMENT-18</div>
<div class="some-class-1">ARRAY-ELEMENT-19</div>
<div class="some-class-2">ARRAY-ELEMENT-20</div>
</div>
<div class="row-wrapper">
<div class="some-class-1">ARRAY-ELEMENT-21</div>
<div class="some-class-1">ARRAY-ELEMENT-22</div>
<div class="some-class-1">ARRAY-ELEMENT-23</div>
<div class="some-class-2">ARRAY-ELEMENT-24</div>
</div>
I've been thinking of using some variable $newList = null; that will be updated at each row, so I can check if it has changed the next one.
any ideas? Thanks in advance :)
It seems you're trying to put an li every three div. In this case, you can do this :
$thumb_p = "<li>\n<div class=\"row-wrapper\">\n";
mysql_free_result($result);
$i = 1;
$j = 0;
foreach ($thumbs_array as &$thumb_link) {
if ($i == $num_thumbs) {
$i = 1;
$thumb_p .= "<div class=\"some-class-2\">" . $thumb_link . "</div>";
$thumb_p .= "</div>\n";
if($j == 3)
{
$thumb_p .= "</li>\n<li>";
$j = 0;
}
$thumb_p .= "<div class=\"row-wrapper\">\n";
$j++;
} else {
$thumb_p .= "\t\n<div class=\"some-class-1\">" . $thumb_link . "</div>";
$i++;
}
}
$thumb_p .= "\n</div>\n</li>\n";
I can't actually try it but I think it's okay. A li is added every three row-wrapper you print.

How do I group a div in some class on MySQL while

I want add a different class on my MySQL result.
<?php while ($fetch = $db->fetch($query)) { ?>
<div class="result"><?php echo $fetch['title']; ?></div>
<?php } ?>
Output should be like this
<div class="group">
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
</div>
<div class="group">
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
</div>
<div class="group">
<div class="result">Title</div>
<div class="result">Title</div>
<div class="result">Title</div>
</div>
Here, every 6 results will have <div class="group"></div>.
Let me know
<div class="group">
<?php
$count = 0;
while ($fetch = $db->fetch($query)) {
if (++$count == 6) {
echo '</div><div class="group">';
$count = 0;
}
echo '<div class="result">' . $fetch['title'] . '</div>';
}
?>
</div>
create a counter that increments every time the loop is ran, then at the beginning of each loop check the value. if the value == 6 then close the current div and open a new one with the class change (you could make 2 counters to flip flop back and forth). Reset your counter after the div change.
--edit added code--
Make yourself 2 'group' div classes, 'group1' and 'group0' for the flip-flop
<div class="group1">
<?php
$count = 0;
$divstyle = 1;
while ($fetch = $db->fetch($query)) {
if (++$count == 6) {
echo '</div><div class="group'.(++$divstyle % 2).'">';
$count = 0;
}
echo '<div class="result">' . $fetch['title'] . '</div>';
}
?>
</div>
This might be a bit bad...
<?php $cnt=0;while ($fetch = $db->fetch($query)) { ?>
<div class="result"><?php if($cnt%6==0){echo "<div class=\"group\">";} echo $fetch['title'];if($cnt%6==0){echo "</div>";} $cnt++;?></div>
<?php } ?>

Categories