I made a mysql table on my website with backend that allows me to upload images of affiliate companies i deal with. I used this script and it works. I now have need to add URL to these.

As you can see i select the directory the iamges are in as the can be different for each page loaded.

This works:

<script language="JavaScript">

	// configuration structure
	var A_TPL = {
		// randomize the array each time page loads
		'random' : false,
		// number of transparency changes during the transition
		//	increase for smoother transition, reduce for less CPU usage
		'steps' : 20,
		// transition duration in seconds
		'transtime': .5,
		// slide time in seconds
		'slidetime': 1,
		// width of the slide (optional)
		'width' : 400,
		// height of the slide (optional)
		'height': 300,
		// alt text for the image (optional)
		'alt' : 'Website name',
		// css class assigned to the slide <img> (optional)
		'css' : ''
	};

	// list of images to display
	var A_ITEMS = [<?php
	
         //EXTRACT MYSQL PATH DATA
	$extract = mysql_query("SELECT * FROM images");

	while ($row = mysql_fetch_assoc($extract))
	{
	$directory = $row['imagedir'];
	}	
		$s_path = ($directory);
		$a_types = array ('jpg', 'jpeg');
		$a_images = array ();
		$h_dir = opendir($s_path);
		while ($s_file = readdir($h_dir)) {
			$s_type = strtolower(substr(strrchr($s_file, '.'), 1));
			if (in_array($s_type, $a_types))
				array_push($a_images, "\n\t\t'$s_path$s_file'");
		}
		closedir($h_dir);
		sort($a_images);
		echo join(',', $a_images);
?>

	];
	// fader initialization	
	var mySlideShow = new tFader (A_ITEMS, A_TPL);

    </script>

as you can see the var A_ITEMS gets the image names from the directory it finds in mysql.

i now want to try get the image directory, image name and url from mysql, the table is set up but im new to javasrcipt and it gives me different errors no matter what i try and do.

Note the lines with //

<script type="text/javascript">

var mygallery=new fadeSlideShow({
	wrapperid: "fadeshow1", //ID of blank DIV on page to house Slideshow
	dimensions: [250, 100], //width/height of gallery in pixels. Should reflect dimensions of largest image
	imagearray: [
		
var A_ITEMS = [<?php
	
	//EXTRACT MYSQL DATA
	$extract = mysql_query("SELECT * FROM images");

	while ($row = mysql_fetch_assoc($extract))
	{
	$imagefolder = $row['imagedir'];
	$imagefilename = $row['imagefile'];
	$imageurl = $row['imageurl'];
	
	echo ('"['$imagefolder'/,'$imagefilename'", "'$imageurl'", "_new"]'
	
	//["directory/imagename.jpg", "http://www.website.com/", "_new"],
	
	//PLEASE NOTE: Do not add trailing comma after very last image element!
	
	}	

?>
	
	],
	displaymode: {type:'auto', pause:2500, cycles:0, wraparound:false},
	persist: false, //remember last viewed slide and recall within same session?
	fadeduration: 500, //transition duration (milliseconds)
	descreveal: "ondemand",
	togglerid: ""
})

</script>

you wil note these rows:

echo ('"'

im tryong to make the echo give data like row below:

//["directory/imagename.jpg", "http://www.website.com/", "_new"],

//PLEASE NOTE: Do not add trailing comma after very last image element!

Thats where i get stuck ... ho can i use the values from MYSQL and the trailing comma ?

I know its something stupid im doing

Dani AI

Generated

Good that ’s quick fix got the immediate output working and that pointed out the bracket mismatch. For a more robust, maintainable solution avoid hand‑concatenating comma‑separated JavaScript in PHP. Build a native PHP array of image records and emit a single JSON value — that prevents quoting bugs, trailing‑comma problems, and makes the client code simple.

<?php
// fetch rows with PDO (use prepared statements in real code)
$items = array();
foreach ($rowsFromDb as $r) {
  $src = rtrim($r['imagedir'], '/') . '/' . ltrim($r['imagefile'], '/');
  $items[] = array('src' => $src, 'url' => $r['imageurl'], 'target' => ($r['target'] ?? '_new'));
}
echo 'var A_ITEMS = ' . json_encode($items) . ';';
?>
// map JSON objects into the plugin's expected array shape (if needed)
var imagearray = A_ITEMS.map(function(i){
  return [ i.src, '<a href="'+i.url+'" rel="nofollow">'+i.url+'</a>', i.target ];
});

Troubleshooting and best practices:

  • Use json_encode instead of manually building strings — it escapes safely and never leaves a stray trailing comma.
  • Keep server paths and web URLs separate: convert Windows backslashes to forward slashes for client URLs.
  • Check the browser console and view-source for the generated var A_ITEMS = ...; line when a syntax error appears; mismatched brackets or stray HTML are common causes (what described).
  • Move away from deprecated mysql_* calls to PDO or mysqli and validate/sanitize imageurl before output to avoid XSS.
  • If performance matters, cache the file list or store full image URLs in the DB instead of scanning directories on every request.

This pattern makes the output predictable, easier to debug, and safer for production use.

Recommended Answers

All 4 Replies

Member Avatar for Member #905211

Line 6 has a starting bracket.
Line 8 has a starting bracket.
Line 29 has an ending bracket.
Where is the second ending bracket?

It also would be cleaner if you used a webservice to get the image urls.

<?php
    //replace line 19
    echo $comma."[\"$imagefolder/$imagefilename\", \"$imageurl\", \"_new\"]" ;

   //add new line 20
    $comma=", ";
?>

stbuchok - thanks for your advise.

urtrivedi Thank you, it works, after making some changes like the imagefolder must be url to image like suggested, weird al these / \ i need to find out what they meen and how to use them ...

Thanlks guys you have no idea how i was stressing up this side

\ this is used to escape a character.

if you want to substitute variable value in string then you must prepare string in double quotes as i did.

$name="mikcy";
$mymessage= "welcome $name";  //'welcome $name'  will not substitute name.
echo $mymessage;

//output is 
welcome micky

Now you want name to be printed in double quotes, then you can not directly use it in string. if you use directly then php will think its end of string, so we use escape character

$name="mikcy";
//$mymessage= "welcome "$name" ";  //this will not work
$mymessage= "welcome \"$name\" ";  //this will work
echo $mymessage;

//output is 
welcome "micky"

I hope its clear now, If yes then mark this thread solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.