I am trying to Iterate over an Associative Array which has the following structure
Output from var_export($data)
<?php
Harvest_Project::__set_state(array(
'_root' => 'project',
'_tasks' => array() ,
'_convert' => true,
'_values' => array(
'id' => '10122036',
'client-id' => '4861417',
'name' => 'ABC',
'code' => '',
'active' => 'true',
'billable' => 'true',
'bill-by' => 'Project',
'hourly-rate' => '145.0',
'budget' => '70.0',
'budget-by' => 'project',
'notify-when-over-budget' => 'true',
'over-budget-notification-percentage' => '80.0',
'over-budget-notified-at' => '2016-09-24',
'show-budget-to-all' => 'false',
'created-at' => '2016-03-15T21:38:40Z',
'updated-at' => '2016-05-31T23:19:58Z',
'starts-on' => '',
'ends-on' => '',
'estimate' => '70.0',
'estimate-by' => 'project',
'hint-earliest-record-at' => '2016-03-16',
'hint-latest-record-at' => '2016-08-11',
'notes' => '',
'cost-budget' => '',
'cost-budget-include-expenses' => 'false',
) ,
))
This is the code which I wrote to Iterate over the Array
<?php
$project_id=10122036;
$result=$api->getProject($project_id);
$data = $result->get( "data" );
echo "<table border='1'>
<tr><td>Project Name</td>
<td>Hourly Rate</td>
</tr>";
foreach($data as $key=>$fruit) {
?>
<tr><td><?php echo $fruit->name;?></td>
<td><?php echo $fruit->{'hourly-rate'};?></td></tr>
<?php }
echo "</table>";
?>
This code only creates the columns and for some reason does not generate the entries for each row in the table. Hence the resulting table is an Empty table.
Kindly suggest where I am going wrong.
Update
This code is for querying Harvest using the Harvest API. This is the PHP Wrapper Library http://mdbitz.com/docs/harvest-api/ which contains relevant classes and methods. getProject($project id) is a method to retrieve project details based on the project ID.
Added html table code.
$project_id='10122036';
$result=$api->getProject($project_id);
echo "<table border='1'>
<tr><th>Project Name</th>
<th>Hourly Rate</th></tr>";
if( $result->isSuccess() ) {
$project = $result->data;?>
<tr><td><?php echo $project->get( "name" );?></td>
<td><?php echo $project->hourly-rate;?></td></tr>
<?php }else{?>
<tr><td colspan="2">No data from API</td></tr>
<?php } ?>
</table>
Since you are trying to get a single project using api, Use the below code
$project_id='10122036';
$result=$api->getProject($project_id);
if( $result->isSuccess() ) {
$project = $result->data;
echo $project->get( "name" );
echo $project->hourly-rate;
}else{
echo "No data from API";
}
If you see the out put "No data from API", then there is not data returned from API
Related
I have an array like this:-
$str = array(
array(
'amount' => 1.87,
'user' => 'hello',
),
array(
'amount' => 0.9,
'user' => 'test',
),
array(
'amount' => 9,
'user' => 'hello',
),
array(
'amount' => 1.4,
'user' => 'test',
)
);
Now I show this data in HTML table like this for user 'test' :-
<thead>
<tr>
<th>Amount</th>
<th>User</th>
</thead>
<tbody>
<tr>
<td><?php
foreach ($str as $new_str) {
if ($new_str['user'] == "test") {
echo $new_str['amount'];
echo "<br />";
}
}
?></td><td><?php
foreach ($str as $new_str) {
if ($new_str['user'] == "test") {
echo $new_str['user'];
echo "<br />";
}
}
?></td>
</tr>
</tbody>
But now the problem is that when I use this code it shows the amount and user as a whole instead of two different rows. How can I fix this? Any help?
You just need to move your foreach loop outside of the <tr>...</tr> structure. This should work:
<?php foreach($str as $new_str){
if($new_str['user']=="test"){
echo "<tr><td>" . $new_str['amount'] . "</td><td>" . $new_str['user'] . "</td></tr>";
}
}
?>
Output (for your data)
<tr><td>0.9</td><td>test</td></tr>
<tr><td>1.4</td><td>test</td></tr>
Your tr was not repeating. output image I hope this will help.
<?php
$str = array(
array(
'amount' => 1.87,
'user' => 'hello',
),
array(
'amount' => 0.9,
'user' => 'test' ,
),
array(
'amount' => 9,
'user' => 'hello',
),
array(
'amount' => 1.4,
'user' => 'test',
)
);
?>
<table>
<thead>
<tr>
<th>Amount</th>
<th>User</th>
</tr>
</thead>
<tbody>
<?php foreach($str as $new_str) {
if($new_str['user'] == "test"){
echo '<tr>';
echo '<td>'.$new_str['amount'].'</td>';
echo '<td>'.$new_str['user'].'</td>';
echo '</tr>';
}
} ?>
</tbody>
</table>
I am developing a web app using cake php 2.
I have an issue while displaying data from 2 tables...
My models :
<?php
class Discipline extends AppModel{
public $hasMany = "Student";
}
?>
<?php
class Student extends AppModel{
public $belongsTo = array(
"Discipline" => array(
"className" => "Discipline",
"foreignKey" => "discipline_id"
)
);
}
?>
Here is my studentscontroller :
<?php
class StudentsController extends AppController{
function admin_index(){
if($this->request->is('put') || $this->request->is('post')){
$student = $this->request->data['Student'];
if($this->Student->save($this->request->data)){
$this->Session->setFlash("L'eleve a bien été modifié","notif");
}
}
$d['student'] = $this->Student->find('all',array(
'contain' => "Discipline"
));
$this->set($d);
}
}
I am trying to display student's data using this view :
<?php
foreach($student as $k => $v){
$v = current($v);
echo "<td>Action</td>";
echo "<td>Label</td>";
echo "<td>".$v['nom']." ".$v['prenom']."</td>";
echo "<td>".$v['sexe']."</td>";
echo "<td>".$v['naissance']."</td>";
echo "<td>".$v['Discipline']['designation']."</td>";
echo "<td>".$v['comite']."</td>";
echo "<td>".$v['classe']."</td>";
echo "<td>".$v['elite']."</td>";
echo "<td>".$v['alerte']."</td>";
echo "<td>".$v['quota1']."</td>";
echo "<td>".$v['quota2']."</td>";
echo "<td>".$v['total']."</td>";
echo "<td>Pris</td>";
echo "<td>Restant</td>";
echo "<td>Supp</td>";
}
?>
But i have an issue on this line :
echo "<td>".$v['Discipline']['designation']."</td>";
It says this error :
notice (8): Undefined index: designation [APP\View\Students\admin_index.ctp, line 47]
I am used to develop on cakephp 3 and I am pretty embarassed with that error.. what to do to display data from Disciplines table from my student view ?
Any idea ? Thx
EDIT: I did a debug on my StudentsController, and I found the data I wanna display :
array(
'student' => array(
(int) 0 => array(
'Student' => array(
'id' => '2',
'prenom' => 'Jean',
'nom' => 'Michel',
'sexe' => '1',
'naissance' => '2015-08-02',
'age' => '12',
'classe' => '1',
'discipline_id' => '1',
'comite' => 'test',
'categorie' => 'test',
'elite' => true,
'alerte' => 'test',
'quota1' => '12',
'quota2' => '12',
'total' => '24',
'note' => 'tete'
),
'Discipline' => array(
**'id' => '1',
'designation' => 'Alpin'**
)
)
)
)
i think you should change the view
Change:
foreach($student as $k => $value){
$v = current($value);
to:
foreach($student as $k => $v){
$v = current($v);
and change:
echo "<td>".$v['Discipline']['designation']."</td>";
to
echo "<td>".$value['Discipline']['designation']."</td>";
You overwrite the variable $v with the first found array, so $v would be $v['Student']
I for myself won't use current to go inside a array for CakePHP, but use $v['Student'] everywhere
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Displaying distinct columns and non-distinct column in one table
I have 3 tables. One is userinfo (which where you can find Name), then logs for login details and evaluation for user evaluation of the activity. They are connected by the field attendee_id.
When I joined them it gives me this output:
What I want is to have an output like this:
When I use GROUP BY Name it returned only one row for each and not returning other data.
It can be done like this. In this example I have hard coded the $rows array, but you can replace that with your array of rows from the database.
<?php
$rows = array(
array(
'name' => 'Juan',
'login' => '09:00:01',
'evaluation' => 'Yes'
),
array(
'name' => 'Juan',
'login' => '09:00:02',
'evaluation' => 'Yes'
),
array(
'name' => 'Juan',
'login' => '09:00:03',
'evaluation' => 'Yes'
),
array(
'name' => 'Jose',
'login' => '09:00:04',
'evaluation' => 'No'
),
array(
'name' => 'Jose',
'login' => '09:00:05',
'evaluation' => 'No'
)
);
?>
<table>
<tr>
<th>Name</th>
<th>Login</th>
<th>Evaluation</th>
</tr>
<?php
$prevName = '';
foreach($rows as $row):
if($prevName == $row['name']) {
$name = '';
} else {
$name = $prevName = $row['name'];
}
?>
<tr>
<td><?php echo htmlspecialchars($name); ?></td>
<td><?php echo htmlspecialchars($row['login']); ?></td>
<td><?php echo htmlspecialchars($row['evaluation']); ?></td>
</tr>
<?php endforeach; ?>
</table>
The result is:
I tried doing this a few times and gave up. In the end, I formatted the output in PHP by checking if the value for 'name' in the current row is different to the previous one:
<?php
//Assume the query has been done
//...
$lastName='';
echo '<table>';
while ($row=mysql_fetch_assoc($query_result))
{
if ($row['name']!=$lastName)
{
$lastName=$row['name'];
} else
{
$row['name']='';
}
echo '<td>'.implode('</td><td>',$row).'</td>;
echo '<tr>';
}
echo '</table>';
It's not pretty but it'll get you the result you want. You can tart it up by
Here is the script I am running and I would like if there are 2 strings that the same to only display one string and not both. I dont know where to add the array_unique() I have added it to my script but it doesnt seem to work properlly, instead it is taking out all the strings with the same value Here is the script I am running and I would like if there are 2 strings that the same to only display one string and not both
//Get slider data from theme options
$company1 = $data['md_media_company_img1'];
$company2 = $data['md_media_company_img2'];
$company3 = $data['md_media_company_img3'];
$company4 = $data['md_media_company_img4'];
$company5 = $data['md_media_company_img5'];
$company6 = $data['md_media_company_img6'];
$company7 = $data['md_media_company_img7'];
$company8 = $data['md_media_company_img8'];
$company9 = $data['md_media_company_img9'];
$company10 = $data['md_media_company_img10'];
$company11 = $data['md_media_company_img11'];
$company12 = $data['md_media_company_img12'];
/*Slides Array*/
$company_name = array(
'company1' => array(
'name' => $company1,
),
'company2' => array(
'name' => $company2,
),
'company3' => array(
'name' => $company3,
),
'company4' => array(
'name' => $company4,
),
'company5' => array(
'name' => $company5,
),
'company6' => array(
'name' => $company6,
),
'company7' => array(
'name' => $company7,
),
'company8' => array(
'name' => $company8,
),
'company9' => array(
'name' => $company9,
),
'company10' => array(
'name' => $company10,
),
'company11' => array(
'name' => $company11,
),
'company12' => array(
'name' => $company12,
)
);
/*check if exist slide*/
$check_exist_company = 0;
$result = array_unique($company_name);
foreach($result as $company => $value) {
if (!empty ($value['name'])){
$check_exist_company = 1;
}
}
?>
<?php if($check_exist_company == 1) {// check if any slide image added in theme option, return custom slide?>
<?php $i = 1; ?>
<?php foreach($company_name as $company => $value) {
if (!empty ($value['name'])) {?>
<li><a class="nivoLink4" rel="<?php echo $i;?>" href="#"><?php echo $value['name'];?></a></li>
<?php ++$i ?>
<?php } ?>
<?php }?>
<?php } ?>
<!--/slider-->
You could just run array_unique() on the source array and just iterate over the result.
I've found some very helpful tutorials and posts on StackOverflow about this topic, but I am completely stuck on one point.
Everything below IS working with the exception of my HABTM Zip data.
Here is my code:
<?php for ($i = 1; $i <= 3; $i++) { // 3 fields at a time ?>
<?php echo $this->Form->input('Plan.' . $i . '.plan_detail_id', array(
'options' => $plans_list,
'type' => 'select',
'empty' => '-- Select a the Plan Detail --',
'label' => 'Select a the Plan Detail'
)); ?>
<?php echo $this->Form->input('Plan.' . $i . '.monthly_cost', array('label' => 'Monthly Cost')); ?>
<?php echo $this->Form->input('Plan.' . $i . '.dental_cost', array('label' => 'Dental Cost')); ?>
<?php echo $this->Form->input('Plan.' . $i . '.age_id', array('label' => 'Select an Age Range', 'empty' => '-- Select an Age Range --')); ?>
<?php echo $this->Form->input('Plan.' . $i . '.applicant_id', array('label' => 'Applicant Type', 'empty' => '-- Select an Applicant Type --')); ?>
<?php echo $this->Form->input('Plan.' . $i . '.state_id', array('label' => 'Select a State', 'empty' => '-- Select a State --')); ?>
<?php echo $this->Form->input('"$i".Zip', array(
'type' => 'select',
'multiple' => 'true',
'label' => 'Select the Zips'
));
<?php } // end for() ?>
My controller action is as follows:
function bulk_add() {
if (!empty($this->data)) {
$this->Plan->create();
if ($this->Plan->saveAll($this->data, array('validate' => 'false'))) {
$this->Session->setFlash(__('The plan has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The plan could not be saved. Please, try again.', true));
}
}
$ages = $this->Plan->Age->find('list', array('order' => array('Age.name ASC')));
$applicants = $this->Plan->Applicant->find('list', array('order' => array('Applicant.name ASC')));
$states = $this->Plan->State->find('list', array('order' => array('State.name ASC')));
$zips = $this->Plan->Zip->find('list', array('order' => array('Zip.title ASC')));
$this->set(compact('planDetails', 'ages', 'applicants', 'states', 'zips'));
$plans_list = array();
$plans = $this->Plan->PlanDetail->find('all', array('order' => array('PlanDetail.name ASC')));
foreach ($plans as $row) {
$plans_list["{$row['PlanDetail']['id']}"] = "{$row['PlanDetail']['name']} - {$row['PlanDetailNote']['name']}";
}
$this->set('plans_list', $plans_list);
}
Day 3 : )
I cannot get my array (with multiple entries) to NOT be indexed numerically. And a keyed array is required for the saveAll() on multiple tables for it to work properly.
I have a complete data dump below with the numeric indexed array, and somehow it needs to be indexed by keys (I can get it to work correctly but ONLY on single record insert)..
My view for bulk_add
<?php for ($i = 1; $i <= 2; $i++) { ?>
<table>
<tr>
<td><?php echo $this->Form->input("$i.plan_detail_id", array(
'options' => $plans_list,
'type' => 'select',
'empty' => '-- Select a the Plan Detail --',
'label' => 'Select a the Plan Detail'
));
?></td>
<td><?php echo $this->Form->input("$i.monthly_cost", array('label' => 'Monthly Cost')); ?></td>
<td><?php echo $this->Form->input("$i.dental_cost", array('label' => 'Vision Cost')); ?></td>
<td><?php echo $this->Form->input("$i.age_id", array('label' => 'Select an Age Range', 'empty' => '-- Select an Age Range --')); ?></td>
<td><?php echo $this->Form->input("$i.applicant_id", array('label' => 'Applicant Type', 'empty' => '-- Select an Applicant Type --')); ?></td>
<td><?php echo $this->Form->input("$i.state_id", array('label' => 'Select a State', 'empty' => '-- Select a State --')); ?></td>
<td>
<?php echo $this->Form->input("$i.Zip", array('multiple' => 'true')); ?>
</td>
</tr>
</table>
<?php } // end for() ?>
<?php
echo $this->Form->end(__('Submit', true));
?>
My controller code:
function bulk_add() {
if (!empty($this->data)) {
$this->Plan->create();
if ($this->Plan->saveAll($this->data, array('atomic' => 'false'))) {
// Debug
debug($this->data);
$this->Session->setFlash(__('The plan has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The plan could not be saved. Please, try again.', true));
}
}
$ages = $this->Plan->Age->find('list', array('order' => array('Age.name ASC')));
$applicants = $this->Plan->Applicant->find('list', array('order' => array('Applicant.name ASC')));
$states = $this->Plan->State->find('list', array('order' => array('State.name ASC')));
$zips = $this->Plan->Zip->find('list', array('order' => array('Zip.title ASC')));
$this->set(compact('planDetails', 'ages', 'applicants', 'states', 'zips'));
$plans_list = array();
$plans = $this->Plan->PlanDetail->find('all', array('order' => array('PlanDetail.name ASC')));
foreach ($plans as $row) {
$plans_list["{$row['PlanDetail']['id']}"] = "{$row['PlanDetail']['name']} - {$row['PlanDetailNote']['name']}";
}
$this->set('plans_list', $plans_list);
}
Here is the debug dump:
Array
(
[Plan] => Array
(
[1] => Array
(
[plan_detail_id] => 36
[monthly_cost] => 0
[dental_cost] => 0
[age_id] => 14
[applicant_id] => 1
[state_id] => 1
)
[2] => Array
(
[plan_detail_id] => 36
[monthly_cost] => 0
[dental_cost] => 0
[age_id] => 2
[applicant_id] => 4
[state_id] => 1
)
)
[1] => Array
(
[1] => Array
(
[Zip] => Array
(
[0] => 487
[1] => 486
[2] => 485
[3] => 484
[4] => 483
)
)
)
[2] => Array
(
[2] => Array
(
[Zip] => Array
(
[0] => 485
[1] => 484
[2] => 483
)
)
)
)
Looks like you built your input names wrong if you want to save many rows. You probably want "$i.Plan.field", and "$i.Zip"
$this->Form->input('Zip'); its in the book
look up find('list') for you nice foreach hack.