CodeIgniter update error? - codeigniter

I'm having a problem on updating here is my view:
foreach($attributes as $dataIndex ){
$firstName = $dataIndex['first_name'];
$f = array(
'name' => 'update_fname',
'value' => $firstName
);
echo "<fieldset><label>First Name:</label></br>".form_input($f)."</br>";
echo form_open(base_url()."index.php/home/save_update");
echo "<label>".form_submit('save_updates','Save')."</label>";
here I already showed in text field the obtained specific attributes from my database but the thing that I want to do is to save the changed item in the text field and save it to the database with a specific id.
here is my controller along with the get attributes:
public function get_attributes($patientID){
if($patientID=== null){
return;
}
$data['attributes'] = $this>patient_manager>get_attributes($patientID);
$this->load->view("edit_patient_view", $data);
}
public function save_update(){
if($this->input->post("save_updates") === false){
return;
}else{
$update = array( 'first_name' => $this->input->post('update_fname', true));
$this->patient_manager->save_attributes_by_id($update);
}
} this is for saving
here is my model:
public function get_attributes($patientID){
$this->db->select('first_name, mid_name,
last_name, age, gender, b_day, marital_status, address, contact_num,
occupation, medical_record');
$this->db->from('patients');
$this->db->where('patient_id', $patientID);
return $this->db->get()->result_array();
}
public function save_attributes_by_id($patientID, $update){
$this->db->where('patient_id', $patientID)
->update('patients', $update, $patientID);
}
and I'm having this error:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: database/DB_active_rec.php
Line Number: 427
A Database Error Occurred
Error Number: 1054
Unknown column 'Array' in 'where clause'
UPDATE patients SET first_name = 0 WHERE patient_id = Array
Filename: C:\xampp\htdocs\system\database\DB_driver.php
Line Number: 330
I think the the system can't get the patient_id from my database.
pls help me thatks in advance.

Try this one
Make array as
$f = array(
'update_fname' => $firstName);
in single updation array set like this, we can use batch updation also
go through this link
http://ellislab.com/codeigniter/user-guide/database/active_record.html#update

Related

CodeIgniter 3 code does not add data to database into 2 different tables (user_info & phone_info)

The problem is when I entered a new name no data is added. A similar thing happen when I entered an already existing name. Still, no data is added to the database. I am still new to CodeIgniter and not entirely sure my query builder inside the model is correct or not.
In the Model, I check if the name already exists insert data only into the phone_info table. IF name does not exist I insert data into user_info and phone_info.
Controller:
public function addData()
{
$name = $this->input->post('name');
$contact_num = $this->input->post('contact_num');
if($name == '') {
$result['message'] = "Please enter contact name";
} elseif($contact_num == '') {
$result['message'] = "Please enter contact number";
} else {
$result['message'] = "";
$data = array(
'name' => $name,
'contact_num' => $contact_num
);
$this->m->addData($data);
}
echo json_encode($result);
}
Model:
public function addData($data)
{
if(mysqli_num_rows($data['name']) > 0) {
$user = $this->db->get_where('user_info', array('name' => $data['name']))->result_array();
$user_id = $user['id'];
$phone_info = array(
'contact_num' => $data['contact_num'],
'user_id' => $user_id
);
$this->db->insert('phone_info',$phone_info);
} else {
$user_info = array(
'name' => $data['name']
);
$this->db->insert('user_info', $user_info);
$user = $this->db->get_where('user_info', array('name' => $data['name']))->result_array();
$user_id = $user['id'];
$phone_info = array(
'contact_num' => $data['contact_num'],
'user_id' => $user_id
);
$this->db->insert('phone_info', $phone_info);
}
}
DB-Table user_info:
DB-Table phone_info:
Extend and change your model to this:
public function findByTitle($name)
{
$this->db->where('name', $name);
return $this->result();
}
public function addData($data)
{
if(count($this->findByTitle($data['name'])) > 0) {
//.. your code
} else {
//.. your code
}
}
Explanation:
This:
if(mysqli_num_rows($data['name']) > 0)
..is not working to find database entries by name. To do this you can use codeigniters built in model functions and benefit from the MVC Pattern features, that CodeIgniter comes with.
I wrapped the actual findByName in a function so you can adapt this to other logic and use it elswehere later on. This function uses the query() method.
Read more about CodeIgniters Model Queries in the documentation.
Sidenote: mysqli_num_rows is used to iterate find results recieved by mysqli_query. This is very basic sql querying and you do not need that in a MVC-Framework like CodeIgniter. If you every appear to need write a manual sql-query, even then you should use CodeIgniters RawQuery methods.

Problems using batch_update in codeigniter

I read similar batch_update Qs but I still have issues. Thanks in advance for help! I receive error message ("One or more rows submitted for batch updating is missing the specified index.") for the following code:
Controller:
public function do_edit_page(){
$id = $this->input->post('page_id', TRUE);
$link_title = $this->input->post('page_link_title', TRUE);
$link = $this->input->post('page_link_sub_title', TRUE);
$this->content_model->update_links($id, $link_title, $link);
$this->index();
}
Model:
public function update_links($id, $link_title, $link){
$data = array(
array(
'page_id' => $id,
'link_title' => $link_title,
'link' => $link
)
);
$this->db->update_batch('content_links', $data, $id);
}
Check the CI documentation: http://ellislab.com/codeigniter/user-guide/database/active_record.html
The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.
so the third parameter should be a column not data.

CodeIgniter Dropdown from query

I am new in codeIgniter and I having a bit of trouble with my database and dropdown menu.
Here is my function to get the information I need...
protected $tbl_name = 'club';
public function get($id = 0, $object = TRUE)
{
// select * from users where id = 0
// check if id is provided
if($id) {
// id provided - retrieve a specific user
$query = $this->db->get_where($this->tbl_name, array('id' => $id));
// check if object type is requested
if($object) {
// object is requested
$data = $query->row();
}
else {
// array is requested
$data = $query->row_array();
}
}
else {
// id not provided - retrieve all users
$query = $this->db->get($this->tbl_name);
// check if object type is requested
if($object) {
// object is requested
$data = $query->result();
}
else {
// array is requested
$data = $query->result_array();
}
}
return $data;
}
Here is where I call it in my controller
$data['clubname'] = $this->club_model->get();
and this is in my view for the dropdown
<tr><td><?php echo form_label('Club Name: ', 'clubname'); ?></td><td><?php echo form_dropdown('clubname', $clubname['name']); ?></td><td><?php echo form_error('clubname'); ?></td></tr>
but I get these errors
A PHP Error was encountered
Severity: Notice
Message: Undefined index: name
Filename: individual/individual_club.php
Line Number: 7
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: helpers/form_helper.php
Line Number: 331
What Am I doing wrong?
The problem lies in your form_dropdown('clubname', $clubname['name']) call. The second parameter is wrong. form_dropdown expects an array. See the documentaiton
From your query's results, you would need to build an array of clubs. Something along the lines of :
// array is requested
$data = array();
foreach ($query->result_array() as $row)
{
$data[$row['club_id']] = $row['club_name'];
}
Replace club_id and club_name with your table's column names for the name and id of the clubs. After that, change your form_dropdown to form_dropdown('clubname', $clubname).
Like this, $clubname is an array of clubs.
Hope this helps!

how can I catch a single variable in array of Object in CodeIgniter?

I have probabbly a eal probleme with array . I have a request :
in my model
public function getHomme ($limit ,$offset)
{
$this->db->select('id,nom,prix,nom_marques,nom_path,quantite,semelle_interieure,libelle_fermeture,libelle_style,libelle_talon,libelle_doublure,libelle_semelle,libelle_dessus');
$this->db->from('chaussure');
$this->db->join('gnr_convenir', 'gnr_convenir.identifiant_chaussure = chaussure.id');
$this->db->join('images', 'images.id_chaussure = chaussure.id');
$this->db->join('marques', 'marques.idmarques = chaussure.identifiant_marques');
$this->db->join('fermeture', 'fermeture.idfermeture = chaussure.identifiant_fermeture');
$this->db->join('style', 'style.idstyle = chaussure.identifiant_style');
$this->db->join('talon', 'talon.idtalon = chaussure.identifiant_talon');
$this->db->join('doublure', 'doublure.iddoublure = chaussure.identifiant_doublure');
$this->db->join('materiauSemelle', 'materiauSemelle.idmateriauSemelle = chaussure.identifiant_semelle');
$this->db->join('dessus', 'dessus.iddessus = chaussure.identifiant_dessus');
$this->db->where('identifiant_genre', 1);
$this->db->order_by("id", "desc");
$this->db->limit($limit,$offset);
$query = $this->db->get();
$ligne= $query->num_rows();
if($query->num_rows()>0)
{
foreach($query->result()as $row)
{
$data[] = $row;
}
$data['ligne'] = $ligne;
return $data;
}
so here I need 2 things:
one the object on data and the other is $ligne (number of rows )
so it seems like this when I try to make var_dump($data)
array
'rows' =>
array
0 =>
object(stdClass)[21]
public 'id' => string '89' (length=2)
public 'nom' => string 'zoro' (length=4)
public 'prix' => string '12460.00' (length=8)
1 =>
object(stdClass)[22]
public 'id' => string '87' (length=2)
public 'nom' => string 'adizero' (length=7)
public 'prix' => string '124000.00' (length=9)
'ligne' => int 2
but when I try to write:
var_dump($data['ligne']) in my controller
I have an error message
A PHP Error was encountered
Severity: Notice
Message: Undefined index: ligne
Filename: controllers/client.php
Line Number: 143
null
I need those 2 data in my view, so in my view I thought to use $ligne like this:
$numberLigne = $ligne ;
As a test, could you echo ligne in your model to make sure that your join query is working right? I have no way of knowing without seeing your full table.
Also, any reason why you use:
if($query->num_rows()>0)
Instead of
if($ligne > 0):
Being that you just set that variable?

Magento Sales Order Grid Joining Table

I am wondering if anyone out there has had much luck joining the order items table to the sale order grid and being able to filter correctly?
I have already been able to complete the join, like so:
protected function _prepareCollection()
{
parent::_prepareCollection();
$collection = Mage::getResourceModel($this->_getCollectionClass())
->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'skus' => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ",")'),
'names' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ",")'),
)
);
$collection->getSelect()->group('entity_id');
$this->setCollection($collection);
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
But I am now trying to add the column filter like so:
protected function _addColumnFilterToCollection($column)
{
if($this->getCollection() && $column->getFilter()->getValue())
{
if($column->getId() == 'skus')
{
$this->getCollection()->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'skus' => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR "|")'),
)
)->getSelect()
->group('`main_table`.entity_id')
->having('find_in_set(?, `main_table`.skus)', $column->getFilter()->getValue());
return $this;
}
if($column->getId() == 'names')
{
$this->getCollection()->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'names' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ",")'),
)
)->getSelect()
->group('`main_table`.entity_id')
->having('find_in_set(?, names)', $column->getFilter()->getValue());
return $this;
}
}
return parent::_addColumnFilterToCollection($column);
}
Each time I try to filter I get this error:
"Column not found: 1054 Unknown column 'main_table.names' in 'having clause...".
Please let me know if you have experienced this before and it you have any pointers?
I had the same problem, when i took al closer look at the problem I did see that the pagination query was causing the error. Alter the getSelectCountSql() in your model, add the following
$countSelect->reset(Zend_Db_Select::HAVING);
Your page will be shown, but you have to alter the getSelectCountSql() tot get the correct number of pages and results.

Resources