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>';
?>
Related
if (isset($_GET['q'])){
$q = $_GET['q'];
//variables
$sql = "SELECT * FROM products WHERE product LIKE '%$q%' OR search LIKE '%$q%'";
$result = $conn->query($sql);
//check database
if($result->num_rows > 0){
while ($row = $result->fetch_array()){
$product = $row['product'];
$productImage = $row['product_image'];
$price = $row['price'];
$seller = $row['seller_name'];
$sellerImage = $row['seller_image'];
$desc = $row['description'];
$search = $row['search'];
$console = $row['console'];
$array = array($product, $price, $productImage);
$arrayDesc = array($desc, $sellerImage);
if (preg_match('/Game/', $seller)){
for ($num = 0; $num < 3; $num++) {
echo '<div class="tile col-md-4 col-sm-3">';
if($num == 0){
echo '<div class="tileTitleBox"><h4>' . $array[$num] . '</h4></div>';
}
if($num = 1){
echo '<p class="price">' . $array[$num] . '</p>';
}
if($num = 2){
echo '<img class="tilePic" src="' . $array[$num] . '"/>';
}
// if($num = 3){
// echo '<div class = "desc"><p>' . $array[$num] . '</p></div>';
// }
echo '</div>';
}//For Iteration Loop - TILE
for($count = 0; $count < 2; $count++){
echo '<div class="tile-description col-md-4 col-sm-3 hidden">';
if($count == 0){
echo '<div class="desc"><p>' . $arrayDesc[$count] . '</p></div>';
}
if($count == 1){
echo '<img class="sellerImg" src="' . $arrayDesc[$count] . '"/>';
}
echo '</div>';
}//end for loop - TILE-DESCRIPTION
So as you see above, I have one for loop which creates the "tile" and the second which is supposed to create the "tile-description". The first one works well to create a single div with the class of "tile" in which my remaining contents is loaded into the div. But in the second for loop the "desc" dev and the "sellerImg" div are separated. The loop needs to go through the array various times.
Shown Bellow:
You can see that the "Tile" div contains "tileTitleBox", "price", and "tilePic". But the "tile-description" which is hidden, is separated into two divs. As opposed to holding both elements inside the same div.
#sam-dufel rightly pointed out you're overwriting the iterator
if($num = 1){
But in fact you shouldn't use any loops here:
echo '<div class="tile col-md-4 col-sm-3">';
echo '<div class="tileTitleBox"><h4>' . $array[0] . '</h4></div>';
echo '<p class="price">' . $array[1] . '</p>';
echo '<img class="tilePic" src="' . $array[2] . '"/>';
echo '</div>';
echo '<div class="tile-description col-md-4 col-sm-3 hidden">';
echo '<div class="desc"><p>' . $arrayDesc[0] . '</p></div>';
echo '<img class="sellerImg" src="' . $arrayDesc[1] . '"/>';
echo '</div>';
I have a page on my site that shows a member directory. I want the members to be listed 3 per row, before it goes to the next row. The code I have, does this for the very first row - but on the second row, its all on one line and doesnt carry down.
The profiles are showing up like this:
uuu
uuuuuuuuuuuuuuuuuuuuuuuuuuu
When they should be doing this:
uuu
uuu
uuu
uuu
This is what my code looks like:
<table>
<tr>
<td colspan="4"><h1>Member Directory</h1></td></tr>
<tr>
<td>
<?php
while($row = mysql_fetch_array($result)) {
if (empty($row['profile']) === false){
echo '<img src="', $row['profile'], ' "width="125">';
} else {
echo '<img src="../../images/template/avatar.png">';
}
echo '</td><td>';
echo '' . ucfirst($row['username']) . '<br />';
echo "Location: " . $row['location'] . "<br />";
echo '</td><td>';
if ($i++ == 2) echo '</td></tr><tr><td>';
}
?>
</td>
</tr>
</table>
Any help would be greatly appreciated, thanks!
Use
if (++$i % 3 == 0) echo '</td></tr><tr><td>';
Explanation:
First of all ++$i first increments $i, and then uses it in whatever is next, this makes for more readable code.
Second, the % is the modulus, which means it sortof subtracts 3 from $i until it is not possible anymore. E.g. 9 % 3 == 0, and 11 % 3 == 2 and so on. This means we know that we have printed 3 rows whenever $i % 3 equals 0.
try this one
<table>
<tr>
<td colspan="4"><h1>Member Directory</h1></td>
</tr>
<?php
$count=0;
while($row = mysql_fetch_array($result))
{
$count+=1;
if($count%3==1)
{
echo '<tr>';
}
echo '<td>';
if (empty($row['profile']) === false){
echo '<img src="', $row['profile'], ' "width="125">';
} else {
echo '<img src="../../images/template/avatar.png">';
}
echo '</td>';
echo '<td>';
echo '' . ucfirst($row['username']) . '<br />';
echo "Location: " . $row['location'] . "<br />";
echo '</td>';
if($count%3==0)
{
echo '</tr>';
}
}
if($count%3!=0)
{
echo '</tr>';
}
?>
</table>
You didn't reset the value of $i, so it kept increasing; another issue is that if you have only seven items, the last row should have four empty cells. So the loop condition needs to be augmented with a row completion status:
$i = 0;
while (($row = mysql_fetch_array($result)) !== false || $i != 0) {
if ($i == 0) {
echo '<tr>'; // start of new row
}
if ($row !== false) {
echo '<td>';
if (empty($row['profile'])) {
echo '<img src="', $row['profile'], ' "width="125">';
} else {
echo '<img src="../../images/template/avatar.png">';
}
echo '</td><td>';
echo '' . ucfirst($row['username']) . '<br />';
echo "Location: " . $row['location'];
echo '</td>';
} else {
echo '<td></td><td></td>'; // no more data
}
$i = ($i + 1) % 3; // advance
if ($i == 0) {
echo '</tr>'; // end of the row
}
}
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>';
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>';
I have a pretty complicated script that doesn't fully work with MySQL, it does partially though. Let me try to explain...
The results of my page are purely image names from a specific folder, means I use this function to get my results:
function get_all_images($dir)
{
$dir = opendir($dir);
$dirArray = array();
while($entryName = readdir($dir))
{
if(($entryName != ".") && ($entryName != "..") && ($entryName != ".svn") && ($entryName != ".htaccess"))
{
$dirArray[] = $entryName;
}
}
closedir($dir);
(sizeof($dirArray)) ? arsort($dirArray) : '';
return (is_array($dirArray)) ? $dirArray : '';
}
This is how I basically get results in my page:
<?php
include('includes/header.php');
$images = get_all_images('i');
$url = str_replace('www.', '', generate_site_url());
$flag = false;
$count = 0;
if (empty($images))
{
echo '<h2>There are no uploaded images</h2><br>';
}
foreach ($images as $image)
{
$filename = $image_name = $image;
$image_link = $url . IMAGES_PATH . $filename;
$user_id = fetch_user_id($image_link);
$delete_link = (isset($_POST['delete_link'])) ? $_POST['delete_link'] : '';
$delete_image = (isset($_POST['delete_image'])) ? $_POST['delete_image'] : '';
if ($delete_admin_submit)
{
unlink('./t/' . $delete_image);
unlink('./t/big' . $delete_image);
adminDelete('./i/' . $delete_image, $delete_link);
header('Location: ' . $imgit_action);
exit();
}
echo '<div class="' . ($count++ % 2 ? "odd-color" : "even-color") . '">';
echo '<table>';
echo '<tr><td class="fullwidth"><a class="preview_img" href="' . $image_link . '"><img src="' . $image_link . '" title="Click to enlarge" width="300" class="thumb" /></a></td></tr>';
echo '<tr><td><span class="default">Direct link:</span> ';
echo '<input type="text" readonly="readonly" class="link-area" onmouseover="this.select();" value="' . $image_link . '" />';
echo '<form method="post" action="" onsubmit="return confirmSingleDeletion();" style="display: inline;"> ';
echo '<input type="submit" class="icon_delete" name="delete_link" value="' . $image_link . '" title="Delete this image" />';
echo '<input type="hidden" name="delete_image" value="' . $image_name . '" />';
echo '</form>';
echo '</td></tr>';
echo ($flag) ? '<hr /><br>' : '';
echo '</table>';
if (!empty($user_id))
{
echo '<br><strong class="normal">Uploader ID:</strong> ';
echo '<em class="normal">' . $user_id . '</em><br>';
}
echo '<br>';
$flag = true;
}
?>
<span class="button-sub">« Back to Index</span>
<?php echo '</div>'; ?>
<?php include('includes/footer_alt.php'); ?>
Now I have not ANY simple clue how to start breaking my results into pages. I'm working here with over 12000 results and it takes a lot for the page to load, I need help to break this big result into pages.
Anyone willing to help me? At least give me a clue how to start? I would be really grateful.
Thanks a lot for reading.
Consider your array as you collect up the file names but after you have sorted them:
$imgs = array(
0 => 'image1.jpg',
1 => 'image2.jpg',
2 => 'image3.jpg',
3 => 'image4.jpg',
4 => 'image5.jpg',
5 => 'image6.jpg',
6 => 'image7.jpg',
7 => 'image8.jpg',
);
// create some vars which you can use in your pagination
$perpage = 3 ;
$page=2;
$range_end = ($perpage*$page)-1;
$range_start = ($perpage*($page-1));
$display=range($range_start,$range_end);
// loop through results
foreach($display as $show){
echo $imgs[$show];
}
Does that get you a start?
Thanks for trying to answer me Cups and Umair Khan, but I found the working solution here:
http://tiffanybbrown.com/2008/12/14/simple-pagination-for-arrays-with-php-5/