Create table with different PHP variables including if statement - php

I have multiple quires retrieving data from mysql database (multiple tables)
SELECT description FROM table1 As descr WHERE type='MYTYPE'; //this shows description of each product type
SELECT count(id) FROM table2 As tp1 WHERE ACTIVE='Y' AND type=1"); //this is to count records that has this product type =1
SELECT count(id) FROM table2 As tp2 WHERE ACTIVE='Y' AND type=2");
SELECT count(id) FROM table2 As tp3 WHERE ACTIVE='Y' AND type=3");
SELECT count(id) FROM table2 As tp4 WHERE ACTIVE='Y' AND type=4");
What I'm trying to have is a table of each type description, along with count of active products has this type: ignoring the count if the count is 0
Here is modest try, I know it isn't perfect, therefore seeking help here, perhaps different approach will be also helpful, but this doesn't work as it have defining variable which has if statement and also logic is not correct?
$tbl_header = '<tr>
<th width=220px align=left>Type description</th>
<th width=10px align=left>Active products of this type</th>
</tr>';
$tbl_data =
if (tp1>0) {
'<tr>
<td width=220px align=left><font color=#000000>'. {descr[0][0]}.'</td></font>
<td width=10px align=left><font color=#FF0000>.'$tp1.'</td></font>
</tr>';}
if (tp2>0) {
'<tr>
<td width=220px align=left><font color=#000000>'. {descr[1][0]}.'</td></font>
<td width=10px align=left><font color=#FF0000>.'$tp2.'</td></font>
</tr>';}
;
echo "<table> $tbl_header $tbl_data </table>";

Try this method. It should work and you don't have to worry about if statements being in the middle of variable
<table>
<tr>
<th width=220px align=left>Type description</th>
<th width=10px align=left>Active products of this type</th>
</tr>
<?php
if ($tp1 > 0) {
?>
<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>
<?php echo descr[0][0]; ?>
</font>
</td>
<td width=10px align=left>
<font color=#FF0000>
<?php echo $tp1; ?>
</font>
</td>
</tr>
<?php
}
?>
<?php
if ($tp2 > 0) {
?>
<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>
<?php echo descr[1][0]; ?>
</font>
</td>
<td width=10px align=left>
<font color=#FF0000>
<?php echo $tp2; ?>
</font>
</td>
</tr>
<?php
}
?>
</table>
How about this?
<?php
$table_head = "<tr>
<th width=220px align=left>Type description</th>
<th width=10px align=left>Active products of this type</th>
</tr>";
if ($tp1 > 0) {
$table_data_1 = "<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>". descr[0][0] . "</font>
</td>
<td width=10px align=left>
<font color=#FF0000>". $tp1. "</font>
</td>
</tr>";
}
if ($tp2 > 0) {
$table_data_2 = "<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>". descr[1][0] . "</font>
</td>
<td width=10px align=left>
<font color=#FF0000>". $tp2. "</font>
</td>
</tr>";
}
echo "<table> $table_ head $table_data_1 $table_data_2 </table>";
?>
<?php
$table_data = "";
$table_head = "<tr>
<th width=220px align=left>Type description</th>
<th width=10px align=left>Active products of this type</th>
</tr>";
if ($tp1 > 0) {
$table_data = "<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>". descr[0][0] . "</font>
</td>
<td width=10px align=left>
<font color=#FF0000>". $tp1. "</font>
</td>
</tr>";
}
if ($tp2 > 0) {
$table_data .= "<tr>
<td width=2 20 px align=l eft>
< font color=# 000000>". descr[1][0] . "</font>
</td>
<td width=10px align=left>
<font color=#FF0000>". $tp2. "</font>
</td>
</tr>";
}
echo "<table> $table_ head $table_data </table>";
?>

Related

How to display multiple values with same ID php

I am creating profile of our users and I want to display their children details. Users data is stored in one table and their children data is stored in other.
For one userid there is more than one children detail.
Now, I want to display different children details with same userid (userid is muraqebeenID in children table).
How can I do that?
My DB look like this:
+-------------+-------------+-----------------+
| ID |muraqebeenId | childName |
+-------------+-------------+-----------------+
| 1 | 1 | John Doe |
+-------------+-------------------------------|
| 2 | 1 | Jane Doe |
+-------------|-------------|-----------------+
Here's my code:
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body >
<?php
//connect to DB
$con=mysqli_connect("localhost", "root", "", "idaramsb_newidara");
if ($con-> connect_error)
{
die("Connection Failed". $con-> connect_error);
}
//set page no.
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
//
$schools="";
if (isset($_GET['schoolId'])) {
$schools = "and tblusers.schoolId=".$_GET['schoolId'];
}
$no_of_records_per_page = 5;
if (isset($_GET['records'])) {
$no_of_records_per_page = $_GET['records'];
}
$offset = ($pageno-1) * $no_of_records_per_page;
$sql1="SELECT count(tblusers.userid) as total FROM tblusers
left JOIN tblschools on tblusers.schoolId=tblschools.schoolId
left JOIN jameastd on tblusers.jameaSanad=jameastd.jameasnd
where tblusers.status = 1 ".$schools." and tblusers.liveUser=1 and (tblusers.roleid = 3 or tblusers.roleid = 4 or tblusers.roleid = 17 or tblusers.roleid = 23 or tblusers.roleid = 27 or tblusers.roleid = 24)";
$totalp = 0;
$sql="SELECT tblschools.schoolId, tblschools.schoolName as scnm, jameastd.jameastd, YEAR(tblusers.dobGregorian) as dobGregoria, tblmuraqebeenswot.*, tblroles.roleid, tblroles.name, tblmuraqebeenchildrendetails.*, tblmuraqebeenspousedetails.*, tblusers.* FROM tblusers
left JOIN tblschools on tblusers.schoolId=tblschools.schoolId
left JOIN jameastd on tblusers.jameaSanad=jameastd.jameasnd
left join tblmuraqebeenswot on tblmuraqebeenswot.muraqebeenId=tblusers.userid
left join tblroles on tblroles.roleid=tblusers.roleId
LEFT JOIN tblmuraqebeenspousedetails on tblusers.userid=tblmuraqebeenspousedetails.muraqebeenId
left JOIN tblmuraqebeenchildrendetails on tblusers.userid=tblmuraqebeenchildrendetails.muraqebeenId
where tblusers.liveUser=1 and tblusers.status=1 and (tblusers.roleid = 3 or tblusers.roleid = 4 or tblusers.roleid = 17 or tblusers.roleid = 23 or tblusers.roleid = 27 or tblusers.roleid = 24) GROUP BY tblusers.userid ORDER by tblusers.roleid LIMIT $offset, $no_of_records_per_page";
$result=mysqli_query($con,$sql);
$result1=mysqli_query($con,$sql1);
$resultcheck=mysqli_num_rows($result);
$no_of_pages = ceil($totalp / $no_of_records_per_page);
$date = date('Y');
while ($rows = mysqli_fetch_assoc($result1)){
$totalp = $rows['total'];
}
while ($rows = mysqli_fetch_assoc($result)) {
$roq=$rows['dobGregoria'];
$age=$date - $roq;
echo "
<table border=1 class=headings id=content>
<tr>
<td colspan=3><span class=bold>Name: </span>".$rows['fullName']."</td>
<td width=200px><span class=bold>ITS ID: </span>".$rows['itsId']."</td>
<td rowspan=5 align=center width=160px><img width=140px src=https://www.idaramsb.net/newidara/assets/img/itsphoto.php?itsid=".$rows['itsId']."></td>
</tr>
<tr>
<td width=220px><span class=bold>Mauze: </span>".$rows['scnm']."</td>
<td width=><span class=bold>Muddat: </span></td>
<td><span class=bold>Age: </span>".$age."</td>
<td><span class=bold>Vatan: </span>".$rows['vatan']."</td>
</tr>
<tr>
<td colspan=><span class=bold>Taqarrur Year: </span>".$rows['taqarrurYear']."</td>
<td width=230px><span class=bold>Mobile No.: </span>".$rows['mobile']."</td>
<td colspan=2><span class=bold>Email: </span>".$rows['email']."</td>
</tr>
<tr>
<td><span class=bold>Jamea Farigh Year: </span>".$rows['farighYear']."</td>
<td><span class=bold>Designation: </span>".$rows['name']."</td>
<td colspan=2><span class=bold>Jamea Sanad: </span>".$rows['jameaSanad']."</td>
</tr>
<tr>
<td colspan=2><span class=bold>Qualification: </span>".$rows['education']."</td>
<td colspan=2><span class=bold>Hifz: </span>".$rows['hifzTill']."</td>
</tr>
<tr>
<td colspan=5 height=50px><span class=bold>Past Mawaze:</span></td>
</tr>
<tr>
<td colspan=2 valign=top height=300px><span class=bold>STRENGTHS</span><br>".$rows['strength']."</td>
<td colspan=3 valign=top><span class=bold>WEAKNESSES</span><br>".$rows['weakness']."</td>
</tr>
<tr>
<td colspan=2 valign=top height=300px><span class=bold>OPPURTUNITIES</span><br>".$rows['opportunities']."</td>
<td colspan=3 valign=top><span class=bold>THREATS</span><br>".$rows['threats']."</td>
</tr>
<tr>
<td colspan=5 align=center valign=top height=200px>
<span class=bold>Family Details</span>
<table height= width=1000px border=1>
<tr>
<td width=500px height=10px style='border-top:0.5px dotted;' align=center colspan=2>
<span class=top>Spouse Details</span><br>
</td>
<td width=500px style='border-left:0.5px dotted; border-top:0.5px dotted;'>
<div class=top>Children Details</div>
</td>
</tr>
<tr>
<td colspan=2>
Name: ".$rows['spouseName']."
</td>
<td colspan=2>
Name: ".$rows['childName']."
</td>
</tr>
<tr>
<td>
ITS ID: ".$rows['ejamatNo']."
</td>
<td>
Teaching in MSB?: ".$rows['teachinginMsb']."
</td>
<td>
ITS ID: ".$rows['ejamatNo']."
</td>
</tr>
<tr>
<td colspan=2>
Qualification: ".$rows['qualification']."
</td>
</tr>
<tr>
<td colspan=2>
Computer Skills: ".$rows['computerLiteracy']."
</td>
</tr>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan=5 align=center height= ><span class=bold>Personality Traits</span>
<iframe height=689px width=100% src=http://139.59.31.87/horizons/presults.php?itsid=".$rows['itsId']."&testid=1&sw> </iframe>
</td>
</tr>
</table>
<br>";
}
?>
</body>
</html>```
Maybe something like this helps:
SELECT * FROM `childTable` WHERE `muraqebeenId` = 1;

php add html element inside while loop

Hello here is my code :
<table class="table table-hover table-bordered">
<tr>
<th class="well" style="text-align:center">ID</th>
<th class="well" style="text-align:center">Email</th>
<th class="well" style="text-align:center">User level</th>
</tr>';
<?
$code_sql = "SELECT user_id, user_email,user_level FROM users ORDER BY user_id ASC ";
$code_query = mysql_query($code_sql) or die(error_sql(mysql_error(),__LINE__,__FILE__));
$sql_rows = mysql_num_rows($code_query);
if($sql_rows > 0){
while($rows = mysql_fetch_object($code_query)){
$user_id = intval($rows->user_id);
$user_level= intval($rows->user_level);
$user_email = htmlspecialchars($rows->user_email);
echo ' <tr>
<td>'.$user_id.'</td>
<td>'.$user_email.'</td>
<td>'.$user_level.'</td>
</tr>';
}
mysql_free_result($code_query);
}else{
echo '<tr>
<td>
<font color="red">no data found</font>
</td>
</tr>';
}
echo '</table>';
?>
the output of the code will be like .
<table class="table table-hover table-bordered">
<tr>
<th class="well" style="text-align:center">ID</th>
<th class="well" style="text-align:center">Email</th>
<th class="well" style="text-align:center">User level</th>
</tr>
<tr>
<td>1</td>
<td>test1#gmail.com</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>test2#gmail.com</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>test3#gmail.com</td>
<td>2</td>
</tr>
<tr>
<td>4</td>
<td>test4#gmail.com</td>
<td>1</td>
</tr>
</table>
(1 = normal user , and 2 = admin for user level) but what I want is something .like that
<table class="table table-hover table-bordered">
<tr>
<th class="well" style="text-align:center">ID</th>
<th class="well" style="text-align:center">Email</th>
<th class="well" style="text-align:center">User level</th>
</tr>
<div id="users">
<tr>
<td>1</td>
<td>test1#gmail.com</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>test4#gmail.com</td>
<td>1</td>
</tr>
</div>
<div id="admins">
<tr>
<td>2</td>
<td>test2#gmail.com</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>test3#gmail.com</td>
<td>2</td>
</tr>
</div>
</table>
I want to add a div id="users" that will contain all users from database that have user_level = 1 and another div id="admins" for user_level = 2.
try the following code, you need to create div's based on conditions like if the iteration number 1 then insert div(open) before content and after content(close)...and the if iteration number is 3 then again insert the div(open) before content an after content(close)
<table class="table table-hover table-bordered">
<tr>
<th class="well" style="text-align:center">ID</th>
<th class="well" style="text-align:center">Email</th>
<th class="well" style="text-align:center">User level</th>
</tr>
<?php
$code_sql = "SELECT user_id, user_email,user_level FROM users ORDER BY user_id ASC ";
$code_query = mysql_query($code_sql) or die(error_sql(mysql_error(),__LINE__,__FILE__));
$sql_rows = mysql_num_rows($code_query);
if($sql_rows > 0){
$i=0;
while($rows = mysql_fetch_object($code_query)){
$user_id = intval($rows->user_id);
$user_level= intval($rows->user_level);
$user_email = htmlspecialchars($rows->user_email);
if($i==0){?><div id='users'><?php }elseif($i==2){?><div id='admins'><?php }
echo ' <tr>
<td>'.$user_id.'</td>
<td>'.$user_email.'</td>
<td>'.$user_level.'</td>
</tr>';
if($i==0){?></div><?php }elseif($i==2){?></div><?php }
$i++;
}
mysql_free_result($code_query);
}else{
echo '<tr>
<td>
<font color="red">no data found</font>
</td>
</tr>';
}
echo '</table>';
?>
Try the below code with tbody to group table rows,
<?php
$code_sql = "SELECT user_id, user_email,user_level FROM users ORDER BY user_level ASC, user_id ASC ";
$code_query = mysql_query($code_sql) or die(error_sql(mysql_error(),__LINE__,__FILE__));
$sql_rows = mysql_num_rows($code_query);
if($sql_rows > 0){
$newgroup = false;
$groupid = array(1=>'users', 2=>'admins');
while($rows = mysql_fetch_object($code_query)){
$user_id = intval($rows->user_id);
$user_level= intval($rows->user_level);
$user_email = htmlspecialchars($rows->user_email);
if($newgroup != $user_level) {
if($newgroup != false) echo '</tbody>';
echo '<tbody id="'.$groupid[$user_level].'">';
$newgroup = $user_level;
}
echo ' <tr>
<td>'.$user_id.'</td>
<td>'.$user_email.'</td>
<td>'.$user_level.'</td>
</tr>';
}
echo '</tbody>';
mysql_free_result($code_query);
}else{
echo '<tr>
<td>
<font color="red">no data found</font>
</td>
</tr>';
}
echo '</table>';
?>
Note:
It would work if you just want to group table rows. But if you want to add additional features like animation then you might need to add more css/jQuery.
Additional suggestions:
1. You should start using mysqli or PDO
2. You should start using css to replace font tags.

How to change date into name

The code below works, but I want to change the months into names, such as "October", "November" and "December" using MONTHNAME. When I do, I get an error like the following:
JulyError in query: unknown column 'July' in 'where clause'.
What am I doing wrong?
<html>
<body>
<?php
$connection = mysql_connect('localhost','root','') or die ('unable to connect');
mysql_select_db("naurah")or die ("cannot select db");
$query = "SELECT YEAR(date) AS year, SUM(total) AS total FROM `order` GROUP BY year";
$result = mysql_query($query) or die ("Error in query:".mysql_error());
if (mysql_num_rows($result) > 0) {
while ($row=mysql_fetch_array($result)) {
echo $row[0];
$query1 = "SELECT * FROM `order` WHERE YEAR(date) = $row[0]";
$result1=mysql_query($query1) or die ("Error in query:".mysql_error());
if (mysql_num_rows($result1) > 0) {
echo '<table border="1" cellpadding="0" cellspacing="0" class="tg">
<strong>
<tr>
<th class="tg-031e">Order ID</th>
<th class="tg-031e" width="100">Date</th>
<th class="tg-031e">Name</th>
<th class="tg-031e">Email</th>
<th class="tg-031e" width="100">Telephone</th>
<th class="tg-031e">Address</th>
<th class="tg-031e">City</th>
<th class="tg-031e">Postcode</th>
<th class="tg-031e">State</th>
<th class="tg-031e">Reference No</th>
<th class="tg-031e">Total</th>
<th class="tg-031e">Order Status</th>
<th class="tg-031e">Postage</th>
<th class="tg-031e">Action</th>
</tr>
</strong>';
while ($row1 = mysql_fetch_array($result1)) {
echo "
<tr>
<td class=\"tg-4eph\" ><div align=\"center\"> $row1[0] </div></td>
<td class=\"tg-031e\" ><div align=\"center\"> $row1[1] </div></td>
<td class=\"tg-031e\" ><div align=\"center\">$row1[3] $row1[2]</div></td>
<td class=\"tg-4eph\" ><div align=\"center\"> $row1[4]</div></td>
<td class=\"tg-031e\" ><div align=\"center\"> $row1[5]</div></td>
<td class=\"tg-4eph\" ><div align=\"center\"> $row1[6]</div></td>
<td class=\"tg-031e\" ><div align=\"center\"> $row1[7]</div></td>
<td class=\"tg-4eph\" ><div align=\"center\"> $row1[8]</div></td>
<td class=\"tg-031e\"><div align=\"center\"> $row1[9]</div></td>
<td class=\"tg-031e\"><div align=\"center\"> $row1[10]</div></td>
<td class=\"tg-031e\"><div align=\"center\"> $row1[11]</div></td>
<td class=\"tg-031e\"><div align=\"center\"> $row1[12]</div></td>
<td class=\"tg-031e\"><div align=\"center\"> $row1[13]</div></td>
<td class=\"tg-4eph\" width=\"100\"><div align=\"center\">
<a href=\"adminupdatetrans.php?orderID= $row1[0]\">
<img src=\"../images/icn_edit_article.png\" title=\"Remove\"/>
</a>
<a href=\"admindeleteorder.php?orderID= $row1[0] \" onclick=\"return confirm('Are you sure want to delete ?')\">
<img src=\"../images/icn_trash.png\" title=\"Remove\"/>
</a>
</td>
</tr>
";
}
echo '
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td >Total All:</td>
';
echo '<td>RM';
echo $row[1];
echo '</td></tr>';
echo '</table>';
}
}
}
?>
</body>
</html>
Assuming that the date is in a timestamp format :),
You can get the names of months thanks to this method :
date("F",mktime(0,0,0,date('m', $timestamp),1,2011));Or like this :
date('F', strtotime("2015-date('m', $timestamp)-01"));

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'

using tcpdf to export current php file

i have a huge table in my php page that contains alot of php coding inside it
i want to export it using tcpdf
the example in samples of thier website prints html as a variable and export it like that
$tbl = <<<EOD
<table cellspacing="0" cellpadding="1" border="1">
<tr>
<td rowspan="3">COL 1 - ROW 1<br />COLSPAN 3</td>
<td>COL 2 - ROW 1</td>
<td>COL 3 - ROW 1</td>
</tr>
<tr>
<td rowspan="2">COL 2 - ROW 2 - COLSPAN 2<br />text line<br />text line<br />text line<br />text line</td>
<td>COL 3 - ROW 2</td>
</tr>
<tr>
<td>COL 3 - ROW 3</td>
</tr>
</table>
EOD;
my table has many many sql and php coding ,,,,etc
any php code i put inside it not appeared on output
how to print it?
and here is my table i want to print
<table class="table" width="95%">
<tbody>
<tr >
<td width="20" class="tabletop">م</td>
<td class="tabletop" >name</td>
<td class="tabletop" style="width:120px">date</td>
<td class="tabletop" style="width:120px">note1</td>
<td class="tabletop" style="width:100px">note2</td>
<td class="tabletop" style="width:90px">note3</td>
</tr>
<? $res=mysql_query($sql);
while($resArr=mysql_fetch_array($res)){ ?>
<tr style="width:700px">
<td class="tabletext"><?= ++$serial;?></td>
<td class="tabletext" ><?= $resArr[stName];?></td>
<td class="tabletext"><?= $resArr['date'];?></td>
<td class="tabletext" ><?= $resArr[matName];?></td>
<td class="tabletext" ><? if($resArr[exam]==1) echo "work";else echo "final";?></td>
<td class="tabletext" ><? if($resArr[exam_type]==1) echo "prac";else echo "test";?></td>
</tr>
<? }?>
</tbody>
</table>
<?php
$print = '<table class="table" width="95%">
<tbody>
<tr >
<td width="20" class="tabletop">م</td>
<td class="tabletop" >name</td>
<td class="tabletop" style="width:120px">date</td>
<td class="tabletop" style="width:120px">note1</td>
<td class="tabletop" style="width:100px">note2</td>
<td class="tabletop" style="width:90px">note3</td>
</tr>';
$res = mysql_query($sql);
while ( $resArr = mysql_fetch_array($res) ){
$print .= '
<tr style="width:700px">
<td class="tabletext">'.$serial.'</td>
<td class="tabletext" >'.$resArr['stName'].'></td>
<td class="tabletext">'.$resArr['date'].'></td>
<td class="tabletext" >'.$resArr['matName'].'></td>
<td class="tabletext">';
if($resArr['exam']==1){
$print .= 'work';
}else{
$print .= 'final';
}
$print .= '</td><td class="tabletext" >';
if($resArr['exam_type']==1){
$print .= "prac";
}else{
$print .= "test";
}
$print .= '</td></tr>';
}
$print .= '</tbody></table>';
then simply use the variable $print

Categories