Links from Array - php

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...

Related

Images are not being displayed on php

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
}

Im new to php, how can I use links to change what loads on a page?

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';
}

PHP pull specific sets of images from a directory

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>";
}
?>

How to while loop images from directory

Surprisingly, Google had nothing.
My old method of uploading images was storing the actual image in the database. I'm trying to change my method to storing the photos in directories. But I'm having troubles figuring out how to display them to the users. I'm trying to while loop the files. Much help is greatly appreciated. Also, PDO is not my strong suit, trying to learn it the best I can. Here is what I have.
$query6 = "SELECT * FROM photos WHERE userID=$memberID";
$result6 = $db->query($query6);
while ($row6 = $result6->fetch(PDO::FETCH_ASSOC)) {
$photo = $row6['photo'];
while ($photo == true) {
echo "<img src=\"images/$memberID/$photo\">";
}
}
The page isn't loading at all. Not even an error. Your help is greatly appreciated. Thank You!
The code does insert the photo name in database and properly upload the photo to the directories. I only need assistance with displaying to the user. :)
while ($photo == true)
{?>
<img src="images/<? echo $memberID;?>/<?echo $photo;?>">
close your img tag.like
echo "<img src=\"images/$memberID/$photo\" />";

<div title="Can I somehow put PHP code in this attribute?"></div> or is there another route I can take?

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.

Categories