1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Impossible PHP - cant be????

Discussion in 'Web Design and Development' started by carlosbutler, Jul 30, 2009.

  1. macrumors 6502a


    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
    echo '#myStyle'. $i .' {... left:' .$xPos*$i. '; ...}' . "\n";

    // working out yPos, based on number of rows
    ... ... ... ... ... ...

    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.
  2. Moderator emeritus


    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++) {
    "#myStyle $i { left: "$xPos*$x 'px; top: '$yPos*$y ,"px; }\n";
  3. macrumors 6502a


    ok thanks i shall give that a go
  4. macrumors 6502a


    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

Share This Page