Jump to content

Recommended Posts

Here is the situation.  I am trying to use a php multi column to display some images that are uploaded to a directory without using a mysql backend.  I have figured out how to do it with mysql:

 

<?php
//set the number of columns
$columns = x;

mysql_connect('localhost','','');
mysql_select_db('test');
$query = "SELECT * FROM table ORDER BY id";
$result = mysql_query($query);

//we add this line because we need to know the number of rows
$num_rows = mysql_num_rows($result);
echo "<TABLE BORDER=\"0\">\n";

//changed this to a for loop so we can use the number of rows
for($i = 0; $i < $num_rows; $i++) {
    $row = mysql_fetch_array($result);
    if($i % $columns == 0) {
        //if there is no remainder, we want to start a new row
        echo "<TR>\n";
    }
    echo "<TD>" . $row['stuff'] . "</TD>\n";
    if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) {
        //if there is a remainder of 1, end the row
        //or if there is nothing left in our result set, end the row
        echo "</TR>\n";
    }
}
echo "</TABLE>\n";
?> 

 

But instead of doing it that way, I am trying to figure out how to get php to count the images in the directory and display them in columns.  Is there a way to do this and if so can somebody please help.  I can get them out of the directory and display on the page already using this:

 

<?php

function strip_ext($name) {
$ext = strrchr($name, '.');
if($ext !== false) {
	$name = substr($name, 0, -strlen($ext));
}
return $name;
}

function removeHyphen($filename) {
$target = $filename;
$patterns[0] = '/-/';
$patterns[1] = '/_/';
$replacements[0] = ' ';
$replacements[1] = ' ';
$filename = preg_replace($patterns, $replacements, $target);
return $filename;
}

function capFirstWord($word) {
$cap = $word;
$cap = explode(' ', $cap);
	foreach($cap as $key => $value) {
		$cap[$key] = ucfirst($cap[$key]); 
	}
$word = implode(' ', $cap);
return $word;
}

function formatFile($name) {
    $name = strip_ext($name);
    $name = removeHyphen($name);
    $name = capFirstWord($name);
    return $name;
}

$mydir = dir('./displayimages');
while(($file = $mydir->read()) !== false) {
	// Security - remove "." and ".." files (directories)
	if ($file != "." && $file != "..") {
		// Output.  This is what is actually outputed by the script and that shows
		// up on the screen (browser).
		echo "<a href='./displayimages/$file'><img border='0' src='./displayimages/$file' width=\"77\" height=\"102\"></a>".formatFile($file)."</a><br>";
	 }
}
$mydir->close();
?>

Link to comment
https://forums.phpfreaks.com/topic/89145-solved-question/
Share on other sites

are you trying to have table rows and columns for your images? is that what you are looking to do?

<? //the rest of your script
?> // end php code to create a table
<table width="100%" border="1">
  <tr>
<? //restart php code
echo "<TD><a href='./displayimages/$file'><img border='0' src='./displayimages/$file' width=\"77\" height=\"102\"></a>".formatFile($file)."</a></TD>
//the rest of your code
?>	

 

if that is what ou are trying to do you need to create a table first

then echo your td columns.

you will problably have to play with it to get it to work for your needs, but that is the basic way to do that.

Link to comment
https://forums.phpfreaks.com/topic/89145-solved-question/#findComment-456549
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.