why pass data in view file just returns first row? - model-view-controller

this code in controller gives me five rows but when i use it in view file it just returns first row like:
a
b
c
d
e
controller:
$data = [
'lesson' => $x->name,
];
echo $data['lesson'].'</br>';
$this->view('manage/add-cat', $data);
view:
echo $data['lesson'].'</br>';
it returns just:
a
!!!
I've tried:
foreach($data['lesson'] as $y):
echo $y;
endforeach;
I'd be thankful if you help :)

Related

how to print 2d array in laravel 5.4.12 by dd();?

please help me to solve this problem that how to die and dump 2D array in laravel and count array in an array.. i tried this but not working
$arr=collect($request->input('title['+$i+']'));
dd($arr->count());
also tried this code
$arr=collect($request->input('title[0]'));
dd($arr->count());
You do not say that you need to count them in your questions but here you go:
The aray:
$arrayname= array(
array('a', 'b', 'c'),
array('r', 't','y', 'u'),
);
The function
for ($i=0; $i < count($arrayname); $i++){
$singlearray[$i]= count($arrayname[$i]);
}
Result
array:2 [▼
0 => 3
1 => 4
]
dd($request->all()) will display contents of the array.
If you want to see the title, do this:
dd($request->title);
try to use:
echo count($arr);
dd($arr);
or
echo count($arr);
print_r($arr);
die;
$a = $request->all();
$flatArray = [];
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));
foreach($it as $v) {
$array[] = $v;
}
dd(count($flatArray));
Resource:
How to Flatten a Multidimensional Array?
i get it from the other site... Answer here
dd(count(request('title')[0]));
dd(count(request('title')['age']));

Active records on codeigniter page

I want print a table from database on view page with where condition, means I want print table of student where class is seven and fee status is paid.
How we do this?
i tried it but not work:
<?php
$students = $this->db->get_where('student' , array('status' => paid,'class_id'=>7))->result_array();
foreach($students as $row):?>
I have used this code to find the answer for you Please see it and try it out
$query = $this->db->get_where('tasks', array('description' => 7,'status' => 1));
echo "<pre>";
print_r($query->result_array());
change your array element as this since paid is a string it must be in quots
array('description' => "paid",'class_id'=>7)

How to add array to $data array CodeIgniter

I have some $data that i am transfering to view, ok that is easy
But i want this
$data['promenjive']=array
(
'1' => prva,
'2' => 'druga',
'3' => 'treca',
);
And
$this->load->view("view_home", $data);
My question is how to echo a single value from $data['promenjive']
To make something like this
1
I dont want to foreach entire array just one element?
In your example prva would echoed from within your view template with:
<?php echo $promenjive[1];?>
To echo 1 from your example you would have to do:
<?php echo array_search('druga',$promenjive);?>

How to use model object in Yii Controller and View

I have following method:
public function actionIndex() {
$companyModel = Company::model()->findAll();
$supplierProductModel = SupplierProduct::model()->findAll();
$this->render('index', array(
'companyData' => $companyModel,
'supplierProductData' => $supplierProductModel,
));
}
Here I have passed model objects to render function and want to access these objects in view (Active Relational Type) but when I am accessing its in view its showing error:
Trying to get property of non-object
view file (index.php)
echo $companyData->name . "\n";
echo $this->companyModel->phone . "\n";
echo $this->companyModel->fax . "\n";
echo $this->companyModel->cell . "\n";
Any help would be appreciated.
you need to declare $this->companyModel in your controller/action
$this->companyModel = Company::model()->findByPk($companyId);
with Company::model()->findAll() you get an array of Company-Models you can iterate over in your view-file.
foreach ($companyData as $companyModel) {
var_dump($companyModel->attributes);
}
It is happening becoz of findAll()
findAll() reruns all rows of company table in multidimensional array, so here
$companyData is multidimensional Array, now change your code in index like bellow,
<?php
foreach ($companyData as $compSub)
{
echo $compSub->name . "\n";
echo $compSub->phone . "\n";
echo $compSub->fax . "\n";
echo $compSub->cell . "\n";
}
?>
If you want a company data(single row), change your query like this
$companyModel = Company::model()->findByPk($id_Of_company);
//$companyModel is single dimensional array, it has all the info of a company.
Send this to view
$this->render('index', array(
'companyData' => $companyModel,
....................
));
Now you can show the data using bellow code
echo $companyData->name . "\n";
echo $companyData->phone . "\n";
echo $companyData->fax . "\n";
echo $companyData->cell . "\n";
You are trying to get all the entries from the database as findAll() returns all data in multidimensional array of objects.If you need all the entries you could iterate over it in the view file and get the results as shown
In the View File do as shown
<?php foreach($companyData as $value){
echo $vlaue->name . "\n";
echo $value->phone . "\n";
echo $value->fax . "\n";
echo $value->cell . "\n";
?>
With this you get all the entries from the table
If you want to get a particular record use condition using Cdbcriteria and pass the object and get the single result

CodeIgniter route and pass values to method

there is something I’m not getting about use route to call a method passing three values.
I have a controller with the following method inside
public function view_day($year, $month, $day)
{
$data['year'] = $year;
$data['month'] = $month;
$data['day'] = $day;
$this->load->view('calendar/view_day', $data);
}
and a page within my views folder with the follows
<?
echo $this->uri->segment(5).'<p>';
echo $day;
?>
finally, within my routes file I have the line below
$route['calendar/date/:num/:num/:num'] = "calendar/view_day/$1/$2/$3";
What I supposed to do is route an url like
http://www.mydomain.com/index.php/calendar/date/2012/06/10
to my calendar controller passing three values (2012, 06 and 10) to my view_day method. Then, collect these three values and pass them to my final page in order to use $day, $month and $year inside my presentation page.
Now, running the url above the result is
10 (returned by the row -> echo $this->uri->segment(5).’‘;)
$3 (returned by the row -> echo $day;)
Basically, what I’m not getting is way the variable $day inside my presentation page is not getting any value as passed inside the url but returns the same text ($3) I have wrote in my route statement.
Thanks
You should define your route like this
$route['calendar/date/(:num)/(:num)/(:num)'] = "calendar/view_day/$1/$2/$3";

Resources