Symfony 1 - How to make a link from a foreach - php

I am really new to Symfony 1. I know it's old and that support has ended for this version. But one of the projects at the company where I do my internship still runs on symfony 1.4.
I have a foreach loop;
<?php foreach ($configuration->getFormFields($form, 'show') as $fieldset => $fields): ?>
<?php //include_partial('service/form_fieldset', array('service' => $service, 'form' => $form, 'fields' => $fields, 'fieldset' => $fieldset)) ?>
<table id="sf_fieldset_<?php echo preg_replace('/[^a-z0-9_]/', '_', strtolower($fieldset)) ?>" cellspacing="0" cellpadding="0" class="sf_admin_st">
<tr>
<th>Property</th>
<th>Value</th>
</tr>
<?php
$i = 0;
foreach ($fields as $name => $field):
?>
<?php $i++;
$class = ($i % 2 == 0) ? 'even' : 'odd';
?>
<?php $attributes = $field->getConfig('attributes', array()); ?>
<?php if ($field->isPartial()): ?>
<?php include_partial('service/' . $name, array('service' => $service, 'form' => $form, 'attributes' => $attributes instanceof sfOutputEscaper ? $attributes->getRawValue() : $attributes)) ?>
<?php elseif ($field->isComponent()): ?>
<?php include_component('service', $name, array('service' => $service, 'form' => $form, 'attributes' => $attributes instanceof sfOutputEscaper ? $attributes->getRawValue() : $attributes)) ?>
<?php else: ?>
<tr class="<?php echo $class ?>">
<td><?php echo $field->getConfig('label') ? $field->getConfig('label') : $field->getName() ?>:</td>
<td><?php echo $form->getObject()->get($name) ? $form->getObject()->get($name) : "-" ?></td>
<?php endif; ?>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</table>
<?php endforeach; ?>
the above mentioned code output table below;
My question is, how can I make a field in the table a link to that specific description. I want the Parent Service value in the table to become a link to that specific value.
I have thought of an if, like this;
<?php if ($field->getConfig('label') == "parent_service_description"): ?>
<td><?php echo $form->getObject()->get($name) ? $form->getObject()->get($name) : "-" ?></td>
<?php else: ?>
<td><?php echo $form->getObject()->get($name) ? $form->getObject()->get($name) : "-" ?></td>
<?php endif; ?>
But my knowledge still lacks a lot to figure this out. I'll really appreciate some help, please?

If you got a route to the details page I would try something like this using url_for and a given $parentId:
<?php if ($field->getConfig('label') == "parent_service_description"): ?>
<td>
<a href="<?php echo url_for('#show_details?id=' . $parentId) ?>">
<?php echo $form->getObject()->get($name) ? $form->getObject()->get($name) : "-" ?>
</a>
</td>

You should use.
echo link_to ($name, "action/module?id=". $sf_params->get("id") . "&params2=2") )
the reason behind is its readability and simplicity.

Related

Adding serial number to the table

I have this PHP code that displays data from the MySQL database. I would like to add a serial number for which I tried with the following code but it doesn't work as it's not in the loop and also it doesn't know endpoint and so I have max at 12:
<?php foreach (range(0, 12) as $number) { echo $number; }?>
Here's the main PHP code. How do I add a serial number to the <td> block:
<tbody>
<?php foreach ($items as $i => $item) : ?>
<tr id="tablelist_item_<?php echo $i; ?>" class="<?php echo $fc_item_classes.' row'.($i%2 ? 1 : 0); ?>" <?php echo $microdata_itemtype_code; ?>>
<?php if ( $buttons_exists || $show_title || count($item->css_markups) ) : ?>
<td><?php foreach (range(0, 12) as $number) { echo $number; }?></td>
<td class="fc_title_col">
<?php if ($show_title) : ?>
<!-- BOF item title -->
<span class="fc_item_title" itemprop="name">
<?php if ($link_titles) : ?>
<?php echo $item->title; ?>
<?php else : ?>
<?php echo $item->title; ?>
<?php endif; ?>
</span>
<!-- EOF item title -->
<?php endif; ?>
<div class="fcclear fc_beforemarkups"></div>
</td>
<?php endif; ?>
<!-- BOF item fields -->
<?php foreach ($columns as $name => $label) : ?>
<td><?php echo isset($item->positions['table']->{$name}->display) ? $item->positions['table']->{$name}->display : ''; ?></td>
<?php endforeach; ?>
<!-- EOF item fields -->
</tr>
<?php endforeach; ?>
</tbody>
I ended up with this: <td><?php echo ($i+1); ?></td>
and it works :)

Codeigniter send GET parameter with site_url?

I have this part of code in my view file:
<?php foreach ($posts as $post) { ?>
<tr>
<td><?php echo $post['id']; ?></td>
<td><?php echo $post['title']; ?></td>
<td><?php echo $post['content']; ?></td>
<td>
<div class="btn-group">
Edit
Delete
</div>
</td>
</tr>
<?php } ?>
Well .. how to send GET parameter with site_url function, this is my anchor part:
a href="<?php echo site_url('admin/blog/show'); ?>" class="btn btn-success">Edit</a>
where to send $post['id'] ?
I see some question in SO, but answers was with hardcode...
here we have a example
echo anchor('news/local/'. $post['id'], 'any text', array('title' => 'The best news!'));
In your controller you will get something like.
echo $this->uri->segment('3');
Also load url helper/library as well.
Should be like this :
Edit
The given code should be like this :
<?php foreach ($posts as $post) { ?>
<tr>
<td><?=$post['id']; ?></td>
<td><?=$post['title']; ?></td>
<td><?=$post['content']; ?></td>
<td>
<div class="btn-group">
Edit
Delete
</div>
</td>
</tr>
<?php } ?>
In your show method access post id like this :
public function show($post_id)
{
echo $post_id;
}
For more : https://www.codeigniter.com/user_guide/helpers/url_helper.html

List to DropDown list in Yii

Please help with this code:
<ul class="activity-projects">
<?php foreach ($ownProjects as $userProject) : ?>
<li>
<a class="<?php echo ($userProject->id == $project_id) ? 'active' : '';?>"
href="<?php echo Yii::app()->createUrl('user/activity', array(
'user_id'=>$user->id,
'project_id'=>$userProject->id,
'date'=>$date)
); ?>"><?php echo $userProject->name; ?></a>
</li>
<?php endforeach; ?>
<?php foreach ($projects as $userProject) : ?>
<li>
<a class="<?php echo ($userProject->project_id == $project_id) ? 'active' : '';?>"
href="<?php echo Yii::app()->createUrl('user/activity', array(
'user_id'=>$user->id,
'project_id'=>$userProject->project->id,
'date'=>$date)
); ?>"><?php echo $userProject->project->name; ?></a>
</li>
<?php endforeach; ?>
</ul>
How to change it to dropdown list, using CHtml::dropDownList. Thanks for watching!
First, you need to define a key-value array like this:
$options = array();
<?php foreach ($ownProjects as $userProject)
array_push($options, array($userProject->id => $userProject->name));
?>
echo CHtml::dropDownList('seletName', '1', $options);
This will be produced an html <select> tag with "seletcName" name. And also option with value "1" will be selected option. You can use your desired value for first and second parameters.
Also you can use CActiveForm#dropDownList for this purpose.
In your form, use the form dropDownList() function.
<?php echo $form->dropDownList(
$model,
'project_id',
CHtml::listData(OwnProjects::model()->findAll(),
'id', 'name'),
array('empty' => '(Select project)','class'=>"form-control")
);
?>
From your example, it looks like OwnProjects is not a model on its own, but a subset of a model. You can customise the query
<?php echo $form->dropDownList(
$model,
'project_id',
CHtml::listData(OwnProjects::model()->findAllByAttributes(array('user_id'=> Yii:app()->user->id),
'id', 'name'),
array('empty' => '(Select project)','class'=>"form-control")
);
?>
This solution finally helps me:
<?php $options = array();
foreach ($projects as $userProject) :
$options[$userProject->id] = $userProject->project->name;
endforeach;
echo CHtml::dropDownList('selectName', '1', $options);
?>

CakePHP 2.0 search database error

Hi Im trying to create a function that searches my Uploads model and displays the information that is in the that table.
Notice (8): Undefined variable: uploads [APP/View/Uploads/search.ctp, line 28]
Warning (2): Invalid argument supplied for foreach() [APP/View/Uploads/search.ctp, line 28]
Before I am even allowed to search I get this error,
This is my search.ctp
<?php $uploads = $this->requestAction('uploads/search');
?>
<div id="search">
<?php echo $this->Form->create('Upload',array('action'=>'search'));?>
<fieldset>
<legend><?php __('Upload Search');?></legend>
<?php
echo $this->Form->input('searchupload', array('label' => false, 'class'=>'searchinput'));
$options = array(
'label' => '',
'value' => 'Search',
'class' => 'searchbutton'
);
echo $this->Form->end($options);
?>
</fieldset>
</div>
<div id="contentbox">
<table>
<?php foreach($uploads as $upload) : ?>
<tr>
<td><?php echo $upload['Upload']['name'] ?></td>
<td><?php echo $upload['Upload']['eventname'] ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
and this is the function in the uploads controller:
function search() {
if (!empty($this->data)) {
$searchstr = $this->data['Upload']['search'];
$this->set('searchstring', $this->data['Upload']['search']);
$conditions = array(
'conditions' => array(
'or' => array(
"Upload.name LIKE" => "%$searchstr%",
"Upload.eventname LIKE" => "%$searchstr%"
)
)
);
$this->set('uploads', $this->Upload->find('all', $conditions));
}
}
Any help would be greatly appreciated!
Thanks in advance
<?php if(!empty($uploads)) : ?>
<?php foreach($uploads as $upload) : ?>
<tr>
<td><?php echo $upload['Upload']['name'] ?></td>
<td><?php echo $upload['Upload']['eventname'] ?></td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<div>
No search matches found
</div>
<?php endif; ?>
This ensures that if an empty set is found then the no search match found is thrown into the webpage
Why are you running the uploads/search twice from the same view?
Try removing this from your view:
<?php $uploads = $this->requestAction('uploads/search'); ?>
It calls the same action you are rendering with search.ctp.
UPDATE:
If you are getting the Undefined Variable notice (that you will not see when in production and debug is set to 0 by the way), all you need to do is set the uploads variable to null in the controller (not the view). Before you run the search do: $this->set('uploads', array()); in the controller so the variable will be defined in the view. You do not want to clutter the view with logic.

cakephp Indirect modification of overloaded property PaginatorHelper

i have the following search form
<?php
echo $this->Form->create('Order', array('action' => 'search','type'=>'get'));?>
<?php echo $this->Form->input('SearchTerm',array('label' => 'Find:')); ?>
<?php $options=array('full_name'=>'By Name','code'=>'By Code','phone'=>'By Phone','email'=>'By Mail'); ?>
<?php echo $this->Form->input('options', array('type'=>'select', 'label'=>'Search:', 'options'=>$options)); ?>
<?php echo $this->Form->end('search'); ?>
<?php if($rs!=0){?>
<?php if($rs!=null) { ?>
results found : <?php print $results; //print_r ($result['Order']['full_name']); ?>
<h1 class="ico_mug">Results Matching Term: <?php print '"'.$term.'"' ;?></h1>
<table id="table">
<tr>
<th>Client Name</th>
<th>Order Code</th>
<th>Phone</th>
<th>Received Order</th>
<th>Working On Order </th>
<th>Order Ready </th>
</tr>
<!-- Here is where we loop through our $posts array, printing out post info -->
<?php foreach ($rs as $order): ?>
<tr>
<td>
<?php echo $this->Html->link($order['Order']['full_name'],
array('controller' => 'orders', 'action' => 'view', $order['Order']['id'])); ?>
</td>
<td><?php echo $order['Order']['code']; ?></td>
<td><?php echo $order['Order']['phone']; ?></td>
<td><?php echo $this->OrderStatus->getStatusString($order['Order']['receive_state']); ?></td>
<td><?php echo $this->OrderStatus->getStatusString($order['Order']['working_state']); ?></td>
<td><?php echo $this->OrderStatus->getStatusString($order['Order']['ready_state']); ?></td>
<td> <?php //echo $this->Html->link($this->Html->image("cancel.jpg"), array('action' => 'index'), array('escape' => false));?><?php echo $this->Html->link($this->Html->image("edit.jpg"), array('action' => 'edit',$order['Order']['id']), array('escape' => false));/*echo $this->Html->link('Edit', array('action' => 'edit', $order['Order']['id']));*/?></td>
</tr>
<?php endforeach; ?>
<tr ><?php //echo $this->Paginator->numbers(array('first' => 'First page')); ?></tr>
<?php
$urlParams = $this->params['url'];
unset($urlParams['url']);
$optionss=array('url' => array('?' => http_build_query($urlParams)));
//$this->Paginator->options($optionss);
$this->Paginator->settings['paramType'] = 'querystring';
?>
<tr ><?php //echo" << ".$this->Paginator->counter(
// 'Page {:page} of {:pages}');
?></tr>
</table>
<?php }else echo"no results found"; ?>
<?php } //endif?>
and this is my controller action
function search($options=null){
$this->set('results',"");
$this->set('term',"");
$this->set('rs',0);
if ((isset($_GET["SearchTerm"]))||(isset($_GET["options"])) ) {
$SearchTerm=$_GET["SearchTerm"];
$options=$_GET["options"];
if (!$options || !$SearchTerm) {
$this->Session->setFlash('Please enter something to search for');
}
else {
$SearchArray = array($options." LIKE " => "%".$SearchTerm."%");
$this->paginate = array('conditions' => $SearchArray,'limit'=>2,'convertKeys' => array($options, $SearchTerm));
$data=$this->paginate('Order');
$this->set('rs', $data);
$this->set('term',$SearchTerm);
}
}
as you can see i am using Get parameters options and SearchTerm. I want theese two to stay in the pagination links. I have tried various fixes that i've found on stackoverflow and on other site, (like ex:
$urlParams = $this->params['url'];
unset($urlParams['url']);
$optionss=array('url' => array('?' => http_build_query($urlParams)));
yet i still get the following error message :
Indirect modification of overloaded property
PaginatorHelper::$settings has no effect
[APP\View\orders\search.ctp, line 75
why is that? and what about a solution to this ? ( even if i use the wxample from the cookbook
$this->Paginator->settings['paramType'] = 'querystring';
i still get the same error :S can you please help/explain ?
If I understand your code correctly, you should be able to replace:
<?php
$urlParams = $this->params['url'];
unset($urlParams['url']);
$optionss=array('url' => array('?' => http_build_query($urlParams)));
//$this->Paginator->options($optionss);
$this->Paginator->settings['paramType'] = 'querystring';
?>
with
<?php $this->Paginator->options(array('url' => $this->passedArgs)); ?>
Form Fields In Pagination
If you are actually trying to put the form elements into the pagination, you will need to alter your code slightly. You will need to build named parameters for each item in the search options. Then, in the action of the controller, you will need to check for both the request->data and the params['named'] versions of the search to make sure you use them in both cases. I would also clean up the code so it is a little more readable.
First, you need to use the cake methods for getting the data. It will make sure that it cleans the requests for you etc. In addition, you will need to account for the search term and filter options being passed as both a named variable and submitted in a form. So you need to update your controller as follows:
function search($options=null){
$this->set('results',""); // where is this used ??
$this->set('rs', 0);
$this->set('SearchTerm', '');
$this->set('FilterBy', '');
$this->set('options', array('full_name'=>'By Name','code'=>'By Code','phone'=>'By Phone','email'=>'By Mail'));
if ($SearchTerm = $this->request->data['Order']['SearchTerm']) or $SearchTerm = $this->params['named']['SearchTerm']) {
if ($FilterBy = $this->request->data['Order']['FilterBy'] or $FilterBy = $this->params['named']['FilterBy'])) {
$this->paginate = array(
'conditions' => array($FilterBy." LIKE " => "%".$SearchTerm."%"),
'limit' => 2,
'convertKeys' => array($FilterBy, $SearchTerm)
);
$this->set('rs', $this->paginate('Order'));
$this->set('SearchTerm', $SearchTerm);
$this->set('FilterBy', $FilterBy);
} else {
$this->Session->setFlash('Please enter something to search for');
}
} else {
$this->Session->setFlash('Please enter something to search for');
}
}
Next, we focus on the view. Remove move this:
$options=array('full_name'=>'By Name','code'=>'By Code','phone'=>'By Phone','email'=>'By Mail');
It shouldn't be in the view. It belongs in the controller (which is where it is now).
Next, cleanup the form:
<?php
echo $this->Form->create('Order', array('action' => 'search','type'=>'get'));
echo $this->Form->input('SearchTerm',array('label' => 'Find:'));
echo $this->Form->input('FilterBy', array('type'=>'select', 'label'=>'Search:', 'options'=>$options));
echo $this->Form->end('search');
?>
*Note that I changed the options name to FilterBy so it is easier to find in the controller. Also there are other things that could be cleaned up in the view. However, I will only address the things that correspond to the question.
Now you need to replace this code:
<tr ><?php //echo $this->Paginator->numbers(array('first' => 'First page')); ?></tr>
<?php
$urlParams = $this->params['url'];
unset($urlParams['url']);
$optionss=array('url' => array('?' => http_build_query($urlParams)));
//$this->Paginator->options($optionss);
$this->Paginator->settings['paramType'] = 'querystring';
?>
<tr ><?php //echo" << ".$this->Paginator->counter(
// 'Page {:page} of {:pages}');
?></tr>
</table>
With this code:
</table>
<p>
<?php
$this->Paginator->options(array('url' => array_merge(array('SearchString' => $SearchString, 'FilterBy' => $FilterBy), $this->passedArgs)));
echo $this->Paginator->counter(array(
'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%')
));
?>
</p>
<div class="paging">
<?php echo $this->Paginator->prev('<< ' . __('previous'), array(), null, array('class'=>'disabled'));?>
| <?php echo $this->Paginator->numbers();?>
| <?php echo $this->Paginator->next(__('next') . ' >>', array(), null, array('class' => 'disabled'));?>
</div>
You an format it differently of course to fit your needs. But the code should work as expected with the search term and filter option applied to pagination.
Good luck and Happy Coding!

Categories