CakePHP : Show database view - cakephp-2.0

I have a view in my database and i want to show the value. but i confused how to call the name of view in my controller. i have made a model, but still i can't show the view. my view database’s name is Vtotaleks. Please help me T_T
here my controller
class HomesController extends AppController{
var $uses = array(
'Vtotaleks',
'SiswaMonitoring',
'Siswa',
'AuthUser',
'PerusahaanOrder');
public function index(){
$this->Lib->cekprivilege();
$totalEks = $this->Vtotaleks->find('all', array('cache' => true));
and this my model
<?php
class Vtotaleks extends AppModel
{
public $usetable = 'vtotaleks'; }
and this my view
<tbody>
<?php $totalEkstrainee=0;foreach($totalEks as $datatotalEks){
$totalEkstrainee+=$datatotalEks['Vtotalekstrainee']['siswa_total'];
?>
<tr>
<td><?php echo $datatotalEks['Vtotalekstrainee']['city_name']?></td>
<td align="right"><strong><?php echo $datatotalEks['Vtotalekstrainee']['siswa_total']?></strong></td>
</tr>
<?php }?>
<tr class="danger">
<td><em>Total</em></td>
<td align="right"><strong><?php echo $totalEkstrainee?></strong></td>
</tr>
</tbody>
and here the error
Notice (8): Undefined variable: totalEks [APP\View\Homes\index.ctp, line 142]
Warning (2): Invalid argument supplied for foreach() [APP\View\Homes\index.ctp, line 142]

Add this line after query
$this->set(compact('totalEks'));

Related

Undefined property: stdClass::$contactno in C:\..\..\Joomla\administrator\components\com_helloworld\views\helloworlds\tmpl\default.php on line 92

i add a contactno field in helloworld component backend. i add a no. of line code same as a Adding categories.
the files loactions:
admin/sql/install.mysql.utf8.sql... i add a contactno field
admin/models/forms/helloworld.xml... i add a contactno field below greeting field
<field name="contactno" type="text" default="Some text"
description="COM_HELLOWORLD_HELLOWORLD_CONTACT_DESC"
label="COM_HELLOWORLD_HELLOWORLD_CONTACT_LABEL"
size="10" />
admin/models/fields/helloworld.php i add a query in protected function getOptions()
$query->select('#__helloworld.id as id,greeting,#__helloworld.contactno as contactno,#__categories.title as category,catid');
admin/views/helloworlds/tmpl/default.php...i add a contactno field th in table below author field
<th width="30%">
<?php echo JHtml::_('searchtools.sort', 'COM_HELLOWORLD_CONTACTNO', 'contactno', $listDirn, $listOrder); ?>
</th>
and td below author
<td align="center"> <?php echo $row->contactno; ?> </td>
when i run a helloworld component i got error
Notice: Undefined property: stdClass::$contactno in C:\xampp\htdocs\Joomla\administrator\components\com_helloworld\views\helloworlds\tmpl\default.php on line 92
what is my mistake...where define a new field...?
i solve by adding in associative array of configuration settings.
file location is: admin/models/helloworlds.php
i add a new fields contactno in array and updating in query
of getListQuery()
$query->select('a.id as id, a.greeting as greeting, a.contactno as contactno, a.published as published, a.created as created')

Cannot use object of type stdClass as array in CodeIgniter

I am trying to display input in table but my code generates the following error:
Fatal error: Cannot use object of type stdClass as array in line 36
This is my view:
<tbody>
<?php
$no=1;
if ($berita){
foreach ($berita as $dt) {
echo "
<tr>
<td>$dt[id_berita]</td> (line 36)
<td>$dt[tanggal]</td>
<td>$dt[judul_berita]</td>
<td>$dt[content]</td>
<td><a class='ui tiny blue edit button' href='".base_url()."index.php/admin/berita/edit/$dt[id_berita]'><i class='edit icon'></i></a>
<a class='ui tiny blue edit button' href='".base_url()."index.php/admin/berita/delete/$dt[id_berita]'><i class='trash icon'></i></a></td>
</tr>
";
$no++;
}
}
?>
</tbody>
What is the issue with my code?
If $dt is object, than you can you it as below
$dt->id_berita
$dt->tanggal

Cakephp find count group by in view

I have a model Gameline and controller GamelinesController and database 'gamelines'.
I want to run this query
Which means there is one record belongs to 2013-02-18 and there is two records belong to 2013-02-25.
After that how to loop count that belongs to each g_time field in view Please help me.
For fields using MySQL functions, like DATE(). You can use Virtual Fields. In your case, you would add something like this to your Gameline model:
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$this->virtualFields = array(
'date' => 'DATE(' . $this->alias . '.g_time)'
);
}
That way, the formatted date will be available as the virtual field date (use another name if you already have a field that's called like that).
Then in your find() operation, fetch the new virtual date field. In order to output those results to your view, you can simply loop over your resultset. Let's say you store your find() result in a view parameter called $data, then you would display the table like:
<table>
<thead>
<tr>
<th>Date</th>
<th>Count</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): ?>
<tr>
<td><?php echo $row['Gameline']['date']; ?></td>
<td><?php echo $row['Gameline']['count']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

Creating an arbitrary property in my Model

Here's my Model:
class Persona extends AppModel {
// This is just some arbitrary property I need to populate in the controller.
public $TipoPersona = '';
}
And here is the Action function in my Controller:
public function details($id = null) {
// Just a typical load by id.
$this->Persona->id = $id;
$this->set('persona', $this->Persona->read());
// Can I do something like?
$this->Persona->TipoPersona = "Mafa Woogly";
}
How can I set the $TipoPersona property in the "Model" here? My intent is to then use that property in the View like:
<tr>
<th>Tipo de Persona:</th>
<td><?php echo h($persona->TipoPersona); ?></td> // Or similar?
</tr>
Any suggestions?
This works, but feels wonky and not strongly typed.
public function details($id = null) {
$this->Persona->id = $id;
$this->set('persona', $this->Persona->read());
$this->set('tipoPersona', "Mafa woogly");
}
<tr>
<th>Tipo de Persona:</th>
<td><?php echo h($tipoPersona); ?></td>
</tr>
The method read() return an array, you can't get the property TipoPersona of this object.
I recommend you add a field in this table to specify type of person and than use the result of read(), like:
<?php echo $persona['Persona']['tipo_persona']; ?>
Try to add it directly with set:
$this->set('tipoPersona', $this->Persona->TipoPersona);
it's property like everyone else in the model. The same way you are setting $this->Persona->id few lines above :)
You could add it to the result array using Model::afterFind() callback.
Are you sure this shouldn't be a regular field in the Model?

Using scripts_for_layout in plugin helper method

i writing my menu plugin helper (similar to cakemenu)
and i want to you js and css related to my plugin
with in scripts_for_layout
how to add them to plugin helper method?
class AdminmenuHelper extends AppHelper {
var $helpers = array('Html', 'Javascript');
function show() {
$output = '
<ul>
<li><img src="/img/admin/icons/home.png" border="0" />;02=0O</li>
<li><img src="/img/admin/icons/document.png" border="0" />#>4C:F8O</li>
<li><img src="/img/admin/icons/document.png" border="0" />0B53>#88</li>
<li><img src="/img/admin/icons/door-open-in.png" border="0" />KE>4</li>
</ul>
';
return $output;
}
In your helper, you can add one of these 2 methods
#1
function beforeRender() {
$html = new HtmlHelper();
$html->script('custom', false);
}
#2
function beforeRender() {
$view =& ClassRegistry::getObject('view');
$view->addScript('<script type="tex/javscript" src="' . Router::url('/js/custom.js') . '"></script>');
}
#2 is lighter because it does not need to instantiate a new helper, but you have to write te entire javascript tag. It’s up to you to choose which one you like more.

Resources