PDA

View Full Version : Impossible PHP - cant be????




carlosbutler
Jul 30, 2009, 01:23 PM
Im trying to get PHP to echo out something like
#myStyle0 {... left:0px; top:0px; ...}
#myStyle1 {... left:144px; top:0px; ...}
#myStyle2 {... left:0px; top:144px; ...}
#myStyle3 {... left:144px; top:144px; ...}

imagine that example as a 2x2 table. so if i wanted 4x2 it would do left 0, 144, 288, 432 etc

i have come up with this:
<?
$xPos = 144; // 144
$yPos = 104; // 104
$numberImages = 16;
$numberRows = ceil($numberImages/4); // four images per row, for example

// working out xPos
for($i=0;$i<4;$i++){
echo '#myStyle'. $i .' {... left:' .$xPos*$i. '; ...}' . "\n";
}

// working out yPos, based on number of rows
for($i=0;$i<$numberRows;$i++){
... ... ... ... ... ...
}
?>

i do not know how to, so to speak, merge the two together so they come out looking like proper CSS. if anyone could help thanks.



angelwatt
Jul 30, 2009, 02:03 PM
Nested loops. I haven't tried the below, but should be pretty close if not right.
$xPos = 144; // 144
$yPos = 104; // 104
$numberImages = 16;
$imgPerRow = 4;
$numberRows = ceil($numberImages/$imgPerRow); // four images per row, for example

// Go down each row
for ($y=0; $y<$numberRows; $y++) {
// Go across each column
for ($x=0; $x<$imgPerRow; $x++) {
echo "#myStyle $i { left: ", $xPos*$x , 'px; top: ', $yPos*$y ,"px; }\n";
}
}

carlosbutler
Aug 1, 2009, 05:59 PM
ok thanks i shall give that a go

carlosbutler
Aug 1, 2009, 06:29 PM
thanks!!! i had seen where i went wrong first time round. in the for loop i had:

for ($i=0;$i<variable;$i++) {...}

i forgot the $ in the variable but that never showed up as an error. it simply didnt run the loop, dont know why no error. if you were wondering what it was for, it was a loop that was able to pick up how many pictures there were in a folder and then dynamically create the HTML and CSS code so that i never had to mess around, simply copy the file into the folder and it adds it:D