Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    57
    Thanks
    14
    Thanked 0 Times in 0 Posts

    php print the array values to an html table

    Hi I need to print the array values to an html table but I am not sure how to do it. Heres my code:

    Code:
    <!doctype html><html><head><title>Dice Roll Simulator</title></head>
    <body>
    
    <?php
    
    $rolls = 26;
    
    $sides = 6;
    
    $results = array_fill(1, $sides, 0);
    
    for($i=1; $i<=$rolls; $i+=1)
    { 
    $roll = rand(1, $sides);
    
    $results[$roll] += 1;
    }
    print "A $sides sided dice was rolled $rolls times<br><br>";
    
    for($i=1; $i<=$sides; $i+=1) 
    { 
    print "number of $i was rolled $results[$i] times<br>"; 
    }
    
    ?>
    </body>
    </html>
    Any help would be greatly appreciated

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    not sure exactly the layout you want but the general principle is to echo out the correct HTML e.g.

    PHP Code:
    echo '<table>';
    for(
    $i=1$i<=$sides$i+=1){ 
         print 
    "<tr><td>number of $i was rolled {$results[$i]} times</td></tr>"
    }
    echo 
    '</table>'
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    57
    Thanks
    14
    Thanked 0 Times in 0 Posts
    What I want to do is assign each result in turn along with its roll number to a two dimensional array in the first for loop and print to an html table with a header row using a second program loop. Any help would be great.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    That's what firepages has done there. If you want the side number and count separate, a simple division is all that's required in the print.
    So like so:
    PHP Code:
    print '<table><tr><th>Side</th><th>Roll Count</th></tr>';

    for(
    $i=1$i<=$sides$i+=1

        
    printf('<tr><td>%d</td><td>%d</td></tr>'$i$results[$i]);
    }
    print 
    '</table>'
    Edit:
    Wait you want a two dimensional array? What exactly is to be in it?

  • Users who have thanked Fou-Lu for this post:

    helen11 (06-27-2012)

  • #5
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    57
    Thanks
    14
    Thanked 0 Times in 0 Posts
    What I want to do is use two separate for loops to roll a dice 26 times and assign each result in turn along with its roll number to a two dimensional array in the first for loop then count the total value of dice rolls over 26 throws and print to an html table.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Still not seeing a need to use multiple dimensions, but I don't know what you want in that output table.
    You can use two arrays to simplify. The first is simply what the roll values are, and offsets will correspond to the roll value. You can then create a second array of the counts using array_count_values to pin the roll number to the count. Something like this:
    PHP Code:
    $aRolls = array();
    $rolls 26;
    $sides 6;

    for (
    $i 0$i $rolls; ++$i)
    {
        
    $aRolls[] = rand(1$sides);
    }

    $aValueCount array_count_values($aRolls);

    print 
    '<table><tr><th>Side</th><th>Roll Count</th></tr>';
    for(
    $i=1$i <= $sides; ++$i

        
    printf('<tr><td>%d</td><td>%d</td></tr>'$i$aValueCount[$i]);
    }
    print 
    '</table>'
    So it still comes down to what you want inside that HTML. The $aRolls will contain each corresponding roll's 0 - 25 in this example, and the $aValueCounts associates the key as the value rolled with the value being the number of times it was rolled.

  • #7
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    57
    Thanks
    14
    Thanked 0 Times in 0 Posts
    What I need to do is:

    Use two separate for loops to roll a dice 26 times and assign each result in turn along with its roll number to a two dimensional array in the first for loop then count the total value of dice rolls over 26 throws and print to an html table with a header row using a second program loop. The total value of the dice rolls plus the average value per throw should be printed out.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Still not seeing a need for multidimensional here. Not to mention it will increase complication to the calculations since you cannot use things like count and array_sum's on the entire array as they are not recursive.
    This doesn't describe very well what the structure of the array should look like. The description you have specifies a two dimensional array, but the data provided is only one dimensional since the roll corresponds to the offset of the array itself, and so can be retrieved from the key. So what is the structure of the rolled array supposed to look like?
    As with the total value of the dice rolls, if that includes every value you need to increment a variable within the population of it if you are using multiple dimensions. The only alternative is to use a loop to count it as well since you cannot use count or array_sum to get this information from multiple dimensions, and this can be grouped into the second loop only if its intended to be used after the loop. If its a sum of the total number of rolls, then that can be calculated during the second loop.

    Show us what the array is supposed to look like, and what the HTML table should look like.

  • #9
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    57
    Thanks
    14
    Thanked 0 Times in 0 Posts
    This is what I have but im not sure if its right.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns = "http://www.w3.org/1999/xhtml">
    <head>
    <title>Dice Roll</title></head>
    <body>

    <?php
    $rolls 
    26;
    $results = array(=> array(), array(), array(), array(), array(), array());
    $total 0;

    for (
    $i=1;  $i <= $rolls; ++$i) {
        
    $val rand(1,6);
        
    $results[$val][] = $i;
        
    $total += $val;
    }

    echo 
    "<table border='1'>";
    echo 
    "<tr><th>Roll Number</th><th>Throws</th><th>Roll Value</th></tr>";

    foreach (
    $results as $val => $throws) {
        echo 
    "<tr><th>$val</th><td>" join(', '$throws) ."</td><td>" count($throws) . "</td></tr>";
    }

    echo 
    "</table><br />";
    printf ('Total : %d, Average: %4.2f'$total$total/$rolls);
    ?>
    </body>
    </html>


  •  

    Tags for this Thread

    Posting Permissions

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