November 19, 200917 yr Hi guys, I'm having difficulty aligning a PHP block which contains 3 images, randomly alternating on refresh, to stay within my CSS wrapper. It aligns fine from the left but once the browser is resized, the image seems to inherit the right hand margin, leaving no gap. Where in the PHP code are you able to apply CSS rules so that I can stop this from happening? As you can tell I am new to PHP. I'll will paste my code example maybe someone could tell me, there is a possibility I can resolve it with just a simple line of PHP Cheers! <!-- start of header --> <?php srand( microtime() * 1000000 ); $num = rand( 1,3 ); switch( $num ) { case 1 : $bannerimg="bannerblue.png"; $url="index.php"; break; case 2 : $bannerimg="bannergreen.png"; $url="index.php"; break; case 3 : $bannerimg="bannergrey.png"; $url="index.php"; break; } $banner = "<center><a href=\"$url\"> "; $banner.= "<img src=\"$bannerimg\" "; $banner.= "width=\"auto\" height=\"auto\" border=\"0\" >"; $banner.= "</a></center>"; echo( $banner ); ?>
November 21, 200917 yr OK, quick and nasty straight to the point reply ;o) Just create a div for each of them with maybe a surrounding div and use float left. Something like this <div id-"bannerhold"> <div class="case1">case 1 : $bannerimg="bannerblue.png"; $url="index.php";</div> <div class="case2">case 2 : $bannerimg="bannergreen.png"; $url="index.php";</div> <div class="case3">case 3 : $bannerimg="bannergrey.png"; $url="index.php";</div> </div> CSS #bannerhold { display: inline; margin: 0 auto; } .case1, .case2, .case3 { float:left; padding: 0 6px 0 0; } The above just aligns the divs with a padding right of 6px. bannerhold with margin: 0 auto; means it's centered. Or of course you could do the very same thing but by using a list ... Plenty of ways around this one ... Hope that helps
November 21, 200917 yr you don't need 3 seperate divs for the images, as your using classes and they are all the same, just make it one div e.g. <div id-"bannerhold"> <div class="case">case 1 : $bannerimg="bannerblue.png"; $url="index.php";</div> <div class="case">case 2 : $bannerimg="bannergreen.png"; $url="index.php";</div> <div class="case">case 3 : $bannerimg="bannergrey.png"; $url="index.php";</div> </div> #bannerhold { display: inline; margin: 0 auto; } .case { float:left; padding: 0 6px 0 0; } It's good to streamline your code a little
November 21, 200917 yr Author I do understand the simplicity in what you are saying, and I have tried these methods. Unfortunately for some reason the browser renders syntax errors whenever I insert <div> tags in PHP blocks which I found very strange. So really its back to the drawing board. I guess my next question is what is the technique used and the correct syntax for styling these PHP blocks in CSS. Thanks for your responses!
November 21, 200917 yr Author If its any use to anyone, the syntax error is as follows Parse error: syntax error, unexpected '<', expecting T_CASE or T_DEFAULT or '}' in /var/www/whitefire/index.php on line 59
November 21, 200917 yr you don't need 3 seperate divs for the images, as your using classes and they are all the same, just make it one div e.g. <div id-"bannerhold"> <div class="case">case 1 : $bannerimg="bannerblue.png"; $url="index.php";</div> <div class="case">case 2 : $bannerimg="bannergreen.png"; $url="index.php";</div> <div class="case">case 3 : $bannerimg="bannergrey.png"; $url="index.php";</div> </div> #bannerhold { display: inline; margin: 0 auto; } .case { float:left; padding: 0 6px 0 0; } It's good to streamline your code a little No you don't, but I generally like to have full control over my elements ... In case I want to mess around with them in the future.... Supposing I wanted to have a bigger margin from 1-2 than from 2-3 or offset the centre image for whatever reason and so forth ... This way I can do that with a swift change.
November 21, 200917 yr If its any use to anyone, the syntax error is as follows Parse error: syntax error, unexpected '<', expecting T_CASE or T_DEFAULT or '}' in /var/www/whitefire/index.php on line 59 You will need to post your code, but it looks like you haven't echo'd the div e.g. <?php echo '<div class="div_name">' ?>
November 21, 200917 yr Author Ok, the code now looks like this <?php srand( microtime() * 1000000 ); $num = rand( 1,3 ); switch( $num ) { <div id-"bannerhold"> <div class="case">case 1 : $bannerimg="bannerblue.png"; $url="index.php";</div> <div class="case">case 2 : $bannerimg="bannergreen.png"; $url="index.php";</div> <div class="case">case 3 : $bannerimg="bannergrey.png"; $url="index.php";</div> </div> } $banner = "<center><a href=\"$url\"> "; $banner.= "<img src=\"$bannerimg\" "; $banner.= "width=\"auto\" height=\"auto\" border=\"0\" >"; $banner.= "</a></center>"; echo( $banner ); ?> I will experiment with the PHP's echo function to see if I can get anywhere
Create an account or sign in to comment