Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts

    captioning images

    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

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by mint suite View Post
    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:
    PHP Code:
    $rows = array('value o image1 id' => 'caption text1',
                           .......
                          
    'value o image n id' => 'caption text n'); 
    regards

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,513
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    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.


    .
    Last edited by mlseim; 12-18-2008 at 09:49 PM.

  • #4
    New Coder
    Join Date
    Jun 2006
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

    Code:
    <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

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,513
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    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.

    PHP Code:

    // 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 ());


    Last edited by mlseim; 12-19-2008 at 03:08 AM.

  • Users who have thanked mlseim for this post:

    mint suite (12-19-2008)

  • #6
    New Coder
    Join Date
    Jun 2006
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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:


    Code:
    <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


    Code:
    // 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>	";
    		}

  • #7
    New Coder
    Join Date
    Jun 2006
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts
    there also seems to be a problem with characters that aren't letters or numbers i.e. *&()) etc.

    ??

    Thanks

  • #8
    New Coder
    Join Date
    Jun 2006
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts
    All fixed!

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

    Code:
    <input value='<?=gallery_image_id?>' name='image_id[]' type='hidden'/>
    Code:
    <input value='<?=$gallery_image_id?>' name='image_id[]' type='hidden'/>
    It works like a dream now.

    Thanks!

  • #9
    New Coder
    Join Date
    Jun 2006
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

  • #10
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,513
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •