caption under images

Ask about a PHP problem here.
Post Reply
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

caption under images

Post 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
libeco
Posts: 104
Joined: Sat May 07, 2011 9:56 am

Re: caption under images

Post 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.
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: caption under images

Post 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?
libeco
Posts: 104
Joined: Sat May 07, 2011 9:56 am

Re: caption under images

Post 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.
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: caption under images

Post 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?
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: caption under images

Post 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);
	?>
Post Reply