Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

carlosbutler

macrumors 6502a
Original poster
Feb 24, 2008
691
2
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

Moderator emeritus
Aug 16, 2005
7,852
9
USA
Nested loops. I haven't tried the below, but should be pretty close if not right.
PHP:
$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

macrumors 6502a
Original poster
Feb 24, 2008
691
2
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.