Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Obtaining radio button set value 1

Status
Not open for further replies.

d0nny

IS-IT--Management
Dec 18, 2005
278
GB
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?

 
Hi

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


Feherke.
[link feherke.github.com/][/url]
 
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?
 
Hi

d0nny said:
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 [tt]value[/tt]="1" and $_POST['heroImage'] will be "1", then you will know the one with [tt]value[/tt] 1 was [tt]checked[/tt].

d0nny said:
Does each of the radio buttons have a separate value?
Definitely yes. The handiest would be to use the [tt]for[/tt]'s loop control variable :
Code:
[b]echo[/b] [green][i]'<td><input type="radio" name="heroImage" value="'[/i][/green][teal],[/teal][navy]$i[/navy][teal]+[/teal][purple]1[/purple][teal],[/teal][green][i]'" '[/i][/green][teal],[/teal][navy]$checked[/navy][teal],[/teal][green][i]' /></td>'[/i][/green][teal];[/teal]
( Note that there is no need to put the interpreter to concatenate ( and possibly to cast to string first ) values before [tt]echo[/tt]ing them. Just enumerate them. )


Feherke.
[link feherke.github.com/][/url]
 
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?
 
Hi

d0nny said:
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
[ul]
[li]you check a single field to find out which image is the hero[/li]
[li]you update a single field when the hero image is changed[/li]
[/ul]
As I understand, you want to store a flag of 0 or 1 for each image. That way
[ul]
[li]you check all images' flag fields to find which image is the hero[/li]
[li]you update two images' flag fields when the hero image is changed[/li]
[/ul]
First I suggest a small change to my earlier code, to make sure there will be no desynchronizations on later changes :
Code:
[b]echo[/b] [green][i]'<td><input type="radio" name="heroImage" value="'[/i][/green][teal],[/teal][navy]$iSort[/navy][teal],[/teal][green][i]'" '[/i][/green][teal],[/teal][navy]$checked[/navy][teal],[/teal][green][i]' /></td>'[/i][/green][teal];[/teal]

Then compare the hero image's number with the current sort order number in savePhotoIntoDB() :
Code:
[navy]$heroImage[/navy] [teal]=[/teal] [navy]$req[/navy][teal][[/teal][green][i]'heroImage'[/i][/green][teal]] ==[/teal] [navy]$sort[/navy] [teal]?[/teal] [purple]1[/purple] [teal]:[/teal] [purple]0[/purple][teal];[/teal]

Feherke.
[link feherke.github.com/][/url]
 
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??
 
Hi

d0nny said:
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:
[b]select[/b]
count[teal](*)[/teal]

[b]from[/b] swb_gallery g
[b]inner[/b] [b]join[/b] swb_gallphoto p [b]on[/b] p[teal].[/teal]gallid[teal]=[/teal]g[teal].[/teal]id [b]and[/b] p[teal].[/teal]sortorder[teal]=[/teal]g[teal].[/teal]hero 

[b]where[/b] g[teal].[/teal]id[teal]=[/teal]$gallery

If the above returns value 0, then set one of the images as hero randomly :
Code:
[b]update[/b] swb_gallery
[teal],([/teal]
  [b]select[/b]
  gallid[teal],[/teal]sortorder
  [b]from[/b] swb_gallphoto

  [b]order[/b] [b]by[/b] rand[teal]()[/teal]
[teal])[/teal] foo

[b]set[/b] hero[teal]=[/teal]foo[teal].[/teal]sortorder

[b]where[/b] foo[teal].[/teal]gallid[teal]=[/teal]swb_gallery[teal].[/teal]id
[b]and[/b] id[teal]=[/teal]$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.
[link feherke.github.com/][/url]
 
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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top