making two tables depending if they have 0 or 1 - php

essentially I want to have two tables one with everyone with Confirmed = 1 and the other with Confirmed = 0.
I have this sql query at the moment
$result = mysqli_query($con,"SELECT * FROM tbl_booking WHERE $row[Confirmed] = 1");
but I keep getting
Notice: Undefined variable: row in C:\xampp\htdocs\test.php on line 19
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in
rest of the code for that page
echo "<table border='1'>
<tr>
<th>ID</th>
<th>BookID</th>
<th>date</th>
<th>time</th>
<th>confirmed?</th>
</tr>";
while($row = mysqli_fetch_array($result) )
{ ?>
<tr>
<td> <?php echo $row['tbl_mem_id']; ?> </td>
<td> <?php echo $row['BookID']; ?> </td>
<td> <?php echo $row['date']; ?> </td>
<td> <?php echo $row['time']; ?> </td>
<td> <?php echo $row['Confirmed']; ?> </td>
<td> <a href=delete.php?BookID=<?php echo $row['BookID']; ?> </a> Delete </td>
<td>
<?php
if ($row['Confirmed'] == 0) { ?>
<a href=confirm.php?BookID=<?php echo $row['BookID']; ?> </a> Confirm </td> <?php } else { ?>
<a href=deny.php?BookID=<?php echo $row['BookID']; ?> </a> Deny </td> <?php
} ?>
</tr> <?php
} ?>
</table>

Are you really trying to put the value in $row['Confirmed'] in the query, or are you just trying to query a field named Confirmed? if its the latter try this:
$result = mysqli_query($con,"SELECT * FROM tbl_booking WHERE Confirmed= 1");
but since also you seem to differentiate the table in php with if ($row['Confirmed'] == 0) you probably need to get all data in one query with this:
$result = mysqli_query($con,"SELECT * FROM tbl_booking");
this won't give you 2 tables as you initially asked for though.
you can create 2 html tables with this:
<?php
$result = mysqli_query($con,"SELECT * FROM tbl_booking WHERE Confirmed = 1");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>BookID</th>
<th>date</th>
<th>time</th>
<th>confirmed?</th>
</tr>";
while($row = mysqli_fetch_array($result) )
{ ?>
<tr>
<td> <?php echo $row['tbl_mem_id']; ?> </td>
<td> <?php echo $row['BookID']; ?> </td>
<td> <?php echo $row['date']; ?> </td>
<td> <?php echo $row['time']; ?> </td>
<td> <?php echo $row['Confirmed']; ?> </td>
<td> <a href=delete.php?BookID=<?php echo $row['BookID']; ?> </a> Delete </td>
<td>
<a href=deny.php?BookID=<?php echo $row['BookID']; ?> </a> Deny </td>
</tr> <?php
} ?>
</table>
<?php
echo "<table border='1'>
<tr>
<th>ID</th>
<th>BookID</th>
<th>date</th>
<th>time</th>
<th>confirmed?</th>
</tr>";
$result = mysqli_query($con,"SELECT * FROM tbl_booking WHERE Confirmed = 0 ");
while($row = mysqli_fetch_array($result) )
{ ?>
<tr>
<td> <?php echo $row['tbl_mem_id']; ?> </td>
<td> <?php echo $row['BookID']; ?> </td>
<td> <?php echo $row['date']; ?> </td>
<td> <?php echo $row['time']; ?> </td>
<td> <?php echo $row['Confirmed']; ?> </td>
<td> <a href=delete.php?BookID=<?php echo $row['BookID']; ?> </a> Delete </td>
<td>
<a href=confirm.php?BookID=<?php echo $row['BookID']; ?> </a> Confirm </td>
</tr> <?php
} ?>
</table>

Related

I want to delete multiple images from source folder through PHP but the code for deleting the multiple images from the folder is not working

<?php include("header.php"); ?>
<?php
if (#$_POST['delete']=="Delete"){
$count=count($_POST['delbx']);
for($i=0;$i<$count;$i++){
$delete = "DELETE FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
$resulty = mysqli_query($conn, $delete) or die(mysql_error());
$select_delete = "SELECT `a_image` FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
$resultrowdy = $conn->query($select_delete);
$rowdy = $resultrowdy->fetch_assoc();
$path="admin/".$rowdy['a_image'];
echo $path;
unlink($path);
echo '<script>window.location="view_user.php"</script>';
}
} ?>
<div class="table-responsive">
<table class="table">
<caption>All Users</caption>
<?php
$sql = "SELECT a_id, a_name, a_phone, a_password, a_role, a_mail, a_image FROM admin";
$result = $conn->query($sql);
if ($result->num_rows > 0) {?>
<thead>
<tr>
<th><form action="view_user.php" method="post"><input name="delete" type="submit" id="delete" value="Delete"></th><th>S. No.</th> <th>Name</th> <th>Phone No.</th> <th>Mail Id</th> <th>Role</th> <th>Password</th> <th>Image</th>
</tr>
</thead>
<?php
while($row = $result->fetch_assoc()) { ?>
<tbody>
<tr>
<th scope="row">
<?php echo $row["a_id"]; ?>
</th>
<td align="center" bgcolor="#FFFFFF">
<input name="delbx[]" type="checkbox" id="delbx[]" value="<?php echo $row["a_id"]; ?>" />
</td>
<td>
<?php echo $row["a_name"]; ?>
</td>
<td>
<?php echo $row["a_phone"]; ?>
</td>
<td>
<?php echo $row["a_mail"]; ?>
</td>
<td>
<?php echo $row["a_role"]; ?>
</td>
<td>
<?php echo $row["a_password"]; ?>
</td>
<td>
<img src="admin/<?php echo $row["a_image"]; ?>" width="60" height="40">
</td>
<th>
Edit
</th>
</tr>
</tbody>
<?php
}
} else {
echo "0 results";
}?>
</table>
</form>
</div>
<?php include("footer.php"); ?>
The code I mention is not deleting the multiple images from the source folder but deleting the multiple data from database whereas I am trying to delete images from the source folder along with data please help thanks in advance
One of the problem is you are deleting the row and trying to select image column from the deleted row.. dont use user supplied variables directly in your query
your code should be
for($i=0;$i<$count;$i++){
$select_delete = "SELECT `a_image` FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
$resultrowdy = $conn->query($select_delete);
$rowdy = $resultrowdy->fetch_assoc();
$delete = "DELETE FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
if(mysqli_query($conn, $delete)){
$path="admin/".$rowdy['a_image'];
unlink($path);
echo '<script>window.location="view_user.php"</script>';
}
}

Difference between two dates in codeigniter

abscondingsince is a date field with value 2016-08-24 and dateofcontactviaphone is a date field with value 2016-08-26 . when I try to echo timespan($row['absconding since'], $row['dateofcontactviaphone']); its giving 2016-08-2446 Years, 8 Months, 3 Days, 5 Hours, 43 Minutes as the output whereas it should have been 2 days and so on. I have no clue where I am making a mistake. My view code is as below:
<table cellpadding="10px" border="1" style="background:none;width:75%;" RULES="ROWS" class="tab_data">
<thead>
<th>Employee Name</th>
<th>Absconding Since</th>
<th>Absconding Days</th>
<th>Phone Log</th>
<th>Comments on call made</th>
<th>Date of Email Sent</th>
<th>Comments/responses to email recorded</th>
<th>View Absconding Checklist</th>
</thead>
<tbody>
<?php
$serial_no = 1;
$i = 0;
foreach($rows as $row){
$i++;
?>
<tr>
<td style="width:10%">
<?php echo $row['name']; ?>
</td>
</td>
<td style="width:10%">
<?php echo $row['abscondingsince'];
echo timespan($row['abscondingsince'], $row['dateofcontactviaphone']);
?>
</td>
<td style="width:10%">
<?php echo $row['dateofcontactviaphone']; ?>
</td>
<td style="width:10%">
<?php echo $row['commentsphone']; ?>
</td>
<td style="width:10%">
<?php echo $row['dateofcontactviaemail']; ?>
</td>
<td style="width:10%">
<?php echo $row['commentsemail']; ?>
</td>
<td style="width:10%">
<?php
if ($row['last_status'] == 'Accepted'){
echo anchor('exits/view_exit_checklist/'.$row['id'],"<i class='fa fa-eye edit_row' alt='View' title='Edit' rel='".$row['id']."' ></i>",array('rel'=>$row->id,'class'=>'edit_row'));
}
else
{
echo "NA";
}
?>
</td>
</tr>
<?php
} ?>
</tbody>
</table>
Use like this:-
echo timespan(strtotime($row['absconding since']),
strtotime($row['dateofcontactviaphone']));

Table is not returning values

I want to print out my database into a table, but it isn't working properly.
Here's my code:
<?php
$title = 'Adminpaneel | Gemeente Loket Den Haag';
require 'header.php';
require 'connect.php';
$query = "SELECT * FROM aanvraag";
$result = mysqli_query($db, $query);
?>
<table border="2" >
<thead>
<tr>
<th>Voornaam</th>
<th>Achternaam</th>
<th>email</th>
<th>datum</th>
<th>adres</th>
<td>huisnummer</td>
<td>Postcode</td>
<td>Woonplaats</td>
<td>Product</td>
</tr>
</thead>
<tbody>
<?php
while( $row = mysqli_fetch_assoc($result) ){ ?>
<tr>
<td> <php echo $row['voornaam']; ?> </td>
<td> <php echo $row['achternaam']; ?> </td>
<td> <php echo $row['email']; ?> </td>
<td> <php echo $row['datum']; ?> </td>
<td> <php echo $row['adres']; ?> </td>
<td> <php echo $row['huisnr']; ?> </td>
<td> <php echo $row['postcode']; ?> </td>
<td> <php echo $row['woonplaats']; ?> </td>
<td> <php echo $row['product']; ?> </td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
require 'footer.aanvragen.php';
?>
The rows are created but the database values are missing. What seems to be the issue?
This is the table structure:
Try with this code for the html table:
<?php
while( $row = mysqli_fetch_assoc($result) ){ ?>
<tr>
<td> <?php echo $row['voornaam']; ?> </td>
<td> <?php echo $row['achternaam']; ?> </td>
<td> <?php echo $row['email']; ?> </td>
<td> <?php echo $row['datum']; ?> </td>
<td> <?php echo $row['adres']; ?> </td>
<td> <?php echo $row['huisnr']; ?> </td>
<td> <?php echo $row['postcode']; ?> </td>
<td> <?php echo $row['woonplaats']; ?> </td>
<td> <?php echo $row['product']; ?> </td>
</tr>
<?php } ?>
as suggested by Abhik Chakraborty
<td> <php echo $row['voornaam']; ?> </td>
One line of the output (but it is the same for all the lines):
PHP needs to start with
So the solution will be:
<td><?php echo $row['voornaam']; ?> </td>
But for all the rows...

looping database results using MySQLi

I am trying to pull a list of registration information from MySQL. I'm changing the query over to object oriented and when I did this, the results are not being displayed inside the table. No error messages are being displayed.
<?php
$sql = "SELECT * FROM `prereg`";
if (!$stmt = $mysqli->query("SELECT * FROM prereg")) {
echo "Query Failed!: (" . $mysqli->errno . ") ". $mysqli->error;
}else{
while ($row = mysqli_fetch_assoc()) {
?>
<tr>
<!-- <td>
<?php echo $row['UID']; ?>
</td> -->
<td>
<?php echo $row['Guradian']; ?>
</td>
<td>
<?php echo $row['Number']; ?>
</td>
<td>
<?php echo $row['Phone']; ?>
</td>
<td>
<?php echo $row['Name']; ?>
</td>
<!-- <td>
<?php echo $row['Address']; ?>
</td> -->
<td>
<?php echo $row['City']; ?>
</td>
<td>
<?php echo $row['State']; ?>
</td>
<!-- <td>
<?php echo $row['Zip']; ?>
</td>
<td>
<?php print $row['Make']; ?>
</td>
<td>
<?php print $row['Ms']; ?>
</td> -->
<td>
<?php print $row['Fuel']; ?>
</td>
<td>
<?php echo $row['Class1']; ?>
</td>
<td>
<?php print $row['Class2']; ?>
</td>
<td>
<?php print $row['Class3']; ?>
</td>
</tr>
<?php } }
$stmt->free(); ?>
What am I doing wrong? I've searched and searched and I cannot find anything that fixes the problem.
REVISED:
<?php
$stmt = $mysqli->query("SELECT * FROM prereg");
if($stmt->num_rows > 0):
while ($row = $stmt->fetch_assoc()):
?>
<tr>
<!-- <td>
<?php echo $row['UID']; ?>
</td> -->
<td>
<?php echo $row['Guradian']; ?>
</td>
<td>
<?php echo $row['Number']; ?>
</td>
<td>
<?php echo $row['Phone']; ?>
</td>
<td>
<?php echo $row['Name']; ?>
</td>
<!-- <td>
<?php echo $row['Address']; ?>
</td> -->
<td>
<?php echo $row['City']; ?>
</td>
<td>
<?php echo $row['State']; ?>
</td>
<!-- <td>
<?php echo $row['Zip']; ?>
</td>
<td>
<?php print $row['Make']; ?>
</td>
<td>
<?php print $row['Ms']; ?>
</td> -->
<td>
<?php print $row['Fuel']; ?>
</td>
<td>
<?php echo $row['Class1']; ?>
</td>
<td>
<?php print $row['Class2']; ?>
</td>
<td>
<?php print $row['Class3']; ?>
</td>
</tr>
<?php endwhile; endif; ?>
</table>
<?php
$stmt = $mysqli->query("SELECT * FROM prereg");
if($stmt->num_rows > 0): ?>
<table>
<?php while($row = $stmt->fetch_assoc()): ?>
<tr>
<?php foreach($row as $val): ?>
<td><?php echo $val; ?></td>
<?php endforeach; ?>
</tr>
<?php endwhile; ?>
</table>
<?php endif; ?>
</center>
when trying to retrofit the code you have provided with the table I already had, the code you gave works, the table I have still wont populate.
If you are using the procedural interface mysqli_fetch_assoc(), you need to provide the mysqli_result.
while ($row = mysqli_fetch_assoc($stmt)) {
If you'd use the object oriented interface, then just use it as its method:
while($row = $stmt->fetch_assoc()) {
Revised:
<?php
$mysqli = new mysqli('localhost', 'username', 'password', 'database_name');
$stmt = $mysqli->query('SELECT * FROM prereg');
?>
<?php if($stmt->num_rows > 0): ?>
<table>
<?php while($row = $stmt->fetch_assoc()): ?>
<tr>
<?php foreach($row as $val): ?>
<td><?php echo $val; ?></td>
<?php endforeach; ?>
</tr>
<?php endwhile; ?>
</table>
<?php else :?>
<p>Table empty</p>
<?php endif; ?>

Responsive Table in Bootstrap with columns with same width

This is the code, and as written in the title, I'm trying to have each column with the same width:
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>N°</th>
<th>Data</th>
<th>Compratore</th>
<th>Venditore</th>
<th>Merce</th>
<th>Quantita'</th>
<th>Prezzo</th>
<th>Pagamento</th>
<th>Azioni</th>
</tr>
</thead>
<tbody>
<?php
$result = query("SELECT numero_contratto, anno_contratto, data, C.denominazione as compratore, V.denominazione as venditore, merce, quantita, info_quantita, prezzo, info_prezzo, pagamento FROM Contratti, Societa V, Societa C WHERE compratore = C.id_societa and venditore = V.id_societa ORDER BY data DESC", $conn);
foreach($result as $r)
{
?>
<tr>
<td>
<?php echo $r['numero_contratto']."/".$r['anno_contratto']; ?>
</td>
<td>
<?php
$data = explode("-", $r['data']);
echo $data[2]."-".$data[1]."-".$data[0];
?>
</td>
<td>
<?php echo $r['compratore']; ?>
</td>
<td>
<?php echo $r['venditore']; ?>
</td>
<td>
<?php echo $r['merce']; ?>
</td>
<td>
<?php echo $r['quantita']." ".$r['info_quantita']; ?>
</td>
<td>
<?php echo $r['prezzo']." ".$r['info_prezzo']; ?>
</td>
<td>
<?php echo $r['pagamento']; ?>
</td>
<td>
<span class="glyphicon glyphicon-remove"></span>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
And this is the output on my web page:
But if you see this Fiddle with static values putted into the table, the problem doesn't show!
http://jsfiddle.net/brVa9/1/
So the problem is inside the db schema or structure.
What I should do to have all the columns with the same width?

Categories