I have a script which will list all the files from the image folder on the server into a drop down box.
$dir='images';
$list=scandir($dir);
if(isset($_POST['submit'])) echo 'Selected: ' . $_POST['image'];
foreach($list as $file)
{
//ignore . (current dir) and .. (parent dir)
if($file!=='.'&&$file!=='..')
{
echo "<option value=\"$file\"";
if(isset($_POST['submit'])&&$_POST['image']==$file) echo 'selected="selected"';
echo ">$file</option>";
}
}
What I am trying to do is when the file name of the image is selected that image is displayed.
echo "<img src='images/". $file ."' />";
I tried using the file verable but it only seams to display the last image in the directory. How would I go about fixing this.
You will have to do this with Javascript. Once the php script loads it has no one of knowing what is happening in the users window. You will want to create an event listener to the onChange even of your option group. And when the selection changes you will want to replace the src of the image tag being used to show a preview.
I'd highly suggest using a JS Library such as Jquery or simliar to help with the event listening, and dom manipulation.
If you mean doing this on the form load, I’d say replace
if(isset($_POST['submit'])&&$_POST['image']==$file) echo 'selected="selected"';
with
if(isset($_POST['submit'])&&$_POST['image']==$file)
{
echo 'selected="selected"';
$selected_file = $file;
}
then this instead
echo "<img src='images/". $selected_file ."' />";
Because at the moment, it’ll just load which ever one is last out of the foreach
Related
I am fetching data from database table and showing result in HTML table. What I want to achieve is that, there is image column in every row, and every image is fetched from its URL (stored in database). I am opening an image in new tab whenever it is clicked.
My question is that, how can I store url of image so that on the next page I don't want to show that real url?
The next page will look like this link:
www.example.com/full_size_image.php
and add
img tag
there to show that page
how can I store that specific image url when someone click on it?
<img src= "<?php echo $_SESSION['link'] ;">
My current code is :
while($result = $sql->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
echo "<td>".$result['c']."</td>";
echo "<td>".$result['UserName']."</td>";
echo "<td>".$result['UserProblemKeyword']."</td>";
echo "<td> <a href ='".$result['UserProblemPicture']."' target='_blank'><img src='".$result['UserProblemPicture']."' height='62' width='62'> </a> </td>";
echo "</tr>";
}
Use a parameter via the GET Request?
So instead of directing to just /full_size_image.php direct to
/full_size_image.php?img=filename.png
then in your php code for /full_size_image.php
You can fetch the file name via
<?php
$img = isset($_GET['img']) ? htmlspecialchars($_GET['img']) : "";
if ($img == "") {
//img not set
}
//do whatever with the filename
I am new to PHP. I want to know how to be able to upload a product image from the back end to the front end of a static website using PHP. Please help me. Thank you in advance.
Select image :
<?php
if(isset($_POST['Submit1']))
{
$filepath = "images/" . $_FILES["file"]["name"];
if(move_uploaded_file($_FILES["file"]["tmp_name"], $filepath))
{
echo "<img src=".$filepath." height=400 width=300 />";
}
else
{
echo "Error !!";
}
}
?>
I have tried the code above. It is running, but the image is displaying only on the same page. However, I need the image to display on another page when I click on submit button.
You can redirect this page when image is uploaded;
header( 'Location:http://anypage.com/?filepath='.$file_path );
and you can use get method;
$filepath=$_GET['filepath']
echo "<img src=".$filepath." height=400 width=300 />";
You can NOT pass the variable from a page to another until you use Session in PHP. But that is not a good practice for product image.
You should store your $filepath in the database and fetch it every where in your application when you need that.
I have read the other threads on this topic but have not found an answer to my particular situation. I have modified the code from http://jsfiddle.net/g9R4H/ to produce an example of what I'm trying to accomplish. In my implementation I may have multiple duplicates of multiple images. One image may have two thumbnails and another may have 3 all on the same page. I have narrowed down the problem to the eq(0) setting and have determined that changing that value to 1 selects a different image when fancy box opens. I have not been able to determine how to set that value based on the image that is clicked. In my actual scripts the links are being generated via php and I do know when I create the trigger links what image value it relates to. I'm quite the rookie when it comes to javascript so keep that in mind when you answer. TIA
<a data-trigger-rel="gallery" class="fancybox-trigger" href="http://fancyapps.com/fancybox/demo/2_b.jpg"><img src="http://fancyapps.com/fancybox/demo/2_s.jpg" alt=""/></a>
<a data-trigger-rel="gallery" class="fancybox-trigger" href="http://fancyapps.com/fancybox/demo/1_b.jpg"><img src="http://fancyapps.com/fancybox/demo/1_s.jpg" alt=""/></a>
<br />
<br />
<a rel="gallery" class="fancybox" href="http://fancyapps.com/fancybox/demo/2_b.jpg"><img src="http://fancyapps.com/fancybox/demo/2_s.jpg" alt=""/></a>
<a rel="gallery" class="fancybox" href="http://fancyapps.com/fancybox/demo/1_b.jpg"><img src="http://fancyapps.com/fancybox/demo/1_s.jpg" alt=""/></a>
$(".fancybox-trigger").click(function() {
$("a[rel='" + $(this).data('trigger-rel') + "']").eq(0).trigger('click');
return false;
});
$(".fancybox").fancybox();
I haven't had any responses, but after some thought I come to a solution using PHP. Basically I am using php to create a separate trigger for each occurrence of duplicate images. When the trigger links are created the trigger array is updated. After all the links have been added, the trigger array is processed and a separate trigger created for each. If someone else is having this issue, hopefully this will help.
<?php
$a=0;
$image_id_array=array();
$trigger_array=array();
$result_set= get_collection_standards($sel_collection);
while ($standard = mysqli_fetch_array($result_set)) {
$image_id=$result["image_id"];
$path=$result["path"].$result["file_name"];
$caption = htmlentities($result["caption"]);
If (!array_key_exists($image_id,$image_id_array)){
$image_id_array[$image_id]=$a;//image_id receives the key value and the value is the photo number in the gallery - 1.
$a+=1;
$link="<a rel=\"feature_gallery\" class=\"fancybox\" href=\"{$path}-l.jpg\"";
} Else { //if the image does exist create a trigger link.
$link="<a data-trigger-rel=\"feature_gallery\" class=\"fancybox-trigger". $image_id_array[$image_id] .
"\" href=\"{$path}-l.jpg\" ";
if (!array_key_exists($image_id,$trigger_array)) { //update the trigger array to be used below.
$trigger_array[$image_id]=$image_id_array[$image_id];
}
}
$link .= " title=\"{$caption}\"><img src=\"{$path}-s.jpg\" ></a>";
echo $link;
}
foreach ($trigger_array as $position) {
echo "<script>";
echo " $(\"a.fancybox-trigger{$position}\").click(function() {";
echo " $(\"a[rel='\" + $(this).data('trigger-rel') + \"']\").eq(" . $position . ").trigger('click');";
echo " $.fancybox.open($(this).attr('tirgger-rel'));";
echo " return false;";
echo "});";
echo "</script>";
}
?>
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.