Pull and update data from database in PHP CodeIgniter foreach loop - php

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.

Related

Unable to get count of POST variable

I want to insert selected rows in the database but I am unable to get count of checked check-boxes. However in one of my form where I am updating records I could get count but here I couldn't. Code is as below
<tr class="odd gradeX">
<td>
<input type="checkbox" class="checkboxes" value="<?php echo $row['student_id']; ?>" name="ids[]" />
</td>
<td>
<?php echo $row['first_name']." ".$row['last_name'];?>
</td>
<td>
<?php echo $row['father_mobile'];?>
</td>
<td class="hidden-480">
<a href="mailto:<?php echo $row['email'];?>">
<?php echo $row['email'];?>
</a>
</td>
<td class="hidden-480">
<?php echo $row['address'];?>
</td>
<td class="center hidden-480">
<?php echo $row1['track_name'];?>
</td>
<td>
<?php echo $row2['level_name'];?>
</span>
</td>
</tr>
<?php
if(isset($_POST['move']))
{
echo "count is" . count($_POST["ids"]);
for($i=0; $i<count($_POST["ids"]); $i++){
echo $sql = "inset into tbl_student_batches set bacth_id='".$_POST['batch_id']."',student_id='".$_POST['ids']."'";
if (mysql_query($sql)) {
$msg = "Selected students moved to next batch successfully";
header("location:convert_level?msg=$msg");
}else{
$errmsg = ("Error while moving.". mysql_error());
}
}
}
you can try in this way
$studentIds = explode(' ', $_POST['ids']);
$totalId= count($studentIds);

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

How: HTML table row click results in POST form linking to subsequent page

Evening all, I have the form, which is populated from a sql database.
<table class="sortable" border="2" cellspacing="2" cellpadding="2">
<tr>
<th> Serial Number </th>
<th> Date </th>
<th> Time </th>
<th> Color </th>
<th> Design </th>
<th> Result </th>
</tr>
<?php
$i = 0;
while ($i < $num)
{
$serial = mysql_result($results, $i, 'serial_number');
$date = mysql_result($results, $i, 'date');
$time = mysql_result($results, $i, 'time');
$airport = mysql_result($results, $i, 'color');
$terminal = mysql_result($results, $i, 'design');
$result = mysql_result($results, $i, 'result');
?>
<tr>
<td> <?php echo $serial; ?> </a></td>
<td> <?php echo $date; ?> </td>
<td> <?php echo $time; ?> </td>
<td> <?php echo $color; ?> </td>
<td> <?php echo $design; ?> </td>
<td> <?php echo $result; ?> </td>
</tr>
<?php
$i++;
}
?>
What I would like to do is have each row of the table clickable. When each row is clicked, one cell of data from that row (the first cell) is sent via (POST) to the next page. Can a form be integrated into each tr??
You specify jQuery in your tags, so I assume you can use that.
// when any row is clicked
$('table').on('click', 'tr', function () {
// get the value
var value = $(this).find('td:first').text();
// redirect the user with the value as a GET variable
window.location = nextPage + '?data=' + value;
});
Where nextPage is the URL of the page you want to redirect to.
The short answer: yes, a form can be integrated into each tr.
The long answer - use just as you did before:
<tr>
<td>
<form method="post" target="details.php">
<input type="submit" name="more" value="<?php echo $serial; ?>"
</form>
<?php echo $serial; ?>
</td>
<td> <?php echo $date; ?> </td>
<td> <?php echo $time; ?> </td>
<td> <?php echo $color; ?> </td>
<td> <?php echo $design; ?> </td>
<td> <?php echo $result; ?> </td>
</tr>
But GET is easier, make a series of links that go details.php?number=123, or whichever:
<td>
<a href="details.php?number=<?php echo $serial; ?>"
<?php echo $serial; ?>
</a>
</td>
Although get can use a form, the data send is not user-customised, so the form data can be generated to use like a link.
Try with that code in echo when create your table:
<td><?php echo(''.$serial.'');?></td>
For each data that you have!
OTHER SOLUTION WITHOUT ACTION
<td><?php echo(''.$serial.'');?></td>
my_page.php - where to send the data
?[variable_name] - where is stored the data

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