Undefined index in controller (CakePhp) - cakephp

I have the following code, which select product from table "products" based on the id.
public function p_details(){
$productIdNum = $this->params['detailID'];
//$this->Product->read(null, $productIdNum);
if(Validation::naturalNumber($productIdNum) == true){
$itemById = $this->Product->find('all', array('conditions' =>
array('Product.id' => $productIdNum)));
if(count($itemById) > 0){
$this->set('itemDetails', $itemById['Product']['id']);
}
}
}
But when I try to print the variable "$itemDetails" in the view,
like <?php echo $itemDetails['Product']['name']; ?>
it's giving me this error:
Undefined index: Product. If I change it to this
like <?php echo $itemDetails['name']; ?>
It's still give me the same error: Undefined index: name.
I can't get to figure this out.

Well, the issue is that with $this->set('itemDetails', $itemById['Product']['id']); you assign an id, not an array, to the itemDetails variable. Change it to $this->set('itemDetails', $itemById);, and it should work.

Related

Return all array values with ACF fields and foreach loop

I created a checkbox field in ACF and want to loop this field again and again and show all these values. First I tried it the way the ACF documentation shows, but the following code results in just the value of the first checked checkbox.
function ms_get_department(){
$departments = get_field('vakgebied');
if($departments):
foreach($departments as $department):
echo '<span class="department-text">' . $department['label'] . '</span>';
endforeach;
endif;
}
I also tried to store all the values inside an array but in the code below it just shows 'Array' and don't know how to show all these data in this case.
function ms_get_department(){
$departments = get_field('vakgebied');
$deps = array();
if($departments):
foreach($departments as $department):
$deps[] = $department['label'];
// $test = '<span class="department-text">' . $department['label'] . '</span>';
endforeach;
return $deps;
endif;
}
Does anyone know how I can solve this problem in a proper way?
It's not clear where you're adding this function. If it's on the single page then the code should work; however, if it's on any other page then you'd need to pass the post ID to ACF Field.
function ms_get_department(){
$departments = get_field('vakgebied', 123); // 123 being the post ID
$deps = array();
if($departments):
foreach($departments as $department):
$deps[] = $department['label'];
// $test = '<span class="department-text">' . $department['label'] . '</span>';
endforeach;
return $deps;
endif;
}
Another thing to check is, make sure ACF field is returning both 'label' and 'value'.

CodeIgniter : Undefined index in model variable

I have the below code in of of my models. Im loading the offer details page and it gave me the error
Severity: Notice
Message: Undefined index: viewc
Filename: statistics/Statistic_model.php
Line Number: 551
Statistic_model:
public function getTotalDetailsViewOfActiveOffer($comid) {
$this->db->select('count(statistic_offer_view_count.id) as viewc');
$this->db->from('statistic_offer_view_count');
$this->db->join('offers', 'offers.id = statistic_offer_view_count.offer_id');
$this->db->where('offers.com_id',$comid);
$this->db->where('offers.approved_status',"2");
$this->db->where('offers.enddate >=',date('Y-m-d'));
$this->db->group_by("offers.com_id");
$query = $this->db->get();
$row = $query->result_array();
$count=$row['viewc']; //this is line 551
}
Any idea how to fix this error?
That error means that the index viewc is not in the array $query->result_array() most likely because your query failed or returned null. You should get into the habit of doing:
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row()->viewc;
}
return false; // or in this case 0 or whatever
In Your Code You Selecting data from database Multiple array's.And assigning at to a variable like a single array.
Your Code....
$row = $query->result_array();
$count=$row['viewc'];
correct way is in your conduction to assign a variable is....
$data = array();
foreah($row as $key => $value){
$data[$key] = $value['viewc'];
}

Codeigniter Model: How to use values from a query for further calculations?

i have a function in a model with the following query, and i want to make some calculations with value1, value2 and value3 outside the query. how to get the values out of this result()?
function ($id,$username$) {
$this->db->select("id,value1,value2,value3,value4");
$this->db->from("table1");
$this->db->where("username=$username and id = $id");
$query=$this->get();
$result = $query->result();
$result['$testvalue'] = (value1 * value2/113) + value3;
return $result; }
can someone help? i already found out how to use values from a table, but not from a query!
ok now i know how to use row_array.
i call the row_array in the controller
$data = $this->model_testmodel->testfunction($id, $username);
...
$this->load->view('pages/view_test', $data);
now i wanna know how the view would look like.
i tried many different ways.
in the moment i am stuck with ...
<?php foreach $data as $value: ?>
<label>Value1:</label>
<input type="text" name="value1" value="<?php echo $value['value1']; ?>">
<label>Value2:</label>
<input type="text" name="value2" value="<?php echo $value['value2']; ?>">
...
<?php endforeach; ?>
but i get two errors:
Message: Undefined variable: data
Message: Invalid argument supplied for foreach()
Try this...
function ($id,$username)
{
$this->db->select("id,value1,value2,value3,value4");
$this->db->from("table1");
$this->db->where(array('id'=>$id,'username'=>$username));
$query=$this->db->get();
$result = $query->row();
$testvalue = (($result->value1 * $result->value2/113) + $result->value3);
//test here
echo $testvalue;
$res = array();
$res['testvalue'] = $testvalue;
$res['value1'] = $result->value1;
$res['value2'] = $result->value2;
$res['value3'] = $result->value3;
return $res;
}
It returns $res as array.Easy to execute of you got problem comment.. if it works accept.Lets enjoy..
Then make a function call and accept array in a variable..like this..
first load model then make function call.from your controller
$this->load->model('model_name');
$data= $this->model_name->function_name($id,$username);
//Fetch returned array like this..
echo $data['testvalue'];
echo $data['value1'];
//so on

view page in cakephp not working,undefined index

i cannot find out what is wrong with my code.the view page doesn't display.
my controller:
public function Modules_View( $id = Null)
{
$module = $this->Module->findByid($id);
$this->set('module',$module);
}
in my .ctp file
<?php echo $module['Module']['moduleName']; ?>
its throwing error in the above line.saying undefined index
i am using PostgreSQL for database
you need to check if you have data or not prior to echo()'ing it, like change:
$module = $this->Module->findByid($id);
to
$module = $this->Module->findById($id); //it should be Id not id
and in view .ctp file , do a check before trying to output data, like
if( !empty($module) AND !empty($module["Module"]) ) {
echo $module['Module']['moduleName'];
}
else {
//show some message as there's no data for related id
}

how to correct find("id = $view")); in cakephp?

I'm learning cakephp from here codes are from the older version of cake so some of the code needs to be updated I have controller which pass data to a view or a layout here is the controller:
BlogController.php
<?php
class BlogController extends AppController {
var $name = 'Blog';
var $uses = array('Blog');
// Used when indexing the page (http://yourdomain.com/blog/)
function index($view = null) {
// What to do if a view is set
if (isset($view))
{
//problem is here
$this->set('article', $this->Blog->find("id = $view"));
$this->render('article');
}
else
{
$this->set('articles', $this->Blog->find('all'));
}
}
}
?>
the problem is this line $this->set('article', $this->Blog->find("id = $view"));
if I replace the line with $this->set('article', $this->Blog->find('first')) it will show me the first item always and nothing will go wrong how can I correct this line so that I can use id?
the layout is article.ctp as follow
<div id="article">
<h1><?= $article['Blog']['title'] ?></h1>
<p class="date"><?= $article['Blog']['date'] ?></p>
<p class="intro"><?= $article['Blog']['introtext'] ?></p>
<p class="main"><?= $article['Blog']['maintext'] ?></p>
</div>
here is the error that I will get by clicking one of items:
Notice (8): Undefined index: id = 2 [CORE\Cake\Model\Model.php, line 2666]
You probably want this:
$this->set('article', $this->Blog->find('first', array('conditions' => array('Blog.id' => $view))));
See: Retrieving Your Data - CakePHP Manual

Resources