Page 1 of 2

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

Posted: Thu Sep 07, 2006 10:15 am
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?

Posted: Thu Sep 07, 2006 2:05 pm
by feyd
Why not?

I didn't know

Posted: Thu Sep 07, 2006 2:13 pm
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.

Posted: Thu Sep 07, 2006 2:15 pm
by feyd
psst.. post what you tried.

ok

Posted: Thu Sep 07, 2006 2:17 pm
by akimm
One sec

Here they are, 2 thta I found within the forum

Posted: Thu Sep 07, 2006 2:23 pm
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); 
?>

Posted: Thu Sep 07, 2006 2:26 pm
by feyd
read up on glob() and it's cousins.

Feyd or someone

Posted: Thu Sep 07, 2006 2:40 pm
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?

Posted: Thu Sep 07, 2006 3:02 pm
by feyd
Roughly, although you can lose the URL. It needs a system level path.

so just

Posted: Thu Sep 07, 2006 4:40 pm
by akimm
/journal06/smile/

Re: so just

Posted: Thu Sep 07, 2006 4:53 pm
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/" ? ;)

It worked on the form..

Posted: Thu Sep 07, 2006 5:07 pm
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.

ok

Posted: Thu Sep 07, 2006 5:20 pm
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?

thus far I have this.

Posted: Thu Sep 07, 2006 5:27 pm
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>

Posted: Thu Sep 07, 2006 6:13 pm
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>';
}