I'm having a problem with an update of one table in the db. I always use the same approach and I have no problem. Although in this case I have an issue...
The code:
the view:
<fieldset>
<legend>Modifica Dati Check-in Cliente</legend>
<?php
$passeggeri='placeholder="Passeggeri"';
$animali='placeholder="Animali"';
$note='placeholder="Note"';
echo form_open('site/esegui_modifica_record_check_in');
echo form_hidden('id',$id[0]->id);
echo form_hidden('rif_parcheggio',$id[0]->rif_parcheggio);
echo form_input('Passeggeri',set_value('passeggeri',$id[0]->passeggeri),$passeggeri);
echo form_input('Animali',set_value('animali',$id[0]->animali),$animali);
echo form_input('Note',set_value('note',$id[0]->note),$note);
?> <br/> <input type="datetime-local" name="data_in_inserita" value="<?php echo strftime('%Y-%m-%dT%H:%M', strtotime($id[0]->data_in));?>" /> <?php
echo form_submit('submit','Modifica');
echo form_close();
?>
</fieldset>
controller:
function esegui_modifica_record_check_in() {
$this->load->model('membership_model');
if($query = $this->membership_model->esegui_modifica_record_check_in())
{
$this->load->view('check_in_auto_succesful');
}
else
{
$this->load->view('admin');
}
}
model:
function esegui_modifica_record_check_in() {
$update_record_check2= array (
'passeggeri' => $this->input->post('passeggeri'),
'animali' => $this->input->post('animali'),
'note' => $this->input->post('note'),
'data_in' => $this->input->post('data_in_inserita')
);
$id=$this->input->post('id');
$this->db->where('id',$id);
$insert = $this->db->update('check2', $update_record_check2);
return $insert;
}
The query is executed but the values are updates as zero (0) character for the modified fields.
It's very strange! this is my first problem with the update function of codeigniter
I don't have a clear idea. Could you please help me?
thanks a lot
by
First thing to debug is to make sure that values are really being passed into your model method. Either debug first on the controller or inside you model:
Controller:
function esegui_modifica_record_check_in()
{
var_dump($this->input->post());
// rest of codes
}
OR
Model:
function esegui_modifica_record_check_in()
{
var_dump($this->input->post());
// rest of codes
}
Then, assuming you haven't fixed it yet. You have a case mismatch on the input name=""
echo form_input('Passeggeri',set_value('passeggeri',$id[0]->passeggeri),$passeggeri);
'passeggeri' => $this->input->post('passeggeri'),
// Passeggeri !== passeggeri
echo form_input('Animali',set_value('animali',$id[0]->animali),$animali);
'animali' => $this->input->post('animali'),
// Animali !== animali
echo form_input('Note',set_value('note',$id[0]->note),$note);
'note' => $this->input->post('note'),
// Animali !== animali
You should take that also into account. You need to follow the same case letters. Make them the same
Either:
echo form_input('Passeggeri',
$this->input->post('Passeggeri')
Or
echo form_input('passeggeri',
$this->input->post('passeggeri')
Sidenote:
As my suggestion above, I would just make it simple and use a normal form.
<form method="POST" action="<?php echo site_url('site/esegui_modifica_record_check_in'); ?>">
<input type="text" name="passeggeri" value="<?php echo $id[0]->passeggeri; ?>" placeholder="Passeggeri" />
<!-- and other text box inputs -->
</form>
Related
New to PHP and CodeIgniter framework.
I have a single question in the database and i can save the answer properly. But i'm struggling to save the answer to the allocated user_id and output the score to the user in the end of the test.
If answer is correct, it will show 1/1 else 0/1. Can someone please assist me?
** From table user_login only id is needed, table results is this and table exercises is that.
This is my Controller:
class Home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Exercise');
}
function exercise_demo() {
//get the exercise out of the database to give to
//the view so that it can be shown.
$this->load->model('Exercise');
//give this to the view in an array
$data = array();
$data['exercise'] = $this->Exercise->load_exercise('Geography', '2');
$this->load->view('exercise_demo', $data);
}
function storedemo() {
$answer = $this->input->post('answer');
$id = $this->input->post('exerciseid');
$this->load->model('Exercise');
$correct = $this->Exercise->check_answer($id, $answer);
//store this in a database table
$data = array(
'answer_given' => $this->input->post('answer'),
'exercise_id' => $this->input->post('exerciseid'),
'result_tf' => $correct,
'user_id' => '0'
);
//transfer data to model
$this->Exercise->insert_result($data);
} }
This is my Model:
class Exercise extends CI_Model {
function __construct() {
parent::__construct();
}
public function load_exercise($topic, $difficulty) {
return $this->db->get_where('exercises', array('topic' => $topic, 'difficulty' => $difficulty))->row();
}
function insert_result($data) {
$this->db->insert('results', $data);
$is_inserted = $this->db->affected_rows() > 0;
if ($is_inserted) {
echo 'Answer saved!';
} else {
echo 'Something went wrong. No insert ):';
}
}
public function check_answer($id, $answer) {
//get the exercise out of the db
$exercise = $this->db->get_where('exercises', array('id' => $id))->row();
return $exercise->solution == $answer ? '1' : '0';
}
}
This is my View exercise_demo:
<div>
<form method="post" action="<?= base_url('index.php/home/storedemo') ?>" >
<div id ="exercise">
<?= $exercise->exercise ?>
</div>
<input type ="hidden" name ="exerciseid" value="<?= $exercise->id ?>">
<input type="submit">
<br />
<br />
<h3> SCORE: </h3>
<div id="result">
<h3><?= $result ?> / 1 </h3> (comment this out to work properly)
</div>
</form>
</div>
</html>
Let's work from start to end.
function storedemo gets the posted answer
It sends it to check_answer to find if it's correct or not.
check_answer returns the string 'correct' if true and 'incorrect' if false.
storedemo then uses insert_result to place the return of check_answer into the database column result_tf
result_tf is a tiny int. You're trying to pass a string into an int column. This is most definitely not going to work.
Have check_answer return the ints 1 or 0 instead of the strings 'correct' or 'incorrect' and that will allow you to insert that into result_tf
If your code still isn't giving expected results, it's because there are other problems. If so, debug it yourself a little longer now that you're a little closer, if you're still absolutely lost after a while, I'd recommend you post a new question.
Question Updated:
I am using codeigniter, The problem that i'm having is that, if the array contains values then the form elements will show. if the array is empty the entire form will not show. so imagine this as a edit form where a person has selected to edit a record. and lets say for whatever reason the record that is being edited has no records, its completly empty. and lets also say the id must be visible and be editable regardless of it being empty or not. so what happens is if the record is empty the entire form elements will not show. so what i'm trying to do is have it display regardless if there are records or not.
<?php
//controler
public function show()
{
$this->load->model('my_model');
$data = array(
'data' => $this->my_model->getdata();
);
$this->load->view('somepage', $data);
}
//model
public function my_model()
{
$q = $this->db->get_where('some query', array('id' => $id));
if($q->num_rows() == 1)
{
foreach($q->result() as $row)
{
$rows[] = $row;
}
return $rows;
}
else
{
return array();
}
}
//view
//note: this will only work if the array has data, otherwise everything between the foreach statement wont show.
<?php foreach($data as $row) : ?>
<input type="text" name="something" value="<?=$row->column1;?>">
<input type="text" name="something" value="<?=$row->column2;?>">
<input type="text" name="something" value="<?=$row->column3;?>">
<input type="text" name="something" value="<?=$row->column4;?>">
<input type="text" name="something" value="<?=$row->column5;?>">
<?php endforeach; ?>
in your view
<?php
if (!empty($data)) {
var_dump($data);
} else {
//print out nothing
}
?>
or you can do following
<p><?= (!empty($data)) ? $data : "" ?></p>
Hi I'm trying to understand session variables, in particular using them with arrays. In the example code below, the user enters a letter and I want to add that submission to a session variable so that the next time the user submits a letter I don't lose the previous entry.
So if the user enters 'e' the array displays 'e', and if the user then picks 's' then the array will now display 'e' and 's'. This is my first experiment with PHP and sessions are proving a little difficult to wrap my head around. Can anyone help me understand how to go about getting the result I want, or where I have gone wrong in the code below? Many thanks in advance.
<?php
session_start();
function example()
{
$_SESSION['lettersGuessed'] = array();
$userLetter = $_GET['input'];
array_push($_SESSION['lettersGuessed'],$userLetter);
print_r($_SESSION['lettersGuessed']);
}
if (strlen($_GET['input'])==1) {
if (ctype_lower($_GET['input']))
{
echo "The user-submitted letter is lowercase.<br>";
example();
}
else
{
echo "Invalid submission<br>";
}
}
?>
<form action="" method="get">
<input name="input" value="Enter a letter!" />
<input type="submit" value="Submit" />
</form>
try it out without array_push in a more simple way
There is a simple change in example function.
Following is complete code
<?php
session_start();
function example() {
$userLetter = $_GET['input'];
$_SESSION['lettersGuessed'][] = $userLetter;
print_r($_SESSION['lettersGuessed']);
}
if (strlen($_GET['input']) == 1) {
if (ctype_lower($_GET['input'])) {
echo "The user-submitted letter is lowercase.<br>";
example();
} else {
echo "Invalid submission<br>";
}
}
?>
<form action="" method="get">
<input name="input" value="Enter a letter!" />
<input type="submit" value="Submit" />
</form>
?>
The problem is that your line in the beginning of example() resets the session variable to a blank array every time the function is called.
Update your example() function as follows:
function example()
{
$_SESSION['lettersGuessed'][] = $_GET['input'];
print_r($_SESSION['lettersGuessed']);
}
Thankfully, PHP is loosely-typed, so you don't have to manually define lettersGuessed as an array. Simply using [] afterwards will cause it to be handled as an array, and then using the = assignment operator will push $_GET['input'] into it.
I have two CHtml::submitButton() in my form, namely "Accept" and "Reject". Each of the buttons has a specific ID ... When any of the two are pressed, i would like to trigger an action in my controller. Below are the code snippets.
=========================================================
View
<?php
echo CHtml::beginForm('mycontrollerName/AcceptUserRegistration','get');
?>
<?php echo CHtml::submitButton('Accept', array('id' => 'accept')); ?>
<? echo ' '; ?>
<?php echo CHtml::submitButton('Reject', array('id' => 'reject')); ?>
<?php echo CHtml::endForm(); ?>
========================================================
Controller
public function actionAcceptUserRegistration() {
$value = $_GET['id'];
if($value == "accept"){
//will do something here
}
if($value == "reject"){
//will do something here.
}
}
======================================================================
When i implement it this way. the get value in the controller side is empty. What I'm I doing wrong? Or is there any other way around this?
You forgot to give the submit button a name (unless your framework does some magic which we cannot know about - you should really post the generated HTML code!).
If you do so, the value in $_GET['buttonname'] will be its value (not its id), i.e. Accept or Reject. This becomes pretty messy as soon as you get into i18n, so you might want to use different names for the buttons and then check isset($_GET['buttonname1']) and isset($_GET['buttonname2'])
Here is a code example for what you are trying to achive:
VIEW:
<?php
echo CHtml::beginForm('mycontrollerName/AcceptUserRegistration','get');
?>
<?php echo CHtml::submitButton('Accept', array('id' => 'accept', 'name' => 'accept')); ?>
<? echo ' '; ?>
<?php echo CHtml::submitButton('Reject', array('id' => 'reject', 'name' => 'reject')); ?>
<?php echo CHtml::endForm(); ?>
Note: I added the name parameter. As name is not displayed to the user, you can use accept instead of Accept.
CONTROLLER:
public function actionAcceptUserRegistration() {
if(isset($_GET['accept'])) {
//accepted
} else if(isset($_GET['reject'])) {
//rejected
}
//I recommend using POST method instead of GET for posting the form.
}
I am currently learning CodeIgniter and I am looking to develop a simple example consisting of 2 forms, let’s call them form a and form b. Form a has one edit field called “LastName” and form b will displays a list of all names in a table matching the value in “LastName” something like
select first_name, last_name from my_table where last_name = :LastName
I need this example to understand the mechanisms of passing variables from one form and controller to another. I am guessing this is using a method like $_POST but no examples on the web look very clear.
So you would have a form...
<form action="/controller/name/" method="post">
<p>Last Name: <input type="text" name="LastName" /></p>
<p><input type="submit" value="Submit"/></p>
</form>
Then in your controller (assuming your already connected to the database):
function index() {
// This is the last name from the form
$LastName = $this->input->post('LastName');
// Create the query
$sql = "SELECT * FROM users WHERE last_name = ?";
// Execute it, replacing the ? with the last name from the form
$query = $this->db->query($sql, array($LastName));
// Show results
foreach ($query->result() as $row) {
echo $row->first_name . "<br />";
echo $row->last_name;
}
}
Your view folder: application/view/form_a.php, application/view/forma_b.php
Your controller folder: application/controller/controller_name.php
Your model folder: application/model/related_model_name.php
Your controller_name.php file:
class Controller_name extends Controller
{
function index()
{
$this->load->view('form_a'); //this loads the form
}
function results()
{
$name= $this->post->input('last_name');
$this->load->model('related_model_name'); //this is the model to fetch the data
$data['names']= $this->related_model_name->searchByLastName($name);
if(!empty($data))
$this->load->view('form_b', $data);
}
}//eoc
Your related_model_name.php file
class Related_model_name extends Model
{
function __construct()
{
parent::Model();
}
function searchByLastName($name)
{
$query = $this->db->get_where('table_name', array('last_name'=>$name));
if($query->nu_rows() > 0)
return $query->results();
}//eof
}//eoc
Your form_b.php view file
do a print_r($data) and that should give you an idea of how to display the data.
it maybe something like
foreach ($names as $name)
{
echo $name->name;
}
I realize this thread is old, but I am new to CodeIgniter and have been working with a similar challenge. My challenge is to create a search form that finds growers in a specific zip code. Here is my solution. It's simpler than I expected and might help someone else.
This code assumes you are connected to your database and have a standard MVC CI application, etc.
I handle most of this task in the model and view, but I do have this method in my controller:
public function result()
{
$zipcode = $this->input->post('zip_code');
$query = $this->db->get_where('growers', array('zip LIKE' => $zipcode));
return $query->result_array();
}
In my model, I used the following method:
public function result()
{
$zipcode = $this->input->post('zip_code');
$query = $this->db->get_where('growers', array('zip LIKE' => $zipcode));
return $query->result_array();
}
I have three views -- one page (located in views/pages/search.php), and two widgets --one for the search form and one for the search results (located in views/widgets/result).
I have the search result form on the same page that the results display. However, each section is contained in its own view file, which I have placed in views/widgets. The code for this section in the page view is:
<div class="search" style="margin-top:0px;">
<?php
$this->load->view('widgets/search');
?>
</div>
</div>
<div id="results">
<div id="grower-info">
<?php
$this->load->view('widgets/result');
?>
</div>
</div>
The search form widget is:
<form action="search-results" method="post">
<input type="text" maxlength="10" name="zip_code" value="zip code" size="10">
<input type="submit" name="submit" value="SEARCH">
</form>
The search result widget is:
<?php
$results = $this->pages_model->result();
foreach ($results as $result)
{
echo '<h4>'.$result['company'].'</h4>';
echo $result['address_1'] . ' ' . $result['address_2'].'<br>';
echo $result['city'].', ' . $result['state'] . ' ' . $result['zip'].'<br>';
echo 'Phone: ' . $result['office_phone'].'<br>';
echo 'Fax: ' . $result['office_fax'].'<br>';
echo 'Website: ' . $result['website'].'<br>';
echo '<br>';
echo '<hr>';
}
if (count($results) < 1) {
echo 'No results found. Please try your search again, or try another search.';
}
?>
I hope that helps someone!