Hi.

I've made a php script that imports daily cartoons to my site, with the possibillity to browse the cartoon archive.
What i want to do is to load the cartoon images (with ajax) and then change the archive link dynamicly..
But i cant get my head around it so if anybody can help it will be mutch apriciated!

here is the code ..
Index.php:
Code:
<?php



$index = 0 ;
$CARTOONS = Array() ;
include("cartoons.php") ;

// attempt to make the cache folder and chmod 777 if it doesn't already exist
// we'll use the @ prefix to supress any errors
@mkdir("cache") ;
@chmod(0777,"cache") ;
//[[[ date processing

$date = $HTTP_GET_VARS['date'] ;

function selectDate()
{
	global $date, $CARTOONS;
	
$today = date("Y-m-d") ;
$todaySec = date("U") ;

if (!$date) {
	$date = date("Y-m-d") ;
	$prevday = date("Y-m-d",strtotime("yesterday")) ;
	$nextday = date("Y-m-d",strtotime("tomorrow")) ;
}
foreach ($CARTOONS as $index => $cartoon) {
	$CARTOONS[$index]['date'] = date($cartoon['dateformat'],strtotime($date)) ;
	//	$CARTOONS[$index]['prevday'] = date($cartoon['dateformat'],strtotime($prevday)) ;
	//	$CARTOONS[$index]['nextday'] = date($cartoon['dateformat'],strtotime($nextday)) ;
}

$prevday = date("Y-m-d",mktime (0,0,0,substr($date, 5, 2),substr($date, 8, 2)-1,substr($date, 0, 4))) ;
$nextday = date("Y-m-d",mktime (0,0,0,substr($date, 5, 2),substr($date, 8, 2)+1,substr($date, 0, 4))) ;
$nextdaySec = date("U",mktime (0,0,0,substr($date, 5, 2),substr($date, 8, 2)+1,substr($date, 0, 4))) ;

//]]]
echo '<table style="width:95%; border:0;"><tr><td style="text-align:left"><b><a href="'.$PHP_SELF.'?date='.$prevday.'">Forige dag</a></b></td><td style="text-align:right">'.($nextdaySec <= $todaySec ? '<b><a href="'.$PHP_SELF.'?date='.$nextday.'">Neste dag</a></b>' : '').'</td></tr></table><br />' ;
}

function cartoons()
{
 global $date, $CARTOONS;

$template = '<table style="width:95%; border:0;"><tr><td style="text-align:center">{{{CARTOON}}}</td></tr></table><br />' ;
foreach ($CARTOONS as $index => $cartoon) {
	//echo "<p><h3><u>{$cartoon['name']}</h3></u><br>\n" ;
	$done = 0 ;
	$cartoon_text = $template ;
	if ($cartoon['cache']) {
		$dh = opendir("cache") ;
		$cache_match = "{$cartoon['name']}__".$date."__" ;
		while ($file = readdir($dh)) {
			if (strstr($file,$cache_match) !== false) {
				// build cartoon text here
				$cartoon_file = "cache/".$file ;
				$cartoon_text = str_replace("{{{CARTOON}}}",$cartoon['cartoon'],$cartoon_text) ;
				$cartoon_text = str_replace("{{{FETCH}}}",$cartoon_file,$cartoon_text) ;
				$done = 1 ;
				break ;
			}
		}
		closedir($dh) ;
	}
	if (!$done) {
		$url = str_replace("{{{DATE}}}",$cartoon['date'],$cartoon['url']) ;
		
		$fopen_check = ini_get('allow_url_fopen') ;
		//$fopen_check = 'Off' ;
		//If allow_url_fopen
		if ($fopen_check == 'On' || $fopen_check == 1 || $fopen_check == 'true') {
			$fh = @fopen($url,"r") ;
			//If not allow_url_fopen - use curl
		}else{
				$ch = curl_init(); 
				curl_setopt($ch, CURLOPT_URL, $url); 
				curl_setopt($ch, CURLOPT_HEADER, 0); 
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
				$fh = curl_exec($ch);
				curl_close($ch); 
		}
		
		if ($fh) { //[[[
			//If allow_url_fopen
			if($fopen_check){
			while (!feof($fh)) {
				$tmp .= fread($fh, 2048) ;
			}
			fclose ($fh) ;
			$bytes = strlen($tmp) ;
			// draw image here
			preg_match($cartoon['regexp'],$tmp,$matches) ;
			//If not allow_url_fopen
			}else{
				$bytes = strlen($fh) ;
				// draw image here
				preg_match($cartoon['regexp'],$fh,$matches) ;
			}
			if ($matches[0]) { //[[[ regexp worked
				$cartoon_file = $cartoon['fetch'] ;
				$matchindex = 0 ;
				for($i=1; $i < count($matches); $i++) {
					$cartoon_file = str_replace("{{{".$i."}}}",$matches[$i],$cartoon_file) ;
				}
				if ($cartoon['cache']) {//[[[
					$bits = explode("/",$cartoon_file) ;
					$cartoon_cachefile = $bits[count($bits)-1] ; // get just the last filename portion
					$cartoon_cachefile = str_replace(".","_",$cartoon['name']) . "__" . $date . "__" . $cartoon_cachefile ;
					$cartoon_cachefile = str_replace(" ","_",$cartoon_cachefile) ;
					$cartoon_cachefile = str_replace("\\","",$cartoon_cachefile) ;
					$cartoon_cachefile = str_replace(":","",$cartoon_cachefile) ;
					$cartoon_cachefile = str_replace("/","",$cartoon_cachefile) ;
					// this will create a filename like "Daily_Dilbert__2006-08-24__dilbert20060824.gif"
					if (file_exists("cache") && file_exists("cache/".$cartoon_cachefile)) {
						$cartoon_file = "cache/".$cartoon_cachefile ;
					} elseif (file_exists("cache")) {
						// save a copy of the cartoon
						if ($fopen_check == 'On' || $fopen_check == 1 || $fopen_check == 'true') {
						 //If allow_url_fopen
						$fh_cartoon = fopen($cartoon_file,"rb") ;
						//If not allow_url_fopen - use curl
						} else {
							$ch2 = curl_init(); 
								curl_setopt($ch2, CURLOPT_URL, $cartoon_file); 
								curl_setopt($ch2, CURLOPT_HEADER, 0); 
								curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1); 
								$fh_cartoon = curl_exec($ch2);
								curl_close($ch2);
						}
						if ($fh_cartoon) {
							$cartoon_binary = '' ;
							$byte_count = 0 ;
							//If allow_url_fopen
							if($fopen_check) {
							while (!feof($fh_cartoon)) {
								$cartoon_binary .= @fread($fh_cartoon,1) ;
								$byte_count++ ;
							}
							fclose($fh_cartoon) ;
							//If not allow_url_fopen 
							}else{
								$cartoon_binary .= $fh_cartoon;
							}
							$fh_cartoon = @fopen("cache/".$cartoon_cachefile,"wb") ;
							if ($fh_cartoon) {
							 //If allow_url_fopen
							 if ($fopen_check) {
								fwrite($fh_cartoon,$cartoon_binary,$byte_count) ;
								//If not allow_url_fopen
								}else{
								fwrite($fh_cartoon,$cartoon_binary) ;	
								}
								fclose($fh_cartoon) ;
							}
							$cartoon_file = "cache/".$cartoon_cachefile ;
						}
					}
				}//]]]
				$cartoon_text = str_replace("{{{CARTOON}}}",$cartoon['cartoon'],$cartoon_text) ;
				$cartoon_text = str_replace("{{{FETCH}}}",$cartoon_file,$cartoon_text) ;
				// reapply the {{{#}}} filters, if any
				for($i=1; $i < count($matches); $i++) {
					$cartoon_text = str_replace("{{{".$i."}}}",$matches[$i],$cartoon_text) ;
				}
				//]]]
			} else {//[[[
				// preg_match didn't find any matches
				$cartoon_text = str_replace("{{{CARTOON}}}",(isset($cartoon['404']) ? '<img src="404comics/'.$cartoon['404'].'" border="0" alt="Fant ikke stripen (regexp feilet)" title="Fant ikke stripen (regexp feilet)" /><br />Fant ingen stripe for denne datoen.' : "Fant ingen stripe for denne datoen."),$cartoon_text) ;
			}//]]]
			//]]]
		} else { //[[[
			// couldn't open page, either fopen isn't allowed to open url's or the page is 404, etc
			$cartoon_text = str_replace("{{{CARTOON}}}",(isset($cartoon['404']) ? '<img src="404comics/'.$cartoon['404'].'" border="0" alt="stripen finnes ikke (404)" title="stripen finnes ikke (404)" /><br />Kunne ikke pne websiden med stripen (404 not found)' : "Kunne ikke pne <a href=\"$url\">$url</a>, 404 Not found."),$cartoon_text) ;
		}//]]]
	}
	$cartoon_text = str_replace("{{{SITE}}}",$cartoon['site'],$cartoon_text) ;
	
	echo "\n$cartoon_text</p>\n" ;
}

}




selectDate();

cartoons();

selectDate();



?>
cartoons.php

Code:
<?php

//[[[ M
$CARTOONS[$index]['name'] = "M" ;
$CARTOONS[$index]['site'] = "http://www.start.no/m/index.php" ;
$CARTOONS[$index]['url'] = 'http://www.start.no/m/index.php?m={{{DATE}}}' ;
$CARTOONS[$index]['dateformat'] = "Ymd" ;
$CARTOONS[$index]['regexp'] = '/http:\/\/www.start.no\/tegneserier\/m\/mstriper\/m([0-9]*).gif/i' ;
$CARTOONS[$index]['fetch'] = 'http://www.start.no/tegneserier/m/mstriper/m{{{1}}}.gif' ;
$CARTOONS[$index]['cartoon'] = '<img src="{{{FETCH}}}" border="0" alt="M" title="M av Mads Eriksen" />' ;
$CARTOONS[$index]['cache'] = 1 ;
$CARTOONS[$index]['404'] = '404M.gif' ;
$index++ ;//]]]

//[[[ Nemi
$CARTOONS[$index]['name'] = "Nemi";
$CARTOONS[$index]['site'] = "http://www.adressa.no/index.jsp?service=tegneserier&stripe=Nemi&date=today" ;
$CARTOONS[$index]['url'] = 'http://www.adressa.no/index.jsp?service=tegneserier&stripe=Nemi&date={{{DATE}}}' ;
$CARTOONS[$index]['dateformat'] = "dmy" ;
$CARTOONS[$index]['regexp'] = '/http:\/\/www.adressa.no\/template\/ver1-0\/gfx\/tegneserier\/Nemi\/nemi_([0-9]*).gif/i' ;
$CARTOONS[$index]['fetch'] = 'http://www.adressa.no/template/ver1-0/gfx/tegneserier/Nemi/nemi_{{{1}}}.gif' ;
$CARTOONS[$index]['cartoon'] = '<img src="{{{FETCH}}}" alt="Nemi" title="Nemi av Lise Myhre" />' ;
$CARTOONS[$index]['cache'] = 1 ;
$CARTOONS[$index]['404'] = '404AdamAtHome.gif' ;
$index++ ;//]]]

?>