Log in

View Full Version : HTML within PHP (IMG)



moose86
01-27-2014, 07:52 PM
Hi all,

I am have this code exactly the way I want it but i wondered if you knew maybe how to add the same img in the html[<a href="<? '$filePath$img' ?>"] as in the php [echo "<img src='$filePath$img'/>";]


<?
$string =array();
$filePath='images/photos/';
$dir = opendir($filePath);
while ($file = readdir($dir)) {
if (eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) {
$string[] = $file;
}
}
while (sizeof($string) != 0){
$img = array_pop($string);
?>
<a href="<? '$filePath$img' ?>" class="galpop" data-galpop-group="gallery">
<?
echo "<img src='$filePath$img'/>";
}
?>
</a>

Thanks

Beverleyh
01-27-2014, 09:14 PM
I think you mean to echo it into the hyperlink href <a href="<?php echo "$filePath$img"; ?>" class="galpop" data-galpop-group="gallery"> but your description is confusing because both methods use PHP to put the image into the HTML. I'm assuming you meant to ask how to add the img into the href.

moose86
01-27-2014, 11:23 PM
Hi Beverley, thank you for responding, i have changed it and have also looked around as to y i am getting an error on the line beguining "<a href..." but canot find an answer, do you know what may be causing the problem?


NEW CODE:

<?
$string =array();
$filePath='images/photos/';
$dir = opendir($filePath);
while ($file = readdir($dir)) {
if (eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) {
$string[] = $file;
}
}
while (sizeof($string) != 0){
$img = array_pop($string);
?>
<a href="<? php echo "$filePath$img"; ?>" class="galpop" data-galpop-group="gallery"></a>;
<?
}
?>

PS, im still new to PHP

Thanks :D

jscheuer1
01-28-2014, 07:55 AM
On the <a href line,


<? php

needs to be either:


<?php

or just (since it looks like you have the shorthand tags enabled):


<?

I'd go with (array is assumed with $varname[] = whatever, ereg and eregi are deprecated and regardless, the regex's can be combined):


<?php
$filePath = 'images/photos/';
$dir = opendir($filePath);
while ($file = readdir($dir)) {
if (preg_match("/\.(png)|(jpg)|(gif)$/i", $file)) {
$string[] = $file;
}
}
while (sizeof($string) != 0){
$img = array_pop($string);
?>
<a href="<?php echo "$filePath$img"; ?>" class="galpop" data-galpop-group="gallery"></a>
<?php
}
?>

But you won't see anything on the page, so maybe (gives thumbnail images):


<?php
$filePath = 'images/photos/';
$dir = opendir($filePath);
while ($file = readdir($dir)) {
if (preg_match("/\.(png)|(jpg)|(gif)$/i", $file)) {
$string[] = $file;
}
}
while (sizeof($string) != 0){
$img = array_pop($string);
?>
<a href="<?php echo "$filePath$img"; ?>" class="galpop" data-galpop-group="gallery"><img border=0 src="<?php echo "$filePath$img"; ?>" height=100></a>
<?php
}
?>

moose86
01-28-2014, 06:45 PM
Thank You both for helping me with this issue, John your code works a treat :D

moose86
01-28-2014, 08:08 PM
Hi, got another question, can I add a statement to display words, if no pics exist in the specified folder?

also can i make it so the file name is the caption of the pic?

traq
01-28-2014, 11:17 PM
Hi, got another question, can I add a statement to display words, if no pics exist in the specified folder?
You'd have to keep track of whether or not there were any images found (e.g., by setting a flag $foundPics = true when you find one), and then you could print a message if none were.


also can i make it so the file name is the caption of the pic?
Meaning, the value you find in $img? Sure:
<img src="<?php echo $filePath.$img; ?>" alt="<?php echo $img; ?>">

jscheuer1
01-29-2014, 02:47 AM
The alt attribute is not a caption. The title attribute could be used to produce a hover tooltip. For an actual caption, an element like a span, div, etc. with that text or that text alone would be required. Like:


<style type="text/css">
.galdiv {
text-align: center;
float: left;
margin-right: 3px;
}
.galdiv img {
height: 100px;
border-width: 0;
}
</style>
<?php
$filePath='album/pics/';
$dir = opendir($filePath);
while ($file = readdir($dir)) {
if (preg_match("/\.(png)|(jpg)|(gif)$/i", $file)) {
$string[] = $file;
}
}
if(sizeof($string) < 1){
echo "No images found in $filePath";
}
while (sizeof($string) > 0){
$img = array_pop($string);
$ifn = substr($img, 0, strrpos($img, '.'));
?>
<div class="galdiv"><a href="<?php echo "$filePath$img"; ?>" class="galpop" data-galpop-group="gallery"><img src="<?php echo "$filePath$img"; ?>" alt="<?php echo $img; ?>" title="<?php echo $ifn; ?>"></a><br><?php echo $ifn; ?></div>
<?php
}
?>