Table is not returning values - php

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...

Related

Pull and update data from database in PHP CodeIgniter foreach loop

I have a view which contains the data which is displayed from the database by a foreach loop and i want is to get the data which is edited from the view and update it in the database one by one but the problem is that database will be updated if there is a where clause but in my case i don't know how to get the key(tid) from view to controller here is the controller function which displays the data
public function bulk()
{
$pid = '';
if($this->input->post('pid'))
$pid = $this->input->post('pid');
$template['menu'] = $this->shared_model->get_flightmenus();
if($pid == '')
{
if($this->uri->segment(3))
$pid = $this->uri->segment(3);
else
$pid = $template['menu'][0]['tid'];
}
$template['pid'] = $pid;
// echo $pid;
// exit();
$template['airline'] = $this->shared_model->get_records('eat_airline');
$template['terminal'] = $this->shared_model->get_records('eat_terminal');
$template['data'] = $this->shared_model->get_recordbyvalue('eat_flight','menu_tid = '.$pid);
$template['main_content'] = $this->load->view('flight/flight_bulk_view', $template, true);
$this->load->view('includes/template', $template);
}
here is the view
<h4><?php echo anchor('login/logout', 'Logout'); ?></h4>
<?php echo anchor('/home', 'Home'); ?>/Flights
<h2>Flights</h2>
<div>
<?php echo form_open('/flight/') ?>
<?php foreach($menu as $row){ $options[$row['tid']] = $row['m_name']; } ?>
<?php $js = 'name="pid" id="pid" onChange="this.form.submit();"'; ?>
<?php echo form_dropdown('pid', $options, $pid, $js); ?>
<?php echo form_close(); ?>
</div>
<?php echo anchor('/flight/display/1/0/'.$pid, 'Create'); ?>
<?php echo anchor('flight/bulk', 'Bulk Update'); ?>
<table>
<tr>
<td>
Name
</td>
<td>
Airline
</td>
<td>
From
</td>
<td>
Stopover
</td>
<td>
Destination
</td>
<td>
Price
</td>
<td>
Unit
</td>
<td>
Tax
</td>
<td>
Protection
</td>
<td>
Fare
</td>
<td>
Week Offer
</td>
<td>
Menu
</td>
<td>
Sort
</td>
<td>
Edit
</td>
<td>
Delete
</td>
</tr>
<?php foreach($data as $row){ ?>
<tr>
<td>
<?php echo $row['f_name'] ?>
</td>
<td>
<?php $options1[0] = ''; foreach($airline as $row1){ $options[$row1['tid']] = $row1['a_name']; } ?>
<?php echo $options[$row['airline_tid']]; ?>
</td>
<td>
<?php $options1[0] = ''; foreach($terminal as $row1){ $options[$row1['tid']] = $row1['t_name']; } ?>
<?php echo $options[$row['terminal_from_tid']]; ?>
</td>
<td>
<?php echo $row['t_stopover'] ?>
</td>
<td>
<?php $options1[0] = ''; foreach($terminal as $row1){ $options[$row1['tid']] = $row1['t_name']; } ?>
<?php echo $options[$row['terminal_destination_tid']]; ?>
</td>
<td>
<?php echo $row['f_price'] ?>
</td>
<td>
<?php echo $row['f_unit'] ?>
</td>
<td>
<?php if($row['f_tax']>0) echo 'Yes'; else echo 'No'; ?>
</td>
<td>
<?php echo $row['f_protection'] ?>
</td>
<td>
<?php echo $row['f_fare'] ?>
</td>
<td>
<?php if($row['f_weekoffer']>0) echo 'Yes'; else echo 'No'; ?>
</td>
<!--
<td>
<?php $options[0] = ''; foreach($menu as $row1){ $options[$row1['tid']] = $row1['m_name']; } ?>
<?php echo $options[$row['menu_tid']]; ?>
</td>
-->
<td>
<?php echo $row['sortindex'] ?>
</td>
<td>
<?php echo anchor('flight/display/2/'.$row['tid'].'/'.$pid, 'Edit'); ?>
</td>
<td>
<?php echo anchor('flight/display/3/'.$row['tid'].'/'.$pid, 'Delete'); ?>
</td>
</tr>
<?php } ?>
</table>
i am unable to come up with the logic of how it will be done
any help will be appreciated
all i can see is that you want to pass the data from your view to controller you can do that by passing your data to url and then use it from the controller for future use.
You are getting values from DB row by row. Each row in your database has a unique ID (hopefully). That is your main criteria for the WHERE clause.
I don't see any inputs in your HTML, so I don't really understand how you are saving the data, but you can either do a hidden input in each row like <input type="hidden" id="[your_row_id_from_DB]" /> or if you have multiple inputs per row - add a name with id appended to it, like <input type="text" name="flight_your_row_id" /> . That way you can parse your names and know which values are submitted.

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?

making two tables depending if they have 0 or 1

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>

how to store json_encode results in a table

I have a database which works by displaying first and last_names of all employees in the database, i can display it but when it displays, it's not formatted. I want to try and put the results in a table but I'm not sure how I would.
I thought I would have to echo json_encode(echo.<td>$posts</td>) or something like that
<?php foreach($query as $row): ?>
<tr>
<td>
<?php $arr = array(
'first_name' => $row->first_name,
'last_name' => $row->last_name,
); ?>
<?php $posts[] = $arr;?>
</tr>
<?php endforeach; ?>
<?php echo json_encode($posts);?>
This is how its displayed now
[{"first_name":"Georgi","last_name":"Facello"},
{"first_name":"Georgi","last_name":"Atchley"}]
Nothing is written between your <tr> and such .. you are just assigning to posts and then printing it out as JSON after the fact which makes no sense.
<?php foreach... ?>
<tr>
<td>
<?php echo $row->first_name ?>
</td>
<td>
<? php echo $row->last_name ?>
</td>
</tr>
<?php endforeach ?>
You're gonna have to build the table 'by hand'. Like this:
<table>
<?php foreach($query as $row): ?>
<tr>
<td>
<?php echo $row->first_name; ?>
</td>
<td>
<?php echo $row->last_name; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<table>
<thead>
<th> First Name </th>
<th> Last Name </th>
</thead>
<tbody>
<?php foreach($query as $row): ?>
<tr>
<td> <?php echo $row->first_name ?> </td>
<td> <?php echo $row->last_name ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

Categories