I am creating a table with a foreach loop in php that creates the columns. Is there a possibility to give the columns an even width like 3 columns -> columnwidth 33%, 4 columns -> 25%, 5 columns -> 20% and so on? Right now it feels like the widths of the columns are random.
some code if you need it:
PHP/HTML
echo '<tr>';
$permgroups = $perms->getPermissiongroups();
foreach ($permgroups as $group) {
if ($group != "user_ID") {
echo '<th>'.$group.'</th>';
}
}
echo '</tr>';
CSS
.structure_content table {
margin-top: 10px;
margin-bottom: 10px;
line-height: 30px;
background: #BDBDBD;
width: 100%;
float: right;
border-top: 1px solid #CEDADC;
border-bottom: 1px solid #CEDADC;
}
.structure_content th {
font-size: 20px;
}
Just give your table table-layout:fixed....
table {
table-layout:fixed;
width:100%;
margin-bottom: 1em;
}
td {
padding:.5em;
border:1px solid red;
}
<table>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<table>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
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>
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:
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>
Any ideas, how to make table look like this (marked with red circles) that there are no border and background color?
table
{
border-collapse:separate;
border-spacing:10px 50px;
}
or whatever you want your spacing to be
You can give your tds some padding?
JSFiddle
<table>
<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>
</tr>
<!-- more rows -->
<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>
</tr>
</table>
CSS:
tr td:nth-of-type(2){
padding:0 20px;
}