cakePHP3 pass variable from controller to view - cakephp-3.0

I have a problem when passing a variable from Controller to view in CakePHP3.
File FincasController.php: I have created a public Method to make a report and be able to pass to a Template I have also created.
public function worklist()
{
$worklist = TableRegistry::get('Fincas');
$query = $worklist
->find()
->select(['id', 'prov', 'municipio', 'paraje', 'poligono', 'parcela', 'f_ult_poda' , 'f_ult_recog' ]);
foreach ($query as $worklist) {
if ( $worklist->f_ult_poda > $worklist->f_ult_recog )
debug($worklist); //it works fine
}
$this->set('finca', $this->$query); //I have tried also $this->$worklist
}
File Template\Fincas\worklist.ctp :
<?php debug($finca);?>
Thanks a lot

$query it justa a variable, it's not an attribute of your FincasControllerõ. Why are you using$this`?
simply
$this->set('finca', $query);

Related

Retrieve and edit data on page using Laravel

I cant display my data, laravel's 404 page popping.
Here is my foreach codes.
#foreach($este as $row)
{{$row['価格']}}
{{$row['間取り']}}
{{$row['販売戸数']}}
{{$row['総戸数']}}
{{$row['専有面積']}}
{{$row['専有面積']}}
{{$row['その他面積']}}
{{$row['所在階/構造 階建']}}
{{$row['完成時期']}}
{{$row['住所']}}
#endforeach
What am I doing wrong here.
I want to edit table with css but data doesn't display.
My controller, and route is here:
public function sumos($id)
{
$este = estates::all();
return view('pages.sumo', ['este' => $este]);
}
Route::get("sumo/{id}", "PagesController#sumos");
Change your controller and route to the following
public function sumos()
{
$data['este'] = estates::all();
return view('pages.sumo', $data);
}
Route::get("sumo", "PagesController#sumos");
then your view to
#foreach($este as $row)
{{$row->価格}}
{{$row->間取り}}
{{$row->販売戸数}}
{{$row->総戸数}}
{{$row->専有面積}}
{{$row->専有面積}}
{{$row->その他面積}}
{{$row->所在階/構造 階建}}
{{$row->完成時期}}
{{$row->住所}}
#endforeach
Don't return the value from the controller . Instead send the values to the view as parameter and then use it in your view
Like
public function sumos()
{
$este = estates::get();
return view('viewName', ['este' => $este]);
}
And in your view
#foreach($este as $row)
write markup for your items here.
#endforeach
1.Actually get is for retrieve all the data from table ,
2.After getting related data pass it on view
public function sumos()
{
$este = estates::get();
return view("editPage" , compact('este'));
}
After View data in table using
#foreach($este as $est) //data here like html css #endforeach
add route to update data and pass it into controller
Then find related row using `
public function find($id){
$estates = estates::find($id);
//after changes
$estates->save();
return redirect()->back();
}
`
firs you have to create a view file
How to use views in Laravel?
then just use this construction:
public function sumos()
{
$este = estates::all();
return view('name_of_your_view_file', compact('este');
}
change your route to
Route::get("sumo/{id}", "PagesController#sumos");

Getting data from related table in Codeigniter -- translating MySQL query into CI MVC

I have worked with examples from a few different StackOverflow posts to get data from related tables in CI, but haven't been successful in connecting my tables in the "where" part, and I'd like to understand how to translate that from a MySQL query to the CI MVC.
This is the MySQL query that works to get the dctag fields from the dctags table where the dctag_id in the articles table matches:
$dctagger = $article['dctag_id'];
$query = "SELECT `dctitle`, `dctag_id` FROM dctags WHERE dctag_id = '$dctagger'";
$query_run = mysql_query($query);
This is the code for the Model, Controller, and View files:
Model -
public function get_article_dctag() {
$this->db->select('*')
->from('dctags')
->where('dctag_id', $this->$data['dctag_id']);
$query = $this->db->get();
return $query->result_array(); }
Controller -
public function article($slug) {
$data['article'] = $this->article_model->get_article($slug);
$data['dctag'] = $this->article_model->get_article_dctag();
$this->load->view('templates/header-article', $data);
$this->load->view('article', $data);
$this->load->view('templates/footer-home'); }
View -
<?php foreach ($dctag as $dctag_item): ?>
<title><?php $dctag_item['dctitle']; ?></title>
<?php endforeach; ?>
Error -
"A PHP Error was encountered... Severity: Notice... Message: Undefined variable: data...Filename: models/article_model.php" (see Model code above)
The problem is the variable $this->... and I don't know what that needs to be. Thanks for explanations/clarification -- in advance!
If your dctag_id is in $data['article'] then use following code
On Controller
public function article($slug)
{
$data['article'] = $this->article_model->get_article($slug);
$data['dctag'] = $this->article_model->get_article_dctag($data['article']['dctag_id']);
$this->load->view('templates/header-article', $data);
$this->load->view('article', $data);
$this->load->view('templates/footer-home');
}
On your Model
public function get_article_dctag($id)
{
$this->db->select('*')
->from('dctags')
->where('dctag_id', $id);
$query = $this->db->get();
return $query->result_array();
}
In the controller you should pass the variable on the get_article_dctag($var)
This will be the reference on your model get_article_dctag() and this should have a parameter of what you are searching for. In your structure it seems that Model cant locate your $data['dctag_id'] in your function
In the controller
$dctag_id = 2
$this->article_model->get_article_dctag($dctag_id);
In your model
function get_article_dctag($dctag_id){
$this->db->where('dctag_id',$dctag_id);}
Hopes it will help you a lot

Laravel Select Query within helper file

I'm coming from codeigniter background. Unlike codeigniter helper directory, i just created helper directory within app directory of Laravel. Just want to know how to execute query within this common function. Here is my codeigniter function.
function show_menu($primary_key_col, $parent_id, $sort_order)
{
$output = "";
$ci =& get_instance();
$ci->db->select("*");
$ci->db->where('is_active', "Y");
$ci->db->where('is_delete', "N");
$ci->db->where('parent_id', $parent_id);
($sort_order!="")?$ci->db->order_by($sort_order, "ASC"):"";
$query = $ci->db->get('tbl_cms_menus');
foreach ($query->result() as $row){
$output .= '<option value="'.$row->$primary_key_col.'">'.$indent.$row->menu_name.'</option>';
}
return $output;
}
I tried something like this in laravel file. but this code did't give me any result. Please tell me where i'm doing wrong in this code. thanks
function databaseTable()
{
$table = DB::table('tbl_cms_menus');
$get_rows = $table->get();
$count_rows = $table->count();
if($count_rows > 0){
foreach ($get_rows as $tbl)
{
echo $tbl->menu_name;
}
}
}
This code will rot so hard that it shipped pre-rotten.
But, if you want to just.. ram it into the app all dry like that.. then add something like this to your base controller class...
$whatever = crazyChainingStuff;
foreach ($whatever ...) { $topMenu .= ... }
View::share('topMenu', $topMenu);
If you want to learn how to write code that will do less damage to your company and your clients then I recommend starting by watching Uncle Bob's "Fundamentals" videos. At least the first 5-6. http://cleancoders.com
It looks like you are trying to generate a drop-down/select with some data from your database, in this case, you should pass the data required for the drop-down/select from your controller to the view where you have written your HTML, for example, in your view, you may have a select like this:
echo Form::select('cms_menu', $cms_menu, Input::old('cms_menu'));
Or this (If you are using Blade):
{{ Form::select('cms_menu', $cms_menu, Input::old('cms_menu')) }}
From your controller you should pass the $cms_menu which should contain the menu-items as an arrtay and to populate that array you may try something like this:
$menuItems = DB::table('tbl_cms_menus')->lists('menu_name','id');
return View::make('your_view_name', array('cms_menu' => $menuItems));
Also, you may use something like this:
// Assumed you have a Page model
$menuItems = Page::lists('menu_name', 'id');
return View::make('your_view_name', array('cms_menu' => $menuItems));
You may also read this article which is about building a menu from database using view composer (More Laravelish way). Read more about Form::select on documentation.
It was too late to give an answer. I was also from CodeIgniter background and when I learnt Laravel then first I try to find how can I write a query in Helper. My Team leader helped me.
I have converted your code in a helper function.
function show_menu($primary_key_col, $parent_id, $sort_order)
{
$query = DB::table('tbl_cms_menus')
->select('*')
->where('is_active', '=', 'Y')
->where('is_delete', '=', 'N')
->where('parent_id', '=', $parent_id);
($sort_order != "")? $query->orderBy($sort_order, "ASC") : "";
$resultData = $query->get()->toArray();
}
Here $resultData will be array format. Now, you can create a foreach loop according to your requirement.

Json call in joomla 2.5 component

I'm making a joomla 2.5 component and i trying to set in model or controller (what's the most appropriate ?) a json response of my DB request (for later get the json with angularJS).
Here's my model (with DB response):
<?php
defined('_JEXEC') or die();
jimport( 'joomla.application.component.modelList' );
class MediastoreModelList extends JModelList
{
function getListQuery()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id, type, designation', 'marque', 'prix');
$query->from('produits');
return $query;
}
}
My empty controller:
<?PHP
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');
class MediastoreController extends JController
{
}
My view
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.application.component.view' );
class MediastoreViewList extends JView
{
function display($tpl = null)
{
$this->items = $this->get('items');
parent::display($tpl);
}
}
and my template
<?php
defined('_JEXEC') or die('Restricted access');
JHTML::script('media/com_mediastore/js/angular.min.js');
JHTML::script('media/com_mediastore/js/app.js');
?>
<?php
echo $this->items;
?>
<div class="content">
<p>Nothing</p>
</div>
How can i do that ?
Thanks a lot,
Antoine
Bit late .....
Hi I am working on something similar.
In your views (sitepart) add an other file called view.raw.php.
You can browse to this file by appending 'format=raw' at the end of the url.
For example: if your component view page in the browser is 'http://test.com/test( IF SEF is on ) then after appending th url shoul be like this
http://test.com/test?format=raw and in case SEF is not on the use & instead of ?
You can use this URL in the angularjs for http.get service.
In view.raw.php file make sure you just echo the results rather than parent::display($tpl);.
This did work for me.
PS: I am using angualrjs too but want to use the component with many menu items and the url keeps changing so am stuck on that part.
Hope this solves your issue.
Regards,
Jai

Query is returning empty arrays in CakePHP (model and controller are set up)

I'm using Cake's find('all') method in the controller to query a table I have, and the requestAction method to use the controller function (and so to output the data) within the view. The problem is that the find method is returning empty arrays(ouput is 'ArrayArrayArray'). The table is named 'users' (as per Cake conventions) and the code for all three files are below:
Code for the view (test.ctp)
<?php
$users = $this->requestAction('/users/read');
foreach($users as $user) {
echo $user;
}
?>
Code for the controller (users_controller.php)
<?php
class UsersController extends AppController {
var $name = 'Users';
function read(){
$users = $this->User->find('all');
if(isset($this->params['requested'])) {
return $users;
}
}
}
?>
Code for the model (user.php)
<?php
class User extends AppModel {
var $name = 'User';
}
?>
I've dwindled it down and discovered that $users is being set to NULL by the $this->User->find('all') statement because that statement is actually not returning any values. I know Cake is finding the table because of a typo I had earlier with the table name ('usres' instead of 'users'). I can't figure out what I'm missing or what I'm doing wrong. Would love some help. :)
The ouput is 'ArrayArrayArray' doen't means itz empty
try
foreach($users as $user) {
debug($user);
}
For me this code is meaningless.
if you want to use requestAction() use it in the proper way. It's used to call (and print) the parts of the application in the view.
Here is how to achieve the working and correct code:
Your Users class:
class Users extends AppController {
function read(){
$this->set('users', $this->User->find('all'));
$this->layout = "ajax";
$this->render();
}
}
Your read.ctp:
<ul><?php
if(isset($users)){
foreach($users as $user) {
echo '<li>'.$user.'</li>';
}
}
?></ul>
Now in your test.ctp you need to call:
<?php echo $this->requestAction(
array(
'controller'=>'users',
'action'=>'read'
)
);?>
And you will see the result in your test action as unsorted list.