Please need help new to php

Discussion in 'PHP' started by piercal, Apr 7, 2012.

  1. #1
    I have been trying to use this code to print 10 columns and 10 rows. I get one or the other, can some one help me figure out how to get both. With the <br /> I get 10 rows, with out it I get 10 columns. Will an IF statement help me? thanks

    $array = array('1' => "O", '2'=>'O', '3'=> '$', '4'=>'M', '5' => '^');

    function populateRoom ($myArray) {
    print "$myArray <br />";
    }

    for($row = 1; $row <= 10; $row++) {
    for($c = 1; $c <= 10; $c++) {
    $randomNumber = rand(1,5);
    $myArray = $array[$randomNumber];
    print $myArray;
    populateRoom ($myArray);

    }}
     
    piercal, Apr 7, 2012 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    all br does is give you a line break.
    you'd be better putting it into a table (or use css, but I think that's a step too far at this stage)

    $array = array('1' => "O", '2'=>'O', '3'=> '$', '4'=>'M', '5' => '^');
    
    function populateRoom ($str) {
       print "<td>{$str}</td>";
    }
    
    print "<table>";
    for($row = 0; $row < 10; $row++) {
       print "<tr>";
       for($col = 0; $col < 10; $col++) {
          $randomNumber = rand(1,5);
          $val = $array[$randomNumber];
          populateRoom ($val);
       }
       print "</tr>";
    }
    print "</table>";
    PHP:


    Untested, but should work.
     
    sarahk, Apr 9, 2012 IP
  3. piercal

    piercal Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you so much
     
    piercal, Apr 9, 2012 IP