Page 1 of 1

caption under images

Posted: Sun Sep 04, 2011 8:43 pm
by Robbedoesie
Hello,
i am trying with all my knowledge about php and tutorials on the internet to make that there are captions under all images. Because there is not much of both(although my knowledge about php is increasing slowly) i can't get the captions under the images.
I am this far;
<body>
<div id="wrap">
    <div id="sidebarleft">
     <?php
    foreach ($images as $image){
            if (file_exists("./thumbs/{$image}")){
                    echo "<a href='?full_img={$image}'><img src='thumbs/{$image}' alt='{$image}' /></a>";
            }else{
                    echo "<a href='?full_img={$image}'><img src='?img={$image}' alt='{$image}' /></a>";
            }
    }
     
?>
</div>
<div id="image">
<?php
        if (isset($_GET['full_img'])){
                echo '<a href="?full_img={$image}"><img src="', htmlentities($_GET['full_img']), '" alt="{$image}" /></a>';
        }
?>
<div id="caption">
<?php
function _applyCaption($image){
		if(isset($_GET['caption'])){
			$caption = $_GET['caption'];
			
			$words = split(" ", $caption);

			$new_caption = array();
			$new_caption_str = "";

			foreach($words as $word){
				if(!isset($new_caption[$line]))
					$line++; 
			 		$new_caption[$line] = "";
	 			
				$new_caption[$line].= $word." ";
			}
		}		
	}
	?>
</div>	

        </div>    
   
    </div>
</div>
</body>
I hope it make any sense and that i am on the right track, but nothing appears under the images yet. Hopefully someone can help me.
Thanks in advance

Re: caption under images

Posted: Sun Sep 04, 2011 9:10 pm
by libeco
You're defining the function _applyCaption, but are not calling it. Also, you are adding to the string $new_caption, but you're not returning it.

Re: caption under images

Posted: Mon Sep 05, 2011 11:11 am
by Robbedoesie
Thanks for your answer.
Do you mean by calling the function _applyCaption something like this?
$white = imagecolorallocatealpha($image, 255, 255, 255;
I thought i better do this with css but i'll guess then the function_applyCaption would'nt work.

To return the string $new_caption, do you mean that i have to echo it out?

Re: caption under images

Posted: Mon Sep 05, 2011 2:10 pm
by libeco
Do you understand how functions work in PHP? You define a function by using the function keyword. Next you call it by using the function name and any possible parameters in between (). You can use the return keyword inside your function to return something, or an echo to echo something out immediately when calling the function.

Re: caption under images

Posted: Wed Sep 07, 2011 8:31 pm
by Robbedoesie
I want to get the caption from the image alt names. Maybe its better to do that without the PHP funtions.
I have ryed to reach the image names with glob() function and get the alt names under the images with preg_replace, but it is not working and maybe i am on the wrong track.
How can i begin best to get the alt names under the images?

Re: caption under images

Posted: Thu Sep 08, 2011 9:03 pm
by Robbedoesie
Well, i got captions under all the images but the caption is the imagename from the last image in the array. This imagename is under all the images were i click on, and it is with the .jpg extension. What am i doing wrong?
<?php
$captions = array("caption" => $image);
 foreach($captions as $caption){
$caption = preg_replace(array('/^.+-thumb--/', '/\.(jpg|jpeg|gif|png)$/', '/_/'), array('','',' '), $captions);
 }
print_r($caption);
	?>