0

我想创建一个 10*10 的表,并用一个 1-100 范围内的数组填充它。每次我尝试回显值时,它都会回显每个单元格中数组的最后一个值。

有人可以查看我的代码并告诉我我做错了什么吗?
打印出来的是:
100 100 100 100 100.....100
100 100 100 100 100.....100

我想要的是什么?
1 2 3 4 5......10
11 12 13 14......20

<?php
function Chart($width, $height, $fill) {
   $cell= range(0,100,1);
   $key=0;
foreach ($cell AS $key => $value) {echo $value;}
print_r ($cell);
$chart = '<table border="0" cellpadding="5">';
    for ($i = 0; $i < $height; $i++) {
    $chart .= "<tr>";
    for ($j = 0; $j < $width; $j++) {
        $chart .= "<td>$value</td>";
    }
    $chart .= "</tr>";
    }
return $chart.="</table>";}
?>
<html>
  <body>
    <?php echo Chart(10, 10, $value); ?>
  </body>
</html>
4

2 回答 2

1

试试这个:

<?php
        $width = 10;
        $height = 10;
        $cell = 0;

        $data = '<table>';

        for ($i = 0; $i < $width; $i++) {
            $data.= '<tr>';
            for ($j = 1; $j <= $height; $j++) {
                $data.='<td>'.++$cell.'</td>';
            }
            $data.= '</tr>';
        }
        $data.= '</table>';

        echo $data;
        ?>

- 谢谢

于 2013-10-21T07:22:58.070 回答
0
<?php

    // http://php.net/manual/en/function.range.php
    // http://stackoverflow.com/questions/13929468/php-for-loop-vs-foreach-with-range
    // http://php.net/manual/en/language.variables.scope.php
    // http://stackoverflow.com/questions/5126261/php-variable-scope-between-code-blocks/5126279#5126279
    // http://stackoverflow.com/questions/5631962/php-variable-scope/5632015#5632015

    // not 100% sure what you intended to do with $fill
    // if the $width is 10 & the $height is 10, there will be 100 <td>s (10*10)
    function chart($width, $height) 
    {
        // set the $chart to be "" initially to append values later
        $chart = "";
        // set $cells to 0 so it can be incremented below
        $cells = 0;

        // keep looping until $h is not less than $height [example] 10x
        for ($h = 0; $h < $height; $h++) 
        {
            $chart .= "\t<tr>\n";

            // keep looping until $w is not less than $width [example] 10x
            for ($w = 0; $w < $width; $w++)
            {
                // http://php.net/manual/en/language.operators.increment.php
                // http://stackoverflow.com/questions/6400518/pre-incrementation-vs-post-incrementation
                // this is pre-incrementing because it starts at 0, but you need it to start at 1 and to make it get to 100
                $chart .= "\t\t<td>".++$cells."</td>\n";
            }
            $chart .= "\t</tr>\n";
        }   

        // return the table with all the content inside of it
        return "<table border=\"0\" cellpadding=\"5\">\n".$chart.'</table>';
    }


?><!DOCTYPE html> <!-- HTML5, this is on the first line after the php end block to prevent a space before the Doctype and thus quirks mode in earlier versions of IE -->
    <?php echo chart(10, 10); // or you can use the php shorttag like <?= Chart(10, 10); ? > ?>

输出

<table border="0" cellpadding="5">
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
        <td>5</td>
        <td>6</td>
        <td>7</td>
        <td>8</td>
        <td>9</td>
        <td>10</td>
    </tr>
    <tr>
        <td>11</td>
        <td>12</td>
        <td>13</td>
        <td>14</td>
        <td>15</td>
        <td>16</td>
        <td>17</td>
        <td>18</td>
        <td>19</td>
        <td>20</td>
    </tr>
    <tr>
        <td>21</td>
        <td>22</td>
        <td>23</td>
        <td>24</td>
        <td>25</td>
        <td>26</td>
        <td>27</td>
        <td>28</td>
        <td>29</td>
        <td>30</td>
    </tr>
    <tr>
        <td>31</td>
        <td>32</td>
        <td>33</td>
        <td>34</td>
        <td>35</td>
        <td>36</td>
        <td>37</td>
        <td>38</td>
        <td>39</td>
        <td>40</td>
    </tr>
    <tr>
        <td>41</td>
        <td>42</td>
        <td>43</td>
        <td>44</td>
        <td>45</td>
        <td>46</td>
        <td>47</td>
        <td>48</td>
        <td>49</td>
        <td>50</td>
    </tr>
    <tr>
        <td>51</td>
        <td>52</td>
        <td>53</td>
        <td>54</td>
        <td>55</td>
        <td>56</td>
        <td>57</td>
        <td>58</td>
        <td>59</td>
        <td>60</td>
    </tr>
    <tr>
        <td>61</td>
        <td>62</td>
        <td>63</td>
        <td>64</td>
        <td>65</td>
        <td>66</td>
        <td>67</td>
        <td>68</td>
        <td>69</td>
        <td>70</td>
    </tr>
    <tr>
        <td>71</td>
        <td>72</td>
        <td>73</td>
        <td>74</td>
        <td>75</td>
        <td>76</td>
        <td>77</td>
        <td>78</td>
        <td>79</td>
        <td>80</td>
    </tr>
    <tr>
        <td>81</td>
        <td>82</td>
        <td>83</td>
        <td>84</td>
        <td>85</td>
        <td>86</td>
        <td>87</td>
        <td>88</td>
        <td>89</td>
        <td>90</td>
    </tr>
    <tr>
        <td>91</td>
        <td>92</td>
        <td>93</td>
        <td>94</td>
        <td>95</td>
        <td>96</td>
        <td>97</td>
        <td>98</td>
        <td>99</td>
        <td>100</td>
    </tr>
</table>   
于 2013-10-21T06:53:32.500 回答