Im using glob() to find any image files matching a rule.
I'm using the following code:
$photo = glob(($_SERVER['DOCUMENT_ROOT'] .'/stocklist/photo/'.$row['Scientific'].'*.jpg'));
print_r(glob(($_SERVER['DOCUMENT_ROOT'] .'/stocklist/photo/'.$row['Scientific'].'*.jpg')));
Which produces the following:
Array ( [0] => /var/www/web/stocklist/photo/Pituophis deppei jani.jpg
[1] => /var/www/web/stocklist/photo/Pituophis deppei jani1.jpg )
Then when i echo the images to the page using the code below, it displays 2 broken image icons...
$length = count($photo);
if($length) {
echo"<ul id='slide'>";
for ($i = 0; $i < $length; $i++) {
echo "<li><img src='".$photo[$i]."' alt='".$row['Name']."'></li>";}
echo "</ul><ul id='slide-pager'>";
for($i2 = 1; $i2 < $length+1; $i2++) {
echo "<li><a href='#".$i2."'>".$i2."</a></li>";
}
echo "</ul>";
}
else {
echo "<img src='/stocklist/photo/placeholder.jpg' class='img-right'><br clear='right'>";
}
Try changing to:
$photo = (glob('stocklist/photo/'.$row['Scientific'].'*.jpg'));
print_r(glob('stocklist/photo/'.$row['Scientific'].'*.jpg'));
That way your returned paths will already be relative to your public folder. You could also do a str_replace as #sergiu suggested, but why not just get rid of it entirely?
Related
I need to read an XML file, i watched some tutorials and tried different sollutions, but for some reason I can't figure out why it doenst work.
The XML file that I want to read: http://www.voetbalzone.nl/rss/rss.xml
This is the code that im using:
$xml= "http://www.voetbalzone.nl/rss/rss.xml"
for ($i = 0; $i < 10; $i++)
{
$title = $xml->rss->channel->item[$i]->title;
}
The error I get: Premature end of data in tag
It works for me like this:
<?php
$xml = simplexml_load_file("http://www.voetbalzone.nl/rss/rss.xml");
for ($i = 0; $i < 10; $i++)
{
$title = $xml->channel->item[$i]->title;
}
?>
Note that you are overwriting the variable $title each time, so that you will have the title of the 10. element in it after the loop finished [I assume that is not what you want?]
To get all 'item'-Elements inside 'channel' as an Array to iterate through you can use xpath like this:
<?php
$xml = simplexml_load_file("http://www.voetbalzone.nl/rss/rss.xml");
$item_array = $xml->xpath("//rss/channel/item");
foreach($item_array as $item) {
echo $item->title . "\n";
}
?>
I would suggest to read about php's SimpleXML here: http://php.net/manual/en/book.simplexml.php
im trying to use a foreach loop with the plugin fotorama.
What im trying to do is load one half sized image for the main gallery image. Which i have working in a foreach, but i want to use a full image for the data-full tag but i cant get it to work.
This is the working code.
<div class="fotorama"
data-allowfullscreen="native"
data-nav="thumbs"
data-fit="scaledown"
data-width="100%"
data-height="100%"
data-arrows="true"
data-click="true"
data-swipe="true">
<?php
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
?>
</div>
this is what im trying to do.
<div class="fotorama"
data-allowfullscreen="native"
data-nav="thumbs"
data-fit="scaledown"
data-width="100%"
data-height="100%"
data-arrows="true"
data-click="true"
data-swipe="true">
<?php
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
$dirname2 = "admin/image-upload/full/";
$images2 = glob($dirname2."*.*");
$fullImgs = "<img data-full=".$image2." src=".$image." /><br />";
foreach($fullImgs as $fullImg) {
echo $fullImg;
}
?>
</div>
thanks in advanced guys
Try this:
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
$dirname2 = "admin/image-upload/full/";
$images2 = glob($dirname2."*.*");
$array = array_merge($images, $images2);
// Supossing both array have same length
$length = count($images);
for($i = 0; $j = $length; $i < $length; $i++, $j++) {
echo '<img data-full=".$images2[$j]." src=".$images[$i]." /><br />';
}
I think what you want is this:
<?php
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
$dirname2 = "admin/image-upload/full/";
$images2 = glob($dirname2."*.*");
//assuming $images and $images2 are the same length...
foreach ($images as $k => $v) {
echo "<img data-full=".$images2[$k]." src=".$v." /><br />";
}
?>
Haven't tested, but should give the idea...
Your code won't work like this. If I understood you correctly you have the same image in big and small format in two different directories. To show them as pair you need to make a connection between those two files - because how should your script know which images belong together? You could use a database, but in your case I think it is easier to make a connection in the filename. For example, name the pictures in the directory for the small images
Image_7263.png
Image_0172.png
And so on. For the big images you simply append _BIG to the end of the name.
Then you use your foreach loop to loop through the directory for the small images. For each image in there, you append _BIG to the end of the filename and include it from the directory for the big ones.
$smallImages = glob ("/path/to/small/images/*.*");
foreach ($smallImages as $img)
{
$name = substr ($img, 0, strlen ($img)-4); //Remove the .png or .jpg
$bigImg = "/path/to/big/images/".name."_BIG.jpg"; // or whatever image type you are using
echo "<img data-full=\"".$bigImg."\" src=\"".$img."\" />
}
I need to create a full page gallery showing only the last 4 images from a directory, i don't have idea how to introduce only the last 4 images saved in the directory, any idea?
<?php
$directory= "img";
$dirint = dir($directory);
while (($archivo = $dirint->read()) !== false){
if (!preg_match('/gif/i', $archivo)
|| !preg_match('/jpg/i', $archivo)
|| !preg_match('/png/i', $archivo)) {
for($x = 0; $x < 3; $x++) {
echo '<img src="'.$directory."/".$archivo.'">'."\n";
echo "<br>";
$x++;
}
}
}
$dirint->close();
?>
Hi i resolve my problem with a slice to the array dirint, thanks for the help here my solution:
<?php
$img_dir = "THEPATHTOYOURDIRECTORY";
$images = scandir($img_dir);
$imageslast = array_slice($images,-4,6);
rsort($imageslast);
with imagelast i make a slice to the array counting the "." and ".." i sorted in reverse to take the last image.
I have this code that shows all the images inside 'images' directory but it is very annoying because all the images are showing on a single page :/
how could i split those images on multiple pages ?
here is the code
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>
<?php
$files = glob("images/*.*");
echo '<div id="design">';
for ($i=0; $i<count($files); $i++) {
$num = $files[$i];
if ($i%3==0){echo '<div class="Row">';}
echo '<img class="img" width="250px" height="250px" src="'.$num.'" alt="random image" />';
if ($i%3==0){echo '</div>';}
}
echo '</div>';
?>
Pagination! Here's a starting point:
// glob list of images
$files = glob('images/*');
// for consistency, you'll have to sort the resulting array...
natcasesort($files);
// get a page number from a query string e.g: ?page=1
$page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT);
// filter_input returns null if there is no page value in the qs,
// so let's check that and add a default value if we need to
$page = $page ?: 1;
// slice the array! get a subset of the files array based on
// an offset (page number) and length (results per page)
$resultsPerPage = 5;
$slice = array_slice($files, (($page - 1) * $resultsPerPage), $resultsPerPage);
You can now display your subset of results as normal. Of course you'll have to supply a series of links for each page... That's straightforward: get the length of your $files array, and use your $resultsPerPage value to figure out how many pages you need to display.
Hope this helps :)
First, try replacnig this line with something more rubust. This will return all files (image or anything else) unless you are sure that only image files are in the folder:
$files = glob("images/*.*");
The $files will result in an array with paths to the images, you can easily use this feature to display only the number of images you want in a page.
like this:
<?php
$imagesPerPage = 10;
if(!isset($_GET["start"]))
{
$start = 0;
}
else
{
$start = $_GET["start"];
}
$files = glob("images/*.*");
for($i = $start; $i < $start + $imagesPerPage; $i++)
{
if(isset($files[$i]))
{
echo "<img src=\"".$files[$i]."\" width=\"100\" height=\"100\" />\r\n";
}
}
$start = $start + $imagesPerPage;
echo "<br />\r\n";
echo "NEXT";
?>
You can follow the same rules and make a pervious link as well!
Please note, stopping (disabling) NEXT or Pervious links is up to yourself!
I am creating an in-house order pulling application. I'm pulling form an ODBC source and placing items in an array. I'm then creating a new flat file for each order being physically worked on. When the user scans/enters an item from that order number it places that item on a new line in the order file that was created.
I'm then reading that order file back to get the items that have been scanned thus far. Where I'm stuck is how to mark that line item that exist in the order file as being completed in the HTML table.
Here is the pertinent code as it relates to my question:
$file_array = file_get_contents($file_ordnumber, "rb");
$items_array = explode("\n",$file_array);
echo "<table>";
for ($i = 0; $i < count($location_array); $i++)
{
echo "<tr>";
if (in_array("$itemno_array[$i]", $items_array)) {
echo "<td>$itemno_array[$i] EXISTS</td>";
}
else {
echo "<td>$itemno_array[$i] NO EXIST</td>";
}
// echo "<td>$location_array[$i]</td>";
echo "<td>$qty_array[$i]";
echo "<td>$pickingseq_array[$i]</td>";
echo "</tr>";
}
echo "</table>";
As you can see I'm iterating through the array and displaying it in a HTML table. I'm curious why my above code isn't working. My result ends up being from the 'else' statement thus ALL lines, even if they exist in the file is showing as "NO EXIST" which is obviously incorrect.
Can you post your Print_r($items_arr);
also try using isset($arrayVar[$key]
and try removing double quotes and see if it works.. like ...
$file_array = file_get_contents($file_ordnumber, "rb");
$items_array = explode("\n",$file_array);
echo "<table>";
for ($i = 0; $i < count($location_array); $i++)
{
echo "<tr>";
if (in_array($itemno_array[$i], $items_array)) {
echo "<td>".$itemno_array[$i]." EXISTS</td>";
}
else {
echo "<td>".$itemno_array[$i]." NO EXIST</td>";
}
// echo "<td>".$location_array[$i]."</td>";
echo "<td>".$qty_array[$i]."";
echo "<td>".$pickingseq_array[$i]."</td>";
echo "</tr>";
}
echo "</table>";
I got this resolved by going about it a different way. I use strpos() to search the flat file itself rather than the array that I exploded from it:
echo "<table>";
//for ($i = 0; $i < count($itemno_array); $i++)
for($i=0;$i<sizeof($itemno_array);$i++)
{
echo "<tr>";
// if (in_array($itemno_array[$i], $items_array)) {
echo "<td>";
$var = $itemno_array[$i];
$newvar = trim($var);
if(strpos($file_array, $newvar ) !==FALSE) {
echo "$var ** EXISTS</td>";
}
else {
echo "$var DOES NOT EXIST **</td>";
}
// echo "<td>$location_array[$i]</td>";
echo "<td>$qty_array[$i]";
echo "<td>$pickingseq_array[$i]</td>";
echo "</tr>";
}
echo "</table>";