pass a keyvalue to my innerloop - zend-framework

In my view I want to show 2 recordsets, like this:
<?php
//var_dump(get_object_vars($projects));
foreach ($projects as $project) :
//var_dump(get_object_vars($project));
?>
<tr>
<td><?= $project['Projectname']?></td>
<td><?= $project['ProjectShortcut']?></td>
<td><?= $project['ProjectCiNumber']?></td>
<td><?= $project['Unitname']?></td>
<td><?= $project['UnitShortcut']?></td>
<td><?= $project['UnitCiNumber']?></td>
<td>
Edit
Delete
</td>
<?php foreach ($dcls as $dcl) : ?>
<td><?= $this->Path?></td>
<td><?= $this->UnitID?></td>
<td><?= $this->Importdate?></td>
</tr>
<?php endforeach;
endforeach; ?
How can I pass the keyvalue UnitID to the inner loop? I want to show all units, that works already and underneath the imports belonging to the unit.
Here also how I pass the data to the view from within my controller:
return new ViewModel([
'projects' => $this->projectTable->fetchAll(),
'dcls' => $this->table->fetchAll()
]);
somehow in the ViewModel or in the view I want to give a parameter UnitID to the dcl collection.
So my questions would be, where shall I do it, probably in my view? And how might be the exact syntax?

Related

Clear the filled data of form in Phalcon Framework

I am new to phalcon and trying to learn this framework. I have created a simple form. Now, When I fill up the form, I am able to store the data in database. After submitting the form I want to be on the same page. Now the issue is, form is still have the filled data it is not clearing up. I have googled and found the clear() method to clear the form data but it seems that its not working. What to do?
Here is my code
//Controller
use Phalcon\Mvc\Controller;
class ProfileController extends Controller
{
public function indexAction()
{
// Add some local CSS resources
$this->assets->addCss("css/styles.css");
$this->assets->addCss("css/bootstrap.min.css");
// And some local JavaScript resources
$this->assets->addJs("js/jquery.js");
$this->assets->addJs("js/bootstrap.min.js");
}
public function createAction(){
//create object of model class
$profile = new Tbl_profile();
$profile->fname= $this->request->getPost('firstname');
$profile->lname= $this->request->getPost('lastname');
$profile->email= $this->request->getPost('email');
$success=$profile->save();
if($success) {
$this->flash->success("Profile created!");
//redirect to same page.
$this->dispatcher->forward(['action' => 'index']);
}else {
}
}
}
//views
<html>
<head>
<title>Title</title>
<?php $this->assets->outputCss(); ?>
</head>
<body>
<h4 class="ml-5 mt-2"> Create Profile</h4>
<?php echo $this->getContent(); ?>
<?php echo $this->tag->form("profile/create") ?>
<table class="table">
<tbody>
<tr scope="row">
<td>First Name</td>
<td><?php echo $this->tag->textField("firstname"); ?></td>
</tr>
<tr scope="row">
<td>Last Name</td>
<td><?php echo $this->tag->textField("lastname"); ?></td>
</tr>
<tr scope="row">
<td>Email</td>
<td><?php echo $this->tag->textField("email"); ?></td>
</tr>
<tr scope="row">
<td></td>
<td><?php echo $this->tag->submitButton("Create");?></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
Once you save the data to database, redirect to the controller instead of forwarding the request because forwarding the request won't reload the page.
$this->request->redirect('profile/index');
Read this what's the difference between redirect and dispatch in phalcon? for more information
You must create form class to have ability using $form->clear() method.
See the doc https://docs.phalconphp.com/cs/3.4/forms

CakePHP 3.x - Creating/updating data without forms

I recently read this post on how to do a CakePHP 3.x POST without forms (doing updates on the Index.ctp page, rather than using Add.ctp or Edit.ctp).
I have 2 issues:
Modifying POST to accept select statements from an array?
My index.ctp is as follows:
<?php foreach ($resources as $resource): ?>
<tr>
<td><?= $this->Number->format($resource->id) ?></td>
<td><?= h($resource->brand) ?></td>
<td><?= h($resource->model) ?></td>
<td><?= h($resource->subtype) ?></td>
<td><?= $this->Form->postButton('Change Type',['controller'=>'Users', 'action'=>'change_resource', $resource->id, $resource->type->name])?></td>
<td><?= $resource->has('studio') ? $this->Html->link($resource->studio->name, ['controller' => 'Studios', 'action' => 'view', $resource->studio->id]) : '' ?></td>
<td><?= h($resource->created) ?></td>
<td><?= h($resource->modified) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $resource->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $resource->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $resource->id], ['confirm' => __('Are you sure you want to delete # {0}?', $resource->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
And then the action as described in the linked StackOverflow post in the corresponding Controller:
public function change_resource($id,$existing_type)
{
$resources = TableRegistry::get('Resources');
$resource = $resources->get($id);
$resource->type = ($existing_type == '')?'$resource->$type->name';
$resources->save($resource);
return $this->redirect($this->referer());
}
Currently I'm getting a syntax error of
unexpected ';' on line 124,
which refers to the $resource->type line in the Controller method. If I remove that line, I get:
unexpected $resources on line 125
which I expect given the ; is removed so it thinks they're all on the same line.
Modifying POST for non-select inputs (eg. varchar/int/text)
I would like to be able to do a POST for the entire table, not just one input. However, while 2 of the inputs are selects from other tables in the form of arrays, the rest are all standard inputs (mostly varchar). I'm wondering how to modify the above controller to do this. Additionally, if I want to do POST for more than one input, do I require separate methods for every single input, or can I put it all in one method?
Please note that your ternary operator syntax is incorrect. You're missing out the colon (:)
Refer to Ternary Operator Example
Syntax: (expr1) ? (expr2) : (expr3)
$resource->type = ($existing_type == '') ? $resource->$type->name : <other statement>;

$this->input->post() and $_POST is NULL in codeigniter

I am getting a problem in codeigniter where i have a view file called view.php.
<?php echo form_open('');?>
<table class="table">
<thead>
<tr>
<th>Full Name</th>
<th>Username</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo form_input('fullname1',$row_data['fullname']);?> </td>
<td><?php echo form_input('username1',$row_data['username']);?></td>
<td><?php echo anchor("welcome/save/".$row_data['rid'],form_button('button',"Save"));?></td>
</tr>
</tbody
</table>
<?php echo form_close();?>
and here is my save method
public function save($rid){
$arr=array('fullname'=>$this->input->post('fullname1'),
'username'=>$this->input->post('username1')
);
var_dump($_POST);
var_dump($arr);
}
both array varable giving me NULL values while the textboxes have the values Please Help.... Thanks
you need to add the url to your form_open method. Try
echo form_open("your_url");
give the path of your save method instead of your_url.
Also check documentation here
The form_open method demands a target url.
In View
# Syntax <?php echo form_open('controllerName/MethodName');?>
<?php echo form_open('welcome/save');?>
// remove this
<td><?php echo anchor("welcome/save/".$row_data['rid'],form_button('button',"Save"));?></td>
// Add this
echo form_submit('mysubmit', 'Submit Post!');
In Controller
public function save(){
$fullname= $this->input->post('fullname1');
$username = $this->input->post('username1');
echo "MY name is ".$fullname." UserName Is : "$username;
}
Read Form Helper Codeigniter and form-validation-using-codeigniter example

ZendFramework echo db values if first string = a?

I have this code at the moment to echo out all my database entries, I am wondering what it looks like in ZF to echo out the entries if the first value of the db entry is a.
Code:
<table>
<?php foreach($this->clubs as $clubs) : ?>
<tr>
<td><a href="<?php echo $this->url(array('controller' => 'club-description', 'action' => 'index', 'club_id' => $clubs->id));?>">
<?php echo $this->escape($clubs->club_name);?></a></td>
<td><?php echo $this->escape($clubs->rating);?></td>
</tr>
<?php endforeach; ?>
</table>
Thanks
Rik
So, based on your comment it sounds like you want to group the clubs by letter on the page. Assuming they are being ordered alphabetically in your database query, the simplest way to do this is to keep a variable which stores the first letter of the last club in the loop. Then, on each iteration, you compare the first letter of the current club with the first letter of the previous club. If they are different, you output a new heading.
With your code this would look something like this:
<?php
$previousLetter = false;
?>
<table>
<?php foreach($this->clubs as $clubs) : ?>
<?php
$firstLetter = substr($clubs->_club_name, 0, 1);
if ($firstLetter != $previousLetter) {
?>
<tr>
<td><?php echo $firstLetter; ?></td>
</tr>
<?php } ?>
<tr>
<td><a href="<?php echo $this->url(array('controller' => 'club-description', 'action' => 'index', 'club_id' => $clubs->id));?>">
<?php echo $this->escape($clubs->club_name);?></a></td>
<td><?php echo $this->escape($clubs->rating);?></td>
</tr>
<?php $previousLetter = $firstLetter; ?>
<?php endforeach; ?>
</table>

Trying to get property of non-object in Zend Framework 1.11 Application

I'm trying to create a table of information in a database query using Zend Framework 1.11, Zend_Db, and Zend_Db_Select.
Initially, I went about this by instantiating a new instance of my table and using $table->select().
However, when I went to JOIN it to another table, I got an error message that I wasn't able to JOIN using this select() method. After doing some research, I found that I had to call the select() method off of my default adapter if I wanted to do this JOIN. Seemed straightforward.
At this point, I have a model, a controller, and a view, but I think something is getting "lost in translation" between my Controller & my View.
Here is the model (inside Application_Model_DbTable_ActivityRequest):
public function getAllActivityRequests()
{
$db = $this->getDefaultAdapter();
$select = $db->select();
$select->from('activityrequest');
$select->where('isDeleted = ?','N');
$select->where('ReqType <> ?','Purchase Product');
$select->order('ReqTime DESC');
$query = $select->query();
$row = $query->fetchAll();
return $row;
}
And the Controller (Admin Controller):
public function getWorkAction()
{
$table = new Application_Model_DbTable_Activityrequest();
$row = $table->getAllActivityRequests();
$this->view->activityrequests = $row;
}
And (relevant part of)the View (admin/get-work):
<?php foreach($this->activityrequests as $activity): ?>
<tr>
<td><?= $activity->CustomerID; ?></td>
<td><?= $activity->ReqType; ?></td>
<td><?= $activity->ReqTime; ?></td>
<td><?= $activity->PickupTime; ?></td>
<td><?= $activity->AddressID; ?></td>
<td><?= $activity->Notes; ?></td>
<td><?= $activity->HeardAbtUs; ?></td>
<td><?= $activity->isDeleted; ?></td>
</tr>
<?php endforeach; ?>
When I do a var_dump of $row in both my model and my controller, I see the array that I'm trying to pass to the view. As soon as I get to the view, though, I get this error on a "non-object."
Any help would be appreciated.
Thanks!
You need to set your adapter / select / dbTable fetch mode.
See http://framework.zend.com/manual/en/zend.db.adapter.html#zend.db.adapter.select.fetch-mode
Alternatively, use the Zend_Db_Table to full advantage
public function getAllActivityRequests()
{
return $this->fetchAll(
$this->select()
->where('isDeleted = ?', 'N')
->where('ReqType <> ?', 'Purchase Product')
->order('ReqTime DESC')
);
}
See http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.fetch-all.select