Data from $this->EventoObreiro->find('all');:
Array (
[0] => Array (
[EventoObreiro] => Array (
[id] => 1
[evento_id] => 2
[obreiro_id] => 5
)
)
[1] => Array (
[EventoObreiro] => Array (
[id] => 2
[evento_id] => 2
[obreiro_id] => 3
)
)
)
HTML Form:
<?php echo $this->Form->create('EventoObreiro', array('url' => '/eventos/presenca/' . $evento['Evento']['id'])); ?>
<?php if(count($obreiros) > 0) { ?>
<?php foreach($obreiros as $k => $obreiro) { ?>
<?php echo($obreiro['Usuario']['nome']); ?>
<?php echo($this->Form->input('EventoObreiro.' . $k . '.obreiro_id', array('type' => 'hidden', 'value' => $obreiro['Obreiro']['id']))); ?>
<?php echo($this->Form->input('EventoObreiro.' . $k . '.evento_id', array('type' => 'radio', 'legend' => false, 'options' => array('1' => 'Sim', '0' => 'Não')))); ?>
<?php } ?>
<?php } ?>
<?php echo $form->button('Salvar presença', array('type' => 'submit', 'class' => 'button', 'name' => 'botaoAdicionar')); ?>
<?php $this->Form->end(); ?>
$obreiros are set from $this->set('obreiros', $this->Obreiro->find('all'));
But my form are not auto populated. How I can do this?
There can be a number of things wrong with this, it is not clear from your code but look into the following:
Make sure that the information from $this->EventoObreiro->find('all'); is actually sent to the view. You can do this using <?php echo debug($obreiros); ?>. If it is not sent you should verify your controller code [maybe even edit your post and add it for clarity.]
I don't see any Usuario in your Array so maybe try using $obreiro['EventoObreiro']['key'] instead of $obreiro['Usuario']['key']
Related
i have an array i and i want to show the array values if the name of same array repeat in the another array and have true value
my arrays like this
$array1 = [
array(
'name' => internal_evidence
'price' => 30
'course_id' => 3
),
array(
'name' => international_evidence
'price' => 450
'course_id' => 3
),
array(
'name' => internal_evidence
'price' => 10
'course_id' => 1
),
array(
'name' => technical_evidence
'price' => 134
'course_id' => 3
),
];
$array2 = [
array(
'id' => 3
'name' => graphic
'price' => 150
'attr' => array(
'internal_evidence' => 'true',
'international_evidence' => 'false',
'technical_evidence' => 'true'
)
),
array(
'id' => 5
'name' => 3dmax
'price' => 300
'attr' => array(
)
),
array(
'id' => 1
'name' => ICDL
'price' => 480
'attr' => array(
'internal_evidence' => 'true',
)
),
];
i want to showing this all attr selected with true value in like this
also I want to sum price of array2 member and array1
<h2>graphic</h2>
<p>internal_evidence</p>
<p>technical_evidence</p>
<small>course price: 150</small>
<small>314</small> <!-- Price with selected evidence -->
<h2>3dmax</h2>
<small>course price: 300</small>
<!-- its not have attr evidence -->
<h2>ICDL</h2>
<p>internal_evidence</p>
<small>course price: 480</small>
<small>490</small> <!-- Price with selected evidence -->
i try this but its don`t work properly
$priceOfAttr = 0;
foreach($array2 as $key => $cat):
echo "<h2>{$cat['name']}</h2>";
foreach($array1 as $pr):
if($pr['course_id'] == $cat['id']):
foreach($cat['attr'] as $m => $optionV):
if($m == $pr['name'] && $optionV == "true"){
echo $m .'<br>';
$priceOfAttr += $pr['price'];
// echo "<small>{$cat['price']}</small><br>";
// echo $cat['price'] + $pr['price']. "<br>";
}
endforeach;
echo $priceOfAttr + $cat['price'] . '<br>';
endif;
endforeach;
echo '<br>';
endforeach;
I'd use a combination array_reduce and array_map to transform your data into what you need, then simply loop over that to display your view:
<?php
// Index your $array1 by [id][name]
$array1ByIdAndName = array_reduce($array1, static function ($byIdAndName, $entry) {
$byIdAndName[$entry['course_id']][$entry['name']] = $entry;
return $byIdAndName;
});
// Transform $array2's `attr` entries into attribute list + compute total price
$array2 = array_map(static function ($entry) use ($array1ByIdAndName) {
$entry['total_price'] = $entry['price'];
$entry['attr'] = array_reduce(array_keys($entry['attr']), static function ($attrs, $attrName) use ($array1ByIdAndName, &$entry) {
if ($entry['attr'][$attrName] === 'true') {
$attrs[] = $attrName;
$entry['total_price'] += $array1ByIdAndName[$entry['id']][$attrName]['price'];
}
return $attrs;
}, []);
return $entry;
}, $array2);
// Display your view
?>
<?php foreach ($array2 as $entry): ?>
<h2><?= $entry['name'] ?></h2>
<?php foreach ($entry['attr'] as $attrName): ?>
<p><?= $attrName ?></p>
<?php endforeach ?>
<small>course price : <?= $entry['price'] ?></small>
<?php if ($entry['total_price'] > 0): ?>
<small><?= $entry['total_price'] ?></small>
<?php endif ?>
<?php endforeach ?>
Demo: https://3v4l.org/nS3Gl
I'm a beginner in Laravel, so my problem is that I want to display the data that an array contains. It's been a week that I try to solve it personally, but I do not get what I want.
This is a part of my controller:
if ( !empty ( $nbrrowsol ) ) {
for ($i=1; $i <=$nbrrowsol ; $i++) {
# Code...
$clients = DB::table('clients')->where('nom',$request->get('nomclient'))->first();
$commercial=DB::table('commercials')->where('name',$request->get('commercial'))->first();
$commande[] = array(
'nom_client' => $request->get('nomclient')[$i],
'organisme' => $request->get('organisme')[$i],
'adresse' => $request->get('adresse')[$i],
'email' => $request->get('email')[$i],
'tel1' => $request->get('tel')[$i],
'tel2' => $request->get('tel2')[$i],
'fax' => $request->get('fax')[$i],
'commercial' => $request->get('commercial')[$i],
'date_reception' => $request->get('datereception')[$i],
'date_prelevement' => $request->get('dateprelev')[$i],
'saved_by' => $request->get('savedby')[$i],
'code' => $request->get('codesol'.$i)[$i],
'nature' => $request->get('naturesol'.$i)[$i]
);
}
dd($commande);
}
This is what I got in the console, but I didn't get the value:
You can use the below syntax for printing the data:
echo '<pre>';
print_r($commande);
die;
Or:
echo '<pre>';
print_r($commande->toArray());
die;
I have created a form for a POST request, but I need to send an array of information that must be not visible in that form.
The array consist in a not specified number of other arrays.
My array is called "$dati" and is made by a not bounded number of arrays with three information each.
The code of my form is:
<?= $this->Form->create(null,['type'=> 'post', 'url'=>['action'=>'selectForSell2',$rassegnaselezionata->id,$showselezionato->id,$proiezioneselezionata->id ]]) ?>
<?= $this->Form->input('stato', ['options' => ['tutti' => 'Tutti i Soci', 'firmato' => 'Soci Firmati', 'approvato' => 'Soci Approvati'] ] ); ?>
<?= $this->Form->input('campo', ['options' => ['cognome' => 'Cognome', 'nome' => 'Nome', 'codicefiscale' => 'Codice Fiscale'] ] ); ?>
<?= $this->Form->input('ricerca', ['label' => false, "class" => " form-control input-medium", "placeholder" => __('Ricerca'), 'visible'=>false]); ?>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
In my projects, I have the following element as hidden.ctp:
if (isset($model)) {
$model .= '.';
} else {
$model = '';
}
foreach ($fields as $field => $values) {
if (is_array($values)) {
echo $this->element('hidden', ['model' => $model . $field, 'fields' => $values]);
} else {
echo $this->Form->hidden($model . $field, ['value' => $values]);
}
}
Then you can just call it with
echo $this->element('hidden', ['fields' => $dati]);
If you want to hidden the value of the array then you can do it in this way.
<?php
$a = array("Name" => "Peter", "Age" => "41", "Country" => "USA");
?>
<input type="hidden" name="data[Menu][arr]" value="<?php pr($a); ?>">
It can produce output like,when you post the data.Check it.it is required or not
Array
(
[Menu] => Array
(
[arr] => Array( [Name] => sradha [Age] => 20 [Country] => IN)
)
)
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.