Goutte - Get table column - php

how can I get just one column not whole table?
<table cellspacing="0" cellpadding="0" align="Center" rules="all" border="1">
<tbody>
<tr>
<td>Entity Name</td>
<td>NV Business ID</td>
<td>Status</td>
<td>Type</td>
</tr>
<tr>
<td>GOOGLE</td>
<td></td>
<td>Expired</td>
<td>Reserved Name</td>
</tr>
<tr>
<td>GOOGLE INC.</td>
<td>NV20161275322</td>
<td>Active</td>
<td>Foreign Corporation
</td>
</tr>
</tbody>
</table>
Here is my try:
$client = new Client();
$crawler = $client->request('GET', 'url');
$form = $crawler->selectButton('Search')->form();
$crawler = $client->submit($form, array(
...
));
$crawler->filter('table tr')->each(function ($node) {
print $node->text()."\n \n";
// print $node->filter('td')->text() . '<br />';
});
It's always returning whole table.
Also tried stn like tr[1], etc..
Can someone help please?
Thanks

I found a solution:
$node->filter('td')->eq(2)->text();
2 means third column, because it's [0,1,2,...]

You can use DOMDocument for getting data from HTML.
PHP code demo
<?php
ini_set("display_errors", 1);
$html = '<table cellspacing="0" cellpadding="0" align="Center" rules="all" border="1">
<tbody>
<tr>
<td>Entity Name</td>
<td>NV Business ID</td>
<td>Status</td>
<td>Type</td>
</tr>
<tr>
<td>GOOGLE</td>
<td></td>
<td>Expired</td>
<td>Reserved Name</td>
</tr>
<tr>
<td>GOOGLE INC.</td>
<td>NV20161275322</td>
<td>Active</td>
<td>Foreign Corporation
</td>
</tr>
</tbody>
</table>';
$result=array();
$object= new DOMDocument();
$object->loadHTML($html);
$requiredColumn=3;
$requiredColumn--;
foreach($object->getElementsByTagName("tr") as $value)
{
$nodelistObject=$value->getElementsByTagName("td");
$columnCounter=0;
foreach($nodelistObject as $tdNode)
{
if($columnCounter==$requiredColumn)
{
if($tdNode->getElementsByTagName("a")->length==0)
{
$result[]=$tdNode->textContent;
}
foreach($tdNode->getElementsByTagName("a") as $aElement)
{
$result[]=$aElement->textContent;
}
}
$columnCounter++;
}
}
print_r($result);

Try the below code:
$content = $crawler->filter( 'table' )->extract( array( '_text' ) );

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>

Print HTML dynamically on a page via PHP based on Condition

I want to print a section of a table if the data is available in JSON file for it. And if the data is not there and it shouldn't do anything.
So, far what I am doing is as below. Checking is the account_type is parent and then generate the table rows with corresponding data.
<?php
$str = file_get_contents('test.json');
$json = json_decode($str, true);
$parent= strtr('<tr>
<td colspan="2" style="color:#263a80;font-size:19px;padding-bottom:20px;padding-top:20px">Parent Information</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">First Name</td>
<td>#fname</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Last Name</td>
<td>#lname</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Mobile Number</td>
<td>#mobile</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Email</td>
<td><a #email target="_blank" rel="noreferrer">#email</a></td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Country</td>
<td>#country</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">State</td>
<td>#state</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">City</td>
<td>#city</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Postal Code</td>
<td>#pcode</td>
</tr>
<tr>
<td style="width:30%;font-weight:700;padding-bottom:10px">Full Home Address</td>
<td>#address</td>
</tr>', ["#fname"=>$json["json"]["register_session_1"]["first_name"],
"#lname"=>$json["json"]["register_session_1"]["last_name"],
"#mobile"=>$json["json"]["register_session_1"]["mobile"],
"#email"=>$json["json"]["register_session_1"]["email"],
"#country"=>$json["json"]["register_session_1"]["country"],
"#state"=>$json["json"]["register_session_1"]["state_online"],
"#city"=>$json["json"]["register_session_1"]["city"],
"#pcode"=>$json["json"]["register_session_1"]["postal_code"],
"#address"=>$json["json"]["register_session_1"]["address"]
]);
if ($json["json"]["register_session_1"]["account_type"]==="parent"){
echo $parent;
}
else {
echo "hahahah";
}
?>
Problem is that it printing the table data anyway. I don't know what I am doing wrong. The if-else condition is not working.
I have this code and I can't seem to figure out why. I have added this code in between the <table></table> tag.

PHP TCPDF heredoc

I got this in my heredoc in my TCPDF.
I basically want to create a dynamic pdf with the data of my database.
$html = <<<EOD
<table border="1">
<thead>
<tr>
<th>firstname</th>
<th>lastname<th>
</tr>
</thead>
<tbody>
<tr>
<th></th>
<th></th>
</tr>
</tbody>
</table>
EOD;
I want to make this dynamic like this, to create the data from my database dynamically.
<?php
foreach($result_set as $result) {
?>
<tr>
<td>
<?php echo $result['firstname']; ?>
</td>
<td>
<?php echo $result['lastname']; ?>
</td>
</tr>
<?php
}
?>
I tried this so far but I cannot find a suitable solution:
$html = <<<EOD
<table border="1">
<thead>
<tr>
<th>Vorname</th>
<th>Nachname</th>
<th>Von</th>
<th>Bis</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<?php echo $result['firstname']; ?>
</td>
<td>
<?php echo $result['lastname']; ?>
</td>
</tr>
</tbody>
</table>
EOD;
can anyone help please.
Ok I somehow did it, but still if there is any better solution feel free to post:)
Code:
$loopHereFirstname = '';
$loopHereLastname = '';
foreach($result_set_random_01 as $result_dish_usr_01) {
$tr_start = '<tr>';
$tr_end = '</tr>';
$td_start = '<td>';
$td_end = '</td>';
$loopHereFirstname .= $result_dish_usr_01['firstname']."\n";
$loopHereLastname .= $result_dish_usr_01['lastname']."\n";
}
$html = <<<EOD
<table border="1">
<thead>
<tr>
<th>firstname</th>
<th>lastname</th>
</tr>
</thead>
<tbody>
$tr_start
$td_start
$loopHereFirstname
$td_end
$td_start
$loopHereLastname
$td_end
$tr_end
</tbody>
</table>
EOD;
You've to break your code into two part if you want to use a loop with <<
<?php
$html =<<<EOD
<table border="1">
<thead>
<tr>
<th>Vorname</th>
<th>Nachname</th>
<th>Von</th>
<th>Bis</th>
</tr>
</thead>
<tbody>
EOD;
foreach($result_set as $result) {
$html.=<<<EOD
<tr>
<td>
{$result['firstname']}
</td>
<td>
{$result['lastname']}
</td>
</tr>
EOD;
}
$html .= '</tbody></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>

Categories