我的目标是让我的PHP乘法表看起来像这样:9x9 array with "x" and bolded numbers:
但相反,我的数组目前看起来像这样:9x9 array with x on the top row and no bolded numbers
到目前为止,这是我的代码:
<?php
echo "<table border =\"1\" style='border-collapse: collapse'>"; //creating the border outline
echo "<td> x </td>";
for ($row=1; $row <= 16; $row++) { // first loop
echo "<tr> \n";
for ($col=1; $col <= 16; $col++) { //2nd loop
$p = $col * $row; //computing values
echo "<td>$p</td> \n";
}
echo "</tr>";
}
echo "</table>"; //closing the table
?>
我知道我需要第三个for循环来帮助加粗第一行和第一列中的数字,并且正确地包含“x”,这样它就不像目前那样位于我的数组之上。任何人都可以帮我解决这个问题吗?谢谢! :)
答案 0 :(得分:1)
您必须添加第一行和第一列。像:
<?php
echo "<table border =\"1\" style='border-collapse: collapse'>"; //creating the border outline
//Add the first row
echo "<tr style='font-weight: bold;'>";
echo "<td >X</td> \n";
for ($col=1; $col <= 16; $col++) {
echo "<td>$col</td> \n";
}
echo "</tr>";
for ($row=1; $row <= 16; $row++) { // first loop
echo "<tr> \n";
//Adding the first column
echo "<td style='font-weight: bold;'>$row</td>";
for ($col=1; $col <= 16; $col++) {
$p = $col * $row; //computing values
echo "<td>$p</td> \n";
}
echo "</tr>";
}
echo "</table>"; //closing the table
?>
这将导致:
<table style="border-collapse: collapse" border="1"><tr style="font-weight: bold;"><td>X</td>
<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>
<td>11</td>
<td>12</td>
<td>13</td>
<td>14</td>
<td>15</td>
<td>16</td>
</tr><tr>
<td style="font-weight: bold;">1</td><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>
<td>11</td>
<td>12</td>
<td>13</td>
<td>14</td>
<td>15</td>
<td>16</td>
</tr><tr>
<td style="font-weight: bold;">2</td><td>2</td>
<td>4</td>
<td>6</td>
<td>8</td>
<td>10</td>
<td>12</td>
<td>14</td>
<td>16</td>
<td>18</td>
<td>20</td>
<td>22</td>
<td>24</td>
<td>26</td>
<td>28</td>
<td>30</td>
<td>32</td>
</tr><tr>
<td style="font-weight: bold;">3</td><td>3</td>
<td>6</td>
<td>9</td>
<td>12</td>
<td>15</td>
<td>18</td>
<td>21</td>
<td>24</td>
<td>27</td>
<td>30</td>
<td>33</td>
<td>36</td>
<td>39</td>
<td>42</td>
<td>45</td>
<td>48</td>
</tr><tr>
<td style="font-weight: bold;">4</td><td>4</td>
<td>8</td>
<td>12</td>
<td>16</td>
<td>20</td>
<td>24</td>
<td>28</td>
<td>32</td>
<td>36</td>
<td>40</td>
<td>44</td>
<td>48</td>
<td>52</td>
<td>56</td>
<td>60</td>
<td>64</td>
</tr><tr>
<td style="font-weight: bold;">5</td><td>5</td>
<td>10</td>
<td>15</td>
<td>20</td>
<td>25</td>
<td>30</td>
<td>35</td>
<td>40</td>
<td>45</td>
<td>50</td>
<td>55</td>
<td>60</td>
<td>65</td>
<td>70</td>
<td>75</td>
<td>80</td>
</tr><tr>
<td style="font-weight: bold;">6</td><td>6</td>
<td>12</td>
<td>18</td>
<td>24</td>
<td>30</td>
<td>36</td>
<td>42</td>
<td>48</td>
<td>54</td>
<td>60</td>
<td>66</td>
<td>72</td>
<td>78</td>
<td>84</td>
<td>90</td>
<td>96</td>
</tr><tr>
<td style="font-weight: bold;">7</td><td>7</td>
<td>14</td>
<td>21</td>
<td>28</td>
<td>35</td>
<td>42</td>
<td>49</td>
<td>56</td>
<td>63</td>
<td>70</td>
<td>77</td>
<td>84</td>
<td>91</td>
<td>98</td>
<td>105</td>
<td>112</td>
</tr><tr>
<td style="font-weight: bold;">8</td><td>8</td>
<td>16</td>
<td>24</td>
<td>32</td>
<td>40</td>
<td>48</td>
<td>56</td>
<td>64</td>
<td>72</td>
<td>80</td>
<td>88</td>
<td>96</td>
<td>104</td>
<td>112</td>
<td>120</td>
<td>128</td>
</tr><tr>
<td style="font-weight: bold;">9</td><td>9</td>
<td>18</td>
<td>27</td>
<td>36</td>
<td>45</td>
<td>54</td>
<td>63</td>
<td>72</td>
<td>81</td>
<td>90</td>
<td>99</td>
<td>108</td>
<td>117</td>
<td>126</td>
<td>135</td>
<td>144</td>
</tr><tr>
<td style="font-weight: bold;">10</td><td>10</td>
<td>20</td>
<td>30</td>
<td>40</td>
<td>50</td>
<td>60</td>
<td>70</td>
<td>80</td>
<td>90</td>
<td>100</td>
<td>110</td>
<td>120</td>
<td>130</td>
<td>140</td>
<td>150</td>
<td>160</td>
</tr><tr>
<td style="font-weight: bold;">11</td><td>11</td>
<td>22</td>
<td>33</td>
<td>44</td>
<td>55</td>
<td>66</td>
<td>77</td>
<td>88</td>
<td>99</td>
<td>110</td>
<td>121</td>
<td>132</td>
<td>143</td>
<td>154</td>
<td>165</td>
<td>176</td>
</tr><tr>
<td style="font-weight: bold;">12</td><td>12</td>
<td>24</td>
<td>36</td>
<td>48</td>
<td>60</td>
<td>72</td>
<td>84</td>
<td>96</td>
<td>108</td>
<td>120</td>
<td>132</td>
<td>144</td>
<td>156</td>
<td>168</td>
<td>180</td>
<td>192</td>
</tr><tr>
<td style="font-weight: bold;">13</td><td>13</td>
<td>26</td>
<td>39</td>
<td>52</td>
<td>65</td>
<td>78</td>
<td>91</td>
<td>104</td>
<td>117</td>
<td>130</td>
<td>143</td>
<td>156</td>
<td>169</td>
<td>182</td>
<td>195</td>
<td>208</td>
</tr><tr>
<td style="font-weight: bold;">14</td><td>14</td>
<td>28</td>
<td>42</td>
<td>56</td>
<td>70</td>
<td>84</td>
<td>98</td>
<td>112</td>
<td>126</td>
<td>140</td>
<td>154</td>
<td>168</td>
<td>182</td>
<td>196</td>
<td>210</td>
<td>224</td>
</tr><tr>
<td style="font-weight: bold;">15</td><td>15</td>
<td>30</td>
<td>45</td>
<td>60</td>
<td>75</td>
<td>90</td>
<td>105</td>
<td>120</td>
<td>135</td>
<td>150</td>
<td>165</td>
<td>180</td>
<td>195</td>
<td>210</td>
<td>225</td>
<td>240</td>
</tr><tr>
<td style="font-weight: bold;">16</td><td>16</td>
<td>32</td>
<td>48</td>
<td>64</td>
<td>80</td>
<td>96</td>
<td>112</td>
<td>128</td>
<td>144</td>
<td>160</td>
<td>176</td>
<td>192</td>
<td>208</td>
<td>224</td>
<td>240</td>
<td>256</td>
</tr></table>
&#13;
答案 1 :(得分:1)
以0开始循环并添加所需输出的条件。试试这段代码,
echo "<table border =\"1\" style='border-collapse: collapse'>"; //creating the border outline
//echo "<td> x </td>";
for ($row = 0; $row <= 16; $row++) { // first loop
echo "<tr> \n";
for ($col = 0; $col <= 16; $col++) { //2nd loop
if($row == 0 && $col == 0)
echo '<td> x </td>';
else if ($row == 0 && $col != 0)
echo "<td>$col</td>";
else if ($row != 0 && $col == 0)
echo "<td>$row</td>";
else {
$p = $col * $row; //computing values
echo "<td>$p</td> \n";
}
}
echo "</tr>";
}
echo "</table>"; //closing the table
输出: