...

View Full Version : captioning images



mint suite
12-18-2008, 09:20 PM
Hi

I've made a cms that adds images to a portfolio site. Once they have been uploaded I want the user to be able to caption them.

The cms has a thumbnail of the image and a text box next to it for the caption, I want to offer the ability to caption all of the images at the same time, i.e. you write in all of the captions and just submit the whole lot together. I have the name for each text field set to make an array i.e. caption[]. Each image is already in the database and has a field for a caption, but it's at this point that I'm stuck. I don't know how to tell the array which row in the table to update. Each image has an id of course, but I don't know how to loop through the array adding each caption to the correct image.

Could someone give me a pointer or two here?

Cheers

oesxyl
12-18-2008, 09:34 PM
Hi

I've made a cms that adds images to a portfolio site. Once they have been uploaded I want the user to be able to caption them.

The cms has a thumbnail of the image and a text box next to it for the caption, I want to offer the ability to caption all of the images at the same time, i.e. you write in all of the captions and just submit the whole lot together. I have the name for each text field set to make an array i.e. caption[]. Each image is already in the database and has a field for a caption, but it's at this point that I'm stuck. I don't know how to tell the array which row in the table to update. Each image has an id of course, but I don't know how to loop through the array adding each caption to the correct image.

Could someone give me a pointer or two here?

Cheers
use a hash array with pair like this:


$rows = array('value o image1 id' => 'caption text1',
.......
'value o image n id' => 'caption text n');


regards

mlseim
12-18-2008, 10:43 PM
Is your CMS a MySQL database?

I'm thinking ahead on the thought that if your form
had a hidden ID for each caption, it might be easy
to insert the captions into the database.

caption <input type="text" name="caption[]" value="$cap">
<input type="hidden" name="id[]" value="$id">

.
.
.

The form would by dynamically generated so that there is a list
of them to change, the caption would appear if one existed, and
the ID is hidden, but there for processing on the PHP script.

Just some thoughts.


.

mint suite
12-18-2008, 11:59 PM
Thanks for getting back to me.

Yep it's all php and mysql

here's what i've got so far in terms of the form.


<form id='caption' enctype="multipart/form-data" method="post" action="conf.php?do=image_caption" >
<h2>Caption images</h2>

<? $result = mysql_query("select * from gallery_images where gallery_name_id = '$gallery_name_id' order by show_order");
while($row = mysql_fetch_array($result)){
?>
<div style="padding:0 0 10px 0">
<p class='admin_thumb'><img src='../images/<? echo $folder ?>/small/<? echo $row['gallery_image_name'] ?>'/></p>
<div class="clear"></div>
<p><input class="inputarea clear" value='<? echo $row['caption'] ?>' name='caption[]' type='text'/></p>
</div>
<? } ?>
<input type='hidden' value='<? echo $gallery_name_id ?>' name='gallery_id' />
<input type="submit" class='submit' value='Save captions'/>
</form>

so your saying that i run two arrays - one for the caption and one for the array - that sounds fine. I'm just not totally sure how to deal with that in the mysql query stage. I guess it's a for each loop, but I'm a bit of a novice here..

cheers

mlseim
12-19-2008, 04:05 AM
Yes ...

Sort of in this way ...
we of course don't know the names of your tables or fields ...

I didn't test any of this. I'm just throwing this out as I think about it.
This might be enough to come up with some ideas.




// Bring in variables from your form ....
// These would be in the form of two arrays.
// $caption
// $id

// Make a MySQL Connection
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

// Loop through the array of variables from your form
// Use the caption array ...
$count=count($caption);
for($i=0;$i<$count;$i++){

// Set the appropriate caption to a matching id number in the MySQL database.
$query="UPDATE example SET caption='$caption[$i]' WHERE id=$id[$i]";
$result = mysql_query($query) or die( "An error has ocured: " .mysql_error (). ":" .mysql_errno ());

}

mint suite
12-19-2008, 12:37 PM
ok - very close. Basically the captions are being added now, but the last caption is being written for all entries.

here's what i've got:



<form id='caption' enctype="multipart/form-data" method="post" action="conf.php?do=image_caption" >
<h2>Caption images</h2>

<? $result = mysql_query("select * from gallery_images where gallery_name_id = '$gallery_name_id' order by show_order");
while($row = mysql_fetch_array($result)){
?>
<div style="padding:0 0 10px 0">
<p class='admin_thumb'><img src='../images/<? echo $folder ?>/small/<? echo $row['gallery_image_name'] ?>'/></p>
<div class="clear"></div>
<p>
<input class="inputarea clear" value='<? echo $row['caption'] ?>' name='caption[]' type='text'/>
<input value='<?=gallery_image_id?>' name='image_id[]' type='hidden'/>
</p>
</div>
<? } ?>
<input type='hidden' value='<? echo $gallery_name_id ?>' name='gallery_id' />
<input type="submit" class='submit' value='Save captions'/>
</form>



And



// CAPTION IMAGES //

if($_GET['do'] == "image_caption"){

$id = $_POST['gallery_id'];

$caption = $_POST['caption'];
$image_id = $_POST['image_id'];

$count=count($caption);
for($i=0;$i<$count;$i++){

$query="UPDATE gallery_images SET caption= $caption[$i] WHERE gallery_image_id = $image_id[$i]";
mysql_query($query) or die("Could not update captions".mysql_error());

}
print "<span class='confirm'>Image captions have been saved</span>";
print "<form enctype='multipart/form-data' method='post' action='gallery-edit.php' >
<input type='hidden' name='edit_gallery' value='".$id."'/>
<br/><input type='submit' class='submit' value='Further edit this gallery'/>
</form> ";
}

mint suite
12-19-2008, 12:45 PM
there also seems to be a problem with characters that aren't letters or numbers i.e. *&()) etc.

??

Thanks

mint suite
12-19-2008, 01:35 PM
All fixed!

I was missing the $ in the variable for the image id in my form.


<input value='<?=gallery_image_id?>' name='image_id[]' type='hidden'/>



<input value='<?=$gallery_image_id?>' name='image_id[]' type='hidden'/>


It works like a dream now.

Thanks!

mint suite
12-19-2008, 01:56 PM
ok - got a problem with ' and " in the caption.

Normally I'd use $caption = mysql_real_escape_string($_POST['caption']); but it doesn't like it because it's not a string.......

Any ideas?

mlseim
12-19-2008, 06:17 PM
Here's my thought on quotes (and quotes make things difficult).

I would escape them when saving to the database,
\' \"

When you retrieve them from the database, remove the slashes
whenever you display them on the browser.

$caption = stripslashes($caption);

So basically, your strings are always escaped except when the are
echoed to the browser (either in your form or some other display).

There's also a deal about "magic_quotes_gpc" ...

Hey, to someone that is really good at MySQL -- can you help us with the quote issue?
I think I'm right about my method above, but the magic_quotes thing may change
how it should be done?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum