How can if statement be used within echo - php

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'

Related

particular Data not getting inserted in a column

I am facing a unknown problem while I go insert data.Some data getting inserted in same table n same column but except one.
The following is my code.
<html>
<body>
<?php
include("index.php");
?>
<form action="addcenter.php" method="post">
<table align="center" border="9" width="600">
<tr>
<td align="center" colspan="5" bgcolor="yellow">
<h1>Add Your Center Details</h1></td>
</tr>
<tr>
<th>District Name</th>
<td><input type="text" name="district"></td>
</tr>
<tr>
<th>Phone No.</th>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<th>Person's Name</th>
<td><input type="text" name="person"></td>
</tr>
<tr>
<th>Designation</th>
<td><input type="text" name="designation"></td>
</tr>
<tr>
<td align="center" colspan="5"><input type="submit" name="submit"
value="submit"></td>
</tr>
</table>
</form>
<?php
include("includes/connect.php");
if(isset($_POST['submit']))
{
$district=$_POST['district'];
$phone=$_POST['phone'];
$person=$_POST['person'];
$designation=$_POST['designation'];
if($district=='' or $phone=='' or $person=='' or $designation='')
{
echo"<script> alert('Please fill the fiels')</script> ";
exit();
}
$query="insert into centers (District,Phone,ContactPerson,Designation)
values('$district','$phone','$person','$designation') ";
if(mysql_query($query))
{
echo"<center><h1>Details Successfully added to your database</h1>
</center> ";
}
}
?>
<table width=" 900" align="center" border="3">
<tr>
<td align="center" colspan="9" bgcolor="orange"><h1>View all
Centers</h1></td>
</tr>
<tr align="center">
<th>SL No.</th>
<th>District</th>
<th>Phone No</th>
<th>Contact Person</th>
<th>Designation</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
$query="select * from centers";
$run=mysql_query($query);
$i=1;
while($row=(mysql_fetch_array($run)))
{
$center_id=$row['center_id'];
$district=$row['District'];
$phone=$row['Phone'];
$contact_person=$row['ContactPerson'];
$designation=$row['Designation'];
?>
<tr align="center">
<td><?php echo $i++ ;?></td>
<td><?php echo $district;?></td>
<td><?php echo $phone ;?></td>
<td><?php echo $contact_person ;?></td>
<td><?php echo $designation ;?></td>
<td>Edit
</td>
<td><input type="button" onclick="deleteme1(<?php echo $center_id ?>)"
name="delete" value="Delete"></td>
</tr>
<script language="javascript">
function deleteme1(delid1)
{
if(confirm("are u sure"))
{
window.location.href='deletecenter.php?del_id1='+delid1+'';
return true;
}
}
</script>
<?php } ?>
</table>
</body>
</html>
The problem is that when I going to insert the Contactperson,district,designation and phone no.Then only the data of Contactperson,district and phone no get inserted but not the designation..I dont know why this is happening even the coding is also right..Please help me. Thankyou
Your "if" statement is assigning '' to $designation. Use '==' for comparison.
if($district=='' or $phone=='' or $person=='' or $designation**=**'')
The if statement says, "if $district is '' or $phone is '' or $person is '' or 'I can assign nothing to $designation (which always succeeds)' - at which point you have successfully assigned '' to $designation. The if statement finishes and you insert '' into designation.

Create table with different PHP variables including if statement

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>";
?>

error on inputing multiple data to database based on session

so i create a shopping cart with session based and then it success input to database but the data inputted is wrong.
the incorrect data after i input on database is barang_id & kuantitas.
here's the code :
if (isset($_SESSION['items'])) {
foreach ($_SESSION['items'] as $key => $value) {
$barang_id = $_SESSION['items'][$key];
$kuantitas = $value;
$query_barang = mysqli_query($conn, "SELECT * FROM `barang` WHERE `id` = '$barang_id'");
// ambil data dari data barang
$rs_barang = mysqli_fetch_array($query_barang);
$harga = $rs_barang['harga'];
$jumlah_harga = $harga * $kuantitas;
$total += $jumlah_harga;
mysqli_query($conn, "INSERT INTO `item_penjualan` (`penjualan_id`, `barang_id`, `harga`, `kuantitas`, `jumlah_harga`) VALUES ('$penjualan_id', '$barang_id', '$harga', '$kuantitas', '$jumlah_harga')");
}
}
and here's the cart code :
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="viewer">
<tr>
<th align="left" scope="col">Kode Barang</th>
<th align="left" scope="col">Nama Barang</th>
<th align="right" scope="col">Harga</th>
<th align="right" scope="col">Qty</th>
<th align="right" scope="col">Jumlah Harga</th>
<th align="right" scope="col">Aksi</th>
</tr>
<?php
$total = 0;
mysqli_select_db($conn,$database_conn);
if (isset($_SESSION['items'])) {
foreach ($_SESSION['items'] as $key => $val) {
$query = mysqli_query($conn, "select barang.id, barang.nama, barang.harga from barang where barang.id = '$key'");
$rs = mysqli_fetch_array($query);
$jumlah_harga = $rs['harga'] * $val;
$total += $jumlah_harga;
?>
<tr>
<td><?php echo $rs['id']; ?></td>
<td><?php echo $rs['nama']; ?></td>
<td align="right"><?php echo number_format($rs['harga']); ?></td>
<td align="right"><?php echo number_format($val); ?></td>
<td align="right"><?php echo number_format($jumlah_harga); ?></td>
<td align="right">+ | - | Hapus</td>
</tr>
<?php
mysqli_free_result($query);
}
}
?>
<tr>
<td> </td>
<td> </td>
<td align="right"> </td>
<td align="right"> </td>
<td align="right"><?php echo number_format($total); ?></td>
<td align="right">Clear</td>
</tr>
<tr>
<td colspan="6">
<hr/>
Checkout
</td>
</tr>
</table>

Display image PHP MYSQLI use WHERE condition

I have a program that insert many records and image to a database. I must display my records and image into the web page (forms-output.php) but only 4 records and images. If I click the link (example: link in the ref_code) and records and image must display into a new web page (forms-full-data.php) but only records and image when I click what to display. Almost all work has been completed, but when I click a field in the ref_code all records have been successfully displayed except the images. Can you help me please?
This is forms-output.php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "profil";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo '
<div class="table-responsive">
<table class="table table-bordered table-striped mb-none" id="datatable-tabletools" data-swf-path="assets/vendor/jquery-datatables/extras/TableTools/swf/copy_csv_xls_pdf.swf">
<thead>
<tr align="center" bgcolor="#E9E9E9">
<td align="center" width="1"><b>No</td>
<td width="10%" align="center"><b>Photo</td>
<td width="100" align="center"><b>Ref Code</td>
<td width="100" align="center"><b>Name</td>
<td width="100" align="center"><b>Date</td>
</tr>
<tr>';
$no = 1; //inisialisasi untuk penomoran data
$sql= "SELECT image, name, ref_code, image, overseas, avaibility, sector, country, date, height, weight, religion, status, children, education, language, language2, experience1, experience2, experience3, experience4, experience5, other1, other2, other3, other4, other5, other6, working_experience, working_experience2, working_experience3, working_experience4, working_experience5 FROM tb_profil";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$gambar="<img src='images/" . $row["image"] . "' width='200'" . "' height='150' ></td><td align='center'>";
$data="<b>$row[name]<br><br>$row[ref_code]<br><br>$row[overseas]<br><br>$row[avaibility]<br><br>$row[sector]<br><br>$row[country]<br><br>$row[date]<br><br>$row[height]<br><br>$row[weight]<br><br>$row[religion]<br><br>$row[status]<br><br>$row[children]<br><br>$row[education]<br><br>$row[language]<br><br>$row[language2]<br><br>$row[experience1]<br><br>$row[experience2]<br><br>$row[experience3]<br><br>$row[experience4]<br><br>$row[experience5]<br><br>$row[other1]<br><br>$row[other2]<br><br>$row[other3]<br><br>$row[other4]<br><br>$row[other5]<br><br>$row[other6]<br><br>$row[working_experience]<br><br>$row[working_experience2]<br><br>$row[working_experience3]<br><br>$row[working_experience4]<br><br>$row[working_experience5]$row[image]";
echo "<tr><td align='center'>" .$no. "</td><td>$gambar<a href='forms-full-data.php?p=$data'>" . $row["ref_code"] . "</td><td align='center'>" . $row["name"] . "</td><td align='center'>" . $row["date"] . "</td></tr>";
$no++;
}
echo "</table>";
} else {
echo "0 results";
}
?>
This is forms-full-data.php:
<?php
// server info
$host = 'localhost';
$user = 'root';
$password = '';
$database_name = 'profil';
$mysqli = new mysqli($host, $user, $password, $database_name);
$res = $mysqli->query("SELECT * FROM tb_profil WHERE ref_code='" . $_GET['p']."'");
$row = $res->fetch_assoc();
$gambar="<img src='images/" . $row["image"] . "' width='150'" . "' height='200' >";
if (isset($_GET['p'])) {
echo "<table width='100%' border='0'>
<tr>
<td rowspan='31' width='20'>
<td width='180'>Full Name</td>
<td rowspan='15' width='230'> </td>
<td rowspan='31' width='200'>$_GET[p]</td>
<td rowspan='5' width='200'>$gambar</td>
</tr>
<tr>
<td>Ref Code</td>
</tr>
<tr>
<td>Overseas Experience</td>
</tr>
<tr>
<td>Avaiability</td>
</tr>
<tr>
<td>Sector</td>
</tr>
<tr>
<td>Country</td>
</tr>
<tr>
<td>Date Of Birth</td>
</tr>
<tr>
<td>Height</td>
<td></td>
</tr>
<tr>
<td>Weight</td>
<td rowspan='17'> </td>
</tr>
<tr>
<td>Religion</td>
</tr>
<tr>
<td>Marital Status</td>
</tr>
<tr>
<td>Children</td>
</tr>
<tr>
<td>Education</td>
</tr>
<tr>
<td>Language</td>
</tr>
<tr>
<td>More Language</td>
</tr>
<tr>
<td rowspan='5'>Experience</td>
<td width='10'>Care For Children</td>
</tr>
<tr>
<td>Cooking</td>
</tr>
<tr>
<td>Care For Infant</td>
</tr>
<tr>
<td>Care For Newborn</td>
</tr>
<tr>
<td>Care For Elderly</td>
</tr>
<tr>
<td rowspan='6'>Other Information</td>
<td>Able To Handle Pork?</td>
</tr>
<tr>
<td>Able To eat Pork?</td>
</tr>
<tr>
<td>Able to care for dog/cat?</td>
</tr>
<tr>
<td>Able to swim?</td>
</tr>
<tr>
<td>Willing to lock after elderly forbidden?</td>
</tr>
<tr>
<td>Willing to work with no off days?</td>
</tr>
<tr>
<td>Working Experience</td>
<td rowspan='5'></td>
</tr>
<tr>
<td>More Working Experience 2</td>
</tr>
<tr>
<td>More Working Experience 3</td>
</tr>
<tr>
<td>More Working Experience 4</td>
</tr>
<tr>
<td>More Working Experience 5</td>
</tr>
</table>";
}
?>
Add '/', before 'images' catalog, while You build path.
Please read about 'SQL injection".
Please use ADODB to manage DB.

Pulling last years data from a table

Hello I'm trying to update this scholarship application box. However when the year changed to 2013 it only displays scholarship applicant info from 2013. I'd like it to display info from 2012. I tried messing around with the date but I cant seem to figure it out. Any help would be greatly appreciated!
<?php
$appYear = date("Y").'-'.(date("Y")+1);
$sql = 'select * from sApplication where studentID = "'.$database->iPrep($_SESSION['ID']).'" AND appYear = "'.$appYear.'" LIMIT 1';
$appID = Scholarship::iFindSQL($sql);
$total = count($appID);
if ($total > 0)
{
$app = array_shift($appID);
}
else
{
$app = 0;
}
?>
<li id="item-2">
<div id="appStatus">
<h3>Application Status</h3>
<blockquote>
<?php if ($app->submitted == ('0000-00-00') || !isset($app->submitted)) { ?>
<table style="border:1px solid #000;" width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td width="50%"><strong>Scholarship<br /> 2013-2014</strong></td>
<td width="50" align="right"> <a style="font-size:16px;" href="welcome.php? app=Scholar">Apply Now</a></td>
</tr>
<tr>
<td><strong>Date Submitted</strong></td>
<td align="right"> </td>
</tr>
<tr>
<td><strong>References</strong></td>
<td align="right"> </td>
</tr>
<tr>
<td><strong>Decision</strong></td>
<td> </td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>
<tr>
<td><strong>Scholarship 2012-2013</strong></td>
<td> </td>
</tr>
<tr>
<td><strong>Decision</strong></td>
<td> </td>
</tr>
</table>
<?php } else { ?>
<table style="border:1px solid #000;" width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td width="90%"><strong>Scholarship 2013-2014</strong></td>
<td width="10%" align="right"> </td>
</tr>
<tr>
<td><strong>Date Submitted</strong></td>
<td align="right"><?=dbOutDate($app->submitted)?></td>
</tr>
<tr>
<td><strong>References</strong> </td>
<td align="right"></td>
</tr>
<tr>
<td colspan="2">
<?php
$refs = Reference::iFindSQL("Select * from reference where appID = '".$app->ID."'");?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php foreach($refs as $ref) { ?>
<tr> <td> <small><?php if($ref->rType == 'Academic Reference'){ echo 'Academic/Artistic/Professional'; } else { echo 'Community Service'; } ?></small></td> <td align="right"><?=$ref->status?></td></tr>
<?php } ?>
</table>
</td>
</tr>
<tr>
<td><strong>Decision</strong></td>
<td align="right">
<?php
if ($app->complete == 'Approved') { echo 'Approved'; }
if ($app->complete == 'Declined') { echo 'Declined'; }
if ($app->complete == 'Pending') { echo 'Pending'; }
if ($app->complete == 'Incomplete') { echo 'Incomplete'; }
Remove the WHERE clause that limits the year. Use ORDER BY to sort by year, descending.
$sql = 'select * from sApplication
where studentID = "'.$database->iPrep($_SESSION['ID']).
'" ORDER BY appYear DESC LIMIT 1';

Categories