Displaying all Images within a Folder using php - php

I've used this php code within my html in order to display all pictures within a folder called uploads. The only problem is that icons images are the only type of image being displayed. What have I done wrong?
<?php
$files = glob("uploads/*.*");
$colCnt=0;
echo '<table border="1" style="width:590px;">';
for ($i=1; $i<count($files); $i++)
{
$colCnt++;
if ($colCnt==1)
echo '<tr>';
echo '<td width="25%" style="font-size:8.5px; font-family:arial">';
$num = $files[$i];
echo '<img src="'.$num.'" align="absmiddle" /> ';
print substr(substr($num,6,100),0,-4);`
echo '</td>';
if ($colCnt==4)
{
echo '</tr>';
$colCnt=0;
}
}
echo '</table>';
?>

The loop seems to be ok.
You have to focus on the result coming from
$files = glob("uploads/*.*");
Try print_r($files) to get the list and then see if it is selecting all the images properly or not.

Related

How can I run a single PHP function that gives diffirent results depending on the button value?

First off I do apologize if this has been asked before or something similar. I'm working on this little project for at home and to help me learn a bit on PHP.
What I am trying to achieve here is to have buttons/links that are dynamically generated by reading a directory from one function. This I have accomplished which works as intended for that aspect. Once those are generated and displayed to the user with the webpage. I want to use only one function for all buttons that depending on the button value/name it is passed to the function displays the corresponding directory in HTML.
How can I pass a value from the dynamic buttons to a variable in a different function?
Code I have so far:
This first function works as intended(it echo's out the specific directory):
function btnSeries(){
$season = "./SomeDirectory/";
$files = glob("./Some/Directory_images/*.*");
for ($i=0, $f=1; $i<count($files) && $f<count($files); $i++, $f++)
{
$num = $files[$i];
print '<div class="col-md-4">';
print '<div class="card">';
print '<img class="card-img-top" src="'.$num.'"alt="Season '.$f.'">';
print '<div class="card-body">';
//print '<br>';
print '<form method="post" action="episodes.php">';
print '<input type="submit" class="btn btn-primary" value="Season'.$f.'" name="button" />';
print '</form>';
//print 'Season '.$f.'';
print '</div>';
print '</div>';
print '</div>';
}
}
The single function that receives the value for Variable from the onclick event
if(array_key_exists('button', $_POST)) {
seasonEpisodes();
}
function seasonEpisodes(){
$buttonValue = "value" //Value passed from button (ie: button name which represent the folder of series)
$season = "./SomeDirectory/";
$files = glob("./Series/.$buttonValue/*.*");
include 'layout/header.php';
print '<td>';
print '<p align="center">';
print '<img border="0" src='.$someVar.' width="370" height="529">';
print '</p>';
print '</td>';
for ($i=0, $f=1; $i<count($files) && $f<count($files); $i++, $f++)
{
$num = $files[$i];
print '<p align="center"><font color="#FFFFFF" size="6">';
print '<a href=".$season.$file.">';
print '<font color="#FFFFFF">.$file.</font></a>'
}
print '</td>'
include 'layout/footer.php';
}
Thought I would Provide an updated Function with the variable being able to be passed on to the second function thank to RGriffiths. Adding this worked like a charm.
function btnSeries(){
$season = "./SomeDirectory/";
$files = glob("./Some/Directory_images/*.*");
for ($i=0, $f=1; $i<count($files) && $f<count($files); $i++, $f++)
{
$num = $files[$i];
print '<div class="col-md-4">';
print '<div class="card">';
print '<img class="card-img-top" src="'.$num.'"alt="Season '.$f.'">';
print '<div class="card-body">';
//print '<br>';
print '<form method="post" action="episodes.php">';
print '<input type="hidden" value="Season'.$f.'" name="seasonClicked" />';
print '<input type="submit" class="btn btn-primary" value="Season'.$f.'"/>';
print '</form>';
//print 'Season '.$f.'';
print '</div>';
print '</div>';
print '</div>';
}
}
if(array_key_exists('button', $_POST)) {
seasonEpisodes();
$buttonValue = $_POST["seasonClicked"]; //Value passed from button
$season = "./SomeDirectory/";
$files = glob("./Series/$buttonValue/*.*");
include 'layout/header.php';
print '<td>';
print '<p align="center">';
print '<img border="0" src='.$someVar.' width="370" height="529">';
print '</p>';
print '</td>';
for ($i=0, $f=1; $i<count($files) && $f<count($files); $i++, $f++)
{
$num = $files[$i];
print '<p align="center"><font color="#FFFFFF" size="6">';
print '<a href=".$season.$file.">';
print '<font color="#FFFFFF">.$file.</font></a>'
}
print '</td>'
include 'layout/footer.php';
}
Hide the input of the season value for each form:
print '<form method="post" action="episodes.php">';
print '<input type="hidden" value="Season'.$f.'" name="seasonClicked" />';
print '<input type="submit" class="btn btn-primary" value="Season'.$f.'"/>';
print '</form>';

PHP issues displaying image gallery

I'm trying to make a gallery with PHP. I want to get all of the images out of a folder and then display them in rows of 3. I kind of have it working but the first 2 images mess up.
This is what I've tried:
$images = glob("$_SERVER[DOCUMENT_ROOT]/gallery/img*.{png,jpg,gif}", GLOB_BRACE);
echo '<table width="100%>';
$count="-1";
foreach($images as $image) {
if ($count%3 == 1) {
echo '<tr>';
}
$url=str_replace("/home/#####/public_html/gallery", "", $image);
echo '<td width="33%"><div class="gallery">';
echo '<img onclick="window.location='.$url.'" src="'.$url.'" alt="Image Alt" width="400" height="300">';
echo '</div></td>';
if ($count%3 == 3) {
echo '</tr>';
}
//echo $count;
$count++;
//echo "|".$count;
}
if ($count%3 != 1) {
echo ',</tr>';
}
echo '</table>';
//echo print_r($images);
This works kind of but it makes this:
(These are just stock photos, the real photos are a bit.. offensive)
I know I'm doing something wrong but I don't know what!
There were some errors in your code (see comments). Maybe try this:
$images = glob("$_SERVER[DOCUMENT_ROOT]/gallery/img/*.{png,jpg,gif}", GLOB_BRACE);
echo '<table style="width:100%">'; // error was here (missing ")
$count = 0; // error was here (counter = "-1")
foreach ($images as $image) {
// start <tr> on 0
if ($count == 0) {
echo '<tr>';
}
$url=str_replace("/home/#####/public_html/gallery/", "", $image);
echo '<td style="width:33%"><div class="gallery">'; // alternative
echo '<img onclick="window.location='.$url.'" src="'.$url.'" alt="Image Alt" width="400" height="300">';
echo '</div></td>';
// end tr at 3
if ($count == 3) {
echo '</tr>';
// reset counter
$count = -1;
}
$count++;
}
echo '</table>';
I think you have trouble with your $count initial value.
Try this:
$count="3";
foreach($images as $image) {
if ($count%3 == 0) {
echo '<tr>';
}
$count++;
...

How to put mysql data into popup window?

I have a web page with images and when user clicks on any of the image, it has to derive data of that particular image from MYSQL database. What I am doing is using a simple JavaScript popup and putting the data from database. However I am just getting the first item from database on all images.
This is the code:
$files = glob("admin/images/paintings/*.*");
echo '<div id="painting"><table border="0" style="width:590px;">';
$colCnt=0;
$i = 0;
while($row = mysql_fetch_array($result))
{
if ($colCnt%4==0)
echo '<tr>';
echo '<td width="25%" style="font-size:8.5px; font-family:arial">';
echo($i);
$num = $files[$i];
echo '<img id="indPainting" src="'.$num.'" align="absmiddle" /> <br> <div id="paintingName">';
print $row['name'];
echo '<div id="openModal" class="modalWindow">
<div>
<p>This is a sample modal window that can be created using CSS3 and HTML5.'.$row['name'].'</p>
Ok
</div>
</div>';
echo '</td>';
$colCnt++;
if ($colCnt==4)
{
echo '</tr>';
$colCnt=0;
}
$i++;
}
mysql_close($con);
include 'footer.php';
?>
$row['name'] is just giving out the first name as it is in a while loop. I am not being able to get other names for other images. How can this be done. Any help would be appreciated.
Does one iteration in your while fetch single image data? And what I can understand according to your code is that you are displaying 4 image in a row.
Can you please format your code a bit..its looking too ugly.
I need to know which statement is calling your modal window.
<?php
$files = glob("admin/images/paintings/*.*");
echo '<div id="painting"><table border="0" style="width:590px;">';
$colCnt=0;
$i = 0;
echo '<tr>';
while($row = mysql_fetch_array($result))
{
$num = $files[$i];
echo '<td width="25%" style="font-size:8.5px; font-family:arial">';
echo '<img id="indPainting" src="'.$num.'" align="absmiddle" /> <br>
<div id="paintingName">';
print $row['name'];
echo '<div id="openModal" class="modalWindow"><div><p>This is a sample modal window that can be created using CSS3 and HTML5.'.$row['name'].'</p>Ok</div>
</div></td>';
$colCnt++;
if ($colCnt % 4 == 0)
{
echo '</tr>';
$colCnt=0;
}
$i++;
}
mysql_close($con);
include 'footer.php';
?>
Try this.
Also see how beautiful the code looks if its properly formatted..
try this
<?php
$files = glob("admin/images/paintings/*.*");
echo '<div id="painting"><table border="0" style="width:590px;">';
$colCnt=4;
while($row = mysql_fetch_array($result))
{
for ($i = 0; $i < $colCnt; $i++) {
echo '<tr>';
echo '<td width="25%" style="font-size:8.5px; font-family:arial">';
echo($i);
$num = $files[$i];
echo '<img id="indPainting" src="'.$num.'" align="absmiddle" /> <br> <div id="paintingName">';
print $row['name'];
echo '<div id="openModal" class="modalWindow">
<div>
<p>This is a sample modal window that can be created using CSS3 and HTML5.'.$row['name'].'</p>
Ok
</div>
</div>';
echo '</td>';
}
if ($colCnt==4)
{
echo '</tr>';
$colCnt=0;
}
}
mysql_close($con);
include 'footer.php';
?>

display 2 or 3 images per row using php mysql

hi i'm disaplyed images from mysql db table but it displays on by one means one row has one image. but i need 3 or 4 image per row. my coding is below. please give some idea.
<?php
include_once("config.php");
$result=mysql_query("SELECT * FROM merchant");
while($res=mysql_fetch_array($result))
{
?>
<?php echo $res['description'];?></p>
<img src="<?php echo $res['image'];?>" width="80" height="80"/>
<?php } ?>
Do it in table like this, You might need to fix it a little bit, but it way how it will work
<table>
<?php
include_once("config.php");
$result=mysql_query("SELECT * FROM merchant");
$count = 0;
while($res=mysql_fetch_array($result))
{
if($count==3) //three images per row
{
print "</tr>";
$count = 0;
}
if($count==0)
print "<tr>";
print "<td>";
?>
<?php echo $res['description'];?></p>
<img src="<?php echo $res['image'];?>" width="80" height="80"/>
<?php
$count++;
print "</td>";
}
if($count>0)
print "</tr>";
?>
</table>
use a <table> to display.
<?php
include_once("config.php");
$result=mysql_query("SELECT * FROM merchant");
$count = 0;
echo '<table>';
while($res = mysql_fetch_array($result))
{
if($count % 2 == 0) echo '<tr>';
?>
<td>
<p><?php echo $res['description'];?></p>
<img src="<?php echo $res['image']; ?>" width="80" height="80"/>
</td>
<?php
if($count % 2 == 0) echo '</tr>';
} ?>

Performing different actions on dynamically generated buttons. PHP

I am working on a PHP Gallery application, and need some help here. Actually I have a page where images from a specific directory are displayed directly. With each one of the images displayed there is a dynamically generated submit button that will be used to delete respective images separately.
Every image has its own submit button, that will be used to delete that image. Being new to php I need some method that can be called to delete only that image from the actual or physical directory.
There is a similarity between image and button that I have coded it such that every image and its respective button has names such as "img_1" and its button is "del_1".
<form id="albumGallery" name="albumGallery" method="POST">
<?php
$dir = htmlspecialchars($_GET["p"]) . "/";
$imgs = array();
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (!is_dir($file) && preg_match("/\.(bmp|jpe?g|gif|png)$/", $file)) {
array_push($imgs, $file);
}
}
closedir($dh);
} else {
die('cannot open ' . $dir);
}
$i=0;
echo "<div id='images'>";
foreach ($imgs as $idx=>$img) {
//$class = ($idx == count($imgs) - 1 ? ' class="last"' : '');
echo '<table style="float: left; border: 1px solid #EFEFEF; border-radius: 5px; padding: 5px; margin: 5px;"><tr><td><a href="'. $dir . $img .'" rel="example_group" ><img src="' . $dir . $img . '" alt="' . $img . '" id="'. "img_" . $i .'"/>
</a></td></tr><tr><td><input type="submit" class="ctrlDelete" value="" id="'. "del_" . $i .'"/></td></tr></table>';
$i++;
}
echo "</div>";
?></form>
So, I need to make a method so that each button deletes its respective image and the form is posted back to self.
For your issue, it is better to use anchors. You can style them as pseudo-buttons, if you want. Then just generate links like delete.php?id=23, which will execute the appropriate deletion script with $_GET argument passed.
Below is the very simple implementation:
<table>
<tr>
<td>Title</td>
<td>Image</td>
<td>Actions</td>
<tr>
<?php
foreach ($table as $row)
{
echo "<tr>";
echo "<td>".$row['title']."</td>";
echo "<td>".$row['image']."</td>";
echo "<td>";
echo "<a href='delete.php?id=".$row['id']."'>Delete</a>";
echo "<a href='edit.php?id=".$row['id']."'>Edit</a>";
echo "</td>";
echo "</tr>";
}
?>
</table>
delete.php and edit.php should contain the following code at the very end:
<?php
header("Location: http://www.example.com/");
?>
#Edward Ruchevits
Thanks for your help :D,
I did not use the header(); method but used the javascript's settimeout(); to redirect my page. Here is my code...
<script type="text/javascript">
setTimeout("window.location = '<?php echo $_SERVER['HTTP_REFERER'] ?>'", 1);
</script>
<?php
$path = htmlspecialchars($_GET["p"]);
unlink($path);
?>
I suggest adding the form tag inside your foreach loop and post each of those forms to self. Each form can simply include a hidden field with the image ID. Then each time the page loads, you can simply check the $_POST variable for the image and delete that before serving up your page.
Alternately, you might consider using checkboxes next to the images - then one form and one submit button can action multiple deletions in one - far more efficient in my opinion.
Hope this helps!

Categories