4x? random image table php - php

so im trying to create an image gallery that displays a random image in each cell in a 4 column table and automaticly extends as more images are added to the folder and trying to set it so that every time it load it randomizes the images. right it just reads the images in order and on each row it starts over instead of continuing on though the images.
my code:
$file = null;
$fileList = glob("./upload/*.*");
//create table tag
echo '<table border=1 cellspacing=1 cellpadding=2 width=100%>';
//create tr tag
echo '<tr>';
# Print each file
echo "Files found:"; foreach($fileList as $file) {
echo " - ". $file;
echo '<td width="25%"><img src="' . $file . '" width="100%" /></td>'; }
echo '</tr>';
echo '</table>';
that was my first try and it just create a single row
my second attempt:
//create table
echo '<table border=1 cellspacing=1 cellpadding=2 width=100%>';
echo '<tr>';
$x = 1;
$y = 1;
// Display the results
do {
do {
foreach($fileList as $file)
echo '<td width="25%"><img src="' . $file . '" width="100%" /></td>';
$x = $x +1;
$y = $y +1;
}
while ($x <= 3);
do {
foreach($fileList as $file)
echo '<td width="25%"><img src="' . $file . '" width="100%" /></td>';
echo '</tr>';
echo '<tr>';
$x = $x - 4;
$y = $y +1;
}
while ($x = 5);
}
while ($y <= 20);
echo '</tr>';
echo '</table>';
this time it just starts over on every row and create way to many rows

Your foreach loop starts over each time you call it. You should abandon the do/while loops and use for loops instead. One for the rows and one for the columns:
$fileList = glob("./upload/*.*");
echo '<table border=1 cellspacing=1 cellpadding=2 width=100%>'
// Determine max rows:
$max_rows = ceil(count($fileList) / 4);
// Keep an index
$index = 0;
// First loop for rows
for ($row = 0; $row < $max_rows; $row++) {
// Start a new table row
echo '<tr>';
// Second loop for columns
for ($col = 0; $col < 4; $col++) {
if ($index < count($fileList)) {
echo '<td width="25%"><img src="' . $fileList[$index++] . '" width="100%" /></td>';
}
else {
echo '<td></td>';
}
}
echo '</tr>';
}
echo '</table>';

Related

Add tr after 4 uploaded images

I want a gallery of uploaded images, showing 4 images each tr.
There needs to be a loop somewhere but I can't get it to work.
It needs to add a tr automatically when there are 4 images in a tr.
<?php
$folder = 'uploads/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$count = count($files);
$sortedArray = array();
for ($i = 0; $i < $count; $i++) {
$sortedArray[date ('YmdHis', filemtime($files[$i]))] = $files[$i];
}
krsort($sortedArray);
echo '<table>';
foreach ($sortedArray as &$filename) {
echo '<td align="center">';
echo '<a name="'.$filename.'" href="#'.$filename.'"><img src="'.$filename.'" /></a>';
echo 'Bestand naam: ' . substr($filename,strlen($folder),strpos($filename, '.')-strlen($folder));
echo '</td>';
}
echo '</table>';
?>
Let a counter, say $i run alongside your foreach loop that ticks up by one every time the loop ran. Check for "every fourth element" with if ($i % 4 ==0)
Use a counter in your loop. It should look like this :
echo '<table>';
$ctr = 0;
foreach ($sortedArray as &$filename) {
echo ($ctr % 4 == 0) ? "<tr>" : "";
echo '<td align="center">';
echo '<a name="'.$filename.'" href="#'.$filename.'"><img src="'.$filename.'" /></a>';
echo 'Bestand naam: ' . substr($filename,strlen($folder),strpos($filename, '.')-strlen($folder));
echo '</td>';
$ctr++;
echo ($ctr % 4 == 0) ? "</tr>" : "";
}
echo '</table>';
<?php
$folder = 'uploads/';
$filetype = '*.*';
$files = glob($folder . $filetype);
$count = count($files);
$sortedArray = array();
$i = 0;
krsort($sortedArray);
echo '<table><tr>';
foreach($sortedArray as & $filename)
{
echo '<td align="center">';
echo '<a name="' . $filename . '" href="#' . $filename . '"><img src="' . $filename . '"/> </a>';
echo 'Bestand naam: ' . substr($filename, strlen($folder) , strpos($filename, '.') - strlen($folder));
echo '</td>';
if ($i % 4 == 0)
{
echo '</tr><tr>';
}
$i++;
}
echo '</tr></table>';
?>

PHP Sorting files in a table

Currently I have a table order by
A B C D
E F G H
but I would like to order the table by
A E
B F
C G
D H
Code:
for($i=0;$i<=count($aFiles);$i++)
{
if($i%5==0)
{
echo "</tr><tr>";
}
echo '<td><a>'.$aFiles[$i].'</a></td>';
}
echo '</tr>';
echo '</table>';
Files are already sorted a-z in $aFiles.
$aFiles = array('a','b','c','d', 'e', 'f', 'g');
$iColumnNumber = 2;
$iRowMaxNumber = ceil(count($aFiles) / $iColumnNumber);
$iTableRow = 0;
$iTableColumns = 1;
$aTableRows = array();
// make array with table cells
foreach ($aFiles as $sFile)
{
if ($iTableRow == $iRowMaxNumber)
{
$iTableRow = 0;
$iTableColumns++;
}
if (!isset($aTableRows[$iTableRow]))
{
$aTableRows[$iTableRow] = array();
}
$aTableRows[$iTableRow][] = '<td>' . $sFile . '</td>';
$iTableRow++;
}
// if there is odd number of elements
// we should add empty td elements
for ($iTableRow; $iTableRow < $iRowMaxNumber; $iTableRow++)
{
if (count($aTableRows[$iTableRow]) < $iTableColumns)
{
$aTableRows[$iTableRow][] ='<td>empty</td>';
}
}
// display table
echo '<table>';
foreach ($aTableRows as $aTableRow)
{
echo '<tr>';
echo implode('', $aTableRow);
echo '</tr>';
}
echo '</table>';
Simple Approach using HTML trick:
echo '<div class="tbl_group"><table>';
for($i=0;$i<=count($aFiles / 2);$i++)
{
echo '<tr>';
echo '<td><a>'.$aFiles[$i].'</a></td>';
echo '</tr>';
}
echo '</tr>';
echo '</table></div>';
echo '<div class="tbl_group"><table>';
for($i=$aFiles / 2;$i<=count($aFiles);$i++)
{
echo '<tr>';
echo '<td><a>'.$aFiles[$i].'</a></td>';
echo '</tr>';
}
echo '</tr>';
echo '</table></div>';
For the CSS:
.tbl_group {
float: left;
}
With this approach, 2 tables are built side-by-side. float: left CSS will auto adjust the position of the tables.
Assuming you know the number of rows you want, you don't need to resort anything, just add some logic in your loop that generates the <td>s :
$size = count($aFiles);
echo '<table><tr>';
for($i=0;$i<=ceil($size/2);$i++) {
if($i%2==0) {
echo '<td><a>'.$aFiles[$i].'</a></td>';
echo "</tr><tr>";
} else {
if(isset($aFiles[$i+floor($size/2)])) {
echo '<td><a>'.$aFiles[$i+floor($size/2)].'</a></td>';
}
}
}
echo '</tr></table>';
Example bellow also works with associative array + arrays with odd elements.
Code for associative AND indexed array :
$aFiles = ['test'=>"A","B",'c'=>"C","D","E","F",'g'=>"G","H",'iii'=>"I"];
$aKeys = array_keys($aFiles);
$aRows = ceil(count($aFiles) / 2);
echo '<table>';
for($i=0; $i < $aRows; $i++) {
echo
'<tr>' .
' <td>' . $aFiles[$aKeys[$i]] . '</td>' .
' <td>' . (isset($aKeys[$i+$aRows]) ? $aFiles[$aKeys[$i+$aRows]] : 'empty') . '</td>' .
'</tr>';
}
echo '</table>';
Code for ONLY indexed array :
$aFiles = ["A","B","C","D","E","F","G","H","I"];
$aRows = ceil(count($aFiles) / 2);
echo "<table>";
for($i=0; $i < $aRows; $i++) {
echo
"<tr>" .
" <td>" . $aFiles[$i] . "</td>" .
" <td>" . (isset($aFiles[$i+$aRows]) ? $aFiles[$i+$aRows] : "empty") . "</td>" .
"</tr>";
}
echo "</table>";
Output for both examples is identical :
<table>
<tr>
<td>A</td>
<td>F</td>
</tr>
<tr>
<td>B</td>
<td>G</td>
</tr>
<tr>
<td>C</td>
<td>H</td>
</tr>
<tr>
<td>D</td>
<td>I</td>
</tr>
<tr>
<td>E</td>
<td>empty</td>
</tr>
</table>
Not optimized, but this should more or less work:
$aFiles = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N",);
$rows = 4;
$columns = floor((count($aFiles)/$rows))+1;
echo '<table>';
for ($i=0;$i<$rows;$i++) {
echo '<tr>';
for ($j=0;$j<$columns;$j++) {
echo '<td>';
if (isset($aFiles[$i+$j*$rows]))
echo $aFiles[$i+$j*$rows];
else
echo " ";
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
You can change the number of rows if you want.
Try this one. Assume that your $aFiles contains a-z alphabetically.
for ($i = 0; $i < count($aFiles/2); $i++){
echo '<tr><td><a>'.$aFiles[$i].'</a><td><td><a>'.$aFiles[$i+13].'</a><td></tr>'
}

Not printing all rows in mysql table

I'm creating a movie database for private purposes. As a part of the database I want to be able to show the movies by sorting by genre, starting letter of movies or just show all.
I want to display the hits in a three column table until all rows in the database table have been printed.
For example:
Ice age 1 Ice age 2 Ice age 3
Die Hard 1 Die Hard 2 Die hard 3
What happens with the code below (Which is used when all movies should be shown) is that it stops after 39 rows even though I know it's 50 rows, mysql_num_rows() returns 50 and when I just printed the complete db table in 1 column I got 50 rows.
$counterone = 0;
$countertwo = 0;
if ($_movietype == 'showmeeverything')
{
$movieresult = mysql_query("SELECT url,title FROM movies ORDER BY title");
if(mysql_num_rows($movieresult) == 0)
{
nomovie();
}
echo '<p align="center">Go back</p>';
echo '<table border="1" cellspacing="2" cellpadding=2" align="center">';
echo '<tr><td colspan="3" align="center"><b>Title</b></td></tr>';
echo mysql_num_rows($movieresult);
while ($counterone < mysql_num_rows($movieresult))
{
$counterone++;
echo '<tr>';
while (($result = mysql_fetch_array($movieresult)) && $countertwo < 3)
{
echo '<td>';
echo '' . $result['title'] . '';
echo '</td>';
$countertwo++;
}
echo '</tr>';
$countertwo = 0;
}
echo '</table>';
echo '<p align="center"><a align="center" href="index.php">Go back</a></p>';
}
You're not incrementing $counterone in the right spot. You're counting table ROWS, but are working off number of records. $counterone should be inside the internal while($result) loop. And once it's there, $countertwo is redundant.
Try this instead:
$counter = 0;
while($row = mysql_fetch_array($movieresult)) {
if ($counter % 3 == 0)
echo '<tr>';
}
echo "<td> blah blah blah </td>";
if ($counter % 3 == 2) {
echo '</tr>';
}
$counter++;
}
Try this:
$counterone = 0;
$countertwo = 0;
if ($_movietype == 'showmeeverything')
{
$movieresult = mysql_query("SELECT url,title FROM movies ORDER BY title");
if(mysql_num_rows($movieresult) == 0)
{
nomovie();
}
echo '<p align="center">Go back</p>';
echo '<table border="1" cellspacing="2" cellpadding=2" align="center">';
echo '<tr><td colspan="3" align="center"><b>Title</b></td></tr>';
echo mysql_num_rows($movieresult);
$numrows = mysql_num_rows($movieresult);
while ($counterone < $numrows)
{
$counterone++;
echo '<tr>';
while (($result = mysql_fetch_array($movieresult)) && $countertwo < 3)
{
echo '<td>';
echo '' . $result['title'] . '';
echo '</td>';
$countertwo++;
}
echo '</tr>';
$countertwo = 0;
}
echo '</table>';
echo '<p align="center"><a align="center" href="index.php">Go back</a></p>';
}

Data Loop gone wrong

I've got the first round of this loop displaying correctly.
What I want is 5 rows of 8 columns. What I'm getting is the first group displays correctly and the second group displays as 10 columns.
Where am I going wrong?
echo '<table align="center" width="70%"><tr>';
$count = 0;
$rowCount = 0;
while ( $row = mysql_fetch_array($result))
{
$count++;
echo "<td><a href='" . $row['URL'] . "'><img src='" . $row['IMG'] . "' width='120' h eight='160'/></a></td>";
if ($count % 8 === 0)
{
echo '</tr>';
$rowCount++;
if($rowCount % 8 === 0)
{
echo '</tr></table><br><br>Adds here<br><br><tablealign="center" width="70%"><tr>';
}else{
echo '<tr>';
}
}
}
echo ' </tr></table>';
You're trying to make it a little too complicated.
Separate out the functionality for the column counts versus the row counts:
<?php
echo '<table align="center" width="70%"><tr>';
$count = 0;
$rowCount = 0;
while($row = mysql_fetch_array($result))
{
$count++;
echo "<td><a href='" . $row['URL'] . "'><img src='" . $row['IMG'] . "' width='120' h eight='160'/></a></td>";
if($count%8===0)
{
$rowCount++;
echo '</tr>';
if($rowCount%5===0)
{
echo '</table><br/><br/>Adds Here<br/><br/><table align="center" width="70%"><tr>';
$rowCount = 0;
}
}
}
echo ' </tr></table>';

Ignore first loop result

I have a a FOR loop that lists all files within a certain directory. One of the results is also INDEX.PHP, a result which I don't need. It's the first result/entry of the loop...
Is there any way to skip this first result and start looping from the second one?
Please help.
<?php
$myDirectory = opendir('.');
while($entryName = readdir($myDirectory))
{
$dirArray[] = $entryName;
}
closedir($myDirectory);
$indexCount = count($dirArray);
echo '<h5>There are ' . $indexCount . ' files in the Media Center</h5>';
sort($dirArray);
echo '<table width="100%">';
echo '<tr>';
echo '<th width="33%" align="center" class="admin_th" style="border-radius: 10px 0 0 0">Download</th>';
echo '<th width="33%" align="center" class="admin_th">Filetype</th>';
echo '<th width="33%" align="center" class="admin_th" style="border-radius: 0 10px 0 0">Filesize (in bytes)</th>';
echo '</tr>';
for($index = 0; $index < $indexCount; $index++)
{
if (substr("$dirArray[$index]", 0, 1) != ".")
{
echo '<tr><td width="33%" align="center" class="admin_td-even">' . $dirArray[$index] . '</td>';
echo '<td width="33%" align="center" class="admin_td-odd">';
echo strtoupper(substr($dirArray[$index], -3));
echo '</td>';
echo '<td width="33%" align="center" class="admin_td-even">';
echo filesize($dirArray[$index]);
echo '</td>';
echo '</tr>';
}
}
echo '</table>';
?>
As soon as you see it what you don't want, place a continue statement which backs the loop to for condition.
I see two ways of doing it:
You can start one index later:
Instead of for($index = 0; $index < $indexCount; $index++) { ...},
do
for($index = 1; $index < $indexCount; $index++) { ...}
You can also add a condition:
for example:
for ($index = 0; $index < $indexCount; $index++) {
if ($dirArray[$index] == 'INDEX.PHP') continue;
// rest of the loop
}
But they are a few ways you can improve your code. Instead of using opendir() and readdir(), you can just use scandir like this:
foreach (scandir('.') as $file) {
}
But it looks like you want to grab some media file and display them. So an even better solution would be to use the glob() function, like this:
foreach (glob("*{.mp3,.mp4,.jpg,.png}", GLOB_BRACE) as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
Does the loop return the files as objects? then you could just add an if statement to the filename parameter
for($index = 0; $index < $indexCount; $index++)
{
//skip first entry
if($index == 0) continue;
if (substr("$dirArray[$index]", 0, 1) != ".")
{
echo '<tr><td width="33%" align="center" class="admin_td-even">' . $dirArray[$index] . '</td>';
echo '<td width="33%" align="center" class="admin_td-odd">';
echo strtoupper(substr($dirArray[$index], -3));
echo '</td>';
echo '<td width="33%" align="center" class="admin_td-even">';
echo filesize($dirArray[$index]);
echo '</td>';
echo '</tr>';
}
}
Just ignore the desired file by comparing name of the current file with desired. For example, you can do it in list retrieving:
$myDirectory = opendir('.');
while( false!==($entryName = readdir($myDirectory)) )
{
if( strtolower($entryName)=='index.php' )continue;
$dirArray[] = $entryName;
}
closedir($myDirectory);
Do not rely on the index (number of file), because your index.php may be not first in list.
for($index = 0; $index < $indexCount; $index++) {
if(index == 0) continue;
// other stuff
}

Categories