Is there a way to iterate thru each image in a directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Is there a way to iterate thru each image in a directory

Post by akimm »

I have been trying to solve this problem multiple ways, and this is just a thought I had just now, can I print each smile graphic in my directory /smile/ onto a php form?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Why not?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

I didn't know

Post by akimm »

Whether it was or not. This is a previously asked question, just a different approach to my problem.

I have 92 .gif smiles. I'd like to print all of them and then be able to select the one i'd like to use on any one journal entry. I tried doing a foreach array, which hadn't worked. I know you're rather experienced any way I can go about this problem? Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

psst.. post what you tried.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

ok

Post by akimm »

One sec
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Here they are, 2 thta I found within the forum

Post by akimm »

first attempt

Code: Select all

<?php 
$smiles = array ( 
option value="smile1" => ['<img src='smile1.gif'], 
//et cetera? 
);?>
Second attempt

Code: Select all

##Piece of code from form

<select name="smile">
<?php 
$smile = array('2guns', 'somethingElse'); 

/** 
 * This generates a drop down with 
 * the array index as the key and 
 * the smilie name as the value in the form 
 **/ 
foreach ($smile as $key => $value) 
{ 
    echo '<option value="' . $key . '">' . $value . '</option>'; 
} 
?> 
</select> 



## Code that writes to the file

<?php 
$smile = array('2guns', 'somethingElse'); 

$img_id = $_POST['smile']; 
echo '<img src="/journal06/smile/' . $smile[$img_id] . '.gif" />'; 

$filename = "journalaug_sept.txt"; 
$handle = fopen($filename, 'a'); 
$write_string = $_POST['date'] . "<font family=arial>" . "<BR>" . "<BR>"; 
$write_string .= $_POST['mood'] . $_POST['smile'] . $smile[$img_id] . "<font family=arial>" . "<BR>" . "<BR>"; 
$write_string .= $_POST['entry'] . "<font family=arial>" . "<BR>" . "<BR>"; 
$write_string .= "<HR NOSHADE SIZE=4 COLOR=BLUE>"; 

fwrite($handle, $write_string); 
        fclose($handle); 
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

read up on glob() and it's cousins.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Feyd or someone

Post by akimm »

Code: Select all

<?php
$files = glob("http://www.akimm.com/journal06/smile/*.gif"); 
##Display $files
<select name="smile">
foreach ($files as $key => $value) 
{ 
    echo '<option value="' . $key . '">' . $value . '</option>'; 
} 
?> 
</select>

Is this the basic idea I'm searching for?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Roughly, although you can lose the URL. It needs a system level path.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

so just

Post by akimm »

/journal06/smile/
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: so just

Post by Chris Corbyn »

akimm wrote:/journal06/smile/
Not likely, unless you have the webroot as the server root which would be plain stupid :)

How about "./journal06/smile/" ? ;)
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

It worked on the form..

Post by akimm »

it displays textual forms of all the contents.. but i doesn't translate them into the textfile and then into the visible journal.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

ok

Post by akimm »

As of now glob() prints all the names, but when entered into the textfile it sets the value as a 1 instead of the name of the smile. Any suggestions?
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

thus far I have this.

Post by akimm »

here is the form thus far

Code: Select all

<form action="journalbox.php" method="POST"><p><b>Date</b><input type="text" value="<b><?php echo date('F j, Y, g:i a');?

></b>" name="date" size="30"></p><p><b>Mood</b>

<select name="mood"><option value="apathetic">apathetic<option value="overjoyed">overjoyed<option 

value="inquisitive">inquisitive<option value="outlandish">outlandish<option value="appreciative">appreciative<option 

value="confused">confused<option value="displeased">displeased<option value="disgruntled">disgruntled<option 

value="sick">sick<option value="terrified">terrified<option value="mental">mental<option value="eruditic">eruditic<option 

value="forlorn">forlorn<option value="convuluted">convuluted<option value="ingenious">ingenious<option 

value="arrogant">arrogant<option value="free">free<option value="stupid">stupid<option value="moronic">moronic<option 

value="pointless">pointless</select><br /><br />
<select name="smile">
<?php 
$files = glob("smile/*.gif"); 
##Display $files  
foreach ($files as $key => $value) 
{ 
    echo '<option value="' . $key . '">' . $value . '</option>'; 
} 
?>
</select> 
<p><b>entry</b><TEXTAREA NAME="entry" COLS="80" ROWS="5" WRAP="virtual"></TEXTAREA></P><p><input type="submit" name="submit" 

value="send your entry!"></p></form>

Here is the file that processes the form.

Code: Select all

<?php
$filename = "journalaug_sept.txt";
$handle = fopen($filename, 'a');
$write_string = $_POST['date'] . "<font family=arial>" . "<BR>" . "<BR>";
$write_string .= $_POST['mood'] . "<font family=arial>" . "<BR>" . "<BR>";
$write_string .= "<img src=$_POST['smile']>"  . "<br>";
$write_string .= $_POST['entry'] . "<font family=arial>" . "<BR>" . "<BR>";
$write_string .= "<HR NOSHADE SIZE=4 COLOR=BLUE>";

fwrite($handle, $write_string);
	fclose($handle);
?>
<h1>Sucessfully posted</h1> 

<br><br>
<?php
echo $_POST['date'] . "<br>";
echo $_POST['mood'] . "<br>";
echo $_POST['smile'] . $_POST['value'] . "<br>";
echo $_POST['entry'] . "<br>";
?>

<br><br>
<a href="http://www.akimm.com/journal06/journal.php">Go To Journal Sean!</a>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

You are setting the value of the select box to the array key. Try setting it to the file name :)

Code: Select all

foreach ($files as $key => $value)
{
    echo '<option value="' . $key . '">' . $value . '</option>';
}
To:

Code: Select all

foreach ($files as $key => $value)
{
    echo '<option value="' . $value. '">' . $value . '</option>';
}
Post Reply