how to get all rows in column to insert db in codeigniter - php

I'm trying to insert all rows which showing view in a table but it insert only one row with value '1' not all rows and not that id's contain in a column:
view:
echo"<table class='table table-hover type-list2'id='traineeList'>
<tr class='success'>
<th>Trainee ID</th>
<th>Trainee Name</th>
<th>Present</th>
</tr>";
foreach($list['trainee'] as $row){
echo "
<tr><td>".str_pad($row->TraineeID,7,"0", STR_PAD_LEFT)."</td>
<td>".$row->Name."</td>
<td><input type='checkbox' name='.$row->TraineeID[]' value='".$row->TraineeID."' checked></td>
</tr>";
}
echo "</table>";
controller:
public function insertAttendance(){
$data = array('TraineeID'>=$this->input->post('TraineeID'));
$attnDate=$this->input->post('attnDate');
$classHour= $this->input->post('classHour');
foreach ($data as $id){
$query="INSERT INTO `tbl_attn_temp` (TraineeID, Date, classHour) VALUES ('".$id."','".$attnDate."','".$classHour."')";
$this->db->query($query);
redirect('attendance/index/');
}

see the given link for your question
http://codefury.net/2009/12/insert-multiple-rows-into-a-database-with-codeigniter/

Your redirect function should be placed outside of your foreach loop, probably. Looking at your code, your foreach loop will run only once and then redirect you to another page.

Please try with this
foreach($_POST['TraineeID'] as $key => $id) {
$attnDate=$_POST['attnDate'][$key];
$classHour= $_POST['classHour'][$key];
$query="INSERT INTO `tbl_attn_temp` (TraineeID, Date, classHour) VALUES ('".$id."','".$attnDate."','".$classHour."')";
$this->db->query($query);
}
redirect('attendance/index/');

Related

Data Tables second table is shown but no reaction

I have implemented a table from data tables
Link [https://datatables.net/]
i would like to use two tables in one site with different columns and datas in the columns after the mysqli connection i insert the data sets with a while mysqli fetch array function the first table works properly
"urlaubstage" -> is correct
but table 2
no matter what i do even var_dump i dint not get any reaction but the table is display correctly on the page but with empty columns
This is the html code
<table id="table_id" class="display">
<thead>
<tr>
<th>Urlaubstage Jahr</th>
<th>Urlaubstage Anspruch</th>
<th>Urlaubstage Beansprucht</th>
<th>Urlaubstage Rest</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($sql)){
$urlaubsTage = $row[4];
echo "<tr>";
echo "<td>{$urlaubsTage}</td>";
echo "<td>Anspruch</td>";
echo "<td>Beansprucht</td>";
echo "<td>Rest</td>";
echo "halllo";
}
echo "</tr>";
?>
</tbody>
</table>
!!!!!!!!!!!!<p>HERE STARTS THE SECOND TABLE</p>!!!!!!!!!!!!!!!!!!!!!!!!
<table id="table_id2" class="display">
<thead>
<tr>
<th>Urlaub Antragsdatum</th>
<th>Urlaub Startdatum</th>
<th>Urlaub Enddatum</th>
<th>Urlaubs Status</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($sql)){
var_dump($row); -> EVEn VAR_DUMP IS NOT SHOWN
echo "<tr>";
echo "<td>Antragsdatum</td>";
echo "<td>Startdatum</td>";
echo "<td>Enddatum</td>";
echo "<td>Status</td>";
echo "halllo";
}
echo "</tr>";
?>
</tbody>
</table>
JQUERY CODE
....
$('#table_id').DataTable();
//FUNKTION FÜR ZWEITE TABELLE
$('#table_id2').DataTable();
....
Picture of Code and Table
ok i got the answer by myself, after the first loop runs the query $sql with mysqli_fetch_array($sql) you cant use it anymore on the second loop why? cause it ran on the first loop and its over solution
rename;
$sql = mysqli_query(same query);
$sql2 = mysqli_query(same query);
first loop while($row = mysqli_fetch_array($sql);
second loop while($row = mysqli_fetch_array($sql2);
You don't need to execute the same query twice. You don't need the while loop either. Try to get the results into an array and then loop the array multiple times.
$result = $conn->query($sql)->fetch_all();
//...
foreach ($result as $row) {
//...
}
// Repeat the same loop again without calling query again
//foreach...

Need someone to correct my mysql query for table creation

I want to create a table as that I can have in the one variable (going to send it to js later on) and then display it. But result of my echo is only table head ('Produkty', 'Ilość', 'Cena'). Can you show me where I made mistakes or correct me?
<?php
header('Access-Control-Allow-Origin: *');
include "database.php";
$dane = array();
$tabela='<table class="table table-striped">
<thead>
<tr>
<th>Produkt</th>
<th>Ilość</th>
<th>Cena</th>
</tr>
</thead>
<tbody>';
$dane=array();
$sql_main="SELECT Products.`Name`,Orders_NEW.`Amount`,((Products.`Price`)*(Orders_NEW.`Amount`)) as 'PRICE' FROM `Orders_NEW` inner join `Products` on Orders_NEW.`Product`=Products.`ID` AND `Order_ID`=669";
$dane = $db->query($sql_main);
foreach($dane as $row)
{
$tabela.="<tr><td>".$row['Name']."</td><td>".$row['Amount']."</td><td>".$row['Price']."</td></tr>";
}
$sql_second="SELECT SUM((Products.`Price`)*(Orders_NEW.`Amount`)) as 'SUMA' FROM `Orders_NEW` inner join `Products` on Orders_NEW.`Product`=Products.`ID` AND `Order_ID`=669";
$dane_second= array();
$dane_second= $db -> query($sql_second);
foreach($dane_second as $row)
{
$tabela.='<thead>
<tr>
<th>Łącznie</th>
<th></th>
<th>'.$row["SUMA"].'</th>
</tr>
</thead>
</table>';
}
echo($tabela);
?>
Edited: Changed foreach into while($row = $dane->fetch_assoc())
Now my result is:
Produkt Ilość Cena
Łącznie
seems like variables like $row['Name'] etc is the problem here
I'm not sure if you have some sort of custom class with database.php, but if $db just contains mysqli object then you need to do the following:
$dane = $db->query($sql_main);
while($row = $dane->fetch_assoc()) {
$tabela.="<tr><td>".$row['Name']."</td><td>".$row['Amount']."</td><td>".$row['Price']."</td></tr>";
}
Same goes for the second query (this assumes your SQL is correct and returning results by the way).
You have a mistake in the code:
foreach($dane_second as $row){
$tabela.='<thead>
<tr>
<th>Łącznie</th>
<th></th>
<th>'.$row["SUMA"].'</th>
</tr>
</thead>
</table>'; //Here's the problem
}
echo($tabela);
Move closing table out of foreach.

codeigniter - remove specific table row without delete it in database

im a newbie for codeigniter. i need some help to solve my problem.
so i have a table filled with data that i GET from database (which only display the last timestamp for category "no_registrasi"). and there is an option in "Selesai" to remove the row in the html table without delete it in database. i already tried to find the solution, but i have no idea how to do this.
this is my table html
<table id="tabel-pas" class="table table-striped table-bordered dt-responsive wrap" cellspacing="0" width="100%">
<thead>
<tr>
<th>Waktu</th>
<th>No. Registrasi</th>
<th>No. Rekam Medis</th>
<th>Nama Dokter</th>
<th>Nama Pemilik</th>
<th>Nama Hewan</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
foreach($daftar_inap_aktif as $row){
echo "<tr>";
echo '<td>'.$row->waktu.'</td>';
echo "<td>".$row->no_registrasi."</td>";
echo "<td>".$row->no_rm."</td>";
echo "<td>".$row->username."</td>";
echo "<td>".$row->nama_pemilik."</td>";
echo "<td>".$row->nama_hewan."</td>";
echo '<td>';
echo 'Tambah | Selesai';
echo'</td>';
echo"</tr>";$no++;
}
?>
</tbody>
</table>
here is my model
function daftar_inap_aktif()
{
$this->db->select('*');
$this->db->select_max('waktu');
$this->db->from('inputrminap');
$this->db->group_by('no_registrasi');
$this->db->order_by('waktu', 'DESC');
$query = $this->db->get();
return $query->result();
}
and here is my controller
function daftar_inap()
{
$data['daftar_inap_aktif'] = $this->rekammedis_model->daftar_inap_aktif();
$this->load->view('daftar_inap_view', $data);
}
I am not sure what are you trying to accomplish, you mean remove that row for that particular session or permanently until set it to display again by admin or so.
Let me give it a shot.
**Removing it temporarily(hiding).. What I would do in that case:
Your View Code:
Selesai';
// Considering that you have base url setup and you have id of that row.
And then in Controller:
Public function some_method(){
$id = $this->uri->segment(3);
$this->some_model->hide_rows_with_id($id);
redirect('url_for_that_view');
}
And finally model:
function hide_rows_with_id($id)
{
$this->db->select('*');
$this->db->select_max('waktu');
$this->db->from('inputrminap');
$this->db->where('id !=', $id); // Added this
$this->db->group_by('no_registrasi');
$this->db->order_by('waktu', 'DESC');
$query = $this->db->get();
return $query->result();
}
This is just an idea, you may want to add more code to model if "id" is an array.
And of course, if the page is reopened from the scratch, all results will be displayed again. To hide it permanently you can create a new column with name something like "hidden" and insert '1' if was hidden through view.. and then in model:
$this->db->where('hidden !=', 1);
Add a unique id for each row you have, And link it with the href you have
e.g HTML:
<tr id="row-1">
<td>xx</td>
<td>xx</td>
<td>xx</td>
<td>xx</td>
<td>xx</td>
<td>xx</td>
<td>Delete</td>
</tr>
Then in your javascript code add something like this:
$('.del_row').on('click', function (e) {
e.preventDefault();
var $rowId = $(this).data('row-id');
$('#row-'+$rowId).remove();
});
So that keep an extra column call status. When its active set it 0 and when its deaxtive set it 1.
So in your query where you load data you have to add new where clause.
WHERE status=0
Note : set status column as int or boolen.

How do I display data from one mySQL table into multiple tables in php?

I have on mySQL table with data and I want to display it in multiple tables. When I try to do it with the code I have now, the data doesn't show up in the second table. Is my code wrong?
$id=$_GET['id'];
$sql = "SELECT * FROM buildings WHERE room = '{$id}' AND number = '81'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<center><h2>Room $id</h2></center> <br> <b>General Information<br>
<table><tr>
<th>Room Type</th>
<th>Department</th>
<th>College</th>
<th>Primary Owner</th></tr>";
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>".$row["type"]."</td>
<td>".$row["Department"]."</td>
<td>".$row["College"]."</td>
<td>".$row["Primary"]."</td>
</tr>";
}
echo "</table>";
}
//CONTACT INFO//
if (mysqli_num_rows($result) > 0) {
echo "<p><b>Contact Information<br><table><tr>
<th>Reservations</th>
<th>CTL Contact Name</th>
<th>CTL Contact Email</th>
<th>CTL Contact Phone</th>
<th>Department Contact Name</th>
<th>Department Contact Email</th>
<th>Department Contact Phone</th>
<th>IT Contact Name</th>
<th>IT Contact Email</th>
<th>IT Contact Phone</th>
</tr>";
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>".$row["reservations"]."</td>
<td>".$row["contact"]."</td>
<td>".$row["contactphone"]."</td>
<td>".$row["deptcontact"]."</td>
<td>".$row["deptcontactemail"]."</td>
<td>".$row["deptcontactphone"]."</td>
<td>".$row["itcontact"]."</td>
<td>".$row["itcontactemail"]."</td>
<td>".$row["itcontactphone"]."</td>
</tr>";
}
echo "</table>";
}
else{
echo("0 results");
}
This is what I get:
http://i57.tinypic.com/2heijyc.png
Read the manual of mysqli_fetch_assoc
You've already called while($row = mysqli_fetch_assoc($result))
This will loop through the entire result array and get each row. mysqli_fetch_assoc returns null when there are no more rows. So the next time you run mysqli_fetch_assoc($result) it will be null.
What you need to do is use mysqli_fetch_all at the top to get an array of the result set. Then you can loop through this array as needed.
As I stated in my comment, this query is subject to SQL injections because you use $id which is sourced by $_GET directly in the query. Read up on prepared statements or if it is an integer, use intval($id).

editable table cell using angularJS

Is there a way to show data inside td and allow user to edit it in a way that the change will take place in the sql (MySQL in my case) table?
im creating a table from database using angularJS:
Creating The json file from the table:
$result = mysql_query("select * from `user script settings`");
//Create an array
$json_response = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$row_array['user_id'] = $row['user id'];
$row_array['script_id'] = $row['script id'];
$row_array['cron_format'] = $row['cron format'];
$row_array['schedule_last_update'] = $row['schedule last update'];
$row_array['next_execution_time'] = $row['next execution time'];
$row_array['script_exec'] = $row['script_exec'];
//push the values in the array
array_push($json_response,$row_array);
}
echo json_encode($json_response);
showing it in the table:
<table class="table table-bordered table-hover">
<thead>
<td>user name</td>
<td>script name</td>
<td>cron format</td>
<td>schedule last update</td>
<td>next execution time</td>
<td>script exec</td>
</thead>
<tbody>
<tr ng-repeat="x in data">
<td>{{x.user_id}}</td>
<td>{{x.script_id}}</td>
<td>{{x.cron_format}}</td>
<td>{{x.schedule_last_update}}</td>
<td>{{x.next_execution_time}}</td>
<td>{{x.script_exec}}</td>
</tr>
</tbody>
</table>
<script>
function customersController($scope,$http) {
$http.get("getData.php")//test3.php return a json file of users table
.success(function(response) {$scope.data = response;});
}
</script>
I want to allow user to change the cron format value and to update the table ...is there "angularish" way to due that?? if not i'll appreciate guidance for a php solution, thx!

Categories