Forum Moderators: coopster
script segment (where filenames have been replaced to keep it simple):
slideshowimages("filename1","filename2",...,"filenameLAST") I don't understand enough about either scripting conventions to figure out how to replace the static content inside the ()s, to include all the image filenames in my database relative to the other content, with the double-quotes and commas correctly placed, with NO comma at the end.
Can anyone suggest a PHP solution to display the content between the ()s? (I don't need the query -- I hope, it's just the display of the query output that is blocking me.)
Thanks, in advance. :)
//connect to db server, select db, set $query
$result = mysql_query($query);
$JS = "slideshowimages(";
while($row = mysql_fetch_array($result)) {
$JS .= "'".$row['file_name']."',"
}
$JS = rtrim($JS,",");
$JS .= ")";
echo $JS;
Something like that. Good luck!
$images_array = array(); // initialize variable
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$images_array[] = "'".$row['file_name']."'";
}
$JS = "slideshowimages(" . implode(",", $images_array) . ")";
echo $JS;
I tried the first suggestion and it appears I left out one very important detail! (Probably why I am so horrid at this sport!)
In addition to the filenames inside the ()s, there also needs to be the directory name -- a constant -- which should list as:
../SiteImages/
so I SHOULD have given the forum this example
slideshowimages("../SiteImages/filename1","../SiteImages/filename2",...,"../SiteImages/ filenameLAST") I'm sorry, but I don't know how to modify what you've given me to modify it on my own. (I am trying NOT to BE PATHETIC....) :(
I checked the query against my db and get the results I expect to see, so I figure I've missed something in the translation of the suggested solutions.
Still absolutely grateful in advance, for any help given.
Here's the present code:
//Slideshow image capture
$query1 = "SELECT `ImageName` FROM `Images` WHERE `State` = '$State' AND `Stop` = '$Stop' AND `TimePeriod` = 'Current'";
$result1 = mysql_query($query1);
define ("IDIR","../SiteImages/");
$JS = "slideshowimages(";
while($row1 = mysql_fetch_array($result1))
{
$JS .= "'IDIR".$row1['ImageName']."',";
}
$JS = rtrim($JS,",");
$JS .= ")";echo "<a href=\"javascript:gotoshow()\"><img src=\"$JS\" name=\"slide\" border=0 width=400 height=300></a>";
echo "<script>";
echo "<!-- ";
//configure the paths of the images, plus corresponding target links
echo "slideshowimages(\"$JS\")";
?>
First, you are doubly calling the slideshowimages function:
$JS = "slideshowimages(";
echo "slideshowimages(\"$JS\")";
Since you have already placed the function call in your $JS variable, you can just
echo $JS;to place it on your page.
Second, placing your $JS variable in the src attribute of the image tag is surely going to confuse the browser.
<img src=\"$JS\" name=\"slide\" border=0 width=400 height=300>
Better to put either a blank image in there or statically define it as the first image in your slide show.
I noticed that in the earlier proposed solution $JS seems to be defined, redefined, and redefined, but assumed it would become clear why at some point when I was more proficient. Now, it appears it is just wrong.
Going back to the original JS, with the static content (below), notice that the portion of the script I am trying to replace is the stuff within the parentheses. I was thinking that I had to echo the non-PHP lines within double-quotes, so that is why I had the second echo you refered to.
What I need is some help in coming up with the PHP to sit inside the () and call out the "../SiteImages/17-00_6-hh042002S.jpg" (where SiteImages is the image directory -- a constant, and each filename is separated by a comma, except the last filename).
Help?
<a href="javascript:gotoshow()"><img src="../SiteImages/17-00_6-hh042002S.jpg" name="slide" border=0 width=400 height=300></a>
<script>
<!--
//configure the paths of the images, plus corresponding target links
slideshowimages("../SiteImages/17-00_6-hh042002S.jpg", "../SiteImages/17-00_7-HH070602s.jpg", "../SiteImages/17-00_10-HH052303s.jpg", "../SiteImages/17-18_7-hh042002s.jpg", "../SiteImages/17-18_15-HH070602s.jpg", "../SiteImages/17-18_18-HH070602s.jpg", "../SiteImages/17-18_19-HH070602s.jpg", "../SiteImages/17-18_20-HH070602s.jpg", "../SiteImages/17-18_21-HH070602s.jpg", "../SiteImages/17-18_25-HH070602s.jpg", "../SiteImages/17-18_28-HH070602s.jpg", "../SiteImages/17-18_29-HH070602s.jpg")
//configure the speed of the slideshow, in miliseconds
var slideshowspeed=2000
var whichimage=0
function slideit(){
if (!document.images)
return
document.images.slide.src=slideimages[whichimage].src
whichlink=whichimage
if (whichimage<slideimages.length-1)
whichimage++
else
whichimage=0
setTimeout("slideit()",slideshowspeed)
}
slideit()
//-->
</script>
Signed,
Hopelessly Lost
[edited by: jatar_k at 5:41 am (utc) on Oct. 17, 2006]
[edit reason] fixed sidescroll [/edit]
$images_array = array [us2.php.net](); // initialize variable
$result = mysql_query [us2.php.net]($query); // run query; something like SELECT * FROM images
while($row = mysql_fetch_array [us2.php.net]($result)) { //means While there are rows in the result, set $row to the array of row values at the current pointer
$images_array[] = "'".$row['file_name']."'"; //push a new image name onto the $images_array array surrounded with single quotes (')
}
$JS = "slideshowimages(" . implode [us2.php.net](",", $images_array) . ")";
echo $JS;
I hope this gives you some help with understanding all of this. Good luck!
I would hate for you to still be confused :)
Thank you so very much for your interest and especially your PATIENCE, I really do appreciate both.
I went ahead and finished roughing out the rest of the page and saved this for last, so I could focus on it fully. I think I've got it, but until my host fixes a little problem that is keeping me from accessing my ftp to test, I won't know for sure.
This is what I came up with, using the suggestions on the implode:
function Images()
{
global $State, $Stop, $ImageName, $comma_separated;
$query5 = "SELECT DISTINCT `ImageName` FROM `Images` WHERE `State` = '$State' AND `Stop` = '$Stop' AND `TimePeriod` = 'Current' ORDER BY `ImageName` ASC";
$result5 = mysql_query($query5)
or die (mysql_error());
$row5 = mysql_fetch_array($result5,MYSQL_ASSOC);
$num_rows5 = mysql_num_rows($result5);
define ("IDIR","../SiteImages/");
$ImageName= $row5["ImageName"];
if ($num_rows5 == 0)
{
$ImageName= "StopAboard.JPG";
}
$slideimage = "IDIR.$ImageName";
$comma_separated = implode(",", $slideimage);
echo $comma_separated;
}
Within the ()s of the Javascript, I entered:
<?php Images();?>
Please set me back on track, if I side-railed somewhere. And thanks, very much, again.
Deb
function Images()
{
global $State, $Stop, $ImageName, $comma_separated;
$query5 = "SELECT DISTINCT `ImageName` FROM `Images` WHERE `State` = '$State' AND `Stop` = '$Stop' AND `TimePeriod` = 'Current' ORDER BY `ImageName` ASC";
$result5 = mysql_query($query5)
or die (mysql_error());
$row5 = mysql_fetch_array($result5,MYSQL_ASSOC);
$num_rows5 = mysql_num_rows($result5);
define ("IDIR","../SiteImages/");
while ($row5 = mysql_fetch_array($result5,MYSQL_ASSOC))
{
extract ($row5);
$ImageName= $row5["ImageName"];
if ($num_rows5 == 0)
{
$ImageName= "StopAboard.JPG";
}
else {
$slideimage = "IDIR.$ImageName";
$comma_separated = implode(",", $slideimage);
echo $comma_separated;
}
}
}
My kingdom for a volunteer developer!
Deb
Warning: implode(): Bad arguments. in /home/themilwa/public_html/Current/Test.php on line 110
Line 110 in the test page is:
$comma_separated = implode(",", $slideimage);
Interesting, because this is taken directly from the PHP manual.
Any suggestions?
function Images()
{
$Images =array();
$query5 = "SELECT DISTINCT `ImageName` FROM `Images` WHERE `State` = '$State' AND `Stop` = '$Stop' AND `TimePeriod` = 'Current' ORDER BY `ImageName` ASC";
$result5 = mysql_query($query5)
or die (mysql_error());
while ($row5 = mysql_fetch_array($result5,MYSQL_ASSOC))
{
$Images[] = "'".$row5["ImageName"]."'";
}
$comma_separated = implode(",",$Images);
return $comma_separated;
}
I ran it through my document, uploaded, looked at it through the browser and saw no change.
I noticed that you removed some things in your suggestion. Were these removed for brevity, or were they wrong?:
- the extract
- the constant, to include my directory in the pathway
- the qualifier for number of rows, to insure that if there are no resulting rows from the query, I don't have an empty set error or warning.
Sigh. <:¦
Both, I guess. The reason you aren't getting anything in the browser is because I changed the function to RETURN the string instead of ECHO it. Therefore when you call the function, it should be done like the following:
echo 'slideshowimages('.Images().');';
I sense a dawning of awareness, but I am not there, yet. PLEASE bear with me, you are my only hope.
I pasted the line of code that you provided within the javascript parentheses in my document, and saw no slideshow, just the default image. So, I went to the test page (no javascript) and did the same thing, to see what output would display.
This is what I SHOULD've gotten ( with line breaks inserted for ease of viewing):
"../SiteImages/17-00_6-hh042002S.jpg",
"../SiteImages/17-00_7-HH070602s.jpg",
"../SiteImages/17-00_10-HH052303s.jpg",
"../SiteImages/17-18_7-hh042002s.jpg",
"../SiteImages/17-18_15-HH070602s.jpg",
"../SiteImages/17-18_18-HH070602s.jpg",
"../SiteImages/17-18_19-HH070602s.jpg",
"../SiteImages/17-18_20-HH070602s.jpg",
"../SiteImages/17-18_21-HH070602s.jpg",
"../SiteImages/17-18_25-HH070602s.jpg",
"../SiteImages/17-18_28-HH070602s.jpg",
"../SiteImages/17-18_29-HH070602s.jpg"
This is what I got (no alterations):
slideshowimages();
If I just run the query and an echo within a while statement, I get the filenames listed in the Should Example above, so the query is working -- as long as I add an extract and a global statement in the function. That leads me to believe that I still need the constant statement, to include the " "../SiteImages/ ", and there is still something wrong with the implode, though I sure do not understand what, since it looks just like the manual syntax requires!
Any other ideas?
I played around with the code in my second test doc, and came up with this code (replacing the return with an echo, and removing the echo from the call function section):
function Images()
{
global $State, $Stop, $comma_separated;
$query5 = "SELECT DISTINCT `ImageName` FROM `Images` WHERE `State` = '$State' AND `Stop` = '$Stop' AND `TimePeriod` = 'Current' ORDER BY `ImageName` ASC";
$Images = array();
$result5 = mysql_query($query5)
or die (mysql_error());
while ($row5 = mysql_fetch_array($result5,MYSQL_ASSOC))
{
$ImageName = $row5["ImageName"];
$Images[] = "\"../SiteImages/$ImageName\" ";
$comma_separated = implode(",",$Images);
echo "$comma_separated";
}
}
And got these results, which (unfortunately) appear to be an indication of an endless loop! There are only supposed to be 12 rows returned from the query, but at least the output appears to be displaying correctly! (See earlier reply for the should example.)
"../SiteImages/17-00_10-HH052303s.jpg" "../SiteImages/17-00_10-HH052303s.jpg" ,"../SiteImages/17-00_6-hh042002S.jpg" "../SiteImages/17-00_10-HH052303s.jpg" ,"../SiteImages/17-00_6-hh042002S.jpg" ,"../SiteImages/17-00_7-HH070602s.jpg" "../SiteImages/17-00_10-HH052303s.jpg" ,"../SiteImages/17-00_6-hh042002S.jpg" ,"../SiteImages/17-00_7-HH070602s.jpg" ,"../SiteImages/17-18_15-HH070602s.jpg" "../SiteImages/17-00_10-HH052303s.jpg" ,"../SiteImages/17-00_6-hh042002S.jpg" ,"../SiteImages/17-00_7-HH070602s.jpg" ,"../SiteImages/17-18_15-HH070602s.jpg" ,"../SiteImages/17-18_18-HH070602s.jpg" "../SiteImages/17-00_10-HH052303s.jpg" ,"../SiteImages/17-00_6-hh042002S.jpg" ,"../SiteImages/17-00_7-HH070602s.jpg" ,"../SiteImages/17-18_15-HH070602s.jpg" ,"../SiteImages/17-18_18-HH070602s.jpg" ,"../SiteImages/17-18_19-HH070602s.jpg" "../SiteImages/17-00_10-HH052303s.jpg" ,"../SiteImages/17-00_6-hh042002S.jpg" ,"../SiteImages/17-00_7-HH070602s.jpg" ,"../SiteImages/17-18_15-HH070602s.jpg" ,"../SiteImages/17-18_18-HH070602s.jpg" ,"../SiteImages/17-18_19-HH070602s.jpg" ,"../SiteImages/17-18_20-HH070602s.jpg" "../SiteImages/17-00_10-HH052303s.jpg" ,"../SiteImages/17-00_6-hh042002S.jpg" ,"../SiteImages/17-00_7-HH070602s.jpg" ,"../SiteImages/17-18_15-HH070602s.jpg" ,"../SiteImages/17-18_18-HH070602s.jpg" ,"../SiteImages/17-18_19-HH070602s.jpg" ,"../SiteImages/17-18_20-HH070602s.jpg" ,"../SiteImages/17-18_21-HH070602s.jpg" "../SiteImages/17-00_10-HH052303s.jpg" ,"../SiteImages/17-00_6-hh042002S.jpg" ,"../SiteImages/17-00_7-HH070602s.jpg" ,"../SiteImages/17-18_15-HH070602s.jpg" ,"../SiteImages/17-18_18-HH070602s.jpg" ,"../SiteImages/17-18_19-HH070602s.jpg" ,"../SiteImages/17-18_20-HH070602s.jpg" ,"../SiteImages/17-18_21-HH070602s.jpg" ,"../SiteImages/17-18_25-HH070602s.jpg" "../SiteImages/17-00_10-HH052303s.jpg" ,"../SiteImages/17-00_6-hh042002S.jpg" ,"../SiteImages/17-00_7-HH070602s.jpg" ,"../SiteImages/17-18_15-HH070602s.jpg" ,"../SiteImages/17-18_18-HH070602s.jpg" ,"../SiteImages/17-18_19-HH070602s.jpg" ,"../SiteImages/17-18_20-HH070602s.jpg" ,"../SiteImages/17-18_21-HH070602s.jpg" ,"../SiteImages/17-18_25-HH070602s.jpg" ,"../SiteImages/17-18_28-HH070602s.jpg" "../SiteImages/17-00_10-HH052303s.jpg" ,"../SiteImages/17-00_6-hh042002S.jpg" ,"../SiteImages/17-00_7-HH070602s.jpg" ,"../SiteImages/17-18_15-HH070602s.jpg" ,"../SiteImages/17-18_18-HH070602s.jpg" ,"../SiteImages/17-18_19-HH070602s.jpg" ,"../SiteImages/17-18_20-HH070602s.jpg" ,"../SiteImages/17-18_21-HH070602s.jpg" ,"../SiteImages/17-18_25-HH070602s.jpg" ,"../SiteImages/17-18_28-HH070602s.jpg" ,"../SiteImages/17-18_29-HH070602s.jpg" "../SiteImages/17-00_10-HH052303s.jpg" ,"../SiteImages/17-00_6-hh042002S.jpg" ,"../SiteImages/17-00_7-HH070602s.jpg" ,"../SiteImages/17-18_15-HH070602s.jpg" ,"../SiteImages/17-18_18-HH070602s.jpg" ,"../SiteImages/17-18_19-HH070602s.jpg" ,"../SiteImages/17-18_20-HH070602s.jpg" ,"../SiteImages/17-18_21-HH070602s.jpg" ,"../SiteImages/17-18_25-HH070602s.jpg" ,"../SiteImages/17-18_28-HH070602s.jpg" ,"../SiteImages/17-18_29-HH070602s.jpg" ,"../SiteImages/17-18_7-hh042002s.jpg"
So, if you can tell me how to stop the endless loop bit, I think we've got it! Don't we?
function Images()
{
global $State, $Stop, $comma_separated;
$query5 = "SELECT DISTINCT `ImageName` FROM `Images` WHERE `State` = '$State' AND `Stop` = '$Stop' AND `TimePeriod` = 'Current' ORDER BY `ImageName` ASC";
$Images = array();
$result5 = mysql_query($query5)
or die (mysql_error());
while ($row5 = mysql_fetch_array($result5,MYSQL_ASSOC))
{
$ImageName = $row5["ImageName"];
$Images[] = "\"../SiteImages/$ImageName\" ";
}
$comma_separated = implode(",",$Images);
echo "$comma_separated";
}
I took the implode and the echo out of the while loop. That is why you were getting multiple results.
Looks like we are getting there ;)
Thank you, thank you, thank you! You've taught me SO MUCH.
God bless you and all you do. 8)
Thanks, again.
(Whew! What a relief! I thought I was going to expire in front of this computer!)
8)