Cakephp find count group by in view - cakephp

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>

Related

Parsing json data and show in loop in a table

I'm developing a website with laravel where I've a json response from server with a blade.php view like below,
[{"id":1,"user_id":"1","patient_name":"kk","age":"44","sex":"Male"},
{"id":2,"user_id":"1","patient_name":"noor","age":"7","sex":"Male"},
{"id":3,"user_id":"1","patient_name":"noor","age":"44","sex":"Male"}]
How can I iterate through this json object so that I can show the data in a table with patient_name,age,sex column in blade.php?
First you would have to convert the JSON to an array with $array_data = json_decode($array, true), then you can pass the data to your view with return view('page', ["array_data"=>$array_data]);, next you would have to parse it in blade like:
<table>
<tr>
<td>id</td>
<td>User id</td>
<td>Patient name</td>
<td>Age</td>
<td>Sex</td>
</tr>
#foreach($array_data as $key=>$value){
<tr>
<td>{{$value["id"]}}</td>
<td>{{$value["user_id"]}}</td>
<td>{{$value["patient_name"]}}</td>
<td>{{$value["age"]}}</td>
<td>{{$value["sex"]}}</td>
</tr>
#endforeach
And so your code in your controller would be:
$array_data = json_decode($array, true);
return view('page', ["array_data"=>$array_data]);
Note that the string page must be the name of your blade template minus the .blade.php, i.e. if your template is called page.blade.php you would use the string page

CakePHP : Show database view

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'));

Mootools 1.3.2 Request.HTML fetch table rows

First off please don't bash me for using old technology (polling) and an old version of Mootools (1.3.2) as I don't have control over these factors.
Ok here's my problem.
I have a page which refreshes every few seconds to fetch new data from the database via AJAX. Ideally the structure of the returned value should be as such:
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
After receiving this table row structure result, I need to append that to the current table in the page which essentially just adds a new record on the table if there are new records. If there's none, then no changes to the table are made.
Currently I am using
var req = new Request.HTML({url: url_to_get_new_rows,
onSuccess: function(html, responseHTML) {
// append table row 'html' here
}
}).send();
However, the returned value in the 'html' variable that I'm supposed to append at the end of the table only returns
1 2 3 4
This obviously is an undesired behavior as I need the tr and td elements to make it work.
I hope someone could help me with this problem.
THANKS!
Javascript:
new Request.HTML({
url:'tr.php',
onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
var tbody = document.id('tbody');
tbody.set('html', tbody.get('html') + responseHTML);
// or
var tr = new Element('table', {'html': responseHTML}).getElement('tr');
tr.inject(tbody);
}
}).get();
HTML:
<table>
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
</tr>
</thead>
<tbody id="tbody">
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
</tr>
</tbody>
</table>

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

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?

Resources