check a radio box from mysql data with function - php

I'm trying to build an edit page for editing my Previous Posts
so when i click edit link in front of a post it sends post id to edit.php
now here is my problem i want to check a radio box in edit page that shows my current post's category
I have a function like this:
function category($name,$postid){
if( $name == $postid)
{
return "checked";
}
}
in edit.php
$postid=$_GET['postid']
$query=mysqli_query($con,"SELECT *FROM category");
while($s=mysqli_fetch_array($query)){
echo
'<table width="200" border="1">
<tbody>
<tr>
<td>'.$s['category_name'].'</td>
<td><input type="radio" name="post_category" value="'.$s['category_name'].'" '.category($s['id'],$postid).'></td>
</tr>
</tbody>
</table>';
}
but it doesn't check the radio box

Its hard to tell without actually seeing what is not matching, but from your naming, it looks like you are matching the Id to the Name. If you are matching Id's, then you should use parseInt to make sure you are dealing with integers and comparing data in the same format.

You should tell to the PHP to write the return from the function:
Edit #1
changing the function to always return something
function category($id, $postid) {
if( $id== $postid)
{
return "checked";
}
return null;
}
The return will fill the var $checked or with "checked" or with null
while($s = mysqli_fetch_array($query)) {
$checked = category($s['id'], $postid);
echo
'<table width="200" border="1">
<tbody>
<tr>
<td>'.$s['category_name'].'</td>
<td>
<input... ' . $checked . '>
</td>
</tr>
</tbody>
</table>';
}

Related

PHP: Update database with checkbox values for each row

I'm currently working on displaying a table containing a list of alarms, where each row contains a checkbox that determines whether the user has already viewed that alarm or not. Here's
an image:
So far, I managed to check/uncheck the checkbox given its state in the database. What I want to do, and don't exactly know how to, allow the user to check/uncheck a checkbox and immediately update the database with this new state without pressing a submit button. The code that I have right now is the following.
<div class="card-body table-responsive p-0">
<form action=modules/alarms.php method="POST">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>Tipus</th>
<th>Data</th>
<th>Vista</th>
</tr>
</thead>
<tbody>
<?php
foreach ($result->fetchAll() as $row){
if ($row[4] == 1){
$status = "checked";
}else{
$status = "";
}
echo
'<tr data-widget="expandable-table" aria-expanded="false">
<td>'.$row[0].'</td>
<td>'.$row[1].'</td>
<td>'.$row[2].'</td>
<td><input type="checkbox" name="chk1[]" value="viewed" '.$status.'></td>
</tr>
<tr class="expandable-body">
<td colspan="5">
<p>
<video width="416" height="416" controls>
<!-- el 42 es la cabecera C:/... fins videos-->
<source src="'.substr($row[3], 42).'" type="video/mp4">
Your browser does not support the video tag.
</video>
</p>
</td>
</tr>';
}
?>
</tbody>
</table>
And the PHP code I have (I don't have the code that will update values on the database, but I'll figure that out. I want to know how I could retrieve the value of the checkbox of every row and its ID so I can update it on the database.
$ei = $_POST['chk1'];
if ($_POST["chk1"] == "chk1") {
for ($i = 0; $i < sizeof($checkbox1); $i++) {
print_r($checkboxl[$i]);
}
}
To solve this problem, first I had to add some fields to the checkbox input. WIth those changes, the table is generated as it follows:
<tr data-widget="expandable-table" aria-expanded="false">
<td>'.$row[0].'</td>
<td>'.$row[1].'</td>
<td>'.$row[2].'</td>
<td><input type="checkbox" name="id" id="viewed" value="'.$row[0].'" '.$status.'></td>
</tr>
After this is done, simply add a script that will retrieve the values from the clicked checkbox and pass them to the necessary PHP function:
$("input").change(function() {
if ($(this).is(':checked')){
var viewed=1;
}else{
var viewed = 0;
}
var id = $(this).val();
$.ajax({
url:"modules/alarms.php",
method:"POST",
data:{viewed:viewed,id:id,},
success: function(data){
},
});
Please note that the 'url' field is the relative path where our PHP function is implemented.
And now, simply update the database with the checkbox value (I'm using PDO objects) as it follows:
<?php
if(isset($_POST["viewed"])) {
$id = $_POST["id"];
$viewed = $_POST["viewed"];
$sql="UPDATE `alarms` SET `viewed` = '$viewed' WHERE (`id` = '$id')";
if($connection->query($sql) === TRUE){
echo "Success";
} else {
echo "error" . $sql . "<br>".$connection->error;
}}?>

Pass value while submitting a form button from model to controller

I am making an application in codeigniter. I am trying to pass value stored in a variable when a submit button is being clicked from model to controller.
How can I achieve this? I have tried the below code so far:
model.php
public function loadadd($mekhala_Id) {
echo form_open('Payment/amount($count)');
$query = $this->db->get_where('tb_unit', array('mandalam_Id' => $mekhala_Id));
echo $count= $query->num_rows();
?>
<h1>Members List</h1>
<table border="1">
<tr>
<th>Unit</th>
<th>Unit Secretary</th>
<th>Amount paid</th>
</tr>
<?php
foreach ($query->result() as $row)
{
//$i=1;
?>
<tr>
<td> <?php echo $row->unitName ;?></td>
<td> <?php echo $row->unit_sec ;?></td>
<td> <?php echo form_input(array('name'=>'na','placeholder'=>'Rupees Paid')) ;?></td>
<td><?php echo form_checkbox(array('name'=>'check','value'=>'paid')) ; ?></td>
</tr>
<?php
// $i++;
}
// echo $query->result();
echo form_submit(array('name'=>'sub','value'=>'submit'));
echo form_close();
}
public function loadpayment($paid,$count){
for($i=1;$i<=$count;$i++)
{
$a='na'.$i;
$paid=array($this->input->post($a));
$this->db->insert('tb_unit',array('Amount'=> $paid));
}
}
}
?>
controller.php public function amount($count) {
$paid=$this->input->post('na');
$this->cms_model->loadpayment($paid,$count);
}
While running this code below is being shown:
An error was encountered
The URI you submitted has disallowed characters.
I think you should read again the main idea of the model, view and controller.
The idea of the models is only to work with the database. You do not use form_open() or other html in models, this should be done in the views.
This tutorial shows more details.
To pass value with submit you can use hidden field in the form as show here.
To pass data in a variable when submit button is being clicked: you can set the variable to the value attribute: echo form_submit(array('name'=>'sub','value'=>"$variable"));

Dynamically update the table on selecting checkbox

I'm very new to php and have been given the task to update the table in the database when user select the check box and then edit the information in the text box. I'm able to pull the information from database and display it on the screen, however I'm not too sure how to update the database when user selects the checkbox and update the field
Here is what I want to achieve:
1) On clicking the checkbox, the row gets editable.
2) After updating the field value, and clicking button "update", the value gets updated in the d/b
<div class="table-responsive">
<table class="table table-condensed">
<thead>
<tr>
<th width="20%">Field Name</th>
<th width="52%">Field Text</th>
<th width="32%">Select to Update</th>
</tr>
</thead>
<tbody>
<?php
if($table = true)
{
while($row = $result->fetch_assoc())
{
$fieldName[] = $row['fieldName'];
$fieldText[] = $row['fieldText'];
$fieldID[] = $row['ID'];
echo "<tr>";
echo "<td>".$row['fieldName']."</td>";
echo "<td>".$row['fieldText']."</td>";
echo "<td>"."<input type= 'checkbox' value='{$row['ID']}', name = ID[]>"."</td>";
echo "</tr>";
}
}
else
{echo "No results found";}
?>
</table>
</div>
Not sure how to proceed further. After checking forums, I found that Jquery is the way to go, but not sure how to make the field updatable.
Please note that I haven't provided the code for the "Update" functionality.
Sorry, i am not good in jquery. But i think for this u could use
$('.your-check-box').on('change', function() {
if(this.checked)
{
//change text on inputs
}
else {
//change inputs on text
}
});
this is quick example:
http://jsfiddle.net/qr4ova98/
For DB update u need to send ajax with data on event.

passing the input name (comes from database) from view to controller

I'm trying to learn codeigniter, the first thing i did is to make simple add edit delete function...
I'm having trouble calling the name of input button to the controller to delete an entire row of table..
model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Delete_model extends CI_Model{
public function delete($data){
$this->db->delete('record_tbl', $data);
}
}
controller
class Delete_controller extends CI_Controller {
public function index()
{
$this->delete();
}
public function delete()
{
$data = array();
$this->load->model('fetch');
$query = $this->fetch->getData();
if ($query)
{
$data['results'] = $query;
}
$this->load->view('delete', $data);
}
public function delete_data(){
$this->load->model('delete_model');
$where = $this->input->get('id');
$data['id'] = $where;
$this->delete_model->delete($data);
redirect('');
}
}
view
<table border="1" width="100%">
<tr>
<th>Name</th>
<th>Address</th>
<th>Phone Number</th>
<th>Status</th>
<th></th>
</tr>
<?php foreach($results as $row) { ?>
<tr>
<td><?=$row->name?></td>
<td><?=$row->addr?></td>
<td><?=$row->pnum?></td>
<td><?=$row->status?></td>
<?php echo form_open('delete_controller/delete_data') ?>
<td style="text-align: center"><input name="<?=$row->id?>" type="submit" value="delete"></input>
</form>
</td>
</tr>
<?php } ?>
</table>
Your form will be doing a POST rather than a GET request. So rather than doing
$where = $this->input->get('id');
You want to do
$where = $this->input->post('id');
Also why don't you pass the value of $row->id to your controller so that you know which input field to target. So in your view do
<?php echo form_open('delete_controller/delete_data/' . $row->id) ?>
Then in your controller
public function delete_data($id){
$this->load->model('delete_model');
$where = $this->input->post($id);
//...
It's going to be $this->input->post ('id') like #Pattle says and you are using the "name" attribute of the form for the ID. What I usually do is use a hidden field (see form_hidden () helper in CI documentation) with the name "id" and the value you want ($row->id).
<?php echo form_hidden ('id', $row->id); ?>
Instead of $this->input->get('id'); use $this->input->post('id');.
And to simplify things up: I probably recommend using <button></button> as control buttons like delete functionality (it also works on edit/update FYI).
Coz you can just add attribute to a button value = $id and name=delete. And it would be like an <input type='submit'> just by adding an attribute of type='submit'
So
VIEW
<table border="1" width="100%">
<tr>
<th>Name</th>
<th>Address</th>
<th>Phone Number</th>
<th>Status</th>
<th></th>
</tr>
<?php foreach($results as $row) { ?>
<tr>
<td><?=$row->name?></td>
<td><?=$row->addr?></td>
<td><?=$row->pnum?></td>
<td><?=$row->status?></td>
<?php echo form_open('delete_controller/delete_data') ?>
<td style="text-align: center">
<?php
echo "<button type='submit' name='delete' value='" . $row->id . "'>" ;
echo "DELETE" ;
echo "</button>" ;
?>
</form>
</td>
</tr>
<?php } ?>
</table>
Then it would be very simple to fetch the id of the row you want to delete. Just do this in your controller $id = $_POST['delete']; or $id = $this->input->post('delete');

Making of new record and showing it inside a div (append to a while loop)

I'm trying to to make a form that will submit a new record and automatically show that as DIV inside of the main DIV and after all the results from the PHP while loop.
Here is the while loop that I've built:
<?php
while ($row_apps = mysql_fetch_array($result_apps))
{
if ( ($row_apps['type'] == 'ar') && ($row_apps['client'] == NULL) ) {
echo '<center>
<div id="_'.$row_apps['app_id'].'" class="tab_listing">
<table width="248" border="0" cellspacing="10px" cellpadding="0px">
<tr>
<td width="100%" colspan="3"><div class="tab_listing_header">'.$row_apps['app_name'].'</div></td>
</tr>
<tr>
<td class="tab_listing_values"><b>App ID: </b>'.$row_apps['app_id'].'</td>
<td class="tab_listing_values"><b>Users: </b>'.$row_apps['users'].'</td>
</tr>
</table>
<div id="edit">Edit</div>
<div id="delete"></div>
</div>
</center><br>';
}
}
?>
Now basically what i need to do is when a form is submitted and the new record is in the database i want it to show a new "block" exact same like the while "blocks" live.
I was thinking about something like:
$.post("create.php",{ value1:$('#value1').val(),value2:$('value2').val(),rand:Math.random() } ,function(data)
{
if(data=='yes')
{
HERE I'M STUCK.
Thanks!
Try this
$.post("create.php",{ value1:$('#value1').val(),value2:$('value2').val(),rand:Math.random() } ,function(data)
{
if(data)
{
$("mainDivSelector").html(data);
}
);
data will contain a result string returned by create.php and that is entirely up to you. What does create.php do at the moment?

Categories