How to call a function for a select box? - php

Hi I have this function on PHP:
<?php
class ConectorDatos {
static function buscarProductos() {
return array(
'Hom' => array( '1VX' => 649.95 ),
'Sam' => array( 'Note2' => 699.95,
'Gala' => 499.95,
'Gel' => 249.95),
'olivi' => array( 'Lumia' => 999.95),
'Obvow' => array( 'One Plus One' => 299.50 )
);
}
And the HTML Page with this code section:
<div id="productos">
<ul class="telefonoEspecifico">
<li>Marca:</li>
<select name="marc" id="marc4">
<?php
****************
?>
</select>
<li>Modelo:</li>
<li>Precio:</li>
So my question is simple but I don't know how to do it...how HTML on the "select" section could "call" the phpfunction and show the necessary information on webpage. What do I have to do?

You need to include 1st php file in 2nd one like this
<div id="productos">
<ul class="telefonoEspecifico">
<li>Marca:</li>
<select name="marc" id="marc4">
<?php
require("file_name");
$obj = new ConectorDatos();
$data=$obj-> buscarProductos();
foreach($data as $key=>$value)
echo "<option>$key</option>";
?>
</select>
<li>Modelo:</li>
<li>Precio:</li>

<?php
class ConectorDatos {
public static function buscarProductos() {
return array(
'Hom' => array( '1VX' => 649.95),
'Sam' => array( 'Note2' => 699.95,
'Gala' => 499.95,
'Gel' => 249.95),
'olivi' => array( 'Lumia' => 999.95),
'Obvow' => array( 'One Plus One' => 299.50)
);
}
}
?>
Click here for a DEMO
<?php $producto = ConectorDatos::buscarProductos();?>
<div id="productos">
<ul class="telefonoEspecifico">
<li>Marca:<?php echo $producto['Hom']['1VX'];?></li>
<select name="marc" id="marc4">
<?php
echo "<option value='".$producto['Sam']['Note2']."'>".$producto['Sam']['Note2'] . "</option>";
echo "<option value='".$producto['Sam']['Gala']."'>".$producto['Sam']['Gala'] . "</option>";
echo "<option value='".$producto['Sam']['Gel']."'>".$producto['Sam']['Gel'] . "</option>";
?>
</select>
<li>Modelo:<?php echo $producto['olivi']['Lumia'];?></li>
<li>Precio:<?php echo $producto['Obvow']['One Plus One'];?></li>

Related

Display the member of one array depending on the other array

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

How can I pass an array of hidden informations in a POST request in Cake 3.2?

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

cakephp2 can't display data from contain

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

Can you compare an array of strings in PHP and if there are two that are the same it will only return it once?

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.

CakePHP not autopopulating

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']

Categories