Hi
I have a form which allows my users to upload up to 5 images, which will ultimately form a gallery set.
In that form, I have up to 5 file inputs, 5 caption inputs, a sort order input field and also a radio button set to choose the 'hero' image.
Here's the form:
Here's my function for dealing with the inputs:
And my savePhotoIntoDB function:
What I want to obtain and I'm struggling with is setting the value of the hero image in the database to 1 whilst the other image entries are all set to 0. That way, I can display only the hero image on a full gallery listings page, and my users can choose what image they upload as their hero image.
But I cannot seem to obtain what radio button is set and only set that image to 1 in the database.
I think the above code sets all the images uploaded in that set to a 1.
Any ideas how I go about amending this code to allow only setting one image to a hero status whilst others are set to 0?
I have a form which allows my users to upload up to 5 images, which will ultimately form a gallery set.
In that form, I have up to 5 file inputs, 5 caption inputs, a sort order input field and also a radio button set to choose the 'hero' image.
Here's the form:
Code:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<table align="center" cellpadding="3" cellspacing="0" border="1">
<tr>
<td>Select images and captions</td>
<td>Sort Order</td>
<td>Hero Image</td>
</tr>
<?php
for ($i=0; $i<5; $i++) {
echo '<tr><td align="left">';
echo '<input type="file" name="photo['.$i.']" /><br />';
echo 'Caption: <input type="text" name="caption['.$i.']" size="50" />';
echo '</td>';
$iSort = $i+1;
echo '<td><input type="text" name="sortorder['.$i.']" size="3" value="'.$iSort.'" /></td>';
$checked = ($i == 0) ? 'checked' : '';
echo '<td><input type="radio" name="heroImage" '.$checked.' /></td>';
echo '</tr>';
}
?>
<tr>
<td colspan="3">
<input type="submit" name="action" value="Save New Gallery" />
</td>
</tr>
</table>
</form>
Here's my function for dealing with the inputs:
Code:
function savePhotos($gallID) {
global $req, $pdo;
$path = '/home/sites/domain.co.uk/public_html';
$phDir = '/images/gallery/';
foreach ($_FILES['photo']['tmp_name'] as $key=>$file){
if ($_FILES['photo']['error'][$key] === UPLOAD_ERR_OK ){
$caption = empty($req['caption'][$key]) ? '' : trim($req['caption'][$key]);
$sort = empty($req['sortorder'][$key]) ? 0 : $req['sortorder'][$key];
list($width, $height, $type, $attr) = getimagesize($file);
$ext = getImageExtension($file);
$fileName = uniqid('photoOrig_'.$gallID.'_', true).$ext;
$filestosave = array();
move_uploaded_file($file, $path.$phDir.$fileName);
$fileName2 = $path.$phDir.$fileName;
if ($width > 800) {
foreach (array(150,800) as $size) {
$outputFile = $path.$phDir.uniqid('photo'.$size.'_'.$gallID.'_', true).$ext;
$cmd1 = "convert " . escapeshellarg($fileName2) . " -resize '$size>' " . escapeshellarg($outputFile);
exec($cmd1);
array_push($filestosave, basename($outputFile));
}
savePhotoIntoDB($gallID,$filestosave[1],$filestosave[0],$caption,$sort);
} else {
$size = 150;
$origPhoto = uniqid('photo'.$width.'_'.$gallID.'_', true).$ext;
rename($fileName2, $path.$phDir.$origPhoto);
array_push($filestosave, $origPhoto);
$outputFile = $path.$phDir.uniqid('photo'.$size.'_'.$gallID.'_', true).$ext;
$cmd2 = "convert " . escapeshellarg($path.$phDir.$origPhoto) . " -resize '$size>' " . escapeshellarg($outputFile);
exec($cmd2);
array_push($filestosave, basename($outputFile));
savePhotoIntoDB($gallID,$filestosave[0],$filestosave[1],$caption,$sort);
}
}
}
}
And my savePhotoIntoDB function:
Code:
function savePhotoIntoDB ($gallID,$originalPhoto,$tbPhoto,$caption,$sort) {
global $pdo, $req;
$heroImage = (isset($req['heroImage'])) ? $req['heroImage'] : 0;
$sql = "INSERT INTO swb_gallPhotos (gallID, thumbnail, photo, caption, sortorder, hero) values (?,?,?,?,?,?)";
$results = $pdo->prepare($sql);
$results->execute(array($gallID,$tbPhoto,$originalPhoto,$caption,$sort,$heroImage));
return;
}
What I want to obtain and I'm struggling with is setting the value of the hero image in the database to 1 whilst the other image entries are all set to 0. That way, I can display only the hero image on a full gallery listings page, and my users can choose what image they upload as their hero image.
But I cannot seem to obtain what radio button is set and only set that image to 1 in the database.
I think the above code sets all the images uploaded in that set to a 1.
Any ideas how I go about amending this code to allow only setting one image to a hero status whilst others are set to 0?