CodeIgniter DataTable with hyperlink - mysql

How can I pass data from datatable using hyperlink. I want to pass it by id. here is my code.
my controller
my model
index
I want to pass the data here when i click User ID. Thank you.
Student Profile

You can create the hyperlink on foreach loop.
Eg. <?php echo base_url('/users/' . $id ); ?>
After you need to create the route record.
Eg. $routes['/users/(:num)'] = 'login/view/$1';
Lastly, create the method that will receive the ID clicked and do what you want.
public function view( $id ) {
echo $id;
}

Related

not able to pass array from controller to view in cakephp 3 using $this->set('data', array received from find())

Not able to pass array variable form controller to view below code
If, I print in controller it is showing "Undefined variable: data"
public function report(){
$results=$this->Nodals->find()->toArray();
$this->set('data',$results);
print_r($data); die();}
Try using compact like this.
public function report(){
$data=$this->Nodals->find()->toArray();
$this->set(compact('data'));
};
Now do dd in your view to check if the data variable sending to the view. I am assuming your view file is report.ctp, so in report.ctp file write this line
<?php
dd($data);
?>
I hope it will help.
$data is not defined in your code if you want to print the query result from
$this->Nodals->find()->toArray(); then you need to store this in $data variable or any other variable
$results=$this->Nodals->find()->toArray();
$this->set('data',$results);
//data will be sent to the template which wrap the $result and in template you can access $reseult by using $data
print_r($results);
die();
or
$data=$this->Nodals->find()->toArray();
$this->set('data',$data);
print_r($data);
die();

cakePHP3 pass variable from controller to view

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);

Yii2 get controller/action from url

How in yii2 get controller/action from url.
Attention!!! I ask about Yii2. There are answers that consern only Yii1 at this forum.
Added.
I find smth like Yii::app()->getUrlManager()->parseUrl('http://eewee.djn'));
but in yii2
Added.
I get refferer url to anather conrtoller, where i want to parse it url by yii2 and get controller/action.
That worked for me
$request = new Request(['url' => parse_url(\Yii::$app->request->referrer, PHP_URL_PATH)]);
$url = \Yii::$app->urlManager->parseRequest($request);
list($controller, $actionID) = \Yii::$app->createController($url[0]);
You receive your action in $actionID and controller name in the $controller->id. The Request object needed because such construction:
list($controller, $actionID) =
\Yii::$app->createController(parse_url(\Yii::$app->request->referrer, PHP_URL_PATH));
not works with parameters in url.
Try this!
<?php echo Yii::$app->controller->id; // controller id ?>
<?php echo Yii::$app->controller->action->id; // controller action id ?>
list($controller, $actionID) = Yii::$app->createController($url);
If you need action as Object try this
$action = $controller->createAction($actionID);

Retrieve every model and return three values

I am looking for a way to retrieve all models in a database. Then loop through all of the models and read out the values for name, firstname and phonenumber.
So far I've gotten this and failed to go past it:
$searchModel = new EmployeeSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
I am then looking to implement those three values in a simple HTML table:
<tr><td>$firstname</td><td>$name</td><td>$phone</td></tr>
The table should be part of a PDF output, so ideally I would save it to a variable:
$html_table = '<tr><td>$firstname</td><td>$name</td><td>$phone</td></tr>';
I would need to get this for every model that fulfills the criteria of status = 'active' in the database.
So far I've only been able to get tables via gridView and not in a HTML template either.
You don't really need a data provider to achieve this, you could simply try :
$models = Employee::find()->where(['status'=>'active'])->orderBy('name ASC')->all();
foreach ($models as $model) {
echo "<tr><td>{$model->firstname}</td><td>{$model->name}</td><td>{$model->phone}</td></tr>";
}
Read more : http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#querying-data
You can get all models like this:
$employees = Employee::find()
->select('firstname, name, phone')
->asArray()
->where(['status'=>'active'])
->all();
This way you will get an array of arrays containing the 3 selected fields, so now you only need to use a foreach to loop through them and create the table:
$html = '<table>';
foreach($employees as $employee) {
$html .= '<tr><td>'.$employee['firstname'].'</td><td>'.$employee['name'].'</td><td>'.$employee['phone'].'</td></tr>';
}
$html .= '</table>'

updating a line in cakephp

I'm trying to create a simple request/accept function. A user adds a person - both users usernames are added to the table along with an autogenerated id. Now when the other user checks their requests they click approve and add an expiry date to the the record. Currently whats happening is that when a user clicks accept and sets an expiry date it creates a new record in the Relationship table.
the structure of my relationship table is
id
partyone
partytwo
active
expirydate
public function add(){
if($this->request->is('post')){
$this->Relationship->create();
if ($this->Relationship->save($this->request->data))
{
$id=$this->Relationship->id;
$this->Session->setFlash('The relationship has been saved');
}
else { $this->Session->setFlash('The relationship could not be saved. Please, try again.'); }
}
}
public function approve(){
if($this->request->is('post')){
$this->Relationship->id;
$this->Relationship->getLastInsertID();
$this->Relationship->save($this->request->data);
$this->Session->setFlash('The information has been saved');}
else{
$this->Session->setFlash('The information couldnt be saved');}
}
}
here is my approve view
<?php
echo $this->Form->create('Relationship', array('action'=>'approve'));
echo $this->Form->input('expirydate',array('label'=>'Expiry Date: ', 'class' => 'dateclass'));
echo $this->Form->end('Submit');
?>
here is my add view
<?php
echo $this->Form->create('Relationship', array('action'=>'add'));
echo $this->Form->input('partyone',array('label'=>'Enter your username: '));
echo $this->Form->input('partytwo',array('label'=>'Username of user: '));
echo "<br />";
echo $this->Form->end('Click here to add relationship');
?>
how can i code this so it updates and not creates a new line with expiry date, please help I am losing my mind over this.
This is not making anysense. Is this a typo :
$this->Relationship->id;
$this->Relationship->getLastInsertID();
It should obviously read :
$this->Relationship->id = $this->Relationship->getLastInsertID();
You have to add a hidden field to your "approve" view with the id of the relationship you are going to edit. If the submitted data contains an id, the save() method will update the specified record instead of creating a new one.
You can find an example in the cookbook: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html#editing-posts
You need to provide more information, but the basic idea is:
On your approve view you need to have a field that stores the id of the relationship to be approved.
You can store the ID as a param for the approve method, or as a hidden field.
function approve($id=null) {
/*Form action = Relationship/Approve/ID*/
$this->Relationship->id = $id;
/*or - hidden field/an input*/
$this->Relationship->id = $this->request->data['Relationship']['id'];
$this->Relationship->save($this->request->data);
}
providing id is set before you save, Cake will update, instead of adding.