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!
Related
I am trying to create a table with few of its column created dynamically, based on database values. Till here things are going good. Now only for the dynamic table headers, I want to create two sub-headers for each header being generated under the columns for showing two different fields. How can this be achieved. Till now what I am trying is not sufficient and needs some insights. So please take a look to my code where 'Module' is the column that is being generated dynamically based on number of modules in database and under each of 'Module' column I want to put 'Attempt' and 'Result' sub-headers. Any help/insight will be very helpful.
<table id="example" class="table table-striped table-hover table-bordered dataTableReport" cellspacing="0" width="100%">
<thead>
<tr>
<th rowspan="2">S.No.</th>
<th rowspan="2">E-mail ID</th>
<th rowspan="2">Name</th>
<?php
$query_select = "SELECT id from tbl;";
$result_select = mysql_query($query_select) or die(mysql_error());
$rows = array();
while($row = mysql_fetch_array($result_select))
$rows[] = $row;
foreach($rows as $row){
$mid = $row['id'];
echo "<th style='width:5%' colspan='2'>Module ".$mid."</th><tr><th>Attempt</th><th>Result</th>";
} ?>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT id FROM tbl;";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($result)){
?>
<th style='width:5%' colspan='2'>Module <?php echo $row['id']; ?></th>
<br><tr><th>Attempt</th><br><th>Result</th></tr>
<?php
}
?>
I have updated your code to mysqli from mysql mainly because it dated and doesn't have all the updated features. I would start learning mysqli to keep up with the competition, and to stop bugs later down the line.
$conn = you should rename this to your database variable.
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.
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.
So I'm trying to make a HTML table that gets data from a MySQL database and outputs it to the user. I'm doing so with PHP, which I'm extremely new to, so please excuse my messy code!
The code that I'm using is: braces for storm of "your code is awful!"
<table class="table table-striped table-hover ">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Description</th>
<th>Reward</th>
<th>Column heading</th>
</tr>
</thead>
<tbody>
<?php
$con = mysql_connect("localhost", "notarealuser", 'notmypassword');
for ($i = 1; $i <= 20; $i++) {
$items = ($mysqli->query("SELECT id FROM `items` WHERE id = $i"));
echo ("<tr>");
echo ("
<td>
while ($db_field = mysqli_fetch_assoc($items)) {
print $db_field['id'];
}</td>");
$items = ($mysqli->query("SELECT name FROM `items` WHERE id = $i"));
echo ("
<td>
while ($db_field = mysqli_fetch_assoc($items)) {
print $db_field['name'];
}</td>");
$items = ($mysqli->query("SELECT descrip FROM `items` WHERE id = $i"));
echo ("
<td>
while ($db_field = mysqli_fetch_assoc($items)) {
print $db_field['descrip'];
}</td>");
$items = ($mysqli->query("SELECT reward FROM `items` WHERE id = $i"));
echo ("
<td>
while ($db_field = mysqli_fetch_assoc($items)) {
print $db_field['reward'];
}</td>");
$items = ($mysqli->query("SELECT img FROM `items` WHERE id = $i"));
echo ("
<td>
while ($db_field = mysqli_fetch_assoc($items)) {
print $db_field['img'];
}</td>");
echo ("</tr>");
}
?>
</tbody>
</table>
However, this code is not working - it simply causes the page to output an immediate 500 Internal Server Error. IIS logs show it as a 500:0 - generic ISE. Any ideas?
You are mixing mysql and mysqli, not closing php code block and you are not selecting a database. Plus you don't have to run a query for each field
Try this:
<table class="table table-striped table-hover ">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Description</th>
<th>Reward</th>
<th>Column heading</th>
</tr>
</thead>
<tbody>
<?php
$con = new mysqli("host","user", "password", "database");
$execItems = $con->query("SELECT id, name, descrip, reward, img FROM `items` WHERE id BETWEEN 1 AND 20 ");
while($infoItems = $execItems->fetch_array()){
echo "
<tr>
<td>".$infoItems['id']."</td>
<td>".$infoItems['name']."</td>
<td>".$infoItems['descrip']."</td>
<td>".$infoItems['reward']."</td>
<td>".$infoItems['img']."</td>
</tr>
";
}
?>
</tbody>
</table>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Description</th>
<th>Reward</th>
<th>Column heading</th>
</tr>
</thead>
<tbody>
<?php
$con = mysqli_connect("hostname","username",'password');
$sql= "SELECT * FROM `items` WHERE id <20 ";
$items = (mysqli_query($sql));
while ( $db_field = mysqli_fetch_assoc($items) ) {?>
<tr><td><?php echo $db_field['id'];?></td></tr>
<tr><td><?php echo $db_field['name'];?></td></tr>
<tr><td><?php echo $db_field['descrip'];?></td></tr>
<tr><td><?php echo $db_field['reward'];?></td></tr>
<tr><td><?php echo $db_field['img'];?></td></tr>
<?php}
</tbody>
</table>
Try these, not tested
Where is the question?
There's many problems with this code.
First, you are confused between PHP and HTML.
Code between is PHP. It's executed on the server, you can have loops and variables and assignments there. And if you want some HTML there you use "echo".
Code outside is HTML - it's sent to the browser as is.
Second - what you seem to be doing is querying each field separately. This is not how you work with SQL.
Here's more or less what you need to do:
//Query all rows from 1 to 20:
$items = $mysqli->query("SELECT id,name,descrip,reward,img FROM `items` WHERE id between 1 and 20");
//Go through rows
while ( $row = mysqli_fetch_assoc($items) )
{
echo "<tr><td>{$db_field['id']}</td>";
//echo the rest of the fields the same way
});
I'm going to go ahead and assume that the code isn't working and that's because there's several basic errors. I'd strongly suggest doing some hard reading around the topic of PHP, especially since you're using databases, which, if accessed with insecure code can pose major security risks.
Firstly, you've set-up your connection using the procedural mysql_connect function but then just a few lines down you've switched to object-orientation by trying to call the method mysqli::query on a non object as it was never instantiated during your connection.
http://php.net/manual/en/mysqli.construct.php
Secondly, PHP echo() doesn't require the parentheses. PHP sometimes describes it as a function but it's a language construct and the parentheses will cause problems if you try to parse multiple parameters.
http://php.net/manual/en/function.echo.php
Thirdly, you can't simply switch from HTML and PHP and vice-versa with informing the server/browser. If you wish to do this, you need to either concatenate...
echo "<td>".while($db_filed = mysqli_fetch_assoc($item)) {
print $db_field['id'];
}."</td>;
Or preferably (in my opinion it looks cleaner)
<td>
<?php
while($db_filed = mysqli_fetch_assoc($item)) {
print $db_field['id'];
}
?>
</td>
However, those examples are based on your code which is outputting each ID into the same cell which I don't think is your goal so you should be inserting the cells into the loop as well so that each ID belongs to its own cell. Furthermore, I'd recommend using echo over print (it's faster).
Something else that may not be a problem now but could evolve into one is that you've used a constant for you FOR loop. If you need to ever pull more than 20 rows from your table then you will have to manually increase this figure and if you're table has less than 20 rows you will receive an error because the loop will be trying to access table rows that don't exist.
I'm no PHP expert so some of my terminology might be incorrect but hopefully what knowledge I do have will be of use. Again, I'd strongly recommend getting a good knowledge of the language before using it.
I have to pull from a table that's been made by someone else and print it onto a web page. Issue is, that column name are stored in the rows. For instance:
FIELD_NAME| FIELD_VALUE
______________________
first-name| John
last-name | Smith
This is how I'm going about grabbing the data
$tableRow = '';
$sql ="SELECT * FROM `wp_cf7dbplugin_submits` WHERE form_name = '".$nameOfForm."' ";
$result = $mysqli->query($sql);
while($tableData = $result->fetch_assoc()){
$fieldName = $tableData['field_name'];
$fieldValue = $tableData['field_value'];
//FIRST NAME
if($fieldName == 'first-name'){
$firstName = $fieldValue;
}
//LAST NAME
if($fieldName == 'last-name'){
$lastName = $fieldValue;
}
//BUILD A ROW FOR THE TABLE
$tableRow .='
<th>'.$firstName.'</th>
<th>'.$lastName.'</th>
';
}
it only KIND of works when I do $tableRow = instead of $tableRow .=
it will only show me one result, but when I do $tableRow .= it will only show me empty but the correct amount of s.
How can I populate an html table with all of the results pulling from a table like this?
EDIT:
I forgot to mention that the whole table looks like this:
<table id="theTable">
<thead>
<tr>
<th style="width:100px;">First Name</th>
<th style="width:100px;">Last Name</th>
</tr>
</thead>
<tbody id="rowsGoHere">
</tbody>
</table>
and I echo the PHP result via ajax,
//ECHO BACK RESULTS
$JSONData = array("true", $tableRow);
echo $_GET['callback']."(".json_encode($JSONData).")";
and use this to insert into table
var trueEncV = encodeURIComponent("true");
$.getJSON("pullTableData.php?callback=?",
{
trueV: trueEncV
},
function(dataBack){
alert(dataBack);
$('#rowsGoHere').html(dataBack[1]);
}
)
That all works fine and dandy.
According to your updated code the <tr> tags are missing. Also, within <tbody> you should use the <td> tag instead of the <th> tag.
$tableRow .='
<tr>
<td>'.$firstName.'</td>
<td>'.$lastName.'</td>
</tr>
';
Can you try
echo json_encode($JSONData);
instead of
echo $_GET['callback']."(".json_encode($JSONData).")";