0

i have some basic php code that pulls an image from a particular image folder when the user asks using a form. I will have many image folders and want to generate a random image instead of using

case 'A' : echo "<a href=\"Alphabet-Letters/Letters-A\">
<img src=\"image/data/A/A_001.jpg\" id=\"A1\" width=\"70\" height=\"120\" title=\"A1\"/>           </a>" ; break;

My question is this as the form is processed with someone using the letter A the picture of that letter appears. The php code for this is

if (array_key_exists('check_submit', $_POST)) 
{


$letters = $_POST['Comments'];

$num_letters = strlen($letters);
for($i = 0; $i < $num_letters; $i++)
{
switch ($letters[$i])   {
case 'A' : echo "<a href=\"Alphabet-Letters/Letters-A\">
<img src=\"image/data/A/A_001.jpg\" id=\"A1\" width=\"70\" height=\"120\" title=\"A1\"      alt=\"Image A\"/>

</a>" ; 
break;

This only pulls the exact image i have asked, but i have hundreds in that folder and would like a more simple code to work with. Please can someone help, they gave advise on using random image from folder, but that only works as a starting point not on the code I already have. Thanks for your time

Aide2001
  • 3
  • 5

4 Answers4

0

The solution posted at the link below should achieve the functionality you are looking for. https://stackoverflow.com/a/4478788/1152375

so you should be able to do something like

case 'A' : echo "<a href=\"Alphabet-Letters/Letters-A\">
<img src=\"image/data/A/" . random_pic("folder_with_pics") . "\" id=\"A1\" width=\"70\" height=\"120\" title=\"A1\</a>"; 
break;
Community
  • 1
  • 1
gh123man
  • 1,372
  • 1
  • 14
  • 24
0

before you get to the output code, you will want to get a list of all files in the relevent directory ( http://php.net/manual/en/function.dir.php ) in a numbered array.

count the number of items in the array ( http://php.net/manual/en/function.count.php ) and randomly pick one ( http://php.net/manual/en/function.rand.php ) using the min and max settings of rand.

Cwissy
  • 2,006
  • 15
  • 14
0

Try using the scandir function to find all the files in the folder, and then use the rand function to randomly choose one:

if(!empty($_POST['check_submit'])) 
{

  $letters = strtoupper(trim($_POST['Comments']));
  $num_letters = strlen($letters);
  for($i = 0; $i < $num_letters; $i++)
  {
    $letter = $letters[$i];
    $folder = 'image/data/'.$letter;
    $files = scandir($folder);
    array_shift($files);
    array_shift($files);
    $index = rand(0, count($files) - 1);
    $file = $files[$index];

    echo "<a href=\"Alphabet-Letters/Letters-{$letter}\">\n";
    echo "<img src=\"image/data/{$letter}/{$file}\" id=\"{$letter}{$index}\" width=\"70\" height=\"120\" title=\"{$letter}{$index}\" alt=\"Image {$letter}\"/>\n";
    echo "</a>\n";     
  }

}
Buchow_PHP
  • 410
  • 3
  • 10
  • Thank you for this, i tried this, but it only works for one folder, i have 26 folders (A-Z) for all the images, so i need this to be for a random image from each folder if the comments match a letter in the $_POST Comments entry, any ideas? – Aide2001 Oct 10 '13 at 20:25
  • Check the line: $folder = 'image/data/'.$letter; For each letter, the corresponding folder will be used. – Buchow_PHP Oct 11 '13 at 08:48
  • thanks Buchow_PHP for the information you gave me regarding this. I m now stuck trying to get each image id into a form to send, when i put the $letter and $index it comes up with the last image echo'd, is there a way to separate the images and pull the different id's to a form. – Aide2001 Oct 22 '13 at 16:30
  • now i'm confused, i thought you were the one that gave me all the help! i used as off the 9.10.13 @ 15.29, and I really dont need you to start drinking again!. The above code works well and i thought this is the code you gave me. Any help to sort my headache out is greatfully received. I've been on this now for about 3 weeks and my wife misses me now. – Aide2001 Oct 24 '13 at 16:46
  • @Aide2001, after the line echo "\n"; Thats assuming you want hidden inputs, just change hidden to text for textboxes. When the form is submitted all the id's will be available in an array: $_REQUEST['image_ids'] – Buchow_PHP Oct 24 '13 at 19:22
  • thanks,its great you helped me.i already have the form set up that are under option arrays.so i might need to tweek this some more.the php code i have before the form to get the image ids was - if ($option == '230') {$imagefiles = "279";}//four letter word and image file Then the form to send it was so this last one is already an array, any ideas how to get 2 arrays? – Aide2001 Oct 25 '13 at 17:19
  • ive worked it out ive brought in a new array and called it to that, works great – Aide2001 Oct 26 '13 at 18:25
0

Found the answer by using @Buchow_php and trial and error.

 for ($i=0; $i<$num_letters; $i++)
{
$image_num = '$image'.$i;


echo "<input type=\"hidden\" name=\"option[$image_num]\" value=\"$skus[$i]\" />";

}

together with the previous code and now it brings all the image files into an array for posting to my submit

Aide2001
  • 3
  • 5