Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Array to html table

  1. #1
    Join Date
    Dec 2006
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array to html table

    I'm trying to take an array and turn it into an html table where the number of columns can be altered. Each number appears in a seperate cell. The code I have reached is:

    PHP Code:
    $input = array('one''two''three''four''five''six''seven''eight''nine''ten');
    $cols 5;
        
        echo 
    "<table border=\"5\" cellpadding=\"10\">";

        for (
    $i=0$i count($input); $i++)
        {
        echo 
    "<tr>";
            for (
    $c=0$c<$cols$c++)
            {
            echo 
    "<td>$input[$i]</td>";
            }
        echo 
    "</tr>";
        }
            
        echo 
    "</table>"
    But instead of it looking like:
    one two three four five
    six seven eight nine ten
    It looks like:
    one one one one one
    two two two two two
    three three three three three
    four four four four four
    five five five five five
    six six six six six
    seven seven seven seven seven
    eight eight eight eight eight
    nine nine nine nine nine
    ten ten ten ten ten
    Where am I going wrong?

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    for () {
    --for () {
    --}
    }

    The inner loop is executed in its entirety each time the outer loop runs once.

    So, you get one of the outer loop (ie a single number) run through the inner loop fully-- outputting an entire row of that number.

    You can't do this just with two loops. You'll need a bit more math than that. But nothing too complex.

    Here's the simplest solution I can think of:
    PHP Code:
    $input = array('one''two''three''four''five''six''seven''eight''nine''ten');
    $cols 5;
        
        echo 
    "<table border=\"5\" cellpadding=\"10\">";

        for (
    $i=0$i count($input); $i++)
        {
        echo 
    "<tr>";
            for (
    $c=0$c<$cols$c++)
            {
            echo 
    "<td>$input[$i+$c]</td>";
            }
        echo 
    "</tr>";
        
    $i += $c;
        }
            
        echo 
    "</table>"
    Note $i+$c.

    There are a couple other ways as well, such as using $c%$cols, which would give the remainder of $c/$cols, which should work for you as the $c value, then just using $c value for the numbers. (Or replace $c with $i in the last sentence if that makes more sense-- you'd only have one.)


    Also, though it doesn't apply so easily in this case, you should look into foreach() loops.

    foreach($array as $item) {
    echo $item;
    }

    foreach($array as $key => $item) {
    echo $key.': '.$item;
    }


    By the way, using "$array[$item]" hasn't always worked for me, though that may be more the case with strings, like "$array['item']".
    End the quotes, and use a dot to join the parts, and that will likely run smoother in at least some cases. I'm not sure about the specifics.
    "something".$array[$item]."something";
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <?php if(count($input)) { ?>
    <table>
    <?php for($i = 0; $i < count($input); ++$i) { ?>
    <?php if(!$i) { ?>
      <tr>
    <?php } else if(!($i % 5)) { ?>
      </tr>
      <tr>
    <?php } ?>
        <td><?php echo $input[$i]; ?></td>
    <?php } ?>
      </tr>
    </table>
    <?php } ?>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Dec 2006
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your replies guys.

    Twey - based on your answer how would I modify it so the table appears as:
    one six
    two seven
    three eight
    four nine
    five ten

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    $i % 5... change that to the number you want, like 2.


    Twey, I understand breaking out of PHP for some long chunks of code, but single lines? Eek. You have more PHP tags now than you had echo statements.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Heh, I started looking at this issue in Lisp, spent a fair while figuring it out (I'm just learning Lisp and I'm pretty bad with this kind of array arithmetic anyway), then switched back to my browser and remembered that there was actually a reason I was playing with it.
    Code:
    (defun splitarr (grp arr)
      (apply #'mapcar
        (append '(list)
          (loop for j from 0 to (- grp 1) collecting
            (loop for i in arr when (= j (mod i grp)) collecting i)))))
    Code:
    <?php
      function splitarr($grp, $arr) {
        $r = array();
        foreach($arr as $k => $v)
          $r[floor($k / $grp)][] = $v;
        return $r;
      }
    ?>
    <table>
    <?php foreach(splitarr(2, $input) as $row) { ?>
      <tr>
    <?php foreach($row as $cell) { ?>
        <td><?php echo $cell; ?></td>
    <?php } ?>
      </tr>
    <?php } ?>
    </table>
    Last edited by Twey; 09-16-2007 at 01:15 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Dec 2006
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    But that appears to give me two rows instead of two columns. How do I use:
    PHP Code:
    $cols 2
    so that changing this number to 3, 4 or 5 etc will change the number of columns?

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Whoops, I meant floor($k / $grp) not $k % $grp. Edited.

    The first parameter passed to splitarr() is the number of elements to put in each group. Change it as you like.
    Twey, I understand breaking out of PHP for some long chunks of code, but single lines? Eek. You have more PHP tags now than you had echo statements.
    Yes, but the output's neater (unless I filled my echoed HTML up with \ns and \ts). Personally, I'd rather use Smarty or Django's template system. A proper templating system is much neater than PHP's default intermixed code and presentation.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Code:
    echo '<table>
    	<tr>
    		<td>
    			&nbsp;
    		</td>
    	</tr>
    </table>';
    //shrug
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Eight spaces per level?

    Doesn't look that much neater than mine, to me, anyway. Especially when you're outputting variables as well, and in real HTML there tend to be attributes:
    Code:
    echo '<table class="someclass">
      <tr class="otherclass" onclick="alert(\'Hello!\');">
        <td onclick='alert("Hello!  How\\\'re you?");'>
          ' . $somevalue . '
        </td>
      </tr>
    </table>';
    
    // or...
    
    echo "<table class=\"someclass\">
      <tr class=\"otherclass\" onclick=\"alert('Hello!');\">
        <td onclick='alert(\"Hello!  How\\'re you?\");'>
          $somevalue
        </td>
      </tr>
    </table>";
    A heredoc block might be appropriate, but they're less efficient than breaking out of PHP parsing mode (actually, so is echo, I think, especially with the double quotes).
    Last edited by Twey; 09-16-2007 at 01:33 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •