I am reading images from the directory 'images' which contains several folders with images. I want to show every image in a div and with the checkbox to decide if it is fashion or not fashion. I am using the following code in order to read files from directory and showing them in browser. The divs is one after other:
<?php
function listFolderFiles($dir){
$html = '';
// Init the session if it did not start yet
if(session_id() == '') session_start();
$html .= '<ol>';
foreach(new DirectoryIterator($dir) as $folder){
if(!$folder->isDot()){
if($folder->isDir()){
// $html .= '<li>';
$user = $dir . '/' . $folder;
foreach (new DirectoryIterator($user) as $file) {
if($file != '.' && $file != '..'){
$image = $user . '/' . $file;
// Check the pictures URLs in the session
if(isset($_SESSION['pictures']) && is_array($_SESSION['pictures'])){
// Check if this image was already served
if(in_array($image, $_SESSION['pictures'])){
// Skip this image
continue;
}
} else {
// Create the session variable
$_SESSION['pictures'] = array();
}
// Add this image URL to the session
$_SESSION['pictures'][] = $image;
// Build the form
//echo $image, "</br>";
$html .= '
<style>
form{
font-size: 150%;
font-family: "Courier New", Monospace;
display: inline-block;
align: middle;
text-align: center;
font-weight: bold;
}
</style>
<form class = "form" action="' . action($image) . '" method="post">
<div>
<img src="' . $image . '" alt="" />
</div>
<label for="C1">FASHION</label>
<input id="fashion" type="radio" name="fashion" id="C1" value="fashion" />
<label for="C2"> NON FASHION </label>
<input id="nfashion" type="radio" name="nfashion" id="C2" value="nfashion" />
<input type="submit" value="submit" />
</form>';
// Show one image at a time
// Break the loop
break 2;
}
}
// $html .= '</li>';
}
}
}
$html .= '</ol>';
return $html;
}
//##### Action function
function action($img){
$myfile = fopen("annotation.txt", "a");
if (isset($_POST['fashion'])) {
fwrite($myfile, $img." -- fashion\n");
// echo '<br />' . 'fashion image';
}
else{
fwrite($myfile, $img." -- nonFashion\n");
// echo '<br />' . ' The submit button was pressed<br />';
}
}
echo listFolderFiles('images//');
?>
When I make call of action($image) from inside form tag, I noticed, that a default value of submit it is parsed to the function, thus it stores the name of the image with the default value and the chosen value for the first image it is parsed to the second image, the chosen value of the second image it is stored to the file with the third image and so on. When I make call in side input submit tag it began storing to the file from the second image with the value of the first one and so on. I couldn't manage to have stored to the file the correct correspondence between the image and the chosen value. Where should I make call of the action function in order to do it properly?