Forum Moderators: coopster
I used a javascript slideshow tutorial and changed it to insert my php database fields homephoto1 - homephoto6. Works perfectly. However, if all 6 database fields were not filled by the user, the screen shows a blank missing image for that particular field. Doesn't exactly look good when a user only has 2 pics and 4 blanks. Is there a way to change this so the empty fields will not show up in the array? Thank you.
<!--
var interval = 4000;
var random_display = 0;
var imageDir = "homes/photo/";
var imageNum = 0;
imageArray = new Array();
imageArray[imageNum++] = new imageItem(imageDir + "<?php echo $home['homephoto1'];?>");
imageArray[imageNum++] = new imageItem(imageDir + "<?php echo $home['homephoto2'];?>");
imageArray[imageNum++] = new imageItem(imageDir + "<?php echo $home['homephoto3'];?>");
imageArray[imageNum++] = new imageItem(imageDir + "<?php echo $home['homephoto4'];?>");
imageArray[imageNum++] = new imageItem(imageDir + "<?php echo $home['homephoto5'];?>");
imageArray[imageNum++] = new imageItem(imageDir + "<?php echo $home['homephoto6'];?>");
var totalImages = imageArray.length;
<?php if($home['homephoto1']!= '') {?>
imageArray[imageNum++] = new imageItem(imageDir + "<?php echo $home['homephoto1'];?>");
<?php
}
?>
Do that for each of the lines.
An easier way would be to put the field names (homephoto1-6) into an array [php.net] and use a foreach [php.net] loop to echo the entire line.
Your code works perfectly. This was the closest I was able to come...but it didn't work.
imageArray[imageNum++] = new imageItem(imageDir + "<?php
$test = $home['homephoto1'];
if ($test!= '') {
echo $home['homephoto1'];
} else {
echo " ";
}
?>");
Doesn't quite do it. Couldn't figure out how to have a php code inside a php code...been trying different combinations now for hours. Thank you so much!
Not quite sure of what you mean by your array and foreach suggestion.
If you put your field names into an array like this:
$fields = array('homephoto1','homephoto2','homephoto3','homephoto4','homephoto5','homephoto6');
then you can use a foreach loop to construct the javascript:
foreach($fields as $onefield) {
if($home[$onefield]!= '')
echo 'imageArray[imageNum++] = new imageItem(imageDir + "' . $home[$onefield] . '"'; // copy and paste to dissect that mess of quotes
You can put them all in the same <?php?> block right after
imageArray = new Array();
and it replaces all six of those javascript lines you wrote.
There's nothing wrong with the way you've done it, this just saves typing (or pasting)
Now if I tell you that a for loop looks like this:
for($variable = 1; $variable < 7; $variable++) {
// this will execute six times with $variable having a value from 1 to 6 incremented by one
}
and if I tell you that this is a perfectly legal statement (provided of course that $variable has been declared, like above, hint hint)
echo $home["homephoto$variable"];
can you tell me how you could accomplish your task without creating an array of field names?
Tough question. Did a little Google search and found a post about Using Variables as the Index of an Array. The example given was:
For k = 1 To 25
Print arrStudents (k)
Next k
///It prints first 25 students from the arrStudents array
But that is still using an array.
Then I found this:
<script type="text/javascript">
function randpic(pic,n) {
// picks a random picture
// pic: path to pictures and first (common) part of picture name
// each picture carries a 3-digit number from 1 to n and ends with ".jpg"
var i=String(1000+Math.ceil(Math.random()*n)).substr(1)
document.writeln("<img src=\""+pic+i+".jpg\" alt=\"Bild Nr. "+i+"\">")
}
</script>
Now I know that I don't want random pics, not all of my photo names end in .jpg, and I know I need to add my php info, etc. I just find this interesting because in effect, it is creating an array...combine that with your $variable...
Am I on the right track?
I'd shown you how you could use an array and a foreach loop to produce it, now the challenge is to take advantage of the way you named your fields to simplify it further, with no array and using a for loop to generate the field names.