I have a txt file that has in it a painting id. This painting id is the same as the name of the image. For example in the txt file the painting id is 00000 and the name of the images in the path I wrote in my echo statement is 00000.jpg.
What I'm trying to do is to display all the images using this for loop and echo statement. But for some reason the images are not displaying. Whats displaying instead is thats what is being displayed instead of the images. Can anyone help me solve this problem please?
<?php
$paintings = file('paintings.txt') or die('ERROR: Cannot find file');
$delimiter = '~';
foreach ($paintings as $painting){
$paintingFields = explode($delimiter, $painting);
$painting_id = $paintingFields[0];
}
for($i=0;$i<count($paintings); $i++){
echo "<img src='resources/paintings/square-medium/%s.jpg' . $painting_id></br>";
}
?>
You're using echo wrong. If you view the source of your generated page you'll see the literal '%s' in the code.
To use '%s' as a format string, use sprintf():
echo sprintf("<img src='resources/paintings/square-medium/$s.jpg'></br>", $painting_id);
Or:
echo "<img src='resources/paintings/square-medium/" . $painting_id . ".jpg'></br>";
Even better, just do the ouputting in PHP mode:
for($i=0;$i<count($paintings); $i++){
?>
<img src="resources/paintings/square-medium/<?= $painting_id ?>.jpg"><br>
<?php
}
Related
What I was looking for before was a way to dynamically create links for the same directory but have different URL variables, that way the same page works from the variables provided to load images from a folder. Below was my example code for loading all of the images inside the directory.
$dir = "./Ch.001/";
$a = glob($dir."*png");
foreach($a as $image){
echo "<img src='". $image ."'>";
}
?>
At the time I wasn't sure what to research to achieve what I wanted but thanks to an answer and some more experience in PHP, my answer to what I was looking for above would have been $_GET variables and urlencode()/htmlspecialchars().
Now I would use a database to store all of the information I wanted to display, so when the series is created from a form I would store the title, description, etc, and then calculate how many rows are in the database, and then make that number + 1 the file name, and maybe add a prefix so it isn't just numbers.
Presuming 001 is 001 to n etc.
Use a simple $_GET parameter, check or cast to int, 0 pad it and then show your links with +1 for next.
<?php
$chapter = str_pad((int) ($_GET['chapter'] ?? '001'), 3, "0", STR_PAD_LEFT);
//
$dir = "./Ch.$chapter/";
//
$images = glob($dir."*png");
if (empty($images)) {
echo 'Go back';
} else {
foreach($images as $image){
echo "<img src='". $image ."'>";
}
echo 'Chapter: '.$chapter.PHP_EOL;
echo 'Goto Chapter';
}
I'm pulling an image from a directory (images) and placing it in an html table row along with my mysql query. PHP finds the image as the mysql DB id matches the image name. i.e. if id = 12 then 12.jpg is pulled from directory using $imgnum in the path.
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$imgnum = $row["id"];
?>
<tr>
<td>
<img src="images/<?php echo $imgnum ?>.jpg"/>
The issue is there may be a series of images all associated with that row, i.e. 1-1.jpg 1-2.jpg and so on, sometimes there may be only one and the code above works fine.
Is there a way to output all images with the name equalling the id but with a dash afterwards so it picks up other associated images? Even if someone can tell me what to learn to achieve this?
cheers
You can use php's glob() to find similar files. Something along the lines of this:
foreach (glob("/path/to/images/folder/" . $imgnum . '-*') as $filename) {
// do something with $filename
}
Thought I'd post what I used from the help above. It now outputs a list of all of the images I needed.
<?php
$imgnum = $id;
foreach (glob("images/" . $imgnum . '-*') as $filename) {
echo "<a href=''><img src='$filename' class='imgsize'></a> <br>";
}
?>
I kind of have a problem.
Maybe I'm doing the hardest way, I do not really know, if you could I'll appreciate for your help.
I upload photo to FTP and URL was upload to mysql in this format (http:///claim/img/box.png, http:///claim/img/box.png) with specific ID
-----------------------------------
ID - URL
1 - http://*/claim/img/box.png, http://*/claim/img/box.png
-----------------------------------
And later I grab this data, and to generated form by ID.
I used explode to divide URL. And with FOR loop, I would display it perefctly, But here is the glitch. Because there is need to make export this form as a doc file and send email with this generated form. I can't end echo.
Because when I do only first part is send, but not images from array.
For example:
$email = "ID 1<br>(Its the start of the form)";
$photos = explode(',', $claim['photos_url']); $arrlength = count($photos);
for($x=0;$x<$arrlength;$x++) {
echo "<img src=".$photos[$x]."><br>"; }
echo "The end of form";
I similar cases I simply use echo "the start of text".$date."the end";
But now, I have no idea how to do it.
Thank you for your help in advance.
your code looks so incomplete , but i think you have an array some how that includes image urls
and you want to echo each one of them :
$images = array("http://127.0.0.1/img/1.jpg","http://127.0.0.1/img/2.jpg","http://127.0.0.1/img/3.jpg");
foreach($images as $image)
{
echo "<img src='".$image."' />";
}
im not sure if i understand u correctly but hope it helps...
I have a piece of php that if a specified folder has a file in, it will display the filename, date created and present a download button, if the folder is empty it will show nothing. This works very well but if I have more than one file in the folder it bunches all the filenames together - what I want is the separate information displayed for every file.
To help you understand the problem here is an image showing the problem and the code. I got very far on my own but its way above my head, I just cant see a simple way to correct the problem. The code may look very awkward and odd as I'm totally new at this but it looks visually right on the browser. I would really appreciate any help thank you.
Here is an image of the problem: http://i46.tinypic.com/m79cvs.png
<?php if (!empty($thelist)) { ?>
<p class="style12"><u>Fix</u></p>
<p class="style12"><?=$thelist?><?php echo " - " ?> <?php $filename = '../../customers/client1/client1.fix.exe';
if (file_exists($filename)) {
echo "" . date ("m/d/Y", filemtime($filename));
}
?> <?php echo " - <a href='download.php?f=client1/client1.fix.exe'><b>Download</b></a> <a href='download.php?f=client1/client1.fix.exe'>
<img src='../css/images/dlico.png' alt='download' width='35' height='32' align='absmiddle' /></a>" ?>
</p>
<?php } ?>
The list ($thelist) contains your files, yes?
You are not working on the $thelist, but on the $filename which is a hardcoded string.
Why? Currently you are outputting <?=$thelist?> and it looks like concatenated string from filenames. I would suggest that $thelist should be something like an array of your files. Then you could iterate over the files and output html dynamically for each entry.
<?php
// define your directory here
$directory = xy;
// fetches all executable files in that directory and loop over each
foreach(glob($directory.'/*.exe') as $file) {
// output each name and mtime
echo $file . '-' . date ("m/d/Y", filemtime($file));
// or you might also build links dynamically
// $directory needs to be added here
echo ''.$file.' - Size: '.filesize($file).'';
}
?>
I want to:
Read in text line from "textfile.txt".
'echo' that line to the page in a <div> element.
Read in a text line from "namefile.txt".
Make this line become some sort of pop-up-text for that <div> element.
My script:
<? PHP
$fhtext = fopen("textfile.txt","a+") or exit("Error 1");
$fhname = fopen("namefile.txt","a+") or exit("Error 2");
while(!feof($fhtext))
{
echo "<div title="HERE IS WHERE I AM STUCK">".fgets($fhtext)."<div/><br />";
}
Could I perhaps go:
echo "<div title="<? fgets($fhname) ?>".fgets($fhtext)."<div/><br />";
?
<?php
$fhtext = fopen("textfile.txt","a+") or exit("Error 1");
$fhname = fopen("namefile.txt","a+") or exit("Error 2");
while(!feof($fhtext) && !feof($fhname))
{
echo "<div title=\"", fgets($fhname), "\">", fgets($fhtext), "<div/><br />";
}
?>
I haven't used PHP in a long time, but this should work:
echo "<div title='" . fgets($fhname) ."'>" .fgets($fhtext). "<div/><br />";
Regarding:
Make this line become some sort of pop-up-text for that '' element.
If you mean 'popup' text, as in tooltips of the type you get when you hover over links/images, this is only available on some elements when their title attribute has been set, not DIVs.
As such you can either change the DIV to a A (link) element. Or use Javascript to detect a hover over the DIV and display a popup.
If you are sure both files have the same number of lines you could use the „file“-function of PHP. This will read the file into an array and you can loop over it with a for-loop:
<?php
$file1 = file('file1');
$file2 = file('file2');
for ($i = 0, $max = count($file1); $i < $max; $i++) {
echo $file1[$i].' '.$file2[$i];
}
Before you dump your fgets() data to the browser, you really ought to HTML encode it first. That will prevent accidental (or not so accidental) problems caused by HTML fragments that might be in your text files, or if the file name can be entered by the user (either as part of the URL or as part of a form).
As a rule of thumb, always HTML encode anything coming from a data source you don't control before spitting it out to the browser. That includes form fields, etc.