I've searched and found out solutions to preload images IF I have their file names. However my gallery and images are dynamic so it runs a php echo image url code to call my images.
<a href="<?php echo $image->imageURL ?>" <?php echo $image->thumbcode ?> >
<?php if ( !$image->hidden ) { ?>
<img title="<?php echo $image->alttext ?>" alt="
<?php echo $image->alttext ?>" src="<?php echo $image->thumbnailURL ?>"
<?php echo $image->size ?> />
<?php } ?></a>
What can I do to preload the next 2 or 3 images in the gallery since I wont be able to give the exact image file for each of my galleries. The only solution I found was to make all the images preload which I don't want.
You can probably hide your images when page load and you can run your preload image function
$('#gallery img').hide();
$('#gallery img').each(function(e) {
$(this).delay(500*e).fadeIn('slow');
})
I'm not clear as to what you're asking, but you can't load a resource without knowing it's path.
Also, I assume $image->size returns the string width="X" height="Y". You'll probably be better off having a height and width property, or store the dimensions in an array.
if($image->width) {
echo 'width="' . $image->width . '"';
}
Related
Beginner JS here.
I am trying to add a PHP variable to a Javascript onclick function. I converted the PHP variable to a JS variable just fine. However, when adding the JS variable to the function I'm not receiving the desired output. What am I doing wrong?
<script>
js_logo_number = "<?php echo $logo_number; ?>";
</script>
<img src='<?php echo $image1[0]; ?>' onclick="openModal();currentSlide(js_logo_number)">
You can do like this :
<img src='<?php echo $image1[0]; ?>' onclick="openModal();currentSlide(<?php echo $logo_number; ?>)">
This code works for me. Be sure you use a let or const (or the outdated var) prefix to declare js_logo_number a new variable. Also make sure that $logo_number is set like this:
<script>
let js_logo_number = <?php isset($logo_number)?$logo_number:null; ?>;
</script>
Then make sure you convert it to an integer in Javascript, if that's how you want to use it:
function currentSlide(num){
console.log('currentSlide fired', parseInt(num));
}
let js_logo_number = "5";
function openModal(){
console.log('openModal fired');
}
function currentSlide(num){
console.log('currentSlide fired', parseInt(num));
}
<img src='https://placekitten.com/200/200' onclick="openModal();currentSlide(js_logo_number)">
When PHP parses a file, it looks for opening and closing tags, which are which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser.
See: PHP tags
1) echo
<?php echo 'if you want to serve PHP code in XHTML or XML documents, use these tags'; ?>
Solution:
<img src='<?php echo $image_src ?>'
onclick="openModal();currentSlide(<?php echo $logo_number ?>)">
2) Short echo
You can use the short echo tag to <?= 'print this string' ?>.
It's equivalent to <?php echo 'print this string' ?>.
Solution:
<img src='<?= $image_src ?>'
onclick="openModal();currentSlide(<?= $logo_number ?>)">
3) If short_open_tag is enabled
<? echo 'this code is within short tags, but will only work '
.'if short_open_tag is enabled'; ?>
Solution:
<img src='<? echo $image_src ?>'
onclick="openModal();currentSlide(<? echo $logo_number ?>)">
See: PHP tags
So I didn't found what I was looking for in last two days.
To be clear I have a table "tracks" in my database.
So I can show from "tracks" the "demo" field wich is an audio file "url"..
<?= $tracks['demo']; ?>
I have multiple url's but then I need to replace the a href
<a href="<?php echo $tracks['demo'];?>"
in a logical way.
In simple terms it's like I want to click on button 1 that loads
url 1 into this a href with the ID, title field from that track.
When you press button 2 you need to load another url and replace url 1 with url2.
I tried many ways including javascript but it won't work.
Problem now is that when I list 4 items it always loads the latest "URL" i have posted in my source code. :)
When I echo out <?php echo $tracks['demo]; ?> on all items I can see the correct url's so the functionality to replace the url is my issue.
* I basically want to load many mp3 url's in a player I made in the footer of my website. It works by hardcoding the urls in the url field but this is not what it should be... *
Code:
<?php while($tracks = mysqli_fetch_assoc($muziek)) : ?>
<div class="col-md-2 viny" id="muziek">
<h4><?= $tracks['title']; ?></h4>
<img src="<?= $tracks['img']; ?>"
alt=" <?= $tracks['title']; ?> />
<p class="artist">Artist: <?= $tracks['artist']; ?></p>
</div>
<?= $tracks['demo'] = $audiourl ; ?>
<button type="button" onclick="play">Play</button>
</div>
<?php endwhile; ?>
*THIS LOOPS PERFECT FOR ALL ITEMS TRACKS ARE LISTED PERFECT *
In my player I have
<a href="<?php echo $audiourl;?>" ><?= $tracks['artist']; ?></b> - <?= $tracks['title']; ?></a>
function play(){
I'm not good at JS... And this is my issue because it now loads the latest output from the php loop...
}
I don't know how you are replacing it but
Why not pass the audio in the function. Here like this
<button type="button" onclick="play(<?= $tracks['demo'] = $audiourl ; ?>)">Play</button>
assign a id to anchor tag
<a id="someId" href="<?php echo $audiourl;?>" ><?= $tracks['artist']; ?></b> - <?= $tracks['title']; ?></a>
and in the play function, you receive it like this
function play(audioUrl){
}
and change the href like this in the play function
If you are using jquery
$('#someId').attr("href", audioUrl);
sometimes the above does not works. Can't remember why but if that does not works try this
$('#someId').href = audioUrl
If you are using javascript
document.getElementById('abcd').href = audioUrl
So I tried many things but I had many issues with the player it's js file using the same id's and so on.
The new logic I tried out seems to work:
<form action="" method="POST">
<input type="submit" value="Play" name="Play">
</form>
<?php
if (isset($_POST["Play"]))
{
echo $tracks['demo'];
}else{
echo ' ';
}
?>
There is more going on but I tried to write it down as simple as this to show the logic. All Track url's are hidden and when the page loads there is an empty $audiourl by default loaded in the a href from my audioplayer. By clicking the button play I show the url in the source but not on the site.
Then the latest $audiourl variable changes to this value.
Hi am unable to replace the error image in php variable and javascript
$youtube_ext='https://i.ytimg.com/vi/'.$youtube_id.'/hqdefault.jpg';
$youtube_original='https://i.ytimg.com/vi/'.$youtube_id.'/maxresdefault.jpg';
where youtube id is the videoid in youtube
<img src='<?php echo $youtube_original;?>'
onError="this.onerror=null;this.src='<?php echo $youtube_ext;?>';" />
You need to use
this.setAttribute('src', <?php echo $youtube_ext;?>)
instead of :
this.src='<?php echo $youtube_ext;?>';
Have a good day !
Im using php to collect my instagram feed however all the images php collects using the instagram api are all placed in the same location(or same div) so i recieve my images
<?php foreach ($result->data as $post): ?>
<div class="images">
<!-- Renders images. #Options (thumbnail,low_resoulution,high_resolution)-->
<a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
<?php endforeach ?> </div>
so all the images collected are turned into links and are all placed in the div images so what i would like to do is equally distribute all 20 images received into these 3 different divs using php or js.
<div class="col-md-4"></div>,
<div class="col-md-4"></div>,
<div class="col-md-4"></div>
This is my first post, code might be awkward. Thanks for the help.
Well, I've gone ahead and built out a proper full solution. It works by first building an array of response objects, then chunking them out for display. If you want to split them up, you'll pretty definitely need to put them into a sub-buffer, like the $results buffer you see below.
<?php
$results = array();
foreach ($result->data as $post):
// Using output buffering as a simple means of building HTML.
ob_start();
?>
<a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>">
<img src="<?= $post->images->thumbnail->url ?>">
</a>
<?php
$results[] = ob_get_contents();
ob_end_clean();
endforeach;
// Now we have an array of elements as HTML. We'll chunk them
// into 3 arrays. We could also randomize or other things here.
$chunked = array_chunk($results, 3);
?>
<div class="col-md-4"><?=implode("\n", $chunked[0])?></div>
<div class="col-md-4"><?=implode("\n", $chunked[1])?></div>
<div class="col-md-4"><?=implode("\n", $chunked[2])?></div>
You will need to split your array and then iterate it within your desired div tag.
Example:
<?php
$chunked = array_chunk($array, 3);
foreach ($chunked as $value) {
foreach($value as $v) {
echo '<div class="col-md-4">';
print_r($v);
echo '</div>';
}
}
If you've ever worked with OpenCart and you're a stickler for correct markup and optimization then you know already what a pain in the * it is.
There is inline js all through the templates since it relies on php variables in the templates to execute the js.
I'm currently working on an HTML5 fork of OC that moves all of this crap out of the templates and moves all the linked js files to the bottom of the page where they are supposed to be.
The problem is a have a client that wants this now, and it will take weeks to move all this scripting properly, so I'm trying to find a reliable way to defer the inline scripting so that it will only execute once all the files have loaded at the bottom.
I tried using defer on the script tag, but Chrome still executes it immediately as soon as it finds it, and I imagine most other browsers will too.
For example here's the carousel module:
<div id="carousel<?php echo $module; ?>" class="es-carousel-wrapper">
<div class="es-carousel">
<ul>
<?php foreach ($banners as $banner) { ?>
<li>
<a href="<?php echo $banner['link']; ?>">
<img src="<?php echo $banner['image']; ?>"
alt="<?php echo $banner['title']; ?>"
title="<?php echo $banner['title']; ?>" />
</a>
</li>
<?php } ?>
</ul>
</div>
</div>
<script type="text/javascript" defer>
$('#carousel<?php echo $module; ?>').elastislide({
speed : 450,
easing : '',
imageW : 290,
<?php if ($limit > 3): ?>
minItems : <?php echo $scroll; ?>
<?php else: ?>
minItems : 1
<?php endif ?>
});
$(window).triggerHandler('resize.elastislide');
</script>
As you can see it's much easier to keep it in the template page and just use the variables from the php, obviously the right way to do this is to implement the variables into js and call them later which is what I did for another client, but that took a long time to do.
Is there a reliable way to implement this using defer?
Thanks.
-Vince