How to create a table with array in Laravel - php
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>
Related
Generate new table after 10 iteration of tr tags [duplicate]
This question already has an answer here: How can I get two items in a foreach loop in PHP? [duplicate] (1 answer) Closed 2 years ago. I have a table with looping tr tags I am looking to break after every second tr tag. like My table looks like this after the for loop. <table class="table"> <thead> <tr> <th>Header</th> <th>Header</th> <th>Header</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>4</td> <td>5</td> <td>6</td> </tr> <tr> <td>7</td> <td>8</td> <td>9</td> </tr> <tr> <td>10</td> <td>11</td> <td>12</td> after modulo logic, I want to show'em like this <table class="table"> <thead> <tr> <th>Header</th> <th>Header</th> <th>Header</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>4</td> <td>5</td> <td>6</td> </tr> </tbody> </table> <table class="table"> <thead> <tr> <th>Header</th> <th>Header</th> <th>Header</th> </tr> </thead> <tbody> <tr> <td>7</td> <td>8</td> <td>9</td> </tr> <tr> <td>10</td> <td>11</td> <td>12</td> </tr> </tbody> </table> And here is my PHP script so far. I have tried all sort of arrangement b ut unable to achieve those layout <?php $num = 1; ?> <table class="Table"> <thead> <tr> <th>Header</th> <th>Header</th> <th>Header</th> </tr> </thead> <tbody> <?php for ( $x = 1; $x <= 12; $x++ ) { if($num%2 == 0) { ?> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> <?php } ?> <?php if($num %2 == 1) { ?> <table class="Table"> <thead> <tr> <th>Header</th> <th>Header</th> <th>Header</th> </tr> </thead> <tbody> <?php } $num++; } ?> </tbody> </table> I know I am doing a silly mistake but can't figure it out. I appreciate your help.
Create a function that returns you a table. For every dataset of size 6, call the function and get your table. You can use array_chunk to chunk the datasets into smaller chunks and use heredoc syntax for better readability. getTable() function: <?php function getTable($data){ $table = <<<EOD <table class="table"> <thead> <tr> <th>Header</th> <th>Header</th> <th>Header</th> </tr> </thead> <tbody> EOD; $rows = ""; foreach(array_chunk($data,3) as $values){ $rows .= "<tr>"; foreach($values as $value){ $rows .= "<td>" . $value . "</td>"; } $rows .= "/<tr>"; } $table .= $rows; $table .= <<<EOD </tbody> </table> EOD; return $table; } Driver code: <?php $arr = [1,2,3,4,5,6,7,8,9,10,11,12]; foreach(array_chunk($arr,6) as $set_for_table){ echo getTable($set_for_table); }
for dynamic tables you can iterate your entire table and set the two tr values. using your example: <?php $total = 100; ?> <?php for ($i=0; $i < $total; $i+=6):?> <table class="table"> <thead> <tr> <th>Header</th> <th>Header</th> <th>Header</th> </tr> </thead> <tbody> <tr> <td><?=$i+1?></td> <td><?=$i+2?></td> <td><?=$i+3?></td> </tr> <tr> <td><?=$i+4?></td> <td><?=$i+5?></td> <td><?=$i+6?></td> </tr> </tbody> </table> <?php endfor; ?> will generate $total/6 tables each with two <tr>
Multiply all td values(numbers) from a table with 3.8
My table is here http://jsfiddle.net/wqk7Lauz/ All I need is to have all values from td:nth-of-type(3) multiplied with 3.8 on page load. HTML $('td:nth-of-type(3)').text(parseFloat($('td:nth-of-type(3)').text()) * 3.8) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table table-bordered mydatatable table-hover display responsive nowrap"> <tbody> <tr> </tr> <tr> <th>ID</th> <th>Nome</th> <th>Preço por 1000</th> <th>Quantidade mínima</th> <th>Quantidade máxima</th> <th>Descrição</th> </tr> <tr> <td>9</td> <td>Product </td> <td> R$1.86 </td> <td>100</td> <td>10000</td> <td>S1 </td> </tr> <tr> <td>10</td> <td>Product </td> <td> R$3.98 </td> <td>1000</td> <td>100000</td> <td>S1</td> </tr> </tbody> </table>
You're very close, you just need to use the callback version of text so you're only handling the text of each individual element, and remove the R$ at the beginning: $('td:nth-of-type(3)').text(function() { return parseFloat($(this).text().replace("R$", "")) * 3.8; }); Example: $('td:nth-of-type(3)').text(function() { return parseFloat($(this).text().replace("R$", "")) * 3.8; }); <table class="table table-bordered mydatatable table-hover display responsive nowrap"> <tbody> <tr> </tr> <tr> <th>ID</th> <th>Nome</th> <th>Preço por 1000</th> <th>Quantidade mínima</th> <th>Quantidade máxima</th> <th>Descrição</th> </tr> <tr> <td>9</td> <td>Product </td> <td> R$1.86 </td> <td>100</td> <td>10000</td> <td>S1 </td> </tr> <tr> <td>10</td> <td>Product </td> <td> R$3.98 </td> <td>1000</td> <td>100000</td> <td>S1</td> </tr> </tbody> </table> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> If you want to add back the R$, and perhaps limit the result to a specific number of places, you can do that with string concatenation and toFixed: $('td:nth-of-type(3)').text(function() { return "R$" + (parseFloat($(this).text().replace("R$", "")) * 3.8).toFixed(2); }); Example: $('td:nth-of-type(3)').text(function() { return "R$" + (parseFloat($(this).text().replace("R$", "")) * 3.8).toFixed(2); }); <table class="table table-bordered mydatatable table-hover display responsive nowrap"> <tbody> <tr> </tr> <tr> <th>ID</th> <th>Nome</th> <th>Preço por 1000</th> <th>Quantidade mínima</th> <th>Quantidade máxima</th> <th>Descrição</th> </tr> <tr> <td>9</td> <td>Product </td> <td> R$1.86 </td> <td>100</td> <td>10000</td> <td>S1 </td> </tr> <tr> <td>10</td> <td>Product </td> <td> R$3.98 </td> <td>1000</td> <td>100000</td> <td>S1</td> </tr> </tbody> </table> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Hi change your script function with this $(".table tr td:nth-child(3)").each(function(){ //Loop //alert($(this).text()) var thrdtd = $(this).text(); //Getting value of td onlyno = thrdtd.replace('R$', ''); // Removing R$ //akert(thrdtd); $(this).text('R$ '+ parseFloat(onlyno * 3.8).toFixed(2)) // Adding R$ and also multiplying at the same time and update that result to td again }); In this first i am getting value of every third td value through loop $(".table tr td:nth-child(3)").each(function(){ and then removing character from it before multiplying and then after multiplying i am updating the same td value
You can use the below code: $('td:nth-of-type(3)').text("R$" + (parseFloat($(this).text().replace("R$", "")) * 3.8).toFixed(2));
Generating dynamic data for html table with both vertical and horizontal header
It's pretty simple to create a dynamic table with horizontal headers OR vertical headers alone. But how can I generate dynamic data for a table with BOTH vertical AND horizontal headers? Is this even possible? Table: <table> <tr> <th></th> <th scope="col">Monday</th> <th scope="col">Tuesday</th> <th scope="col">Wednesday</th> <th scope="col">Thursday</th> <th scope="col">Friday</th> <th scope="col">Saturday</th> <th scope="col">Sunday</th> </tr> <tr> <th scope="row">Week 1</th> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <th scope="row">Week 2</th> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> </tr> <tr> <th scope="row">Week 3</th> <td>3</td> <td>3</td> <td>3</td> <td>3</td> <td>3</td> <td>3</td> <td>3</td> </tr> <tr> <th scope="row">Week 4</th> <td>4</td> <td>4</td> <td>4</td> <td>4</td> <td>4</td> <td>4</td> <td>4</td> </tr> </table>
Yes it can be easily done in PHP. I've done a small example using a for-loop to give you an easier idea of how this might be accomplished <table> <tr> <th></th> <th scope="col">Monday</th> <th scope="col">Tuesday</th> <th scope="col">Wednesday</th> <th scope="col">Thursday</th> <th scope="col">Friday</th> <th scope="col">Saturday</th> <th scope="col">Sunday</th> </tr> <?php // Create week row for ($week = 0; $week < 10; $week++) { echo " <tr>"; echo " <th scope='row'>Week " . $week . "</th>"; // Create day cells for ($day = 0; $day <= 7; $day++) { echo " <td>" . $day . "</td>"; } echo " </tr>"; } ?> </table>
To dynamically generate a table horizontally and vertically, you will need 2 loops, the first one being for the amount of rows and the one inside of it for the cols. <?php $rows = 10; $cols = 20; echo '<table>'; for($r=0; $r <= $rows; $c++){ echo "<tr>"; for($c=0; $c <= $cols; $r++){ echo "<td>$r</td>"; } echo "</tr>"; } echo '</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'
foreach loop not generating the right html table
I'm trying to get this output from returned data fetched from mysql: <table> <thead> <tr> <th></th><th><img src='img1.jpg'></th><th><img src='img2.jpg'></th> </tr> </thead> <tbody> <tr> <td colspan='5'>Features</td> <td>LCD</td><td>Yes</td><td>No</td> <td>Auto Pilot</td><td>No</td><td>No</td> <td>Fast Generation</td><td>Yes</td><td>No</td> <td>Dual Cores</td><td>No</td><td>Yes</td> </tr> </tbody> </table> But I have trouble getting the following code to achieve that output with one foreach loop. It uses in_array to check whether each value from $featured_tests exists in the returned data. $featured_tests = array("LCD","Auto Pilot","Fast Generation","Dual Cores"); $table_head ="<table><thead><tr><th></th>"; $table_colspan1 = "</tr></thead><tbody><tr><td colspan='5'>Features</td></tr>"; $table_end ="</tr></tbody></table>"; foreach($rows as $row) { $special_features = explode(",",$row->special_features); $header_image .= "<th><img src='".$row->image_url."'></th>"; foreach($featured_tests as $featured_test) { $featured .= "<tr><td>".$featured_test."</td>"; if(in_array($featured_test,$special_features)) { $featured .= "<td>Yes</td>"; } else { $featured .= "<td>No</td>"; } } } $table_html = $table_head.$header_image.$table_colspan1.$featured.$table_end; But the result I'm getting is a mess. Each value in $featured_tests is iterating over and over again for each product and thus results in a very long table. Can anyone help me correct my code to get the ideal output? Here's the result: <table> <thead> <tr> <th></th><th><img src='img1.jpg'></th><th><img src='img2.jpg'></th> </tr> </thead> <tbody> <tr> <td colspan='5'>Features</td> </tr> <tr> <td>LCD</td><td>Yes</td> </tr> <tr> <td>Auto Pilot</td>No<td></td> </tr> <tr> <td>Fast Generation</td><td>Yes</td> </tr> <tr> <td>Dual Cores</td>No<td> </tr> <tr> <td>LCD</td><td>No</td> </tr> <tr> <td>Auto Pilot</td>No<td></td> </tr> <tr> <td>Fast Generation</td><td>No</td> </tr> <tr> <td>Dual Cores</td>Yes<td> </tr> </tbody> </table>
you are creating new rows <tr> inside on most deep foreach... take this example to make what you want: <table> <thead> <th>Col 1</th><th>Col 2</th> </thead> <tbody> <?php foreach($large_array as $less_array): ?> <tr> <?php foreach($less_array as $row): ?> <!-- <td> contents etc</td>--> <?php endforeach?> </tr> <?php endforeach;?> </tbody> </table>