I am trying to pass a value from my database to an input, but I throw the following error.I'm not sure if for this case you should use return $ this-> db-> get () -> row (); in the model. Any help is welcome.
A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass could not be converted to string
Filename: usuarios/vConsulta_Horarios.php
Line Number: 590
Backtrace:
File: C:\xampp\htdocs\SAE\application\views\usuarios\vConsulta_Horarios.php
Line: 590
Function: _error_handler
File: C:\xampp\htdocs\SAE\application\controllers\cCalendar.php
Line: 26
Function: view
File: C:\xampp\htdocs\SAE\index.php
Line: 315
Function: require_once
Model (mCalendar)
public function lunes(){
$this->db->select('MIN(horario.hrs_ini)');
$this->db->from('horario');
$this->db->join('usuarios','horario.rut_usu = usuarios.rut_usu');
$this->db->where('usuarios.rut_usu','17811942-4');
$this->db->where('horario.lunes','ATTE. ESTUDIANTES');
return $this->db->get()->row();
Controller (cCalendar)
public function index(){
$this->load->view('layouts/header.php');
$this->load->view('layouts/menu.php');
$this->load->model('mCalendar');
$data['start_lunes'] = $this->mCalendar->lunes();
$this->load->view('usuarios/vConsulta_Horarios.php',$data);
$this->load->view('layouts/footer.php');
}
View
<div class="col-md-2">
<div class="form-group">
<label>fecha</label>
<input tupr="text" id="fecha_actual" name="fecha_actual" value="<?php echo $start_lunes;?>">
</div>
</div>
Even though you are only selecting one column, you still need to specify it when you output, because $start_lunes is the object that represents the entire row, and as the error tells you, it can't be converted to a string.
I think echo $start_lunes->hrs_ini should work.
Based on your updated function, I think it would be easier if you aliased the MIN expression in your select
$this->db->select('MIN(horario.hrs_ini) AS min_hrs');
Then refer to that in your view: echo $start_lunes->min_hrs
As return $this->db->get()->row(); returns object you are getting this error
You need to refer this link
So now, I will suggest you one thing to return data properly.
In your Controller change return $this->db->get()->row();
to
return $this->db->get()->result_array();
Now you are getting data in the format of Array and you can echo this in your View using
// In your view
foreach($start_lunes as $var){
$value = $var['MIN(horario.hrs_ini)'];
}
<input type="text" id="fecha_actual" name="fecha_actual" value="<?php echo $value?>"
Related
Result from MODAL:
Result from FORM:
DESCRIPTION:
So I wanna make a FORM from MODAL that will send data to actual FORM's table, but at the first IMAGE that not send like the FORM one.
CODE:
This is code from FORM attribute:
public function setLandCertificateIdAttribute($values)
{
// dd($values);
$this->attributes['land_certificate_id'] = implode(',', $values);
}
This is code using MODAL:
public function form(Model $model)
{
$this->multipleSelect('land_certificate_id', 'Nomor Sertifikat')
->options(LandCertificate::where('data_order_id', $model->id)->pluck('number', 'id'));
}
And the MODAL always return : implode(): Invalid arguments passed
I'm using laravel-admin project btw
implode(): Invalid arguments passed: It means that the function is expecting an array of data.
The implode function in PHP is easily remembered as "array to string"
In your modal html set your land_certificate_id name as array.
For example.
<input type="text" name="land_certificate_id[]"></input>
I am trying to use form_helper.php a helper class of codeigniter 2.2.0
and following is the error it's giving out when I try to do this :
controller file
$this->load->helper('form');
view file
<?php
$attributes = array('class' => 'form-horizontal', 'id' => 'admin-form');
echo form_open($this, $attributes);
?>
Error:
A PHP Error was encountered
Severity: Warning Message: strpos() expects parameter 1 to
be string, object given Filename: helpers/form_helper.php
Line Number: 53
A PHP Error was encountered
Severity: 4096 Message: Object of class CI_Loader could not
be converted to string Filename: helpers/form_helper.php
Line Number: 61
Although it does print the tag.
What I could be doing wrong here ?
Your code:
echo form_open($this, $attributes);
is in-correct. Correct it to:
echo form_open($YOUR_FORM_ACTION, $attributes);
Reference
Problem is occuring because, you are passing $this, an object instead of form action (string).
You can't use $this as the first argument as it is an internal CI object. Try:
echo form_open('controller/method', $attributes);
I want to get a list of productTypes from the database and output them to a dropdownlist which will then be used to ajax populate a second dropdown list.
Step 1: Controller gets productTypes from database
Step 2: Controller converts the productTypes object to a list
Step 3a: Output list to test in view
Step 3b: Populate a dropdown with the list view
Snippet of controller
public function init()
{
$this->dynamicTypes();
$this->render('calculator', array('types' => $this->_types));
}
public function dynamicTypes()
{
$this->_types = CHtml::listData(ProductType::model()->findAll(), 'id', 'type');
}
View File Snippet
<?php
// Step 3a (this works fine)
echo '<pre>';
print_r($types);
echo '</pre>';
// Step 3b - Returns an error
echo $form->dropDownList('productTypes',1, array($types));
?>
<?php $this->endWidget(); ?>
</div>
With step 3b, I have tried the following:
echo $form->dropDownList('productTypes',1, array($types));
Error Msg: get_class() expects parameter 1 to be object, string given
According to http://www.yiiframework.com/doc/api/1.1/CHtml#dropDownList-detail, the dropDownList method takes the following arguments:
public static string dropDownList(string $name, string $select, array $data, array $htmlOptions=array ( ))
where the first argument is a string indicating the name of the input field.
What have I done wrong and how can I fix this?
UPDATE
echo CHtml::dropDownList('productTypes',1, array($types));
seems to work, but when I look at the dropdown in the view, for some reason, there is a 0 in the dropdownlist. For some reason, it is putting the options into an optiongroup
<select name="productTypes" id="productTypes">
<optgroup label="0">
<option value="1">Scrap</option>
<option value="2">Coin</option>
<option value="3">Bar</option>
</optgroup>
</select>
SOLVED:
Removed array($types), replaced with just $types
echo CHtml::dropDownList('productTypes',1, $types);
Seems like the $form is an object of the CActiveForm class. This version of the dropDownList method takes an instance of CModel class.
See the method signature here CActiveForm::dropDownList
Use
echo CHtml::dropDownList('productTypes',1, array($types));
instead of
echo $form->dropDownList('productTypes',1, array($types));
SOLVED
I solved this issue with Jonathan Hussey help
I changed this line:
$mModel->getCollection()->load($mId)->getData();
for this:
$mModel->getCollection()->addFieldToFilter('met_id',$Id)->getSelect();
Problem
I created custom module which added tab to admin product page with additional text field.
When I try save this product I get this error:
a:5:{i:0;s:140:"Cannot send headers; headers already sent in /home/nano/domains/mydomain/public_html/gw/lib/Varien/Data/Collection/Db.php, line 693";i:1;s:1630:"#0 /home/nano/domains/mydomain/public_html/gw/lib/Zend/Controller/Response/Abstract.php(148): Zend_Controller_Response_Abstract->canSendHeaders(true)...
I saw that this error created in Observer.php:
$mId = $collection['m_id'];
$mModel->getCollection()->load($mId)->getData(); <-- this line give an error
$data['met_id'] = $mId;
$data['product_id'] = $product->getId();
$data['metf1'] = $this->_getRequest()->getPost('f1');
$mModel->setData($data);
$mModel->save();
Do you have any ideas how fix this ?
EDIT
content of admin template tab file:
<?php
$product = Mage::registry('current_product');
$mItem = Mage::getModel('mmodel/mmodel')->getCollection()->
addFilter('product_id',$product->getId())->getFirstItem();
echo '<div class="input-field">
<label for="f1">File</label>
<input type="text" class="input-text" name="f1" id="f1" value='.$mItem['f1'].' />
</div>';
Debug backtrace after line $mModel->getCollection()->load($mId)->getData(); from Observer.php
SELECT `main_table`.* FROM `mmodel` AS `main_table`
Debug Backtrace:
File Line Function
/home/nano/domains/mydomain/public_html/gw/app/code/local/GW/MModel/Model/Observer.php 42 printDebugBacktrace
/home/nano/domains/mydomain/public_html/gw/app/code/core/Mage/Core/Model/App.php 1338 saveProductTabData
/home/nano/domains/mydomain/public_html/gw/app/code/core/Mage/Core/Model/App.php 1317 _callObserverMethod
/home/nano/domains/mydomain/public_html/gw/app/Mage.php 468 dispatchEvent
/home/nano/domains/mydomain/public_html/gw/app/code/core/Mage/Core/Model/Abstract.php 466 dispatchEvent
/home/nano/domains/mydomain/public_html/gw/app/code/core/Mage/Catalog/Model/Product.php 548 _afterSave
/home/nano/domains/mydomain/public_html/gw/app/code/core/Mage/Core/Model/Abstract.php 319 _afterSave
/home/nano/domains/mydomain/public_html/gw/app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php 714 save
/home/nano/domains/mydomain/public_html/gw/app/code/core/Mage/Core/Controller/Varien/Action.php 419 saveAction
/home/nano/domains/mydomain/public_html/gw/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php 250 dispatch
/home/nano/domains/mydomain/public_html/gw/app/code/core/Mage/Core/Controller/Varien/Front.php 176 match
/home/nano/domains/mydomain/public_html/gw/app/code/core/Mage/Core/Model/App.php 354 dispatch
/home/nano/domains/mydomain/public_html/gw/app/Mage.php 704 run
/home/nano/domains/mydomain/public_html/gw/index.php 87 run
When working with collections you should only pass arguments to ->load() if you want the SQL for that collection to be logged or output. If you don't want the collection to return all items you can pull the select object from the collection with ->getSelect() and filter with standard Zend methods this way.
If you trace back your collection command you will see because you pass an argument it echo's out the collection SQL.
$mModel->getCollection()->load($mId)->getData();
Have a look at lib/Varien/Data/Collection/Db.php as per the error message and find the load() method. You will see that it accepts two aguments, $printQuery and $logQuery, you have passed an argument to $printQuery. A few lines down in the method you see:
$this->printLogQuery($printQuery, $logQuery);
Looking at the printLogQuery() method you will see that anything passed as the $printQuery argument which evaluates to true triggers the echo on line 693 as per the error message:
echo is_null($sql) ? $this->getSelect()->__toString() : $sql;
This is what is sending headers in your case. Remove the argument from ->load() or pass false and it should fix your problem.
i am trying to populate two form fields from data that is retrieved from a database, in order for the user to update them. The table is called records and it is quite simple:
Record_ID
title
content
My model:
function get_data()
{
$r = $this->uri->segment(3);
$query = $this->db->get_where('records', array('Record_ID' => $r));
return $query->result();
}
My controller:
function set_values()
{
$data = $this->entries_model->get_data();
$this->load->view('update_view', $data);
}
and my update record view:
<?php
echo form_open('site/update',$data);?>
Title:
<?php echo form_input('title',set_value('title'));?>
Content:
<?php echo form_input('content',set_value('content'));
echo form_submit('submit', 'Submit');?>
<?php echo form_close();?>
The problem is that i get the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/update_view.php
Line Number: 10
My question is twofold:
How do i access this data in my view form and
how do i populate the respective fields with it.
I am new to Codeigniter, my questions may look simplistic but any help would be appreciated. Thanks in advance.
There are a few things going on here:
$data is an array or object that is passed to a view. It's ELEMENTS are then available as variables in the view. So, $data['myelement'] = 'somevalue' in the controller would be accessed as $somevalue in the view.
If you pass a 2nd parameter to the form_open() method, it is expected to be a key/value pair of attributes for the tag that will be generated. like, array('class' => 'form_class', 'id' => 'form_id')
If you want to set the values of your form inputs, use the view helper function set_value(). In your case, use the controller to set elements in the $data array you'll pass to the view. $data['form_values'] = array('title' => $title, 'content' => $content);
Then, in the view:
You should pass a array to your view file. So replace:
$data = $this->entries_model->get_data();
with:
$data['entries_data'] = $this->entries_model->get_data();
and on your view file replace:
echo form_open('site/update',$data);?>
with:
echo form_open('site/update',$entries_data);?>
first you need to pass data in proper way
replace
$data = $this->entries_model->get_data();
with:
$data['data'] = $this->entries_model->get_data();
for setting value in set_value you need to do the in-line condition check to check either data is an object or not if object then put value other wise just empty
<?php echo form_input('title',set_value((is_object($data)?$data->title:'')));?>
you have to do the same thing for your all form fields
Jcory has answered your question but let me add a little to it.
In you model instead of return $query->result(); do this return $query->row(); this is because using returning a return object requires that you should loop through the resultset in your view
Instead of $data = $this->entries_model->get_data(); do this $data['entry'] = $this->entries_model->get_data();
In your view do this <?php echo form_input('title',set_value('title',$entry->title));?>
I hope these changes may solve the problem