Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-20-2012, 03:58 PM   PM User | #1
helen11
New Coder

 
Join Date: Nov 2011
Location: South Africa
Posts: 46
Thanks: 12
Thanked 0 Times in 0 Posts
helen11 is an unknown quantity at this point
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
helen11 is offline   Reply With Quote
Old 06-21-2012, 02:01 AM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,904
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
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)
firepages is offline   Reply With Quote
Old 06-22-2012, 05:36 PM   PM User | #3
helen11
New Coder

 
Join Date: Nov 2011
Location: South Africa
Posts: 46
Thanks: 12
Thanked 0 Times in 0 Posts
helen11 is an unknown quantity at this point
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.
helen11 is offline   Reply With Quote
Old 06-22-2012, 06:05 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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?
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
helen11 (06-27-2012)
Old 06-27-2012, 04:09 PM   PM User | #5
helen11
New Coder

 
Join Date: Nov 2011
Location: South Africa
Posts: 46
Thanks: 12
Thanked 0 Times in 0 Posts
helen11 is an unknown quantity at this point
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.
helen11 is offline   Reply With Quote
Old 06-27-2012, 04:58 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 06-27-2012, 05:13 PM   PM User | #7
helen11
New Coder

 
Join Date: Nov 2011
Location: South Africa
Posts: 46
Thanks: 12
Thanked 0 Times in 0 Posts
helen11 is an unknown quantity at this point
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.
helen11 is offline   Reply With Quote
Old 06-27-2012, 06:58 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 06-28-2012, 06:36 PM   PM User | #9
helen11
New Coder

 
Join Date: Nov 2011
Location: South Africa
Posts: 46
Thanks: 12
Thanked 0 Times in 0 Posts
helen11 is an unknown quantity at this point
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>
helen11 is offline   Reply With Quote
Reply

Bookmarks

Tags
array, html, php, table

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:45 PM.


Advertisement
Log in to turn off these ads.