I'm building a project that loads Fantasy Football stats of players. I'm using NFL.com's API and I'm stuck right now. While doing some searching I saw some similar topics already answered on stackoverflow, but they didn't quite fit my needs.
Here's the links to the JSON files I'm using:
http://api.fantasy.nfl.com/v1/players/stats?statType=seasonStats&season=2014&week=1&format=json
http://api.fantasy.nfl.com/v1/players/stats?statType=seasonStats&season=2014&week=2&format=json
http://api.fantasy.nfl.com/v1/players/stats?statType=seasonStats&season=2014&week=3&format=json
Here's a link to an unflattened version so you can actually see the structure properly: http://codebin.org/view/89722b2e
As you can see, each of those links is picking a different week of the season. The name, position, and team I only need to pull once, but I need to pull the weekly stats from each JSON file for each player. I'm not sure exactly how to go about doing it.
This is the code I'm using:
$json = file_get_contents("http://api.fantasy.nfl.com/v1/players/stats?statType=seasonStats&season=2014&week=1&format=json");
$data = json_decode($json);
$path = $data->players;
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Team</th>
<th>Position</th>
<th>Season Points</th>
<th>1</th>
<th>2</th>
</tr>
</thead>
<tbody>
<?php foreach ($path as $player) {
$parts = explode(" ", $player->name);
$lastname = array_pop($parts);
$firstname = implode(" ", $parts);
$player->name = $lastname.", ".$firstname;
?>
<tr>
<td><?php echo $player->name; ?></td>
<td><?php echo $player->teamAbbr; ?></td>
<td><?php echo $player->position; ?></td>
<td><?php echo $player->seasonPts; ?></td>
<td><?php echo $player->weekPts; ?></td>
<td></td>
</tr>
<?php } ?>
</tbody>
</table>
The blank is being saved for the week 2 points. Any help would be greatly appreciated.
Related
So I need to get data from multiple tables and I'm having trouble doing so.
I have two model classes :
Thesis_Supervisor : has a supervisor id and a thesis id
Thesis : has every information about the thesis (id, subject, author...)
What I want to do is, considering a supervisor id, I want to know which thesis he supervises and all the infos about the thesis.
Here's my code :
in my Thesis Supervisor Controller I have this function :
public function listOfThesisBySupervisor($id=null){
$result = $this->ThesisSupervisor->find('all', [
'conditions' => ['supervisor_id' => $id],
]);
$resultset=array();
foreach($result as $row){
$this->loadModel('Theses');
$query = $this->Theses->find('all');
array_push($resultset,$query->where(['id' => $row->these_id])->toArray());
}
$this->set('Thesis', $resultset);
}
}
this function is :
getting all the thesis linked to a given supervisor
uses a foreach to go through the different thesis
stores the thesis infos in the variable resultset
returns it to the view
here's the code in the view :
<div class="thesis index large-9 medium-8 columns content">
<h3><?= __('Thesis') ?></h3>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th scope="col"><?= $this->Paginator->sort('id') ?></th>
<th scope="col"><?= $this->Paginator->sort('sujet') ?></th>
<th scope="col"><?= $this->Paginator->sort('type') ?></th>
<th scope="col"><?= $this->Paginator->sort('date_debut') ?></th>
<th scope="col"><?= $this->Paginator->sort('date_fin') ?></th>
<th scope="col"><?= $this->Paginator->sort('signature') ?></th>
<th scope="col"><?= $this->Paginator->sort('auteur_id') ?></th>
<th scope="col" class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($Theses as $thesis): ?>
<tr>
<td><?= $this->Number->format($thesis->id) ?></td>
<td><?= h($thesis->subject) ?></td>
<td><?= h($thesis->type) ?></td>
<td><?= h($thesis->beginning) ?></td>
<td><?= h($thesis->ending) ?></td>
<td><?= h($thesis->sign) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
So with this code, I do find a correct number of results, but they are not recognised in the view, I get this type of error for each field of the thesis :
Trying to get property 'id' of non-object
I tried many different syntaxes to do this (I tried removing the ->toArray in the foreach to have an object but I have an undefined property error for each field of the thesis), but none of them worked. I think the problem is adding the results of each foreach loop to the resultset variable, but I don't see how to fix it...
EDIT : as ndm suggested in his comment I looked more into the associations (as my model classes are linked together with associations) and here my final code :
$result = $this->ThesisSupervisor->find('all', [
'conditions' => ['supervisor_id' => $id],
'contain' => ['Thesis']
]);
$resultset=array();
foreach ($result as $row){
$resultset[]=$row["thesis"];
}
$this->set('Theses', $resultset);
it is much simpler than my original code and works perfectly.
I have a couple of columns on an index page where I would like to include line breaks.
<thead>
<tr>
<th><?= $this->Paginator->sort('name') ?></th>
<th><?= $this->Paginator->sort('temp', ['label' => 'Temperature (C)']) ?></th>
<th><?= $this->Paginator->sort('vol', ['label' => 'Volume (m^3)']) ?></th>
</tr>
</thead>
What I would like is for the column "units" to be underneath the "unit name". Essentially, I want to replace the space before the units with a "br" tag.
Ideally, these "th" items should render out similar to the following:
<th>Temperature<br>(C)</th>
<th>Volume<br>(m^3)</th>
Recommendations?
This can be done like so:
<?= $this->Paginator->sort('temp', 'Temperature<br>(C)', ['escape' => false]) ?>
See the documentation for an example on using html in sort links.
I using cakephp framework 2. to develop a Employee daily attends, in my application index page i have have 3 options edit , apply holiday, and deleted holiday. Two links, edit and apply working correctly but for delete i am using post link. When there is one delete button as required it did't pass parameter to delete function, but creating copy of same link mean two delete button then the last one pass parameter, i did'd now there is any thing wrong in my code. Here is my View code
<?php
$i=1;
foreach($holidays as $holiday):?>
<tr >
<td><?php echo $i++; ?></td>
<td><?php echo $holiday['Holiday']['title'] ?></td>
<td><?php echo $holiday['Group']['title'] ?></td>
<td><?php echo $holiday['Shift']['title'] ?></td>
<td><?php echo $holiday['Holiday']['from_date'] ?></td>
<td><?php echo $holiday['Holiday']['to_date'] ?></td>
<td><?php echo $this->Html->link('<span class="glyphicon glyphicon- pencil"></span> Edit',array('action'=>'edit', $holiday['Holiday']['id']),array('class'=>'btn btn-warning','escape'=>false)); ?>
<?php echo $this->Html->link('<span class="glyphicon glyphicon-refresh"></span> Apply',array('action'=>'applyholiday', $holiday['Holiday']['id']),array('class'=>'btn btn-success','escape'=>false)); ?>
<?php
echo $this->Form->postLink(
'<span class="glyphicon glyphicon-trash"></span> Delete',
array('action' => 'delete', $holiday['Holiday']['id']),
array('confirm' => 'Are you sure?','escape'=>false,'class'=>'btn btn-danger')
);
?>
</td>
</tr>
<?php endforeach; ?>
<?php unset($holiday); ?>
Here is my HolidaysController code for delete function.
public function delete($id){
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
debug($id);
exit;
if($this->Holiday->delete($id)){
$this->Flash->success(__('Record Deleted Success.'));
return $this->redirect($this->referer());
}
}
First : postLink Creates an HTML link, but access the URL using method POST , (see more information here)
means your method of request is post.
And Second:Your condition in delete function is:
if($this->request->is('get')){
//
}
Means your request is never going to inside your function.
Hence just try using:
if($this->request->is('post')){
//
}
instead.
You need to use post instead of get in your controller.
if(($this->request->is('post') || $this->request->is('put')){
//Code goes here
}
//PagesTable.php
class PagesTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Books');
}
}
//BooksTable.php
class BooksTable extends Table
{
public function initialize(array $config)
{
$this->hasMany('Pages');
}
}
From the above two models, I want to retrieve pages that belong to a particular book in the BooksController.
After several hours of going through documentation, I somehow built this that is able to achieve what I want.
$pages = $this->Books->Pages->find()->where(['Pages.book_id' => $id]);
I have a feeling that I have not exploited cake here because it looks like I am building a custom query rather than just pulling the associated data. But as a beginner in cake 3.0 (with no experience in cake 2.x), this was the best I could do.
Am I going the right way ?
Isn't there a better way to find the pages of a book (rows of an associated hasMany model)?
there is a simpler way check this:
in the controller
$maestro = $this->Maestro->get($id, [
'contain' => ['DetalleMaestro']]);
$this->set('maestro', $maestro);
this search in the db all the maestro records and the associated records detallemaestro
in the view
<?php if (!empty($maestro->detalle)): ?>
<h5><?= __('Fuentes de Informacion Asociadas') ?></h5>
<table cellpadding="0" cellspacing="0">
<tr>
<th><?= __('Nombre 1') ?></th>
<th><?= __('Nombre 2') ?></th>
<th><?= __('Nombre 3') ?></th>
<th><?= __('Nombre 4') ?></th>
</tr>
<?php foreach ($maestro->detalle as $detalle): ?>
<tr>
<td><?= h($detalle->1) ?></td>
<td><?= h($detalle->2) ?></td>
<td><?= h($detalle->3) ?></td>
<td><?= h($detalle->4) ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
this checks if Maestro has a association and display it
hi all just wanting to know if you were printing stuff out in a foreach loop how would you code it so that only a specific set of data pertaining to that person. For example I want it to print when active in my relationships table is =false or 0
this is the current code I have in my view.
<?php foreach($Relationships as $relationship):?>
<tr>
<td align='center'><?php echo $relationship['Relationship']['partyone']; ?></td>
<td align='center'><?php echo $relationship['Relationship']['partytwo']; ?></td>
<td> </td>
<td><?php echo $this->Html->link('approve', array('action'=>'approve', $relationship['Relationship']['id'])); ;?>
<td><?php echo $this->Html->link('Decline', array('action'=>'decline', $relationship['Relationship']['id'])); ;?>
</td>
</tr>
<?php endforeach; ?>
The error was in my find condition in the controller, not the view. here is the line of code I had to write for my controller
$this->set('Relationships', $this->Relationship->find('all', array('conditions' => array('Relationship.partytwo' => $userid,'Relationship.active'=>false))));