cakePHP formHelper postLink image - php

I want to output an image with a hyperlink wrapped around it instead of just a text hyperlink using cakePHP's formHelper::postLink function.
Does anyone know how to do this? I tried multiple things but couldn't get it working.
<?php echo $this->Form->postLink(
'Delete',
array('action' => 'delete', $country['Country']['id']),
array('confirm' => __('Are you sure you want to delete ').$country['Country']['name'].'?')
)?>
So instead of 'Delete' I want to display an image.

Here's what works for me.
echo $this->Form->postLink(
$this->Html->image('icn_trash.png', array('alt' => __('Effacer'))), //le image
array('action' => 'delete', $artist['Artist']['id']), //le url
array('escape' => false), //le escape
__('Êtes-vous sûr de vouloir effacer artiste #%s?', $artist['Artist']['id']) //le confirm
); //le voila

Try this :
echo $this->Form->postLink(
$this->Html->image('delete.png',
array("alt" => __('Delete'), "title" => __('Delete'))),
array('action' => 'delete', $items['Item']['id']),
array('escape' => false, 'confirm' => __('Are you sure?'))
);

If I understand your question correctly I dont think you want to use $this->Form->postLink
I think this page is exactly what you're after: http://book.cakephp.org/view/1441/image
This uses $this->Html->image to create the image and then you can pass a URL through as one of the parameters to specify the surrounding anchor link.

You can wrap in image inside a link element, but you need to set the escape option to false, like this:
echo $this->Html->link(
$this->Html->image('your_image_here.jpg', array(
'alt' => 'Alternative Text for your image',
'title' => 'Optional tooltip text for your image'
),
array(
'controller' => 'YourController',
'action' => 'someAction'
),
array(
'escape' => false // Add this to avoid Cake from printing the img HTML code instead of the actual image
)
);
That should do the trick.

echo $this->Html->link(
$this->Html->image("recipes/6.jpg", array("alt" => "Brownies")),
array(
'controller' => 'recipes',
'action' => 'view',
'id' => 6,
'comments' => false
)
)

function delete_image(){
if ($this->Session->read('Auth.User.id')) {
$this->User->id = $this->Session->read('Auth.User.id');
$this->User->updateAll(
array('User.image' => "''"),
array('User.id' => $this->User->id)
);
$this->Session->setFlash('The image has been deleted.');
$this->redirect(array('action' => 'profile'));
}
}

Related

Add icon to a link cakephp 2

I'd like to add a icon <i> tag to a Cakephp link.
Here is my code :
<?= $this->Html->link($this->Html->tag('i', '', array('class' => 'fa fa-shopping-cart')).'Cart', array('controller' => 'shop', 'action' => 'cart')) ?>
This line generates :
<i class="fa fa-shopping-cart"></i>Cart
Why < is replaced by its hexa value? My charset is UTF-8.
Thanks!
Add option 'escape' set to false:
<?= $this->Html->link($this->Html->tag('i', '', array('class' => 'fa fa-shopping-cart')).'Cart', array('controller' => 'shop', 'action' => 'cart'), array('escape' => false)) ?>
Documentation page about HtmlHelper.
Html->link($this->Html->tag('i', '',['class' => 'fa fa-shopping-cart']).'Cart',['controller' => 'shop', 'action' => 'cart'], ['escape' => false]); ?>

Image Link with DIV tag

I have the following Link Format which i want to display using Cakephp HTML helper
<a href="product_n1_details.html" title="" class="view_image">
<img src="images/slider/1n.jpg" alt="">
<div class="link_overlay icon-search"></div>
</a>
What i have tried?
<?php echo $this->Html->link($this->Html->image('products/'.$pro['Product']['display_photo']."<div class=\'link_overlay icon-search\'></div>", array('alt' => $pro['Product']['name'])), array('controller' => 'products', 'action' => 'view', $pro['Product']['id']), array('escape' => false, 'class' => 'view_image') ); ?>
The image helper returns the entire <img> tag, so put the div code after your image helper call (spaced out for clarity):
echo $this->Html->link(
$this->Html->image(
'products/'.$pro['Product']['display_photo'],
array(
'alt' => $pro['Product']['name']
)
) . "<div class='link_overlay icon-search'></div>", // put it here!
array(
'controller' => 'products',
'action' => 'view',
$pro['Product']['id']
),
array(
'escape' => false,
'class' => 'view_image'
)
);
scrowler is correct.
All Html helper functions return strings so if you want to make it a little cleaner for yourself you could do this
$image = $this->Html->image( . . . );
$div = $this->Html->tag( 'div', ... );
Then finally
echo $this->Html->link( $image . $div, ... );
Hope that helps.

Cake PHP Form getting data from input and passing it to the url

I have my view controller that grabs a code number from the url
users/view/10024 or users/view/10004 etc
in my user index.ctp
echo $this->Form->create(null, array(
'url' => array('controller' => 'users', 'action' => 'view2'),
'type' => 'get',
'inputDefaults' => array(
'label' => false,
'div' => false
)
));
echo $this->Form->input(
'Enter Code',
array('label' => 'Code')
);
$options = array(
'label' => 'Submit',
'div' => array(
'class' => 'glass-pill',
)
);
I get this
/users/view?Enter+Code=1001
I want
/users/view/1001
So i want to grab the input data and put it in the url
This cannot be done using PHP.
Instead you need to use the rewriting engine of Apache: MOD_REWRITE http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
Create a simple rewrite rule that transforms the URL in the way you want to achieve.
You can see an example on rewriting URL's here: http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

CakePHP: Using FormHelper to pass search query to controller parameter

In CakePHP 2.4, how do I pass a form directly to a controller parameter? My controller takes a parameter in the format /Model/index/by:keyword, however FormHelper keeps inserting a question mark and an equals sign into the URL: Model/index?by%3A=keyword.
This is the form I've been using. Is there any way to change this default behavior?
echo $this->form->create('Post', array('action' => '/index', 'type' => 'get', 'class' => 'navbar-form'));
echo $this->form->input("by:", array('label' => '', 'placeholder' => 'Search', 'class' => 'form-control'));
echo $this->form->end();
I'd try to create the form like this:
echo $this->Form->create('Post', array('type' => 'GET', 'url' => array('controller' => 'yourpostcontroller', 'action' => 'search')));
echo $this->Form->input('search', array('class' => 'form-control'));
echo $this->Form->button('Search', array('div' => false, 'class' => 'btn'));
echo $this->Form->end();
In your "Search" action (inside the controller) do not forget to read the request:
if(!empty($this->request->query['search']) ...{
//do something
}
Hope it helps.

# in a CakePHP $this->Html->link

I was wondering how I go about adding a "#" to a cakephp url without it being escaped. Ive tried 'escape' => false but to no success. I am doing this to try and link to a id="comments" section of the page that has contains the comments. Here is my code for the linking:
<?php echo $this->Html->link($post['Post']['total_comments'].' comments', array('controller' => 'posts', 'action' => 'view', $post['Post']['slug'].'#comments'),array('class' => 'comments-icon')); ?>
Any help would be appreciated.
Try this:
echo $this->Html->link($post['Post']['total_comments'].' comments', array(
'controller' => 'posts',
'action' => 'view',
$post['Post']['slug'],
'#' => 'comments' //Or #comments only without key
), array('class' => 'comments-icon'));

Categories