I'm having a problem to insert a single data from my database
Example: I have two data's which is the question1 with ID:1 and question2: with ID:2. Those 2 questions does have different button. The problem is once I click the question1 or question2 button it inserting both of the ID's in my database.
Like this:
Here's My Controller
$data['posts'] = $this->Post_Model->get_posts();
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
$this->load->library('form_validation');
if($this->form_validation->run()==FALSE) {
if($this->input->post("add")) {
$this->Post_Model->count_up();
redirect('posts');
}
}
My Model
function count_up(){
for($i=0; $i<count($this->input->post('hidden')); $i++){
$data = array(
'post_id' => $this->input->post("hidden[$i]")
);
$this->db->insert("userspost", $data);
}
}
My View
<?php
$iq = 0;
$i = 0;
$arrtry = array();
foreach($posts->result() as $post){
?>
<br>
<div class="card card-nav-tabs">
<div class="card-header card-header-primary">
<!-- colors: "header-primary", "header-info", "header-success", "header-warning", "header-danger" -->
<div class="nav-tabs-navigation">
<input class="form-control" value="<?php echo $arrtry[$iq++] = $post->id; ?>" name="hidden1[<?php $i; ?>]" type="hidden">
<input class="btn btn-primary" type="submit" name="add" value="UP" />
</div>
</div>
</div>
<?php
$i++;
}
?>
My problem is i want to insert question2 ID without inserting question1 ID. Hope you guys can help thanks!
What is happening is that you have all your inputs within the same form with multiple submit buttons.
I can imagine a few ways you could solve this:
Ajax way. Require some JS and another controller method.
One form for each button: So you will always have one $this->input->post('hidden'). No need to iterate over it.
With only one form You could set the button for something like this:
<input class="btn btn-primary" type="submit" name="add-up" value="<?= $i; ?>" />
So on your controller/model you can get the index that was clicked:
/* controller/model */
$index = $this->input->post('add-up');
$hidden_value = $this->input->post("hidden")[$index]
Related
I can't update my table. Anyone to help me
Code in view :
<form method="post"
<?php echo form_open_multipart('home/savecovid') ?>>
<div class="form-group">
<input type="radio" name="anotasi1" value="Positif"
<?php echo set_radio('anotasi','positif'); ?>/>Positif<br>
<input type="radio" name="anotasi1" value="Negatif"
<?php echo set_radio('anotasi','negatif'); ?>/>Negatif<br>
<input type="radio" name="anotasi1" value="Netral"
<?php echo set_radio('anotasi','netral'); ?>/>Netral
</div>
<button type="submit" name="saveCovid">Submit</button>
</form>
Code in my controller :
public function savecovid()
{
$db = \Config\Database::connect();
$anotasi1 = $this->request->getPost('anotasi1');
$builder = $db->table('tbl_anotasi');
$data = [
'anotasi' => $anotasi1
];
$builder->set('anotasi');
$builder->update($data);
}
I want to update my table
My Table
use model oky and entity
$model =new Mymodel();
$model->update($id,$data)
go this link
https://codeigniter.com/user_guide/models/model.html
I have work one project and there user can follow and unfollow another user like instagram. I had done to user listing and follow and unfollow system. But when i follow more than one user, in user listing panel follow and unfollow button both show. if i follow 3 user then user listing panel 3 button show. One button is Unfollow and two button is follow shows. My view side code is below:
<?php foreach($latestCompany as $companyRow):?>
<div class="col-md-6">
<div class="home-list-pop">
<div class="col-lg-3 col-md-3 col-sm-4 col-xs-12">
<img src="<?= base_url('uploads/company_logo/'.$companyRow['company_logo']);?>" alt="" />
</div>
<div class="col-lg-9 col-md-9 col-sm-8 col-xs-12 home-list-pop-desc">
<h3><?= $companyRow['company_name'];?></h3>
<h4><?= $companyRow['company_city'].', '.$companyRow['company_country'];?></h4>
<div class="btn-listing">
<?php foreach($agentNetwork as $agentNetworkRow):?>
<?php if($agentNetworkRow['follower_id'] == $memberId && $agentNetworkRow['following_id'] == $companyRow['member_id']):?>
Unfollow
<?php else:?>
Follow
<?php endif;?>
<?php endforeach;?>
</div>
</div>
</div>
</div>
<?php endforeach;?>
My Controller code:
<?php
public function index(){
$member_userdata = $this->session->userdata('isMemberLoggedIn');
$memberId = $member_userdata['memberId'];
$latestCompany = $this->member_model->getLatestMember();
$agentNetwork = $this->member_model->getAgetNetworkById($memberId);
$this->load->view('includes/header');
$this->load->view('index', ['latestCompany'=>$latestCompany, 'agentNetwork'=>$agentNetwork]);
$this->load->view('includes/footer');
}
?>
My Model code
public function getLatestMember(){
$this->db->order_by('member_id', 'DESC');
$query = $this->db->get_where('li_members', array('membership_status'=>'Y'));
return $query->result_array();
}
public function getAgetNetworkById($memberId){
$query = $this->db->get_where('li_agent_network', array('follower_id'=>$memberId));
return $query->result_array();
}
Below i provide sample image
Above image i follow both user, so i want show only unfollow button.
First you need to use the <form> Tag and you just need to use $(this).closest("form").serialize(); to get closest form data and then simply use $(".mybtn").not($this).attr("disabled", false); to enable all button and not the button which is pressed.
Demo Code:
$(document).ready(function() {
//Stops the submit request
$("#form").submit(function(e) {
e.preventDefault();
});
//checks for the button click event
$(".mybtn").click(function(e) {
var $this = $(this)//use as selector
dataString = $(this).closest("form").serialize(); //get closest form only
console.log(dataString)
$this.attr("disabled", true); //set attr disable
//enable all button not (this)
$(".mybtn").not($this).attr("disabled", false);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<form name="form">
<button class="btn btn-success user_status mybtn" >FOLLOW</button>
<input type="hidden" name="followId" value="1">
<input type="hidden" name="userId" value="1">
</form>
<form name="form">
<button class="btn btn-primary user_status mybtn" >UNFOLLOW</button>
<input type="hidden" name="followId" value="2">
<input type="hidden" name="userId" value="1">
</form>
would like some advice/help on how to connect form controller to post form method in my CI site. I want to data submitted from one viewer to another. Thank you for the help!!
Here is the controller Im using (Form.php), took if from another site:
Form.php
<?php
class Form extends CI_Controller {
public function __construct() {
parent::__construct();
}
// Show form in view page i.e view_page.php
public function form_show() {
$this->load->view("addEdit");
}
// When user submit data on view page, Then this function store data in array.
public function data_submitted() {
$data = array(
'file_name' => $this->input->post('file'),
'title' => $this->input->post('title')
);
// Show submitted data on view page again.
$this->load->view("profile", $data);
}
}
?>
Its to connect to this code:
addEdit.php
<form method="post" action="postAction.php" enctype="multipart/form-data">
<div class="form-group">
<label>Image</label>
<?php if(!empty($imgData['file_name'])){ ?>
<img src="uploads/images/<?php echo $imgData['file_name']; ?>">
<?php } ?>
<input type="file" name="image" class="form-control" >
</div>
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control" placeholder="Enter title" value="<?php echo !empty($imgData['title'])?$imgData['title']:''; ?>" >
</div>
Back
<input type="hidden" name="id" value="<?php echo !empty($imgData['id'])?$imgData['id']:''; ?>">
<input type="submit" name="imgSubmit" class="btn btn-success" value="SUBMIT">
</form>
When I first tried to make it work I got this error:
404 Page Not Found
The page you requested was not found.
http://culturedkink.com/index.php/register/postAction.php(the url)
postAction.php is the form Im trying to get the data to work from
The end result is to have info submitted from addEdit.php be seen on profile.php with the help of postAction.php
make routes for it first.
config/routes.php
$route['add'] = 'Controller_name/data_submitted';
$route['edit/(:any)'] = 'Controller_name/data_submitted/$1';
where is your add/edit button put this there
for add
Add New
for edit button
$row['id'] is an example i m giving. you can get data by name and id..whatever you want.
Update
//controller
public function data_submitted($id=0) {
$data=array();
$data['dataDetails']=$this->get_profile_data_by_id($id);
$data['view'] = 'folder_name/addEdit';
if ($id > 0) {
$profileArray = [
'file_name' => $this->input->post('file'),
'title' => $this->input->post('title')
];
if ($this->User_model->editById($id, $profileArray)) {
$id = $id;
}
}
else{
$profileArray = [
'file_name' => $this->input->post('file'),
'title' => $this->input->post('title')
];
if ($this->User_model->add($id, $profileArray)) {
$id = $id;
}
}
$this->load->view("profile", $data);
}
form view page
<?php echo isset($dataDetails) ? "Update" : "Add"; ?>
First check your form method and action. Your action does not exist. First check how CI works with form. The action should have a method declared in a controller. The url looks like this,
When you submit the form the data will be submitted in this method. Whatever you need to do with this form data you can do that in this method.
I'm new to laravel. I'm saving some checkbox value to the database using loop the loop work but it only saves the last value in the array to the database.
this is my form
<form action="{{url('resortf')}}" method="post" enctype="multipart/form-data">
<input hidden name="h_id" value="{{ $hotel->id}}">
#foreach($facility as $facilities)
<div class="col-md-4">
<img src="{{$facilities->image}}" width="50px" height="50px;">
<label>{{$facilities->name}}</label>
<input type="checkbox" value="{{$facilities->id}}" name="facilities[]">
</div>
#endforeach
<div class="row">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-success" value="Next">
<input type="reset" class="btn btn-success" value="Reset">
</div>
</form>
form working fine; $facilities and $hotel are passed from the controller.
this is the store function
public function store(Request $request) {
$resortfacility = new Resortfacility;
$loop = $request->get('facilities');
foreach ($loop as $value){
$resortfacility->h_id = $request->get('h_id');
$resortfacility->f_id = $value;
$resortfacility->save();
}
}
is there any other way to do this that works?
Your problem occurs because you create one instance of Resortfacility, and then you fill in its values and save. The problem is, every time you perform changes to this object in a loop, you are updating existing object and that's why there is only one record in your database, the last one from the loop.
Try making new instance of Resortfacility inside the loop like this:
public function store(Request $request) {
$loop = $request->get('facilities');
foreach ($loop as $value){
$resortfacility = new Resortfacility;
$resortfacility->h_id = $request->get('h_id');
$resortfacility->f_id = $value;
$resortfacility->save();
}
}
The other answer is correct but, bulk insertion might be helpful, as creating new object within foreach loop will make query for every record.
public function store(Request $request)
{
$facilities = $request->get('facilities');
$data=array();
foreach($facilities as $facility)
{
$data[] =[
'f_id' => $facility,
'h_id' => $request->get('h_id'),
];
}
}
Resortfacility::insert($data);
This return of print_r($query->result()); would be:
Array ( [0] => stdClass Object ( [guest_name] => Test Name [guest_gender] => Male [guest_nic_pp_dl] => 123456789 ) )
What I need is to pass those values into input text boxes, radio buttons and dropdowns in the view respectilvely.
For example, I need 'guest_name' to be in an input, 'guest_gender' value to be selected on the view, and dropdown value corresponding to 'guest_nic_pp_dl' to be selected on a dropdown (HTML select).
Controller:
function get_customer_details() {
$guest_name = $this->input->post('guest_name');
$this->banquet_model->talk_to_new_guest_table($guest_name);
$this->load->view('/main/banquet_view');
}
Model:
function talk_to_new_guest_table($guest_name) {
$query = $this->db->query(" SELECT guest_name, guest_gender, guest_nic_pp_dl
FROM new_guest
WHERE guest_name LIKE '$guest_name%'
LIMIT 1 ");
if($query->num_rows()>0) {
return $query->result();
}
else {
return 0;
}
}
View:
<div class="control-group">
<label for="guest_name" class="control-label"><i class="icon-user"></i> Name: </label>
<div class="controls">
<div class="input-append">
<input type="text" id="appendedInputButtons" class="span2" name="guest_name" value="<?php echo set_value('guest_name'); ?>">
<input class="btn" type="submit" name="searchGuest" value="Search">
</div>
<?php echo form_error('guest_name'); ?>
</div>
make some changes in
Controller :
$guest=$this->banquet_model->talk_to_new_guest_table($guest_name);
//creating data array from returned result
$data['guest_name']=$guest->guest_name;
$data['guest_gender']=$guest->guest_gender;
$data['guest_nic_pp_dl']=$guest->guest_nic_pp_dl;
//loading view with data
$this->load->view('/main/banquet_view',$data);
more important all these data array element will be available as variable on view page like
$data['guest_gender'] as $guest_gender
The answers from Rajeev Ranjan and Prasanth are ok but on the line return $query->result(); you can do thisreturn $query->row(); the reason is because the result() returns an array of objects which needs to be iterated while the row() object returns a single object which you can reference without iterating with a loop. I hope this will help
Try something on the lines of:
Controller:
function get_customer_details() {
$guest_name = $this->input->post('guest_name');
$data = $this->banquet_model->talk_to_new_guest_table($guest_name);
if ($data != 0) {
$this->load->view('/main/banquet_view', $data);
}
}
Model:
function talk_to_new_guest_table($guest_name) {
$query = $this->db->query(" SELECT guest_name, guest_gender, guest_nic_pp_dl
FROM new_guest
WHERE guest_name LIKE '$guest_name%'
LIMIT 1 ");
if($query->num_rows()>0) {
return $query->result();
}
else {
return 0;
}
}
View:
<div class="control-group">
<label for="guest_name" class="control-label"><i class="icon-user"></i> Name: </label>
<div class="controls">
<div class="input-append">
<input type="text" id="appendedInputButtons" class="span2" name="guest_name" value="<?php echo $guest_name; ?>">
<input class="btn" type="submit" name="searchGuest" value="Search">
</div>
<?php echo form_error('guest_name'); ?>
</div>