In Cake, we can add url in image() like this:
$this->Html->image('image src...', array('url' => 'some address...');
and output is:
<img src="image src..." />.
How to add class and others attributes to a tag?
echo $this->Html->link(
$this->Html->image($imageSrc, array(
'class'=>'class_of_image',
'height' => '50',
'width' => '100',
'alt' => 'awesome close-up of me eating pizza'
)),
array(
'controller' => 'your_controller',
'action' => 'the_action'
),
array(
'class' => 'class_of_anchor',
'escape' => false //to allow the image tag within the link
)
);
Feel free to make any/all parts of this into a single line - but for StackOverflow reasons, it's easier to read like this.
Related
First of all, sorry if I'm asking for help regarding this question but I have been working on this for almost a day now. I searched this site and was able to find a post similar to my question but I cannot get it to work.
Anyway, I am using CakePHP 2.x, and I need to show reports via charts. I saw this plugin for CakePHP CakePHP GoogleCharts Plugin by Scott Harwell. I did everything step-by-step and somehow my views keep on appearing empty. I made sure it wasn't because of conflicting files so I decided to make a new CakePHP project, unfortunately, it didn't work.
Here are my codes:
Controller:
<?php
App::uses('AppController', 'Controller');
App::uses('GoogleCharts', 'GoogleCharts.Lib');
class ChartsController extends AppController {
public $uses = array('Student');
public $helpers = array('GoogleCharts.GoogleCharts');
public function test_chart(){
$student= $this->Student->getStudentAges();
$studentAgesChart= new GoogleCharts();
$studentAgesChart->type('LineChart');
$studentAgesChart->options(array('title' => "Student Ages"));
$studentAgesChart->columns(array(
//Each column key should correspond to a field in your data array
'name' => array(
'type' => 'string',
'label' => 'Student Name'
),
'age' => array(
'type' => 'number',
'label' => 'Student Age'
)
));
foreach($student as $row){
$studentAgesChart->addRow(array('age' => $row['Student']['age'], 'name' => $row['Student']['name']));
}
$this->set(compact('studentAgesChart'));
debug($studentAgesChart);
}
}
?>
View:
<div id="chart_div" >
<?php
$this->GoogleCharts->createJsChart($studentAgesChart);
?>
</div>
Debug($studentAgesChart):
object(GoogleCharts) {
[private] type => 'LineChart'
[private] columns => array(
'name' => array(
'type' => 'string',
'label' => 'Student Name'
),
'age' => array(
'type' => 'number',
'label' => 'Student Age'
)
)
[private] rows => array(
(int) 0 => array(
(int) 0 => 'Student 1',
(int) 1 => '17'
),
(int) 1 => array(
(int) 0 => 'Student 2',
(int) 1 => '16'
)
)
[private] options => array(
'width' => (int) 400,
'height' => (int) 300,
'title' => 'Student Ages',
'titleTextStyle' => array(
'color' => 'red'
)
)
[private] callbacks => array()
[private] div => 'chart_div'
}
Model:
public function getStudentAges(){
$student_ages = $this->find('all',
array(
'order' => array('Student.name' => 'ASC'),
'limit' => 3,
'fields' => array(
'Student.name',
'Student.age'
)
)
);
return $student_ages;
}
The way I see it, it doesn't contain any errors but my view is empty.
#Scrappy, do you see any output (JavaScript or HTML) included in the DOM when you view source on your page? Or, is the plugin not generating any content at all? If you do see JavaScript code added, is your page reporting JS errors in the console that might prevent the chart from loading properly? If you do not see the JS code, then you likely have not loaded the plugin in your bootstrap file.
Is the page available on the public Internet for review?
The plugin has not seen updates from me in a few years, but there are a few developers that have forked it and updated it for CakePHP 3.0 too. But, this version should still be working unless something changed on the Google end, which I am sure I would have heard about if that was the case.
I have the following link to generate the following link with helpers:
<a href="/rooms" class="ln-room">
<span class="room-icon cls-icon"></span>
<span class="lable">rooms</span>
</a>
I cannot supply the span tags inside $combine, as HtmlHelper::link() calls htmlspecialchars_decode(), thus converting all html to characters.
echo $this->Html->link($combine, array(
'controller' => 'rooms',
'action' => 'index'
),
array('class' => 'ln-room')
);
If the only way is to create a custom helper, how can it be done?
echo $this->Html->link($combine, array(
'controller' => 'rooms',
'action' => 'index'
),
array(
'class' => 'ln-room',
'escape' => false // <--- THIS
)
);
I need to generate html tags like the below .. to be precise, i need to add a class to the anchor tag added to the list, but i dnt seem to get it to work ..
<li class="myLIPrevclass"></li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li class="myLINextclass"></li>
The challenge is,
i cant seem to get a class across to the a tag in the next and the previous, i need to solve this
Even if i leave the a tag blank for the previous and next, CakePHP goes ahead and adds a '>' or '<' which in my case I do not need.
This is what I have tried ..
<?php
echo $this->Paginator->prev(
__(''),
array(
'tag' => 'li',
'class' => 'previous',
),
__(''),
array(
'tag' => 'li',
'class' => ''
)
);
echo $this->Paginator->numbers(
array(
'separator' => '',
'tag' => 'li',
'currentClass' => 'active',
'currentTag' => 'a'
)
);
echo $this->Paginator->next(
__(''),
array(
'tag' => 'li',
'class' => 'next',
),
__(''),
array(
'tag' => 'li',
'class' => ''
)
);
?>
For the disabled titles (for both previous and next) even if i pass in null, Cake add a '>' or '<', and I don't want that
I have checked
Creating Pagination With CakePHP For Custom Template Links
and
Add class to pagination links at CakePHP 2.2
My Cake Version is 2.4.x
I'll appreciate any pointers or help ..
Thanks
Is it possible to create an image with a link that also has a pop up alert [$confirmMessage] using the html helper in CakePHP?
This is my current text link:
$this->Html->link('Clear list', array('controller' => 'items', 'action' => 'clearlist', $model['Model']['id']), array(), 'Clear list?')
This how the image helper creates images with links:
echo $this->Html->image("recipes/6.jpg", array( "alt" => "Brownies", 'url' => array('controller' => 'recipes', 'action' => 'view', 6)));
However this allows only an htmlattributes array as the arguements for the link.
The $confirmMessage alert is not an html attribute is it?
This is the code I tried:
echo $this->Html->link($this->Html->image("clearall.png", array("alt" => "Clear list")), array('controller' => 'items', 'action' => 'clearlist', $model['Model']['id']), array(), 'Clear list?');
However this code printed the correct html for my img as text:
<img src="/img/clearall.png" alt="Clear list" />
Do I have to give up on htmlhelper in this case?
CakePHP does do this with the Html helper and you were really close!
<?php echo $this->Html->link($this->Html->image('clearall.png', array(
'alt' => 'Clear list')
), array(
'controller' => 'items',
'action' => 'clearlist',
$model['Model']['id']
), array(
'escape' => false,
'confirm' => 'Clear list?'
)); ?>
You could also have done it without the helper like so:
<a href="/items/clearlist/<?php echo $model['Model']['id']; ?>"
onclick="return confirm('Clear list?');">
<img src="/img/clearall.png" alt="Clear list" />
</a>
Thanks to ADmad and rtconner for showing me this in IRC.
How do you style the paginator with CSS used in CakePHP?
I can't find a way to attach a CSS class / ID to each of the "span" generated by the default pagination helper in CakePHP.
see: http://www.switchonthecode.com/tutorials/cakephp-part-6-pagination
and https://book.cakephp.org/3/en/views/helpers/paginator.html
What stands out here is that you can pass options to next(), prev(), and numbers()
what you want to do is pass the class option.
e.g.
$paginator->prev(
'<< Previous',
array(
'class' => 'PrevPg'
),
null,
array(
'class' => 'PrevPg DisabledPgLk'
)
).
$paginator->numbers(
array(
'class' => 'numbers'
)
).
$paginator->next(
'Next >>',
array(
'class' => 'NextPg'
),
null,
array(
'class' => 'NextPg DisabledPgLk'
)
),
array(
'style' => 'width: 100%;'
)