Invalid syntax - Glob image directory scan for carousel - php

I have the following syntax error with this code:
Warning: natcasesort() expects parameter 1 to be array, boolean given in Website/assets/country-gallery-js.php on line 14
Warning: Invalid argument supplied for foreach() in Website/assets/country-gallery-js.php on line 15
<?php $thumbs = glob("img/thumb/*.{jpg,png,gif}", GLOB_BRACE); ?>
<?php
if(count($thumbs)) {
natcasesort($thumbs);
foreach($thumbs as $thumb) {?>
<li class="item">
<a class="fancybox" rel="gallery1" href="img/large/<?php echo basename($thumb) ?>">
<img src="<?php echo $thumb ?>" class="img-circle" width="100%" alt="" />
</a>
</li>
<?php
}
}
else {
echo "Sorry, no images to display!";
}
?>
I am unsure as to why. The code scans a folder for images to display as a carousel. It scans a thumbnail and large image folder for lightbox. When images are in the folder, it works like a treat.. when the folder is empty it should echo the 'else' text code. Instead it displays this syntax.
Can anyone help me figure this one out and stop the syntax from appearing?

Use is_array instead of count
<?php $thumbs = glob("img/thumb/*.{jpg,png,gif}", GLOB_BRACE); ?>
<?php
if (is_array($thumbs)) {
natcasesort($thumbs);
foreach ($thumbs as $thumb) {
?>
<li class="item">
<a class="fancybox" rel="gallery1" href="img/large/<?php echo basename($thumb) ?>">
<img src="<?php echo $thumb ?>" class="img-circle" width="100%" alt="" />
</a>
</li>
<?php
}
} else {
echo "Sorry, no images to display!";
}
?>

I believe using the braces syntax:
glob("img/thumb/*.{jpg,png,gif}", GLOB_BRACE);
wont work, this probably will:
glob("img/thumb/*.jpg,img/thumb/*.png,img/thumb/*.gif", GLOB_BRACE);
The first step to what you ought to do to check where the problem lies is check what kind of value the variable $thumbs holds. This would have told you that the problem probably lies within the glob function.

Related

How to target the N first iterations of a while loop?

I'm displaying images on my site. The first 18 images should be written like this:
<img class="item lazy" data-src="<?php echo $path; ?>" src="<?php echo $path; ?>" />
And the rest of them (from the 19th) should be written like that:
<img class="item lazy" data-src="<?php echo $path; ?>" />
I used a while loop but it didn't work (it would show each item displayed within the loop 18 times):
while (...) { // while loop to display items from the database
$itemCount = 0;
while($itemCount <= 18) {
// show items ($itemId)
}
}
I can't think of anything... any suggestions?
<?php
$n=0;
$dir="C:/Chose a folder";
$url="http://....";
$files=scandir($dir);
foreach($files as $filenm) {
if( $n++<18 ) echo "<img class='item lazy' data-src='$url/$filenm' src='$url/$filenm' />";
else echo "<img class='item lazy' data-src='$url/$filenm/>";
}
?>
Note, the src should be the URL of the file, not the local filename

unable to fetch image from the database

I am trying to display images on my web page, the content is getting fetched from my database, but the issue I'm facing is in displaying the image. please, can anyone guide me how should I display the image?
I mean to say the path what I should give
here 'image' is my column name and this is my view
<?php
if( !empty($results) ) {
foreach($results as $row) {?>
<div class="col-sm-4">
<img src="<?php echo base_url('uploads/');$image?>" alt="">
<h3><?php echo $row->title; ?></h3>
<p><?php echo $row->content; ?></p>
</div>
<?php
} ?>
<?php }
?>
Hope this will help you :
Use this : <img src="<?php echo base_url('uploads/'.$row->image);?>" alt="">
The whole code should be like this :
<?php
if( !empty($results) ) {
foreach($results as $row) {?>
<div class="col-sm-4">
<img src="<?php echo base_url('uploads/'.$row->image);?>" alt="">
<h3><?php echo $row->title; ?></h3>
<p><?php echo $row->content; ?></p>
</div>
<?php
} ?>
<?php }
?>
If $image is the column name(which contains image name) then Replace
<img src="<?php echo base_url('uploads/');$image?>" alt="">
with
<img src="<?php echo base_url();?>uploads/<?php echo $row->image;?>" alt="">
If $image has image path then remove ';' and add '.' on echo base_url('uploads/');$image?> line
You issue here is the following code;
<?php echo base_url('uploads/');$image?>
This is because you are not concatenating the string, rather, just saying it's variable name, so, use of the following will provide the output;
<?php echo base_url('uploads/') . $image' ?>
This replaces the semi-colon part way through for a period (.) which is the PHP concatenation operator
Obviously, there is the issue you are not setting $image, which would require (before usage) of;
$image = $row['image_column_name'];
// Or
$image = $row->img_column_name

WideImage output as strange characters instead of image

I am trying to showing cropped images as thubmnails with WideImage.
I made a function with an array of filenames as parameter ($images) where I produce my html
foreach ($images as $img) {
$pathToThumb = str_replace("/upload/images", "/upload/images/thumbs", $img);
$thumb = WideImage::loadFromFile($pathToThumb)->crop('center', 'center', 150, 100)->output('jpg', 100); ?>
<div class="lingerie">
<a class="fancybox" rel="group" href="<?php print $img; ?>">
<img src="<?php echo $thumb; ?>" alt=""/>
</a>
</div>
<?php } ? ?>
For now, all I get is
����JFIF``���u i$�J�$��]5�����j4���%�o�WEʴ���^[7��|��6�d��P$g�bx�e��wh����N��c��rp=Oz��h�F�Z��"��u8q�oN��la_28�g�����1��2G��1^Uy�EJ��'��|��9F[����Vn��2m�j�i��ַߣ,4���? ����2zp3�P[|���>���1���8$u��+cQ��:x���-�II^r1��{U�+O��T�x��*b��3����zv�m �)b�U��j�+'B�`m������a����T��r���Tq�1�*�����{D_s�!AbG;v��������/i5��u�ݶ]�����M�]�]���#$Z���74"+ar]�-�',�B���J��W�}KY�/7H#��7�3-n�=���v�Ps�3�C~��N��h��j�q��6�����- and so on
What is the reason for these weird digits?

make img draggable in td elements

foreach($photos as $photo) {
$lastChild = "";
if( $count%3 == 0 && $count != 0 )
echo "</tr><tr>";
$count++;
?> <td>
<a href="<?php echo ($photo['source'])?>" title="<?php echo ($photo['name'])?>">
<img id="thumb" class="thumb" src="<?php echo ($photo['picture'])?>">
</a></td>
This is the script for draggable. Ive done this but it still does not work
only the first one
does the foreach loop affects?
$thumb.draggable({
revert: "invalid", // when not dropped, the item will revert back to its initial position
helper: "clone",
});
im unable to set ui-draggable to all the img src other than the first img.
any one can help?
Use
<img class="thumb" src="<?php echo ($photo['picture'])?>">
instead of
<img id="thumb" class="thumb" src="<?php echo ($photo['picture']); ?>" />
in your PHP and
$('.thumb').draggable({
instead of
$thumb.draggable({
in your jQuery. This should fix it.
If you absolutely need IDs on the images for any reason, make them unique like this:
<img id="thumb-<?php echo $count; ?>" class="thumb" src="<?php echo ($photo['picture'])?>">

Php while loop exclude first of array

<div id="prettyphoto" align="left"> <img src="http://images.idealer1.com/getimage/700/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" />
<ul class="gallery clearfix">
<?php foreach ($images as $image) { ?>
<li><img src="http://images.idealer1.com/getimage/100/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" /></li>
<? } ?>
</ul>
</div>
I am trying to take this foreach loop of thumbnails and exclude the first image and have it load as a larger image I can't seem to figure out the proper way to accomplish this.
You could use array_shift():
<?php $firstImage = array_shift($images); ?>
<!-- do something with $firstImage -->
<?php foreach ($images as $image): ?>
...
<?php endforeach; ?>
or if you don't need a reference to the first image, array_slice():
<?php foreach(array_slice($images, 1) as $image): ?>
...
<?php endforeach; ?>
Also note the use of the alternative syntax for control structures which makes reading the mixture of PHP and HTML a bit easier (but is not related to your problem).
You can iterate the array "manually", with next(), current(), etc:
<?php
$images = array(
array('vpid' => 1, ),
array('vpid' => 2, ),
array('vpid' => 3, ),
array('vpid' => 4, ),
);
$image = current($images);
?>
<div id="prettyphoto" align="left"> <img src="http://images.idealer1.com/getimage/700/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" />
<ul class="gallery clearfix">
<?php
next($images);
while(list($idx, $image) = each($images)) { ?>
<li><img src="http://images.idealer1.com/getimage/100/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" /></li>
<? } ?>
</ul>
</div>
Read: http://www.php.net/manual/en/function.each.php
With array_shift() you could "loose" the first image (you have to put it back in after you're done).
With array_slice() you would duplicate data, which is bad practice in general.
you can use some idea from this:
foreach($array as $key=>$value)
{
if($key==0)
echo "code for the large image";
else
echo "code for the thumbnail image";
}

Categories