I use Highslide jQuery Gallery to load albums and their thumbnails on a single page.
Users can click the thumbnail and each album then loads.
The website runs slow if I have more than 10 albums per page (due to loading thumbs and pics)
I use base64 encoding on the data in the database.
How can I load only the thumbails instead of whole albums?
The album will only load once the user has clicked the thumbnail..
<?php
//vars
$albumsQuery = mysql_query("select * from albums");
$album_count = 0;
// start loop
while ($album = mysql_fetch_array($albumsQuery)) {
$album_count++;
$unserializePhotos = unserialize(base64_decode($album['photos']));
$unserializeDescriptions = unserialize(base64_decode($album['descriptions']));
$firstPhoto = '';
$first_photo_count = 0;
foreach ($unserializePhotos as $k => $v) {
if ($first_photo_count == 0) {
$firstPhoto = $v['name'];
}
$first_photo_count++;
}
$first_desc_count = 0;
foreach ($unserializeDescriptions as $k => $v) {
$unserializeDescriptions[$k]=htmlspecialchars($v);
if ($first_desc_count == 0) {
$firstDesc = htmlspecialchars($v);
}
$first_desc_count++;
}
?>
<div class="highslide-gallery">
<a class='highslide' id="thumb<?php echo $album_count; ?>" href='/albums/<?php echo $firstPhoto; ?>' onclick="return hs.expand(this, {slideshowGroup: <?php echo $album_count; ?>})">
<img src='/albums/<?php echo $firstPhoto; ?>' height="100px" width="100px" />
</a>
<div class="hidden-container">
<?php
$photoDescIndex = 0;
foreach ($unserializePhotos as $k => $v) {
if ($v['name'] != '' && $v['name'] != $firstPhoto){
?>
<a class='highslide' href='/albums/<?php echo $v['name']; ?>' onclick="return hs.expand(this, {slideshowGroup: <?php echo $album_count; ?>})">
<img src='/albums/<?php echo $v['name']; ?>' />
</a>
<?php
}
$photoDescIndex++;
}
?>
</div>
You are loading full image into thumbnail: <img src='/albums/<?php echo $firstPhoto; ?>' height="100px" width="100px" />. Browser requires more time to load big photo and even more time to resize it. You should prepare small 100x100 thumbnails on the server side.
You should not load all albums <div class="hidden-container">...</div> explicitly. Load content of selected album via AJAX on demand (when user clicked on thumbnail). Additionally you may start to pre-load albums in background after page loading.
Do not assign event handlers directly to each onclick="..." - use event delegation e.g.
$("body").on("click", ".highslide", function() {
var album_count = this.id.slice(5); // a id="thumb<?php echo $album_count; ?>"
var target = $(this).next("div.hidden-container");
// TODO: load album album_count into target via AJAX
// TODO: after load: hs.expand(this, {slideshowGroup: album_count});
});
Related
So I have a table where each cell is a name of a game and when you click it it needs to show in a fancybox the results of the user which clicked the cell (I use table Indexes to get the GameID and the Session variable to get userID) which will be used to load the results from a second PHP page.
If I click on a cell for the first time the fancybox will not display anything and after I close fancybox and click on any cell again it works fine. Am I doing something wrong?
This is the whole javascript:
$(".jogos").fancybox({
'hideOnContentClick': true,
'onComplete':function(element)
{
var gameIdx = $(element).index();
var cateIdx = $(element).parent().parent().index();
var gameIdxPHP;
var catIdxPHP;
var gameID;
var userId = '<?php echo $_SESSION['userID']; ?>'
<?php
for ($i=1; $i<= count($categoryArray);$i++)
{
for ($j=1; $j<=count($categoryArray[$i-1]->gamelist);$j++)
{
?>
catIdxPHP = '<?php echo $i ?>' -1;
gameIdxPHP = '<?php echo $j ?>' -1;
if (catIdxPHP == cateIdx && gameIdxPHP == gameIdx)
{
gameID = '<?php echo $categoryArray[$i-1]->gamelist[$j-1]->GameID; ?>';
$("#graphic").load("backoffice/resUserNivel2short.php", {userId:userId,gameID:gameID}, function(){ });
}
<?php
}
}
?>
}
});
HTML
<div style="display:none">
<div id="data">
<div id="graphic">
</div>
</div>
</div>
Sample code of the link
<a href="#data" class="jogos" id="cat<?php echo $i; ?>jogo<?php echo $j; ?>" >
You have display:none on the parent of your fancybox therefor the grafic isnt displayed.
The Grafic element isn't inside the dom yet if you use display:none initially. Try to use clip: rect instead as a class and add/remove that class using the fancybox callbacks.
Try this code:
$('.jogos').fancybox({
'onStart': function() {
$("#data").removeClass('hidden');
},
'onClosed': function() {
$("#data").addClass('hidden');
}
});
CSS:
.hidden {
clip: rect(1px 1px 1px 1px);
position: absolute;
)}
HTML:
<div>
<div id="data" class="hidden">
<div id="graphic">
</div>
</div>
</div>
update :
The problem now is after using firebug I found out that the first element gets the title attribute and other elements don't and after hovering on any link the title attribute of the first link disappears any ideas?
I have a which page opens when I click on a link like this:
<a class="pictures" href="#" onClick="MM_openBrWindow('content_image.php','images','menubar=yes,scrollbars=yes,resizable=yes,width=650,height=700')" title="Images"></a>
This page has images stored in a folder:
$open = opendir($path);
while($images = readdir($open)){
if ($images != "." && $images != "..") {
$allimages[] = $images;
}
}
foreach($allimages as $val){
?>
<div class="uploaded_image"><img src="../content_img/<?php echo $val; ?>" align='middle' width='150px' height='100px'>
<form><img src="images/delete.gif"/> <textarea name='text_area' rows=1 cols=20 >../content_img/<?php echo $val; ?></textarea> <input type='button' value='select path' onClick='javascript:this.form.text_area.focus();this.form.text_area.select();'> Copy path and paste in image path field </form></div>
<?php
}
closedir($open);
?>
I am trying to delete images using a regular link with href to the page with the following attributes:
?do=delete&img=<?php echo $val; ?>
and then unlink() the image. However, the image is deleted as soon as I hover on the link without clicking. I don't know why.
I tried to do it using AJAX:
$(document).ready(function(e) {
$("div.uploaded_image").on("click","a.del_img",function(e){
e.preventDefault();
var img = $(this).attr("title");
var qst = "?img="+img+"&path=content_img";
var ajax = false;
ajax = new XMLHttpRequest();
ajax.open("post","del_image.php"+qst);
ajax.onreadystatechange = function(){
if(ajax.readyState == 4 && ajax.status == 200){
}
}
ajax.send(null);
setTimeout(ref,1500);
});
});
function ref(){
window.location = "content_image.php"
}
and this PHP code work uses AJAX to delete:
$img = $_REQUEST['img'];
$path = $_REQUEST['path'];
unlink("../".$path."/".$img);
When I click on the delete link for the first time it doesn't work. I have to click again to delete the image. Any help please?
$open = opendir($path);
while($images = readdir($open)){
if ($images != "." && $images != "..") {
$allimages[] = $images;
}
}
foreach($allimages as $val){
?>
<div class="uploaded_image"><img src="../content_img/<?php echo $val; ?>" align='middle' width='150px' height='100px'>
<form><img src="images/delete.gif"/> <textarea name='text_area' rows=1 cols=20 >../content_img/<?php echo $val; ?></textarea> <input type='button' value='select path' onClick='javascript:this.form.text_area.focus();this.form.text_area.select();'> Copy path and paste in image path field </form></div>
<?php
}
closedir($open);
?>
replace code with this code
change
$("div.uploaded_image").on("click","a.del_img",function(e){
e.preventDefault();
to
$("div.uploaded_image a").click(function(e){
e.preventDefault();
...
sorry for the lengthy question. i'll try to explain.
i have created a list of images that are visible when you hover over an element (using css)
each image is also a radio button to be chosen and used as part of a form.
All the images are basically all the images in a certain directory that you can also upload to.
I've managed to add a 'delete' button next to each image which will trigger a function that calls a php script to delete that particular image without reloading page. using xmlhttp.which works fine.
But the list of images doesn't refresh itself. As in the image i just deleted is still visible when i hover over the element until i refresh the page then it refreshes the list.
obviously i don't want to refresh the page, that was the whole point of using ajax. so any ideas what i can do? code is below:
<script>
function deleteImage(){
var answer = confirm('Are you SURE you wish to delete this?')
if (answer) {
var image = document.getElementById('filepath').value;
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("uploadmsg").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","delete.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("image="+image);
}
}
then the php/html to call it all.
<?php
$dir = "../images/";
$dh = opendir( $dir );
while( $filename = readdir( $dh ) ) {
$filepath = $dir.$filename;
$gallery[] = $filepath;
$list[] = $filename;
}
sort( $gallery );
sort( $list);
$num_pics = count($gallery);
?>
<div id='chooseimg' ><input type="text" value="Choose Image"><span><ul>
<?php
$a = 2;
while($num_pics > $a)
{
?>
<li>
<input type='radio' name='image' value='<?php echo $list[$a]; ?>'>
<?php echo $list[$a]; ?>
<div id='imglist'><img src='<?php echo$gallery[$a]; ?>' /></div>
<button type='button' onclick="deleteImage()">Delete</button>
<input type="hidden" id="filepath" value="<?php print $gallery[$a]; ?>"/> </li>
<?php
$a ++; }
?>
</ul></span></div>
<div id="uploadmsg"> </div>
the file delete.php contains:
<?php
$image = $_POST['image'];
if (!empty($image)){
unlink($image);
echo "<b>Image deleted<b>";
}
?>
and the css to make the hover over thing work is basically a span inside a div #chooseimage that is set to left:99999px; but when hover over chooseimg it moves to left:0px;
i don't think anyone needs any more info on that but let me know if you do.
as a side note i'm not sure if this actualy ajax? maybe someone can clarify for me.
if you could help i'd be very grateful. i've googled for hours but can't find anything.
ok so for anyone who has the same problem i figured it out.
the code to display all the images along with their radio and delete buttons are stored in a separate php file:
<?php
$dir = "../images/";
$dh = opendir( $dir );
while( $filename = readdir( $dh ) ) {
$filepath = $dir.$filename;
$gallery[] = $filepath;
$list[] = $filename;
}
sort( $gallery );
sort( $list);
$num_pics = count($gallery);
?>
<div id="choose"><div id="echo">
<div style="background-color: #F04D8E; color: #ffffff;">Choose Image</div></div>
<span><ul>
<?php
$a = 3;
while($num_pics > $a)
{
$image = $gallery[$a];
?>
<li>
//the radio button triggers an event which then showsthe image selected
<input id="radio" type='radio' name='image' onchange="thumb('<?php echo $image; ?>')"
value='<?php echo $list[$a]; ?>'><?php echo $list[$a]; ?>
<div id='gallery'>
// the delete button sends the images 'id' in its parameter
<button style="float:right;" type='button'
onclick="deleteImage('<?php echo $image; ?>', '<?php echo $list[$a]; ?>' )">
Delete</button>
<img src='<?php echo $image; ?>' /></div>
</li>
<?php
$a ++;
}
?>
</ul></span></div><br>
in the main page i call this script into a div straight away and also refresh it after the deleteImage function has been requested:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
window.onload = function(){
$('#result').load('show.php');
}
function deleteImage(id, name){
var answer = confirm('Are you SURE you wish to delete ' + name + ' ?')
if (answer) {
$.post("delete.php", { image: id })
.done(function(){
$('#uploadmsg').html("<b>" + name + " deleted </b>");
$('#result').ready(function(){
$('#result').load('show.php');
});
});
}}
function thumb(image){
$('#echo').html("<img src='"+image+"'>");
}
then all i need in the page is a div with id as result
oh and the delete.php page is still:
<?php
$image = $_POST['image'];
if (!empty($image)){
unlink($image);
}
?>
someone said this is dangerous? but i can't see why.
and questions feel free to ask
thanx
I have an image scroller with a selection of images in it. These images are pulled out of the database. Above the scroller are a list of folders.
When the user click a new folder I want the list of images to update without refreshing the page. I have had a go at making this with ajax but its confusing me a little and is not working at all. Here is what i have got so far
first the default html/php page
the images that are there now a just for test. these will be the default images in no folder when the query is written
About.php
the folders list
<div id="folders">
<ul id="foldername">
<li>test0</li>
<li>test1</li>
<li>test2</li>
</ul>
</div>
then the scroller
<div id="scrolimg">
<img name="" src="" width="150" height="150" alt="" class="singlscrolimg" />
</div>
getImage.php
include('../Connections/dbcon.php');
mysql_select_db($database_dbcon, $dbcon);
$query_rs_image = "SELECT * FROM images WHERE imgfolder='".$_GET['id']."' AND status='1'";
$rs_image = mysql_query($query_rs_image, $dbcon) or die(mysql_error());
$row_rs_image = mysql_fetch_assoc($rs_image);
$totalRows_rs_image = mysql_num_rows($rs_image);
do { ?>
<img name="<?php echo $row_rs_image['imgname']; ?>" src="/images/uploads/<?php echo $row_rs_image['smfile']; ?>" alt="<?php echo $row_rs_image['imgname']; ?>" />
<?php } while ($row_rs_image = mysql_fetch_assoc($rs_image)); ?>
and at the top of about.php
<script>
function getImages(id)
{
$.ajax({
type: "GET",
url: 'getImage.php',
data: "id=" + id,
success: function(data) {
$('#scrolimg').html(data);
}
});
}
</script>
I have an application here where I am using jwplayer in basic jquery slider: APPLICATION
Info for basic jquery slider here: http://basic-slider.com/
Now this is what I have found out, if a slider contains multiple videos, then it displays the slider, but if a slider has one video only, it does not display video or slider.
Now what is strange is if I replace the videos for images, then it works if I have a single image as it does not display the slider but displays the image, this is fine. But my question is how to do the same thing for the video?
Here is demo for slider with images: APPLICATION
Below is code for video and slider:
$vidquery = "SELECT s.SessionId, q.QuestionId, v.VideoId, VideoFile
FROM Session s
INNER JOIN Question q ON s.SessionId = q.SessionId
INNER JOIN Video_Question vq ON q.QuestionId = vq.QuestionId
INNER JOIN Video v ON vq.VideoId = v.VideoId
WHERE s.SessionId = ?";
$vidqrystmt=$mysqli->prepare($vidquery);
// You only need to call bind_param once
$vidqrystmt->bind_param("i",$session);
// get result and assign variables (prefix with db)
$vidqrystmt->execute();
$vidqrystmt->bind_result($vidSessionId,$vidQuestionId,$vidVideoId,$vidVideoFile);
$arrVideoFile = array();
while ($vidqrystmt->fetch()) {
$arrVideoFile[ $vidQuestionId ][] = basename($vidVideoFile);
}
$vidqrystmt->close();
?>
<form action='results.php' method='post' id='exam'>
<?php
//start:procedure video
$vid_result = '';
if(empty($arrVideoFile[$key])){
$vid_result = ' ';
}else{
?>
<div id="banner-video_<?php echo $key; ?>">
<ul class="bjqs">
<?php
$i = 0;
foreach ($arrVideoFile[$key] as $v) { ?>
<li><div id="myElement-<?php echo $key.'-'.$i; ?>">Loading the player...
<script type="text/javascript">
jwplayer("myElement-<?php echo $key.'-'.$i; ?>").setup({
file: "<?php echo 'VideoFiles/'.$v; ?>",
width: 480,
height: 270
});
<?php $i++; ?>
</script>
</div>
</li>
<?php } ?>
</ul>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#banner-video_<?php echo $key; ?>').bjqs({
animtype : 'slide',
height : 300,
width : 700,
responsive : true,
randomstart : false,
automatic : false
});
});
</script>
<?php
}
//end:procedure video
?>
</div>
</form>
UPDATE:
If anyone knows how to code it so that if it is able to detect if single video, then do not place it in slider, then I believe this could solve it.
What I am saying is that by your example it looks like all your slider does it slide a image or video over and display the next or previous, but if you have only one item why not just skip all that entirely.
You have an array : $arrVideoFile
You can use count($arrVideoFile); to get the total values in the array.
So
if(count($arrVideoFile) != 0 || $arrVideoFile) != 1)
{
//do the stuff you pasted above
}
else
{
//if(array == 0) maybe show a default image
//if(array == 1) lets just show that
//if image show <img> if video show <object> or in your case
//<div id="myElement-<?php echo $key.'-0'; ?>"></div>
}
Let me know if this helps