PDA

View Full Version : arrays and loops



acc
November 17th, 2007, 12:53 PM
new to PHP, so I'm learning as I go about creating my websites.

what I'm trying to do is simply create a loop that will create the my buttons (an image for each button)

Here's what I have, right now simply lists the text of Jan to Dec



<?
// foreach example - loop through each entry in an array

$month = array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

foreach ( $month as $element ) {

echo "$element , ";


}
?>


To create image buttons I use this code



<img class="imgLink btnArchive" src="/images/btnJan.jpg" onmouseover="(this.src)='/images/btnJan.jpg';" onmouseout="(this.src)='/images/btnJan.jpg';" alt="" />



So all I basically want to do is create a loop with the array that will change the image name with the name of the month to display all 12 buttons


I tried this, but I don't know how to deal with quotations in php and adding array values into code



echo '<img class="imgLink btnArchive" src="/images/btn &element .jpg" onmouseover="(this.src)='/images/btn &element .jpg;"

onmouseout="(this.src)=/images/btn &element .jpg';" alt="" />'



Thanks very much

simplistik
November 17th, 2007, 12:57 PM
<?php
echo '<img class="imgLink btnArchive" src="/images/btn'.$element.'.jpg" onmouseover="(this.src)=/images/btn'.$element.'.jpg;" onmouseout="(this.src)=/images/btn '.$element.'.jpg;" alt="" />';
?>

acc
November 17th, 2007, 01:13 PM
oh wow... thanks so much, work perfectly..

my mind is still working in ASP... great...

acc
November 17th, 2007, 02:21 PM
after making the mouseover images, the mouseover does not seem to work

I searched and I saw people suggesting the usage of the backslash, so I tried to implement this on the code



echo '<a href="'.$element.'">';echo '<img class="imgLink btnArchive" src="/images/btn'.$element.'.jpg" onmouseover="(this.src)=\''/images/btn'.$element.'Ov.jpg''\';" onmouseover="(this.src)=\''/images/btn '.$element.'.jpg''\';" alt="" />';



any idea why? or is there a better way to approach this?

thanks

simplistik
November 17th, 2007, 02:47 PM
yea because you have escape the quote marks... for instance if you start an echo with


echo "this should be a quote";

if you want quote to be in quotations you can't just do


echo "this should be a "quote"";

you have to do


echo "this should be a \"quote\"";

same w/ single quotes

it's not a better way it's what needs to be done if you want to use a apostrophe or a quote.

acc
November 18th, 2007, 12:36 PM
thanks for clearing that up for me simplistik, my mindset is still on ASP, works nicely now