how to show non matching mysql result - php

i m trying to create school fees recipt system
i create two table in phpmyadmin 1 is admission and 2nd is fees
1.admission
i create admission php form when i fill the admission form all detail save in admission table
2.fees
the same thing i create it in fees page
in admission table have all student info who are study in our school
and in fees table all info of student who paid the fees of the month
but now i want student fees report who are paid and who are not paid the fees
this code showing only paid student report
but i want both result who are paid and who are not paid how can i do this
please help me to fix this issue
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("student", $con);
echo "<div id='non-printable'><table class='sortable' border='1' cellpadding='10'>";
echo "<tr> <th>No</th> <th>Name</th><th>Date</th><th>GRN</th> <th>Reference</th><th>Class</th><th>Roll No</th><th>Fees</th></tr>";
// get results1 from database
$result1 = mysql_query("SELECT fees.id,fees.name,fees.date,fees.grn,fees.reference,fees.class,fees.rollno,fees.fees, admission.mothername "." FROM fees, admission ". " WHERE fees.name = admission.name AND fees.date BETWEEN '2013-04-01' AND '2013-04-14' AND fees.class='6' order by class ASC");
while($row = mysql_fetch_array($result1))
{
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['grn'] . '</td>';
echo '<td>' . $row['reference'] . '</td>';
echo '<td>' . $row['class'] . '</td>';
echo '<td>' . $row['rollno'] . '</td>';
echo '<td>' . $row['fees'] . '</td>';
echo '<td>' . $row['mothername'] . '</td>';
echo "</tr>";
//Increment the value of the Total_total variable
//by the salary value of one row till the while loop finishes
$Total_fees=$Total_fees+$row['fees'];
}
echo "<tr>";
echo '<td>Total</td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '<td>' . $Total_fees .'</td>';
echo "</tr>";
// close table>
echo "</table>";
mysql_close($con);
?>

UPDATED
<?php
echo "<div id='non-printable'><table class='sortable' border='1' cellpadding='10'>";
echo "<tr> <th>No</th> <th>Name</th><th>Date</th><th>GRN</th> <th>Reference</th><th>Class</th><th>Roll No</th><th>Fees</th></tr>";
$dbserver = 'localhost';
$dblogin = 'root';
$dbpassword = '';
$dbname = 'student';
//opening connection
$mysqli = new mysqli($dbserver, $dblogin, $dbpassword, $dbname);
if (mysqli_connect_errno())
{
printf("Connection failed: %s\n", mysqli_connect_error());
exit();
}
//opening connection
$result = $mysqli->query("SELECT `name`, `mothername` FROM `admission` WHERE `class` = '6' ORDER BY `name` ASC") or die($mysqli->error.__LINE__);
while($student = $result->fetch_assoc())
{
$subresult = $mysqli->query("SELECT * FROM `fees` WHERE `name` = '".$student['name']."' AND `date` BETWEEN '2013-04-01' AND '2013-04-14'") or die($mysqli->error.__LINE__);
if($row = $subresult->fetch_assoc())
{
echo "<tr>";
echo '<td>Student</td>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['grn'] . '</td>';
echo '<td>' . $row['reference'] . '</td>';
echo '<td>' . $row['class'] . '</td>';
echo '<td>' . $row['rollno'] . '</td>';
echo '<td>' . $row['fees'] . '</td>';
echo '<td>' . $student['mothername'] . '</td>';
echo "</tr>";
}
else
{
echo "<tr>";
echo '<td>' . $student['name'] . '</td>';
echo '<td> didn\'t pay any fee.</td>';
echo "</tr>";
}
}
echo '</table>';
mysqli_close($mysqli);
?>

$result1 = mysql_query("SELECT fees.id, fees.name, fees.date, fees.grn,
fees.reference, fees.class, fees.rollno, fees.fees, admission.mothername ".
"FROM admission LEFT JOIN fees ON fees.name = admission.name".
"WHERE fees.date BETWEEN '2013-04-01' AND '2013-04-14' AND fees.class='6'
order by class ASC");
Use the Left join so you can get all the rows of admission table and the corresponding rows
of fees table. In this way you can get the blank data of fee table also.

Related

Using checkbox in echo as table

I have a code where I echo a table:
<?php
// connect to the database
include('core/base.php');
// get results from database
$result = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM uitslag ORDER BY ID ASC")
or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Nummer</th><th>Naam</th><th>Telefoon</th><th>Binnen</th> <th>Adres</th> <th>Postcode</th> <th>Wijk</th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysqli_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['Naam'] . '</td>';
echo '<td>' . $row['Telefoon'] . '</td>';
echo '<td>' . $row['Binnen'] . '</td>';
echo '<td>' . $row['Adres'] . '</td>';
echo '<td>' . $row['Postcode'] . '</td>';
echo '<td>' . $row['Wijk'] . '</td>';
echo '<td>Aanpassen</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
The row 'Telefoon' either echos 1 or 0. How can I echo a checkbox which is checked when 1 instead of echoing the actual number?
Define input element of type checkbox.Like this.
$telefoon = ($row['Telefoon']==1)?'checked':'';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['Naam'] . '</td>';
echo "<td><input type='checkbox' $telefoon></td>";
echo '<td>' . $row['Binnen'] . '</td>';
echo '<td>' . $row['Adres'] . '</td>';
echo '<td>' . $row['Postcode'] . '</td>';
echo '<td>' . $row['Wijk'] . '</td>';
<?php
if($row['Telefoon']==1){
echo "<td><input type='checkbox' checked></td>";
}
else {
echo "<td><input type='checkbox' ></td>";
}
?>

Why is my ForEach loops only returning one line?

<!doctype html>
<?php
?>
<html>
<head>
<title>Midterm Review</title>
</head>
<body>
<h3>Tools Not Currently in Stock</h3>
<?php
$conn = mysqli_connect(This part works );
mysqli_select_db(this part works);
$query = "SELECT * FROM `midterm` WHERE stock='0'";
$result = mysqli_query($conn, $query);
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
foreach ($result as $row) {
$row = mysqli_fetch_array($result);
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
echo '</table>';
};
mysqli_close($conn);
?>
<!--<form method="post" action="midterm_confirmation.php">
<label>Part Number: </label><input type="text" name="partNumber" /><br />
<label>Description: </label><input type="text" name="description" /></br />
<label>Stock: </label><input type="number" name="stock" /><br />
<label>Price: </label><input type="text" name ="price" /></br />
<label>Received Date: </label><input type="text" name="receivedDate" /></br />
<input type="Submit" value="Add to Stock">
</form> -->
</body>
</html>
Basically my end result is that I am getting one table row, instead the two I am getting. Any suggestions? The table I have populates everything but is only running once, instead of posting for each line I have where stock is equal to zero.
See the snippet below. P.S. Don't mix templates and database layer, it's a bad smell...
<?php
// establish connection to $conn variable
$query = mysqli_query($conn, "SELECT * FROM `midterm` WHERE stock='0'");
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
while ($row = mysqli_fetch_assoc($query)) {
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
}
echo '</table>';
mysqli_close($conn);
?>
The problem is in closing of </table> It should be kept outside the loop.
while ($row = mysqli_fetch_assoc($query)) {
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
// echo '</table>';// this is wrong.
};
echo '</table>';// this is correct. closing table inside loop is wrong, do it outside the loop.
You can simply use a while loop to do it like below:
<?php
// establish connection to $conn
$query = mysqli_query($conn, "SELECT * FROM `midterm` WHERE stock='0'");
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
while ($row = mysqli_fetch_assoc($query)) {
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
}
echo '</table>'; // this should be outside the loop
mysqli_close($conn);
?>
Or if you want to use a foreach then write an extra function for it:
<?php
function mysql_fetch_all($result) {
$rows = array();
while ($row = mysql_fetch_array($result)) {
$rows[] = $row;
}
return $rows;
}
// establish connection to $conn
$query = mysqli_query($conn, "SELECT * FROM `midterm` WHERE stock='0'");
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
foreach (mysql_fetch_all($result) as $row){
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
}
echo '</table>'; // this should be outside the loop
mysqli_close($conn);
?>

How can I add a class for td in reference to two columns

I have a render of row of database
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['tex1'] . '</td>';
echo '<td>' . $row['tex2'] . '</td>';
echo '<td>' . $row['tex3'] . '</td>';
echo '<td>' . $row['tex4'] . '</td>';
echo '<td>' . $row['tex5'] . '</td>';
echo '<td>' . $row['tex6'] . '</td>';
echo '<td>' . $row['tex7'] . '</td>';
echo '<td>' . $row['tex8'] . '</td>';
echo '<td>' . $row['tex9'] . '</td>';
echo '<td>' . $row['tex10'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
And in the tex1 is a date, my problem is for a class for tr, when the tex2 is empty and the date of tex1 is older than 30 days, tr have class red, the date of tex1 is inserted manually,how can I do to change the class on the basis of this?
I'm not sure if I understood what you wanted properly but I'll give it a shot. You want to add a class ("red") to the , whenever the condition is met that either the date is older than 30 days old (tex1) or the First Name is empty (tex2).
echo "<table border='1' cellpadding='10'>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
// I assume since you're just printing it out, the date is a String
// So first convert it to a timestamp
$date = strtotime($row['tex1']);
// Determine the difference, I'm doing it this way to make it easy for you to understand
$days = 60*60*24*30; // 30 days
if (empty($row['tex2']) || time()-$date > $days) {
echo '<tr>';
} else {
echo '<tr class="red">';
}
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['tex1'] . '</td>';
echo '<td>' . $row['tex2'] . '</td>';
echo '<td>' . $row['tex3'] . '</td>';
echo '<td>' . $row['tex4'] . '</td>';
echo '<td>' . $row['tex5'] . '</td>';
echo '<td>' . $row['tex6'] . '</td>';
echo '<td>' . $row['tex7'] . '</td>';
echo '<td>' . $row['tex8'] . '</td>';
echo '<td>' . $row['tex9'] . '</td>';
echo '<td>' . $row['tex10'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
You should check for the conditions what you have mentioned in your question. And this can be done like this.
if(strtotime($row['tex1']) + 30 * 24 * 60 * 60 < time() && $row['tex2'] =="") {
$class="class='red'";
} else {
$class= "";
}
echo "<tr $class>";// id condition satisfies, class='red' will get echo, else echo will be null.
Try a conditional when writing the element:
echo '<tr'.
( ((strtotime($row['tex1']) < strtotime('30 days ago'))
&& (empty($row['tex2']))) ? ' class="red"' : '').
'>';

How to add BR between mysql record output

I have a PHP page where user can select a province (ex. A, B) then click search. The output will be coming from the database. Now how can I group the data and add BR in between? example:
user choose province: prov_A
output:
program name: FLOOD CONTROL-prov_A-ALLOCATION
FLOOD CONTROL-prov_A-ALLOCATION
ROADS-prov_A-ALLOCATION
ROADS-prov_A-ALLOCATION
I want to separate flood control from roads. Btw, I'm using Wordpress for this. A line break between will do. Also, the field 'allocation' from my excel file contain amounts like 7,000,000.00 or 1,000,000.00 but after importing to phpmyadmin it becomes 7.0 and 1.0. What data field should I use?
CODE:
$sql = "SELECT * FROM projects where province = '$province'";
//select database
mysql_select_db('pdmu');
//query for counting
$query="SELECT COUNT(proj_id) AS 'cnt' FROM `projects` WHERE province = '$province'";
$result=mysql_query($query);
$row2 = mysql_fetch_assoc($result, MYSQL_ASSOC);
print_r($row2['cnt']); print_r('Records Found');
//echo $row2['cnt'] . 'RECORDS FOUND!';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
echo "<table>";
echo "<tr>";
echo "<th>";
echo "PROGRAM NAME";
echo "</th>";
echo "<th>";
echo "PROVINCE";
echo "</th>";
echo "<th>";
echo "MUNICIPALITY/CITY";
echo "</th>";
echo "<th>";
echo "NO. OF PROJECT";
echo "</th>";
echo "<th>";
echo "PROJECT NAME";
echo "</th>";
echo "<th>";
echo "ALLOCATION";
echo "</th>";
echo "<th>";
echo "PHYSICAL STATUS";
echo "</th>";
echo "<th>";
echo "FINANCIAL STATUS";
echo "</th>";
echo "<th>";
echo "REMARKS";
echo "</th>";
echo "</tr>";
echo $count . $row2;
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo '<tr>' . '<td>' . $row['prog_name'] . '</td>' . '<td>' . $row['province'] . '</td>' . '<td>' . $row['municipality'] . '</td>' . '<td>' . $row['proj_no'] . '</td>' . '<td>' . $row['proj_name'] . '</td>' . '<td>' . $row['allocation'] . '</td>' . '<td>' . $row['ph_status'] . '</td>' . '<td>' . $row['fin_status'] . '</td>' . '<td>' . $row['remarks'] . '</td>' .'</tr>';}
mysql_close($conn);
<?php
$sql = "SELECT * FROM projects where province = '$province'";
//select database
mysql_select_db('pdmu');
//query for counting
$query="SELECT COUNT(proj_id) AS 'cnt' FROM `projects` WHERE province = '$province'";
$result=mysql_query($query);
?>
<?php $row = mysql_fetch_assoc($result);
while($row_result = mysql_fetch_array($retval, MYSQL_ASSOC))
{
?>
<tr>
<td><?=$row_result['your fields'];?></td>
</tr>
<?php } ?>

Display the result by selection each column from a html table using php

Is it possible to have filtering option in each column of html table in which the data is fetched from mysql as we do in excel sheet?
<html>
<?php
// connect to the database
include('connect-db.php');
$result = mysql_query("SELECT * FROM log WHERE type='c' ") ;
echo "<div style='height:auto;'><table border='0' cellpadding='10' class='table table-striped table-bordered'>";
echo "<tr> <th>ID</th> <th>Date</th> <th>Type</th> <th>Batch name</th> <th>No.of Pages</th> <th>User</th><th>file</th><th>Status</th><th></th><th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
$id=$row['batch_file'];
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['assdate'] . '</td>';
echo '<td>' . $row['type'] . '</td>';
echo '<td>'.substr($row['batch_file'], 0, -4).'</td>';
echo '<td>' . $row['no_pages'] . '</td>';
echo '<td>' . $row['assign'] . '</td>';
echo '<td><a href="copy_test_uploads/'.$row['batch_file'].'"target="_blank"><img src="pdf.png" style=width:35px;></td>';
echo '<td>' . $row['status'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table></div>";
?>
</html>

Categories