Codeigniter Error Trying to get property of non-object - codeigniter

I am getting error of " Trying to get property of non-object" my controller code is
public function productDetails($pro_name,$product_id) {
$data['prodRating'] = $this->ProductsModel -> get_one($product_id);
$this->load->view('home',$data);
}
Model Code
function get_one($pro_id){
$query= $this->db->select('ratingid, pro_item_id, pro_total_points, pro_total_rates, proid ')
->from('tblproducts')
->where('proid',$pro_id)
->join('tblprorating','tblprorating.pro_item_id = tblproducts.proid','left')
->get();
return $query->result();
}
view code
<span dir="ltr" class="inline">
<input id="input-<?=$prodRating->proid ?>" name="rating"
<?php if ($prodRating->$pro_total_rates > 0 or $prodRating->pro_total_points > 0) { ?>
value="<?php echo $prodRating->pro_total_points / $prodRating->pro_total_rates ?>"
<?php } else { ?>
value="0"
<?php } ?>
<?php if ($this->session->userdata('userid') == false) { ?>
data-disabled="false"
<?php } else { ?>
data-disabled="<?= $rated ?>"
<?php } ?>
class="rating "
min="0" max="5" step="0.5" data-size="xs"
accept="" data-symbol="" data-glyphicon="false"
data-rating-class="rating-fa">
</span>
What is problem in my code if i use foreach loop then i can solve my problem. But in this code i not want to use the foreach loop. I want to access the fields of database. but i am getting error.

Return return $query->row() for single item. result() is for many. Further you should check num_rows() before attempting to access the result object/array and handle 0 rows properly as it is good practice.
In this case:
if ($query->num_rows() > 0) {
return $query->row();
}
return false;

Related

How I can active $record

I have table like this
public function getDoctorPhotoRecord($count)
{
$this->db
->select('u.id, fullname, image, unvan_code, baslik, onayli')
->from('user as u')
->join('user_detail as ud', 'u.id = ud.id')
->join('tanim_sehir as us', 'ud.sehir_code = us.code')
->where('group', 'doktor')
->where('image != \'\' ')
->order_by('rand()')
->limit($count);
$query = $this->db->get();
return $query->result_array();
}
and I want this code work as
<?php
$onay = $record->detail->onayli;
?>
<?php
if ($onay == 0) {
?>
<i class="fa fa-user-md" aria-hidden="true"></i> Bu <b>Doktor</b> siz misiniz?
<?php
} else {
?>
<span class="verified" id="verifyuser" title="" data-toggle="tooltip" data-placement="bottom" data-original-title="Onaylı Üye"><i class="fa fa-check"></i></span>
<?php
}
?>
When I tried it getting data with this :
<?php echo $item['onayli'];?>
but I want active This code
<?php
$onay = $record->detail->onayli;
?>
How I can active this code ?
Thank you

Codeigniter after insert success message show in view

I am new in Codeigniter and I need to show success and error message after insert data's in database.
How can I show the message in the view page?
This is my coding:
Model
function addnewproducts($data)
{
if($data['product_name']!="" && $data['product_qty']!="" && $data['product_price']!="" && $data['date']!="")
{
$res=$this->db->insert('product_list',$data);
return $this->db->insert_id();
}
else
{
return false;
}
}
Controller
function addnewproduct()
{
$this->load->model('products');
$data['product_name'] = trim(strip_tags(addslashes($this->input->post('product_name'))));
$data['product_qty'] = trim(strip_tags(addslashes($this->input->post('product_qty'))));
$data['product_price'] = trim(strip_tags(addslashes($this->input->post('product_price'))));
$data['datetime']=date('d-m-Y');
$res = $this->products->addnewproducts($data);
if($res==true)
{
$data['success'] = 'Successful';
$this->load->view('addproduct',$data);
}
}
View
<p><?php echo $success; ?></p>
There are many ways but below is which i recommend:
Set temp session in controller on success or error:
$res = $this->products->addnewproducts($data);
if($res==true)
{
$this->session->set_flashdata('success', "SUCCESS_MESSAGE_HERE");
}else{
$this->session->set_flashdata('error', "ERROR_MESSAGE_HERE");
}
In View you can display flashdata as below:
echo $this->session->flashdata('success');
or
echo $this->session->flashdata('error');
Source : Codeigniter official website https://codeigniter.com/userguide3/libraries/sessions.html
I appreciate that you got your answer but I think flash data is bit old now, as we can use bootstrap to alert if any error and that looks good to on web page.
In controller
$res = $this->products->addnewproducts($data);
if($res==true)
{
$this->session->set_flashdata('true', 'write_the_message_you_want');
}
else
{
$this->session->set_flashdata('err', "write_the_message_you_want");
}
In View
<?php
if($this->session->flashdata('true')){
?>
<div class="alert alert-success">
<?php echo $this->session->flashdata('true'); ?>
<?php
} else if($this->session->flashdata('err')){
?>
<div class = "alert alert-success">
<?php echo $this->session->flashdata('err'); ?>
</div>
<?php } ?>
Controller:
function addnewproduct()
{
$this->load->model('products');
$data['product_name'] = trim(strip_tags(addslashes($this->input->post('product_name'))));
$data['product_qty'] = trim(strip_tags(addslashes($this->input->post('product_qty'))));
$data['product_price'] = trim(strip_tags(addslashes($this->input->post('product_price'))));
$data['datetime']=date('d-m-Y');
if($this->products->addnewproducts($data));
{
$this->session->set_flashdata('Successfully','Product is Successfully Inserted');
}
else
{
$this->session->set_flashdata('Successfully','Failed To
inserted Product');
}
// redirect page were u want to show this massage.
redirect('Controller/Fucntion_name','refresh');
}// close function
view :
On Redirect Page write This code top of Form
<?php if($responce = $this->session->flashdata('Successfully')): ?>
<div class="box-header">
<div class="col-lg-6">
<div class="alert alert-success"><?php echo $responce;?></div>
</div>
</div>
<?php endif;?>

Magento - How to validate my radio button?

in my custom module, I have a form which has radio buttons.
When I click the submit button it does not validate the radio button.
<?php
$question = Mage::getModel('emme_question/question')->getCollection()->getLastItem();
$answers = $question->getSelectedAnswersCollection();
?>
<h4><?php echo $this->escapeHtml($question->getValue()); ?></h4>
<ul>
<?php foreach ($answers as $answer): ?>
<li>
<label><?php echo $this->escapeHtml($answer->getValue()) ?></label>
<input class="required required-field" type="radio" name="my_custom_answer" value="<?php echo $answer->getId() ?>" required>
</li>
<?php endforeach ?>
and
<?php
// app/code/local/Envato/Custompaymentmethod/Model/Paymentmethod.php
class Envato_Custompaymentmethod_Model_Paymentmethod extends Mage_Payment_Model_Method_Abstract {
protected $_code = 'custompaymentmethod';
public function validateRadioIsSelected()
{
$var options = $$('input.Classname');
for( i in options ) {
if( options[i].checked == true ) {
return true;
}
}
return false;
}
public function getOrderPlaceRedirectUrl()
{
return Mage::getUrl('custompaymentmethod/payment/redirect', array('_secure' => false));
}
}
Parse error: syntax error, unexpected 'options' (T_STRING) in /home/mmstore9/public_html/demo/app/code/local/Envato/Custompaymentmethod/Model/Paymentmethod.php on line 27
use this magetno default validation class to validate the radio button
validate-one-required-by-name
OR
validate-one-required
I solved
public function validate()
{
foreach (Mage::getModel('emme_question/question')->load(1)->getSelectedAnswersCollection() as $answer)
{
if ($answer->getIsCorrect())
{
if ($answer->getId() == $_POST['my_custom_answer'])
{
Mage::getSingleton('core/session')->addSuccess('Risposta esatta');
} else
{
Mage::throwException('Risposta sbagliata!');
}
}
}
}

Cakephp3 pass (custom) validation to flash message

It is simple to pass a message to flash via:
$this->Flash->error(__('The user could not be saved. Please, try again.'));
But when there are more errors from:
$package->errors();
I use just a simple foreach loop:
foreach ($package->errors() as $error=>$value)
{
foreach ($value as $single_error)
{
$error_array[] = ($single_error);
}
}
Then I pass it to a flash element:
$this->Flash->custom($error_array, [
'key' => 'custom']);
And in the flash message:
if ($message > 0) {
foreach ($message as $m) {
echo h($m).'<br />';
}
} else {
echo h($message);
}
I wonder it here is a better way of handling an array of validation errors.
I am using the following method if there are errors:
Controller:
$errors = $action->errors();
$errorMessages = [];
array_walk_recursive($errors, function($a) use (&$errorMessages) { $errorMessages[] = $a; });
$this->Flash->error(__('Your action cannot be saved!'), ['params' => ['errors' => $errorMessages]]);
Template/Element/Flash/error.tcp:
<?php if (isset($params) AND isset($params['errors'])) : ?>
<ul class="collection with-header">
<li class="collection-header"><h5><?= __('The following errors occurred:') ?></h5></li>
<?php foreach ($params['errors'] as $error) : ?>
<li class="collection-item"><i class="material-icons">error</i><?= h($error) ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Result:
Just for anyone interested, I am using MaterializeCSS.

create a select dropdown from database in codeigniter

I am new with codeigniter.I want to make a select dorpdown that gets its value and title from database.I tried some codes but it did not work.There are my codes:
Model
function get_sec_info(){
$records=$this->db->query('SELECT sec_id,name FROM section');
if($records->num_rows() > 0)
return $records->result();}
Controller
function sec_ifo(){
$data['rec']=$this->mymodel->get_sec_info();
$this->load->view('article',$data);}
View
<select name="section">
<?php foreach($rec as $row) {?>
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>"
<?php } ?>
It does not show any error and any option to show
In the controller you set "red" $data['red'] and in the view you access "rec" foreach($rec
Model:
function get_sec_info(){
$this->db->select('sec_id,name');
$records = $this->db->get('section');
return $records->result();
}
Controller:
function sec_ifo(){
$this->load->model('mymodel');
$this->data['red'] = $this->mymodel->get_sec_info();
$this->load->view('article',$this->data);
}
View:
<select name="section">
<?php foreach($red as $row) { ?>
<option value="<?php echo $row->sec_id; ?>"><?php echo $row->name; ?></option>
<?php } ?>
Model
public function getClasse() {
$query(`enter code here`);
$result = $this->db->query($query)->result_array();
foreach ($result as $key => $rows) {
$resultado[] = $rows['DescricaoClasse'];
}
return $resultado;
}
Controller:
public function getClasse() {
$this->load->model('Decisao_monocratica_model');
return $this->Decisao_monocratica_model->getClasse();
}
View
<select id="ClasseProcesso" class="input-xlarge" name="classeProcesso">
<option value="0">Todos os Tipos</option>
<? foreach ($classeProcesso as $key => $classe) { ?>
<option value="<? echo $classe ?>"><? echo $classe ?></option>
<? } ?>
</select>

Resources