Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...keep up the good work with this forum, I think this is the best one around. ...you actually try to help people learn for themselves. ...I commend you on providing a very good, open learning atmosphere, where usually egos are left behind..."

Geography

Where in the world do Tek-Tips members come from?

Obtaining radio button set valueHelpful Member! 

d0nny (IS/IT--Management)
29 Aug 12 2:13
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:

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?

feherke (Programmer)
29 Aug 12 2:38
Hi

Quote (d0nny)

CODE

echo '<td><input type="radio" name="heroImage" '.$checked.' /></td>'; 
5 radio buttons with identical name and no value ? And you would like to find out which of them was checked ? Think again.

Feherke.
http://feherke.github.com/

d0nny (IS/IT--Management)
29 Aug 12 2:46
Ok, thanks Feherke, but that's not quite the help I was after.
If I add a value to the radio buttons (say value="1") how do I know which one is set?
Does each of the radio buttons have a separate value?
feherke (Programmer)
29 Aug 12 6:16
Hi

Quote (d0nny)

If I add a value to the radio buttons (say value="1") how do I know which one is set?
Supposing only one of those 5 will have value="1" and $_POST['heroImage'] will be "1", then you will know the one with value 1 was checked.

Quote (d0nny)

Does each of the radio buttons have a separate value?
Definitely yes. The handiest would be to use the for's loop control variable :

CODE --> (fragment)

echo '<td><input type="radio" name="heroImage" value="',$i+1,'" ',$checked,' /></td>'; 
( Note that there is no need to put the interpreter to concatenate ( and possibly to cast to string first ) values before echoing them. Just enumerate them. )

Feherke.
http://feherke.github.com/

d0nny (IS/IT--Management)
29 Aug 12 7:53
Thanks Feherke.
That is clearer to me now in terms of laying out the radio buttons.
What still confuses me is how I check which one is selected. As I say, I want to set a value in the database alongside each image where only one entry (per gallery ID) has a hero image set to 1.
If say the user selects the 4th image as the hero image, that would have a value of 4 which I could obviously retrieve in the $_POST data but how do I grab any of the radio values and set the database value to 1 for any particular image?
Helpful Member!  feherke (Programmer)
29 Aug 12 8:45
Hi

Quote (d0nny)

What still confuses me is how I check which one is selected.
Sorry, I do not fully understand your code and intentions, so my answer may not fit your needs properly.

Personally I would store the order number of the hero image somewhere in a single place. I suppose there is an entry somewhere for each gallery set, with an id, a name, maybe date. There I would put the hero order number too. That way
  • you check a single field to find out which image is the hero
  • you update a single field when the hero image is changed
As I understand, you want to store a flag of 0 or 1 for each image. That way
  • you check all images' flag fields to find which image is the hero
  • you update two images' flag fields when the hero image is changed
First I suggest a small change to my earlier code, to make sure there will be no desynchronizations on later changes :

CODE --> (fragment)

echo '<td><input type="radio" name="heroImage" value="',$iSort,'" ',$checked,' /></td>'; 

Then compare the hero image's number with the current sort order number in savePhotoIntoDB() :

CODE --> (fragment)

$heroImage = $req['heroImage'] == $sort ? 1 : 0; 

Feherke.
http://feherke.github.com/

d0nny (IS/IT--Management)
29 Aug 12 12:05
Actually, that worked a treat. Thanks Feherke!

And I think I like your suggestion of storing the hero image ID somewhere else as I now have a problem with image deletions.
Lets say I delete the hero image from the DB and therefore all other images in that set have a hero value set to 0, I now don't have a hero image to display on the gallery page.

So perhaps storing the hero image ID in the gallery table.
Then if a user deletes an image I have another function that updates the hero image reference in the gallery table??


PS - Any idea where 'jpadie' has gone??
feherke (Programmer)
29 Aug 12 12:48
Hi

Quote (d0nny)

Then if a user deletes an image I have another function that updates the hero image reference in the gallery table??
From the point of view of deletion storing the hero image id separately is less beneficial, but I would say, still better.

You will have to check with a separate statement whether the set hero image still exists :

CODE --> MySQL

select
count(*)

from swb_gallery g
inner join swb_gallphoto p on p.gallid=g.id and p.sortorder=g.hero 

where g.id=$gallery 

If the above returns value 0, then set one of the images as hero randomly :

CODE --> MySQL

update swb_gallery
,(
  select
  gallid,sortorder
  from swb_gallphoto

  order by rand()
) foo

set hero=foo.sortorder

where foo.gallid=swb_gallery.id
and id=$gallery 

( No idea why I wrote the samples in MySQL. Anyway, with other databases you may have better options. So tell us what are you using. )/my

Feherke.
http://feherke.github.com/

d0nny (IS/IT--Management)
31 Aug 12 4:53
Yes, mySQL.
I'll give this a try but for now I haven't written the delete function - trying to persuade the client otherwise! But ultimately, they will make mistakes and I don't want to be bothered to fix the database!
jpadie (TechnicalUser)
2 Sep 12 17:52

Quote (D0nny)


PS - Any idea where 'jpadie' has gone??

I've been on holiday. Not far away from where you live!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close