2013-10-21 2 views
0

10 * 10 테이블을 만들고 1-100 범위의 배열로 채우고 싶습니다. 값을 에코하려고 할 때마다 각 셀의 배열의 마지막 값을 표시합니다.배열의 각 요소를 추출하여 테이블에 인쇄 하시겠습니까?

누군가 내 코드를보고 내가 뭘 잘못하고 있다고 말할 수 있습니까? 인쇄됩니다 무엇
은 다음과 같습니다
100 100 100 100 100 ..... 100
100 100 100 100 100 ..... 100

I입니다 싶습니다 무엇?
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> 

답변

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> 
+0

감사합니다. 훌륭한 작품입니다. –

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; 
     ?> 

- 감사합니다.

+0

위대하고 간단합니다, 고마워요. –

+0

항상 환영합니다. 내 대답이 마음에 들면 투표를하십시오. –

관련 문제