Forum Moderators: coopster

Message Too Old, No Replies

Slideshow Pulling Database Fields

Removing Empty Fields From Array

         

oceanwave

7:32 pm on Sep 3, 2007 (gmt 0)

10+ Year Member



Hi,

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;

cameraman

11:09 pm on Sep 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Keeping with the way you have it written:

<?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.

oceanwave

11:44 pm on Sep 3, 2007 (gmt 0)

10+ Year Member



Hi Cameraman. Glad to see you! I had read another post from you along the same lines when I searched this forum (but not similar enough for me to figure this out), so when I saw you were kind enough to answer, I breathed a sigh of relief.

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.

cameraman

9:27 am on Sep 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aw shucks, thanks for the compliment!
Now I feel bad that I got busy and didn't check back sooner...

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?

oceanwave

2:50 pm on Sep 4, 2007 (gmt 0)

10+ Year Member



Hi Cameraman,

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?

cameraman

6:06 pm on Sep 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops, sorry, I was a little ambiguous with the words "your task" - by "your task" I meant the task of using php to fill in the javascript that you want on your page, the script that will produce:
imageArray[imageNum++] = new imageItem('images/fred.jpg');
imageArray[imageNum++] = new imageItem('images/joe.jpg');
imageArray[imageNum++] = new imageItem('images/bill.jpg');
imageArray[imageNum++] = new imageItem('images/sue.jpg');
imageArray[imageNum++] = new imageItem('images/kim.jpg');
imageArray[imageNum++] = new imageItem('images/terry.jpg');

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.

oceanwave

6:40 pm on Sep 4, 2007 (gmt 0)

10+ Year Member



Thanks Cameraman. I thought you gave me a homework assignment!

cameraman

7:23 pm on Sep 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I did!

Your mission, should you choose to accept it, is to figure out how to use a for loop and no field list array (like I did) to produce the javascript for the array of images.