PHP Multiplication Table -- Need for loop to add a row (16x16) - php

My goal is to get my PHP Multiplication table to look like this: 9x9 array with "x" and bolded numbers:
but instead, my array currently looks like this:9x9 array with x on the top row and no bolded numbers
Here is my code so far:
<?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
?>
I know I need a third for loop to help bold the numbers in the first row and first column, as well as properly include the "x" so that it is not on top of my array like it currently is. Can anyone help me fix this issue? Thanks! :)

You have to add the first row and column. Like:
<?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
?>
This will result to:
<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>

Start the loop with 0 and add condition for desired output. Try this code,
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
Output:

Related

How do I create a 10x10 addition table using PHP?

I need a hand with this project I am working on, I cant seem to find out why my code isn't working. My goal is to display a addition table similar to this one:
My code for this table so far is:
<!DOCTYPE html>
<html>
<body>
<?php
$cols = 10;
$rows = 10;
?>
<?php echo "<table border=\"1\">";
<?php echo "<table>";
for($a = 1; $a < $rows;$a++)
echo'<tr>';
for($b = 1;$b < $cols; $b++);
echo '<td>'($answer = $a + $b); '</td>'
echo"</table>";
?>
<br>
</body>
</html>
There are a bunch of typos and omissions. The worst are the missing or extraneous semicolons
Here is working code using array notation
https://onlinephp.io/c/a97d8
<?php
$cols = 10;
$rows = 12;
$table[]="<table><tbody><tr><td>+</td>";
for($a = 1; $a < $rows;$a++) {
$table[] = "<td>$a</td>";
for($b = 1;$b < $cols; $b++) $table[]= "<td>".($answer = $a + $b)."</td>";
if ($a<$rows-1) {
$table[] = "</tr>";
$table[] = "<tr><td>$a</td>";
}
}
$table[] = "</tr></tbody></table>";
echo implode($table);
?>
Output
Note the CSS - it is quite complex
table tr:nth-of-type(2n+3) : stripe from 3rd row
table {
border-collapse: collapse;
font-weight: bold;
color: blue;
font-family: Arial, Helvetica, sans-serif;
}
table td {
border: 1px solid #ccc;
width: 2em;
padding: 5px;
text-align: center;
}
table tr:first-of-type {
background-color: gold;
color: black;
}
table tr td:first-child {
background-color: pink;
color: black;
}
table tr:first-child>td:first-child {
background-color: orange;
}
table tr:nth-of-type(2n+3) {
background-color: #f2f2f2;
}
<table>
<tbody>
<tr>
<td>+</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>
</tr>
<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>
<td>11</td>
</tr>
<tr>
<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>
</tr>
<tr>
<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>
</tr>
<tr>
<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>
</tr>
<tr>
<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>
</tr>
<tr>
<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>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>
<td>17</td>
</tr>
<tr>
<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>
<td>17</td>
<td>18</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<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>
</tr>
<tr>
<td>10</td>
<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>
</tbody>
</table>
Easiest way to do this is with PHP and CSS Grid:
https://paiza.io/projects/oHTe6JJGbeXX85Hij6X0VQ?language=php
<div class="table">
<?php
$rows = 10;
$cols = 10;
for($i=0; $i<$rows+1; $i++)
{
for($j=0; $j<$cols+1; $j++)
{
echo "<div>";
echo $i == 0 && $j == 0 ? "+" : $i + $j;
echo "</div>";
}
}
?>
</div>
Result:
.table {
display: grid;
grid-template-columns: repeat(11, 1fr);
}
.table > div {
font-family:sans-serif;
padding: 5px 5px;
border: solid 1px;
text-align: center;
}
.table > div:first-child {
background-color: orange;
}
.table > div:nth-child(11n + 12) {
background-color: pink;
}
.table > div:nth-child(n+2):nth-child(-n+11) {
background-color: goldenrod;
}
<div class="table">
<div>+</div><div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div><div>8</div><div>9</div><div>10</div><div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div><div>8</div><div>9</div><div>10</div><div>11</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div><div>8</div><div>9</div><div>10</div><div>11</div><div>12</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div><div>8</div><div>9</div><div>10</div><div>11</div><div>12</div><div>13</div><div>4</div><div>5</div><div>6</div><div>7</div><div>8</div><div>9</div><div>10</div><div>11</div><div>12</div><div>13</div><div>14</div><div>5</div><div>6</div><div>7</div><div>8</div><div>9</div><div>10</div><div>11</div><div>12</div><div>13</div><div>14</div><div>15</div><div>6</div><div>7</div><div>8</div><div>9</div><div>10</div><div>11</div><div>12</div><div>13</div><div>14</div><div>15</div><div>16</div><div>7</div><div>8</div><div>9</div><div>10</div><div>11</div><div>12</div><div>13</div><div>14</div><div>15</div><div>16</div><div>17</div><div>8</div><div>9</div><div>10</div><div>11</div><div>12</div><div>13</div><div>14</div><div>15</div><div>16</div><div>17</div><div>18</div><div>9</div><div>10</div><div>11</div><div>12</div><div>13</div><div>14</div><div>15</div><div>16</div><div>17</div><div>18</div><div>19</div><div>10</div><div>11</div><div>12</div><div>13</div><div>14</div><div>15</div><div>16</div><div>17</div><div>18</div><div>19</div><div>20</div></div>

How to make dynamic rowspan codeigniter?

I have table rowspan dynamic on CodeIgniter app like this :
<table>
<tr>
<td>No</td>
<td>Data 1</td>
<td>Data 2</td>
<td>Qty</td>
<td>Data 3</td>
</tr>
<?php
$source1 = $this->db->query("select * from table")->result_array();
$no=1;
foreach($source1 as source1){ ?>
<tr>
<?php
$source2 = $this->db->query("select * from table where data1='$source1[data1]'");
$total_source2 = $source2->num_rows();
$source3 = $source2->result_array();
?>
<td rowspan="<?php echo $total_source2 ?>"><?php echo $no; ?></td>
<td rowspan="<?php echo $total_source2 ?>"><?php echo $source1['data1']; ?></td>
<?php foreach($source3 as $source3){ ?>
<td><?php echo $source3['data2'] ?></td>
<td><?php echo $source3['qty'] ?></td>
<td><?php echo $source3['data3'] ?></td>
</tr>
<?php } ?>
<?php $no++; } ?>
</table>
This is result of my code:
How to make it like this :
?
Thanks
table {
border: 1px solid #000;
border-collapse: collapse;
width: 100%;
}
table td,
table th {
border: 1px solid #000;
text-align: center;
}
<table>
<tr>
<th>No</th>
<th>Data1</th>
<th>Data2</th>
<th>Qty</th>
<th>Price</th>
<th>Sub Total</th>
<th>Total</th>
</tr>
<tr>
<td rowspan="2">1</td>
<td rowspan="2">ABCDE</td>
<td>Data2 a</td>
<td>1</td>
<td>100</td>
<td>100</td>
<td rowspan="2">620</td>
</tr>
<tr>
<td>Data2 b</td>
<td>4</td>
<td>130</td>
<td>152</td>
</tr>
<tr>
<td rowspan="3">2</td>
<td rowspan="3">ABC</td>
<td>Data2 c</td>
<td>2</td>
<td>400</td>
<td>800</td>
<td rowspan="3">1560</td>
</tr>
<tr>
<td>Data2 d</td>
<td>2</td>
<td>200</td>
<td>400</td>
</tr>
<tr>
<td>Data2 e</td>
<td>3</td>
<td>120</td>
<td>360</td>
</tr>
<tr>
<td>3</td>
<td>ASS</td>
<td>Data 2 f</td>
<td>1</td>
<td>100</td>
<td>100</td>
<td>100</td>
</tr>
</table>
Whenever you have to apply row-span or col-span then always try to write that content outside of for-each
<table>
<tr>
<td>No</td>
<td>Data 1</td>
<td>Data 2</td>
<td>Qty</td>
<td>Data 3</td>
</tr>
<?php
$source1 = $this->db->query("select * from table")->result_array();
$no=1;
foreach($source1 as source1){ ?>
<tr>
<?php
$source2 = $this->db->query("select * from table where data1='$source1[data1]'");
$total_source2 = $source2->num_rows();
$source3 = $source2->result_array();
?>
<td rowspan="<?php echo $total_source2 ?>"><?php echo $no; ?></td>
<td rowspan="<?php echo $total_source2 ?>"><?php echo $source1['data1']; ?></td>
<?php foreach($source3 as $source3){ ?>
<td><?php echo $source3['data2'] ?></td>
<td><?php echo $source3['qty'] ?></td>
<?php } ?>
<td rowspan="<?php echo $total_source2 ?>"><?php echo $source3['data3'] ?></td>
</tr>
<?php $no++; } ?>
</table>

How to create a table with array in Laravel

I am new in programming world. I am trying to create an application with laravel5.1 . I have bunch of data as multidimensional array. Now I want to create a table with these data. Return data as like below:
[
[
{ "id":581,"user_id":101,"amount":"500.00","charge":"10.00", "rcvd_account":"01916811723","account_type":"personal","acc_carrier":"bKash","req_date":"2016-04-26 12:46:43","response_date":"2016-05-24 15:41:40","remark":"","tnx_id":"bkp_71535117","status":1,"processed_by":"824"},
{"id":712,"user_id":429,"amount":"200.00","charge":"10.00","rcvd_account":"01723214356","account_type":"personal","acc_carrier":"bKash","req_date":"2016-05-01 23:05:00","response_date":"2016-05-24 15:40:53","remark":"Mon chaise tai","tnx_id":"bkp_30513790","status":2,"processed_by":"824"},
{"id":995,"user_id":17,"amount":"2000.00","charge":"100.00","rcvd_account":"01819529861","account_type":"agent","acc_carrier":"dbblm","req_date":"2016-05-24 17:30:25","response_date":"2016-05-24 17:30:51","remark":"","tnx_id":"dbma_18209839","status":1,"processed_by":"824"}
],
[
{"id":1004,"user_id":560,"amount":"1200.00","charge":"24.00","rcvd_account":"0191125478","account_type":"personal","acc_carrier":"dbblm","req_date":"2016-05-24 19:30:54","response_date":"2016-05-24 19:36:35","remark":"Account balance and transaction amount mismatch. Account balance is BDT 9000 grater than transaction amount.","tnx_id":"dbmp_98010253","status":2,"processed_by":"824"}
],
[
{"id":1005,"user_id":598,"amount":"5000.00","charge":"250.00","rcvd_account":"01819529861","account_type":"agent","acc_carrier":"bKash","req_date":"2016-05-24 19:32:40","response_date":"2016-05-24 19:36:37","remark":"","tnx_id":"bka_89541626","status":1,"processed_by":"824"},
{"id":1006,"user_id":598,"amount":"1980.00","charge":"10.00","rcvd_account":"01911205478","account_type":"personal","acc_carrier":"bKash","req_date":"2016-05-24 19:33:24","response_date":"2016-05-24 19:36:51","remark":"","tnx_id":"bkp_64898681","status":1,"processed_by":"824"},
{"id":1007,"user_id":598,"amount":"5000.00","charge":"250.00","rcvd_account":"0185421365","account_type":"agent","acc_carrier":"dbblm","req_date":"2016-05-24 19:33:44","response_date":"2016-05-24 19:36:53","remark":"","tnx_id":"dbma_8505249","status":1,"processed_by":"824"},
{"id":1008,"user_id":598,"amount":"2000.00","charge":"100.00","rcvd_account":"0214214521","account_type":"agent","acc_carrier":"dbblm","req_date":"2016-05-24 19:34:02","response_date":"2016-05-24 19:36:49","remark":"Account balance and transaction amount mismatch. Account balance is BDT 15240 grater than transaction amount.","tnx_id":"dbma_97558593","status":2,"processed_by":"824"},
{"id":1009,"user_id":598,"amount":"200.00","charge":"4.00","rcvd_account":"018954213652","account_type":"personal","acc_carrier":"dbblm","req_date":"2016-05-24 19:34:41","response_date":"2016-05-24 19:36:54","remark":"","tnx_id":"dbmp_21560669","status":1,"processed_by":"824"}
]
]
And I want to create a table As Like below:
<table class="table m-t-40">
<thead>
<tr>
<th>#</th>
<th>Purpose</th>
<th>DC</th>
<th>Amount</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Withdrawl Balance</td>
<td>satataunit</td>
<td>bKash</td>
<td>500.00</td>
<td rowspan="3">2700</td>
</tr>
<tr>
<td>2</td>
<td>Withdrawl Balance</td>
<td>satataunit</td>
<td>bKash</td>
<td>200.00</td>
</tr>
<tr>
<td>3</td>
<td>Withdrawl Balance</td>
<td>satataunit</td>
<td>dbblm</td>
<td>2000.00</td>
</tr>
<tr>
<td>4</td>
<td>Withdrawl Balance</td>
<td>mahienterprise</td>
<td>dbblm</td>
<td>1200.00</td>
<td rowspan="1">1200</td>
</tr>
<tr>
<td>5</td>
<td>Withdrawl Balance</td>
<td>mizanur</td>
<td>bKash</td>
<td>5000.00</td>
<td rowspan="5">14180</td>
</tr>
<tr>
<td>6</td>
<td>Withdrawl Balance</td>
<td>mizanur</td>
<td>bKash</td>
<td>1980.00</td>
</tr>
<tr>
<td>7</td>
<td>Withdrawl Balance</td>
<td>mizanur</td>
<td>dbblm</td>
<td>5000.00</td>
</tr>
<tr>
<td>8</td>
<td>Withdrawl Balance</td>
<td>mizanur</td>
<td>dbblm</td>
<td>2000.00</td>
</tr>
<tr>
<td>9</td>
<td>Withdrawl Balance</td>
<td>mizanur</td>
<td>dbblm</td>
<td>200.00</td>
</tr>
</tbody>
<tfoot>
<tr>
<th colspan="4">Total</th>
<th >18080</th>
</tr>
</tfoot>
</table>
I am trying with below code:
$total = 0;
$sn = 0;
foreach($results as $key => $element) {
$sn++;
for($i=0; $i<count($element); $i++){
$total += $element[$i]->amount;
$table .= "<tr><td>".$sn."</td><td>Withdrawl Balance</td><td>". AppHelper::getDc($element[$i]->user_id) ."</td><td>". $element[$i]->acc_carrier ."</td><td>". $element[$i]->amount. (end($element) ? '</td><td rowspan="'.count($element).'">'.$total.'</td>': '')."</tr>";
}
$table .= "</tbody></table>";
And My Output data as like below:
<table class='table m-t-30'>
<thead>
<tr>
<th>#</th>
<th>Purpose</th>
<th>DC</th>
<th>Acc. Carrier</th>
<th>Amount</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>2</td>
<td>Withdrawl Balance</td>
<td>arifdc</td>
<td>dbblm</td>
<td>560.00</td>
<td rowspan="2">560</td>
</tr>
<tr>
<td>2</td>
<td>Withdrawl Balance</td>
<td>arifdc</td>
<td>bKash</td>
<td>350.00</td>
<td rowspan="2">910</td>
</tr>
<tr>
<td>2</td>
<td>Withdrawl Balance</td>
<td>mahienterprise</td>
<td>bKash</td>
<td>1200.00</td>
<td rowspan="6">2110</td>
</tr>
<tr>
<td>2</td>
<td>Withdrawl Balance</td>
<td>mahienterprise</td>
<td>dbblm</td>
<td>2000.00</td>
<td rowspan="6">4110</td>
</tr>
<tr>
<td>2</td>
<td>Withdrawl Balance</td>
<td>mahienterprise</td>
<td>dbblm</td>
<td>1000.00</td>
<td rowspan="6">5110</td>
</tr>
<tr>
<td>2</td>
<td>Withdrawl Balance</td>
<td>mahienterprise</td>
<td>bKash</td>
<td>1520.00</td>
<td rowspan="6">6630</td>
</tr>
<tr>
<td>2</td>
<td>Withdrawl Balance</td>
<td>mahienterprise</td>
<td>bKash</td>
<td>1980.00</td>
<td rowspan="6">8610</td>
</tr>
<tr>
<td>2</td>
<td>Withdrawl Balance</td>
<td>mahienterprise</td>
<td>dbblm</td>
<td>3500.00</td>
<td rowspan="6">12110</td>
</tr>
</tbody>
Here is problem with "rowspan" its not set as expected. Please experts help me to find out where is the problem is? And please advise me how can I reach as my expected goal? Thanks in advance dears
Your Problem is not clear to me. You can try the following way:
$table .= "<tbody>";
for($i=0; $i<count($element); $i++){
$table .= "<tr><td>".$sn."</td><td>Withdrawl Balance</td><td>". AppHelper::getDc($element[$i]->user_id) ."</td><td>". $element[$i]->acc_carrier ."</td><td>". $element[$i]->amount."</td>";
// If table has the extra column for rowspan then $extraRow has value otherwise it will be empty
$extraRow = '';
$total = 0.0;
if ($i == 0) {
for($i = 0; $i<=2; ++$i) {
// SUM of first three rows
$total += $element[i]->amount;
}
$extraRow .= "<td rowspan='3'>$total</td>";
} elseif ($i == 3) {
// SUM of forth row
$total = $element[3]->amount;
$extraRow .= "<td rowspan='1'>$total</td>";
} elseif ($i == 4) {
for ($i = 4; $i<=8; ++$i) {
// Sum of fifth to ninth rows
$total += $element[i]->amount;
}
$extraRow .= "<td rowspan='5'>$total</td>";
}
// Adding $extraRow
$table .= $extraRow.'</tr>';
}
$table .= "</tbody>";
Try to use Laravel Blade Template.
Laravel Blade Template
And also try to retrieve datas from database using Eloquent ORM.
Laravel Eloquent ORM
Assuming you have a db and tables already created,
I would suggest you use laravel's seeds to populate your db:
Then query these results using laravel's eloquent/model functions
pass them to your view and use blade to generate your table
<table>
<tr>
<th>#</th>
<th>Purpose</th>
<th>DC</th>
<th>Acc. Carrier</th>
<th>Amount</th>
<th>Total</th>
</tr>
#foreach ($results as $result)
<tr>
<td>{{$result->id}}</td>
<td>{{$result->purpose}}</td>
<td>{{$result->dc}}</td>
<td>{{$result->carrier}}</td>
<td>{{$result->amount}}</td>
<td rowspan="6">{{$result->total}}</td>
</tr>
#endforeach
</table>

How can if statement be used within echo

I have to check the results that are coming from fetch array, I am using else if or if to get the remarks. But I do not know how to use it with in echo, When I run this code all the remarks statements come in same form as written in code.
In this I have to check the results that is coming from fetch array, so for this I am using else if or if to get the remarks..but i am do not know how to use it with in echo. When I run this code all the remarks statements come in same form as written in code
Code:
<?php
$answer = '';
include('config.php');
if(isset($_GET["results"]))
$answer = $_GET["results"];
$id = $_GET["idk"];
switch ($answer)
{
case 'Nursery':
$qqqs = mysql_query("select * from result where u_id='$id' AND sc='Nursery' ");
$rows=mysql_fetch_assoc($qqqs);
$a=$rows['u_id'];
$b=$rows['name'];
$c=$rows['fname'];
$d=$rows['reg'];
$e=$rows['sc'];
$f=$rows['ss'];
$g=$rows['se'];
$h=$rows['e1'];
$i=$rows['u1'];
$j=$rows['m1'];
$k=$rows['s1'];
$l=$rows['ss1'];
$m=$rows['i1'];
$n=$rows['e2'];
$o=$rows['u2'];
$p=$rows['e3'];
$pp=$rows['u3'];
$pic=$rows['picture'];
$total=$h+$i+$j+$k+$l+$m+$n+$o+$p;
$totals=230;
$res = ( $totals / $total) * 100;
// 0 digit after the decimal point
$res = round($res); // 67
// 1 digit after the decimal point
$res = round($res, 1); // 66.7
// 2 digits after the decimal point
$res = round($res, 2); // 66.67
echo "<html>
<head>
</head>
<body>
<table align='center' border='4' bgcolor='white' width='500' class='table table-bordered'>
<tr>
<td bgcolor='orange' colspan='4'><h2 align='center'>Leads Grammar School</h2><p align='center'>Babar Road, Kirri Jamandan, Multan</p></td>
</tr>
<tr>
<td bgcolor='yellow' colspan='4'><h2 align='center'>Monthly Test Feb-March</h2></td>
</tr>
<tr>
<td colspan='2'><img src='images/$pic' width='100px' height='100px' align='right'></td>
</tr>
<tr>
<td align='right'>Student's Name</td>
<td colspan='3'>$b</td>
</tr>
<tr>
<td align='right'>Father's Name</td>
<td colspan='3'> $c</td>
</tr>
<tr>
<td align='right'>Registration</td>
<td colspan='3'>$d</td>
</tr>
<tr>
<td align='right'>Student Class</td>
<td colspan='3'>$e</td>
</tr>
<tr>
<td align='right'>Exams</td>
<td colspan='3'>$f</td>
</tr>
<tr bgcolor='gray'>
<th>Subject Name</th>
<th>Marks</th>
<th>Obtained Marks</th>
<th>Grades</th>
</tr>
<tr>
<tr><td>English(Writing)</td>
<td>50</td>
<td> $h</td>
</tr>
<tr>
<tr ><td>Urdu(Writing)</td>
<td>50</td>
<td>$i</td>
</tr>
<tr>
<tr ><td>Math(Writing)</td>
<td>50</td>
<td> $j</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>General Knowledge</th></tr>
<tr>
<tr ><td>Science</td>
<td>10</td>
<td> $k</td>
</tr>
<tr>
<tr ><td>S.Studies</td>
<td>10</td>
<td>$l</td>
</tr>
<tr>
<tr ><td>Islamiat</td>
<td>10</td>
<td>$m</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>Poems</th></tr>
<tr>
<tr ><td>English </td>
<td>15</td>
<td> $n</td>
</tr>
<tr>
<tr ><td>Urdu</td>
<td>15</td>
<td>$o</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>Book Reading</th></tr>
<tr>
<tr><td>English </td>
<td>10</td>
<td>$p</td>
</tr>
<tr>
<tr >
<td>Urdu</td>
<td>10</td>
<td>$pp</td>
</tr>
<tr>
<td><h1>Total Marks</h1></td>
<td>230</td>
<td>$total</td>
<td>$res</td>
</tr>
<tr><td><h2>Remarks</h2></td>
<td colspan='3'>
if(91=<$res<=100)
{
echo 'Exceptional';
}
else if(81=<$res<=90)
{
echo 'Excellent';
}
else if(71=<$res<=80)
{
echo 'Very Good';
}
else if(61=<$res<=70)
{
echo 'Good';
}
else if(51=<$res<=60)
{
echo 'Fair';
}
else if(40=<$res<=50)
{
echo 'Pass';
}
else if(01=<$res<=39)
{
echo ' Needs Improvement';
}
else if($res==0)
{
echo ' Needs Improvement';
}
</td>
</tr>
";
break;
}
?>
Try this:
<?php
$answer = '';
include('config.php');
if(isset($_GET["results"]))
$answer = $_GET["results"];
$id = $_GET["idk"];
switch ($answer)
{
case 'Nursery':
$qqqs = mysql_query("select * from result where u_id='$id' AND sc='Nursery' ");
$rows=mysql_fetch_assoc($qqqs);
$a=$rows['u_id'];
$b=$rows['name'];
$c=$rows['fname'];
$d=$rows['reg'];
$e=$rows['sc'];
$f=$rows['ss'];
$g=$rows['se'];
$h=$rows['e1'];
$i=$rows['u1'];
$j=$rows['m1'];
$k=$rows['s1'];
$l=$rows['ss1'];
$m=$rows['i1'];
$n=$rows['e2'];
$o=$rows['u2'];
$p=$rows['e3'];
$pp=$rows['u3'];
$pic=$rows['picture'];
$total=$h+$i+$j+$k+$l+$m+$n+$o+$p;
$totals=230;
$res = ( $totals / $total) * 100;
// 0 digit after the decimal point
$res = round($res); // 67
// 1 digit after the decimal point
$res = round($res, 1); // 66.7
// 2 digits after the decimal point
$res = round($res, 2); // 66.67
$out = "<html>
<head>
</head>
<body>
<table align='center' border='4' bgcolor='white' width='500' class='table table-bordered'>
<tr>
<td bgcolor='orange' colspan='4'><h2 align='center'>Leads Grammar School</h2><p align='center'>Babar Road, Kirri Jamandan, Multan</p></td>
</tr>
<tr>
<td bgcolor='yellow' colspan='4'><h2 align='center'>Monthly Test Feb-March</h2></td>
</tr>
<tr>
<td colspan='2'><img src='images/$pic' width='100px' height='100px' align='right'></td>
</tr>
<tr>
<td align='right'>Student's Name</td>
<td colspan='3'>$b</td>
</tr>
<tr>
<td align='right'>Father's Name</td>
<td colspan='3'> $c</td>
</tr>
<tr>
<td align='right'>Registration</td>
<td colspan='3'>$d</td>
</tr>
<tr>
<td align='right'>Student Class</td>
<td colspan='3'>$e</td>
</tr>
<tr>
<td align='right'>Exams</td>
<td colspan='3'>$f</td>
</tr>
<tr bgcolor='gray'>
<th>Subject Name</th>
<th>Marks</th>
<th>Obtained Marks</th>
<th>Grades</th>
</tr>
<tr>
<tr><td>English(Writing)</td>
<td>50</td>
<td> $h</td>
</tr>
<tr>
<tr ><td>Urdu(Writing)</td>
<td>50</td>
<td>$i</td>
</tr>
<tr>
<tr ><td>Math(Writing)</td>
<td>50</td>
<td> $j</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>General Knowledge</th></tr>
<tr>
<tr ><td>Science</td>
<td>10</td>
<td> $k</td>
</tr>
<tr>
<tr ><td>S.Studies</td>
<td>10</td>
<td>$l</td>
</tr>
<tr>
<tr ><td>Islamiat</td>
<td>10</td>
<td>$m</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>Poems</th></tr>
<tr>
<tr ><td>English </td>
<td>15</td>
<td> $n</td>
</tr>
<tr>
<tr ><td>Urdu</td>
<td>15</td>
<td>$o</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>Book Reading</th></tr>
<tr>
<tr><td>English </td>
<td>10</td>
<td>$p</td>
</tr>
<tr>
<tr >
<td>Urdu</td>
<td>10</td>
<td>$pp</td>
</tr>
<tr>
<td><h1>Total Marks</h1></td>
<td>230</td>
<td>$total</td>
<td>$res</td>
</tr>
<tr><td><h2>Remarks</h2></td>
<td colspan='3'>";
if(91=<$res<=100)
{
$out .= 'Exceptional';
}
else if(81=<$res<=90)
{
$out .= 'Excellent';
}
else if(71=<$res<=80)
{
$out .= 'Very Good';
}
else if(61=<$res<=70)
{
$out .= 'Good';
}
else if(51=<$res<=60)
{
$out .= 'Fair';
}
else if(40=<$res<=50)
{
$out .= 'Pass';
}
else if(01=<$res<=39)
{
$out .= ' Needs Improvement';
}
else if($res==0)
{
$out .= ' Needs Improvement';
}
$out .="</td></tr>";
echo $out;
break;
}
?>
PHP does not have support for this syntax:
if(01=<$res<=39)
Instead you'll have to do this:
if(1 <= $res && $res <= 39)
Also, don't start your numbers with a 0 or they'll be interpreted as octal numbers (base 8) which will have some strange consequences.
EDIT upon closer inspection
Oh... additionally you indeed cannot have conditional statements inside an echo. You'll have to build the whole string in code first, and then echo it.
Simply end your first echo and then use your if statement to echo the remaining of the table.
<?php
$answer = '';
include('config.php');
if(isset($_GET["results"]))
$answer = $_GET["results"];
$id = $_GET["idk"];
switch ($answer)
{
case 'Nursery':
$qqqs = mysql_query("select * from result where u_id='$id' AND sc='Nursery' ");
$rows=mysql_fetch_assoc($qqqs);
$a=$rows['u_id'];
$b=$rows['name'];
$c=$rows['fname'];
$d=$rows['reg'];
$e=$rows['sc'];
$f=$rows['ss'];
$g=$rows['se'];
$h=$rows['e1'];
$i=$rows['u1'];
$j=$rows['m1'];
$k=$rows['s1'];
$l=$rows['ss1'];
$m=$rows['i1'];
$n=$rows['e2'];
$o=$rows['u2'];
$p=$rows['e3'];
$pp=$rows['u3'];
$pic=$rows['picture'];
$total=$h+$i+$j+$k+$l+$m+$n+$o+$p;
$totals=230;
$res = ( $totals / $total) * 100;
// 0 digit after the decimal point
$res = round($res); // 67
// 1 digit after the decimal point
$res = round($res, 1); // 66.7
// 2 digits after the decimal point
$res = round($res, 2); // 66.67
echo "<html>
<head>
</head>
<body>
<table align='center' border='4' bgcolor='white' width='500' class='table table-bordered'>
<tr>
<td bgcolor='orange' colspan='4'><h2 align='center'>Leads Grammar School</h2><p align='center'>Babar Road, Kirri Jamandan, Multan</p></td>
</tr>
<tr>
<td bgcolor='yellow' colspan='4'><h2 align='center'>Monthly Test Feb-March</h2></td>
</tr>
<tr>
<td colspan='2'><img src='images/$pic' width='100px' height='100px' align='right'></td>
</tr>
<tr>
<td align='right'>Student's Name</td>
<td colspan='3'>$b</td>
</tr>
<tr>
<td align='right'>Father's Name</td>
<td colspan='3'> $c</td>
</tr>
<tr>
<td align='right'>Registration</td>
<td colspan='3'>$d</td>
</tr>
<tr>
<td align='right'>Student Class</td>
<td colspan='3'>$e</td>
</tr>
<tr>
<td align='right'>Exams</td>
<td colspan='3'>$f</td>
</tr>
<tr bgcolor='gray'>
<th>Subject Name</th>
<th>Marks</th>
<th>Obtained Marks</th>
<th>Grades</th>
</tr>
<tr>
<tr><td>English(Writing)</td>
<td>50</td>
<td> $h</td>
</tr>
<tr>
<tr ><td>Urdu(Writing)</td>
<td>50</td>
<td>$i</td>
</tr>
<tr>
<tr ><td>Math(Writing)</td>
<td>50</td>
<td> $j</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>General Knowledge</th></tr>
<tr>
<tr ><td>Science</td>
<td>10</td>
<td> $k</td>
</tr>
<tr>
<tr ><td>S.Studies</td>
<td>10</td>
<td>$l</td>
</tr>
<tr>
<tr ><td>Islamiat</td>
<td>10</td>
<td>$m</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>Poems</th></tr>
<tr>
<tr ><td>English </td>
<td>15</td>
<td> $n</td>
</tr>
<tr>
<tr ><td>Urdu</td>
<td>15</td>
<td>$o</td>
</tr>
<tr><th align='center' colspan='3' bgcolor='orange'>Book Reading</th></tr>
<tr>
<tr><td>English </td>
<td>10</td>
<td>$p</td>
</tr>
<tr>
<tr >
<td>Urdu</td>
<td>10</td>
<td>$pp</td>
</tr>
<tr>
<td><h1>Total Marks</h1></td>
<td>230</td>
<td>$total</td>
<td>$res</td>
</tr>
<tr><td><h2>Remarks</h2></td>
<td colspan='3'>";
if(91=<$res<=100)
{
echo 'Exceptional';
}
elseif(81=<$res<=90)
{
echo 'Excellent';
}
else if(71=<$res<=80)
{
echo 'Very Good';
}
elseif(61=<$res<=70)
{
echo 'Good';
}
elseif(51=<$res<=60)
{
echo 'Fair';
}
elseif(40=<$res<=50)
{
echo 'Pass';
}
else if(01=<$res<=39)
{
echo ' Needs Improvement';
}
elseif($res==0)
{
echo ' Needs Improvement';
}
echo "</td>";
echo "</tr>";
break;
}
?>
You can use the ternary operator for conditions within echo:
echo "start of string....".(bool_condition1 ? "condition is true" : "condition is false").".... more string";
Yes, you can have if/else conditions in an echo by concatenating:
$x = 10;
echo "X is " . ($x==10? "equal" : "not equal") . " to 10";
I would also recommend that you create a function that return the string evaluating the score.
Try to mix as little logic as possible with the presentation.
You can't have an if statement inside echo. What you can do is to move all your if's outside the echo, and store the result in a variable. Eg:
if(91 <= $res && $res <= 100) // Note how the condition *should* be written.
{
$remark = 'Exceptional';
}
else if(...
and then use the variable in your table output:
<tr><td><h2>Remarks</h2></td>
<td colspan='3'>$remark
You could also use ternary operators, in combination with a custom function:
echo "fixed part".(inbetween($res,91,100)?'Exceptional':(inbetween($res,81,90)?'Excellent':'SOMETHING ELSE'));
function inbetween($int,$min,$max)
{
return ($int>=$min && $int<=$max);
}
The syntax of this ternary operator can be seen in the following example:
echo "This is the result: " . ( $testiftrue ? 'TRUE' : 'FALSE' );
This will test the condition $testiftrue, and then display 'TRUE' or 'FALSE'

php strip_tags not outputting whole string

i have this table as a variable i want to use strip_tags function on:
<?php $tbl='
<table id="patients">
<tr>
<th>Pt. username</th>
<th>Pt. number</th>
<th>Full Name</th>
<th>Added on</th>
</tr> <tr><td><a href="profile.php?username=fringo123"</a>fringo123</td>
<td>3</td>
<td>fringo123 shreiwekh</td>
<td>2014-02-12 22:16:53</td>
</tr> <tr class="alt"><td><a href="profile.php?username=kamal"</a>kamal</td>
<td>24</td>
<td>kamal kab</td>
<td>2014-03-06 03:01:24</td>
</tr> <tr><td><a href="profile.php?username=kissaye"</a>kissaye</td>
<td>20</td>
<td>shreiwekh</td>
<td>2014-03-03 14:34:59</td>
</tr> <tr class="alt"><td><a href="profile.php?username=maas12345"</a>maas12345</td>
<td>7</td>
<td>maas12345</td>
<td>2014-02-13 20:23:56</td>
</tr> <tr><td><a href="profile.php?username=maas123456"</a>maas123456</td>
<td>8</td>
<td>maas123456</td>
<td>2014-02-13 20:25:51</td>
</tr> <tr class="alt"><td><a href="profile.php?username=marly"</a>marly</td>
<td>9</td>
<td>Marly Mario</td>
<td>2014-02-19 22:02:50</td>
</tr> <tr><td><a href="profile.php?username=mohamad"</a>mohamad</td>
<td>10</td>
<td>mohamad salem</td>
<td>2014-02-19 22:27:48</td>
</tr> <tr class="alt"><td><a href="profile.php?username=rami1234"</a>rami1234</td>
<td>6</td>
<td>rami1234</td>
<td>2014-02-13 20:21:48</td>
</tr> <tr><td><a href="profile.php?username=ramizmo"</a>ramizmo</td>
<td>25</td>
<td>ramy om</td>
<td>2014-03-06 03:26:52</td>
</tr> <tr class="alt"><td><a href="profile.php?username=saeed"</a>saeed</td>
<td>16</td>
<td>Saeed Shaweesh</td>
<td>2014-02-28 21:59:37</td>
</tr> <tr><td><a href="profile.php?username=sheeerokh"</a>sheeerokh</td>
<td>21</td>
<td>sheeerokh mo7sen</td>
<td>2014-03-03 14:43:45</td>
</tr> <tr class="alt"><td><a href="profile.php?username=sheeerokh22"</a>sheeerokh22</td>
<td>22</td>
<td>sheeerokh mo7sen22</td>
<td>2014-03-03 14:45:50</td>
</tr> <tr><td><a href="profile.php?username=shinyor"</a>shinyor</td>
<td>23</td>
<td>shinyor kalb</td>
<td>2014-03-03 14:47:40</td>
</tr></table>';
when i run strip_tags i only get the four table head parts but not the rest of the table. what am i doing wrong please? this is what i get:
Pt. usernamePt. numberFull NameAdded on
You have just to close the bracket for example.
<a href="profile.php?username=fringo123"</a>fringo123
to
fringo123
etc.

Categories