I have a certain number of images that are displaying per page. I want to set a maximum limit for the number of images shown on any page, for example setting a property such that no more than eight are shown. I've written this logic in PHP but I'm still seeing all images showing on any given page, ignoring any limit I set. The code:
$counter = 0;
foreach ($device as $value) {
$entry = $value;
echo "<head>";
echo '<script type="text/javascript">',
'window.setInterval(function() { ',
"document.getElementById('$counter').src='/latimage.php?&dev=$entry&random='+new Date().getTime();",
'},1000)',
'</script>';
echo "</head>";
echo "<body onLoad='setTimeout('refresh()',1000)'>";
echo "<td>$entry<img id= '$counter' width='100%' height='auto'></img></td>";
$counter = $counter + 1;
if ($counter == 4 || $counter == 8) {
echo " <tr>";
}
the best way is to fetch only 8 images from database instead of fetching all images at once.Use pagination to set limit and offset for each page.
You need to add a $_GET['variable'];
Something like this,
if(isset($_GET['last_image'])) {
$last_image = $_GET['last_image'];
}
else {
$last_image = 0;
}
$device[] = //however you get the images
$size_of_array = count($device);
for ($counter = $last_image; $counter < $counter + 8 && $counter < $size_of_array; $counter++) {
$entry = $device[$counter];
echo "<head>";
echo '<script type="text/javascript">',
'window.setInterval(function() { ',
"document.getElementById('$counter').src='/latimage.php?&dev=$entry&random='+new Date().getTime();",
'},1000)',
'</script>';
echo "</head>";
echo "<body onLoad='setTimeout('refresh()',1000)'>";
echo "<td>$entry<img id= '$counter' width='100%' height='auto'></img></td>";
}
Then next button
echo 'Next';
This is the basic idea behind a pagination.
Related
I am trying to make a website but I am very new to the jquery stuff and I added jquery load more comment into my website
My question is, I have a load more button that works the way I intended it to work however when all the data are loaded or when there is no comment I can't seem to get the load button to turn hidden
//jquery script
<script>
$(document).ready(function() {
var commentCount = 20;
var qidNow = "<?php echo $qid; ?>";
$("button").click(function() {
commentCount = commentCount + 20;
$("#comments").load("includes/load-comments.inc.php", {
commentNewCount: commentCount,
qid: qidNow
});
});
});
//load-comments page
<?PHP
require 'dbh.inc.php';
include 'function.inc.php';
$commentNewCount = $_POST["commentNewCount"];
$qid = $_POST["qid"];
$count = 0;
$sqlComment = "SELECT comment, commentTime, commenterId FROM comments WHERE commentOn = ?
LIMIT ?";
$stmtComment = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmtComment, $sqlComment)) {
header("Location: ../viewtopic.php?error=sqlerror4");
exit();
} else {
//bind parameter to the placeholder
mysqli_stmt_bind_param($stmtComment, "ii", $qid, $commentNewCount);
//run parameter inside database
mysqli_stmt_execute($stmtComment);
$commentData = mysqli_stmt_get_result($stmtComment);
}
if (mysqli_num_rows($commentData) > 0) {
while ($row = mysqli_fetch_assoc($commentData)) {
$count++;
echo "<div class='individualComment'>";
echo "<div class='commentCount'> คอมเม้นที่" . $count . "</div>";
echo "<div class='actualComment'>" . $row['comment'] . "</div>";
echo "<div class='commentDateBx'>" . dateReformat($row['commentTime']) . "</div>";
echo "<div class='commenterIdBx'>ID :" . $row['commenterId'] . "</div>";
echo "</div>";
}
}else {
echo "There is no comment yet";
}
Hello guys so into web page im gettting date/time variable which looks like 2018-12-05 07:19:54 and I am getting IN or EXIT. Here is cauculating if person is coming in or going out and how long he has been in and I need seperate every entrence from 2018-12-05 00:01 to 2018-12-05 23:59:59.
Btw the file looks like this
Does anyone know how to do math? Example (2018-12-05 07:19:54)-(2018-12-05 07:50:05) = 31min in the end it would be at work he was 31min and cauculate that untill the day ends. Anyone knows any ideas how to cauculate that sort of kind stuff?
You can using date_diff function in PHP
For example:
$date1=date_create("2018-12-05 07:19:54");
$date2=date_create("2018-12-05 07:50:05");
$diff=date_diff($date1,$date2);
echo $diff->format("'%i Minute %s Seconds'");
For running code check here
Check here for complete format response
PHP's Date Time class has method called diff. This method would return a DateInterval object, you can easily retrieve values of year, month, day... from that object.
Soo this is what i have done and it cauculates the time from in to exit
<?php
error_reporting(0); //disable all errors and notices
require_once "Classes/PHPExcel.php";
$chosenPerson = $_GET["dla_darbuotojo_pasirinkimas"];
$tmpfname = "visi.xls";
$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$excelObj = $excelReader->load($tmpfname);
$worksheet = $excelObj->getSheet(0);
$lastRow = $worksheet->getHighestRow();
$aInT = "Administracija[In]";
$vInT = "Vartai[In]";
$aExT = "Administracija[Exit]";
$vExT = "Vartai[Exit]";
$goingIn = false;
$goingExt = false;
$goingInValue = 0;
$goingExtValue = 0;
echo "<table>";
for ($row = 1; $row <= $lastRow; $row++) {
if ($chosenPerson == ($worksheet->getCell('D'.$row)->getValue()) ) {
if (!$goingIn or !$goingExt) {
//checking if the person alredy went in
if ((($worksheet->getCell('G'.$row)->getValue()) == $aInT) or (($worksheet->getCell('G'.$row)->getValue()) == $vInT)) {
//if the person went in
$goingIn = true;
echo "<tr><td>";
$goingInValue = $worksheet->getCell('F'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('D'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('F'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('G'.$row)->getValue();
echo "</td><td>";
echo "</td><td>";
echo "</td><td>";
for ($erow= $row +1; ; $erow++){
if ($chosenPerson == ($worksheet->getCell('D'.$erow)->getValue()) ) {
if ((($worksheet->getCell('G'.$erow)->getValue()) == $aExT) or (($worksheet->getCell('G'.$erow)->getValue()) == $vExT)) {
$goingExtValue = $worksheet->getCell('F'.$erow)->getValue();
$goingExt=true;
echo $worksheet->getCell('D'.$erow)->getValue();
echo "</td><td>";
echo $worksheet->getCell('F'.$erow)->getValue();
echo "</td><td>";
echo $worksheet->getCell('G'.$erow)->getValue();
echo "</td><td>";
$date1=date_create($goingInValue);
$date2=date_create($goingExtValue);
$diff=date_diff($date2,$date1);
echo $diff->format("'%h Valandos %i Minutes %s Sekundes'");
echo "</td><tr>";
$goingIn = false;
$goingExt = false;
break;
$row=$erow;
}
}
}
}
}
//echo "<tr><td>";
//echo $worksheet->getCell('D'.$row)->getValue();
//echo "</td><td>";
//echo $worksheet->getCell('F'.$row)->getValue();
//echo "</td><td>";
//echo $worksheet->getCell('G'.$row)->getValue();
//echo "</td><tr>";
}
}
echo "</table>";
?>
I have in my database 2 rows of information so my code create will create positivoNegativo.png twice and the number 10 twice. When I click on the FIRST positivoNegativo.png my number FIRST 10 is incremented (exacly as I wanted to). Now my issue, when I click on the SECOND positivoNegativo.png, the FIRST number is incremented again! I just can't increment the SECOND number by clicking on the SECOND positivoNegativo.png
<html>
<body>
<?php
include_once("./classe/conexao.php");
$busca = $pdo->prepare("select * from anuncios");
$busca->execute();
$linha = $busca->fetchAll(PDO::FETCH_OBJ);
$classe = 0;
foreach ($linha as $lista) {
echo "<p class='demo'>10</p>";
echo "<img src='imagens/positivoNegativo.png'usemap='#mapa'>";
echo "<map name='mapa'>";
echo "<area shape='rect' coords='1,1,73,59' onclick='aumenta($classe)'>";
echo "</map>";
echo "<span>$lista->titulo</span>";
$classe++;
}
?>
<script>
function aumenta(classe) {
var numero = document.getElementsByClassName('demo')[classe].innerHTML;
numero++;
document.getElementsByClassName('demo')[classe].innerHTML = numero;
}
</script>;
</body>
You need to use different map for each image.
foreach ($linha as $lista) {
echo "<p class='demo'>10</p>";
echo "<img src='imagens/positivoNegativo.png'usemap='#mapa$classe'>";
echo "<map name='mapa$classe'>";
echo "<area shape='rect' coords='1,1,73,59' onclick='aumenta($classe)'>";
echo "</map>";
echo "<span>$lista->titulo</span>";
$classe++;
}
Try this:
<?php
include_once("./classe/conexao.php");
$busca = $pdo->prepare("select * from anuncios");
$busca->execute();
$linha = $busca->fetchAll(PDO::FETCH_OBJ);
$classe = 0;
foreach ($linha as $lista) {
echo "<p class='demo".$classe."'>10</p>";
echo "<img src='imagens/positivoNegativo.png' onclick='aumenta($classe)'>";
echo "<span>$lista->titulo</span>";
$classe++;
}
?>
<script>
function aumenta(classe) {
var numero = document.getElementsByClassName('demo'+classe).innerHTML;
numero++;
document.getElementsByClassName('demo'+classe).innerHTML = numero;
}
</script>;
I didn't test the code, but it might give you some hints.
I left out the whole <map> thing because it doesn't make sense in your code. There's only an up?
I need a fresh pair of eyes to look at this!
I took code directly from one of of my old fiddles https://jsfiddle.net/RachGal/fs9u6mwe/1/ to display photos in a college galley site. However it only shows one. When i include bootstrap and the function in my php it doesn't seem to read it! Why could this be?
My php looks like this
<?php
echo "<html><head><meta charset='utf-8'>";
echo "<script type='text/javascript' src='scripts/bootstrap.js'></script>";
echo"<link rel='stylesheet' type='text/css' href='css/style.css'>";
include "scripts/show.php";
echo" <title>Gallery Display</title></head><body>";
echo "<header>";
echo "<h1>The Ultimate Gallery Compiler</h1>";
echo "<div id='menu'><a class='head' href='index.html'>Upload Photo</a> <a class='head' href='gallery.html'>Browse Gallery</a></div>";
echo "</header>";
echo "<div id='content'>";
echo "<h2>Browse Gallery</h2>";
$subfolder = $_POST["category"];
if ($subfolder == "0"){
echo("Please <a href='gallery.html'>go back</a> and select a category");
echo "</div><br><br>";
echo "<footer>";
echo "<p class='foot'>© Copyright 2015-2016 MMA2 Rachel Gallen, Ysabel Pheifer and Rebecca Merrigan.</p>";
echo "</footer>";
exit();
}
$countcontents = file_get_contents("categories.txt"); //read file to get count
$countarray = explode('*', $countcontents); //get count of each category into an array
$categories = array("fashion", "music", "sports", "technology", "animals", "health", "other");
//output title according to if the gallery has images in it or not
for($i=0; $i< count($countarray); $i++)
{
if ($subfolder == $categories[$i]){
if (intval($countarray[$i]) == 0) {
echo "<h3>No images have been uploaded for the " .$subfolder. " category yet</h3>";
}
else
{
echo "<h3>Here are the images for the " .$subfolder. " category</h3>";
echo "<p>There are ".$countarray[$i]." photos in this category</p><br>";
}
}
}
$folder = "images/".$subfolder."/";
// Open the appropriate subfolder, and display its contents.
// http://php.net/manual/en/function.opendir.php
// also referenced https://www.youtube.com/watch?v=nGLi4ykt__0
if ($dir = opendir($folder)) {
$images = array();
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
$images[] = $file;
}
}
closedir($dir);
}
echo '<div id="slider">';
echo "<ul id='slides'>";
foreach($images as $image) {
echo '<li class="slide"><img src="';
echo $folder.$image;
echo '" alt=""/></img></li>';
}
echo "</ul>";
echo '</div>';
echo "<p> </p><a href='gallery.html'>Reselect</a>";
echo "</div>";
echo "<footer>
<p class='foot'>© Copyright 2015-2016 MMA2 Rachel Gallen, Ysabel Pheifer and Rebecca Merrigan.</p>
</footer>";
echo "</body></html>";
?>
Any help would be appreciated!
by the way the javascript is
//Reference https://jsfiddle.net/RachGal/fs9u6mwe/1/
$(document).ready(function() {
//INDEX IMAGES SLIDER
$(function slider() {
//configuration
var width = 360;
var speed = 1000;
var pause = 3000;
var current = 1;
//cache DOM
var $slider = $('#slider');
var $slides = $slider.find('#slides');
var $slide = $slides.find('.slide');
setInterval(function() {
//move image the defined width and speed to the left
$slides.animate({
'margin-left': '-=' + width
}, speed, function() {
//count number of slides and loop back to first from last
current++;
if (current === $slide.length) {
current = 1;
$slides.css('margin-left', 0);
}
});
}, pause);
});
}
);
You have not included jQuery in your pages, you are using jQuery in your script but it is not loaded at all.
A nice way to find these things out is using the developer bar by pressing F12 when using chrome, then at network you can see which files were loaded and in console if there were any javascript errors.
This file is called by ajax request. And result coming here I want to place into two different in calling function.
<?php
//Some processing gives $text
$s=nl2br($text);
$data['x'] = $p;
$data['y'] = $q;
//Start from here
echo "<b>Positive count : $x with $p % </b>"; echo "</br>";
echo "<b>Negative count : $y with $q % </b>"; echo "</br>";
echo "</br>";
echo "Page content : ";
echo "</br>";
echo "</br>";
echo $s;
//End. This content should be place in <div1>. Want to send this as a json string
and
//Start from here
echo "First 5 post";
$result = mysqli_query($con,"select post from facebook_posts where p_id > (select MAX(p_id) - 5 from facebook_posts)");
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
echo $row['post'];
echo '<br/>';
}
//End. This content should be placed in <div2> Want to send this as a json string
If there is single variable then we can easily do it using :
$resultArray = array("resultOne" => $result1,"resultTwo" => $result2);
echo json_encode($resultArray);
at receiving end:
document.getElementById("myFirstDiv").innerHTML=xmlhttp.responseText.resultOne;
document.getElementById("mySecondDiv").innerHTML=xmlhttp.responseText.resultTwo;
But how above complex result could be place into to json variable?
You could use output buffering in PHP:
ob_start();
// Generate content for div 1...
$div1 = ob_get_clean();
ob_start();
// Generate content for div 2...
$div2 = ob_get_clean();
$result = array("div1" => $div1, "div2" => $div2);
echo json_encode($result);