CakePHP MySQL Values - php

echo $this->Form->input('quantity', array('options' => array('1','2','3','4')));
I have this code to input the values 1,2,3 or 4 into the quantity field in my database.
If a user selects 1 it inputs 0, 2 inputs 1 etc.
If the code is
echo $this->Form->input('quantity');
The user can input what they like and guess what...
It works. What am I doing wrong?

I think you're trying to implement a drop-down box. I think you can try this:
$options = array('1' => '1', '2' => '2', '3' => '3', '4' => '4');
echo $this->Form->input('quantity', array('options' => $options, 'default' => '1'));
this will create a select drop down box.
For detail please see here.

Related

PHP - how to randomly get something from array

how can i get something randomly from a array for example i have this array I want to grab one of those lines randomly how can I do that using PHP?
$quizes = array(
'3-1' => '2',
'4+4' => '8',
'7-5' => '2',
'4+2' => '6',
'9-3' => '6',
'1+2' => '3',
'9+9' => '18',
'3+2' => '5',
'2*3' => '6',
'5*3' => '15',
'6+6' => '12',
'3+4' => '7',
'7-4' => '3',
'6+2' => '8',
'3*2' => '6',
'7+6' => '13',
'1+1' => '2',
'4*4' => '16',
'10-3' => '7'
);
What i have tried
$rand_keys = array_rand($quizes, 2);
echo $quizes[$rand_keys[0]] . "\n";
echo $quizes[$rand_keys[1]] . "\n";
but this only returns results such as 2 7, 15 2, 3 2 and more
Please help thank you
You can randomize the array order and take the first element. The code would look like this:
shuffle($quizes);
Well, you are getting exactly what you are asking for - value that belongs to randomly chosen key.
To get key => value pair execute:
echo $rand_keys[0] . " => " . $quizes[$rand_keys[0]] . "\n";
Of course you can format output however you'd like to.
Each of the "rows" in your code is actually two parts: a key and its corresponding value. For example, in the first row '3-1' is the key and '2' is its value. The => operator indicates this relationship.
The array_rand function you used will return a random key (or set of keys, if you specify the second parameter) from your array.
$key = array_rand($quizes); // returns a random key e.g. '3-1'
Then you can use that key to get the corresponding value,
$value = $quizes[$key]; // returns the value that matches $key
There are various ways to output them from that point. If you want it to look kind of like it does in the code, you can use
echo "$key => $value";

Codeigniter - dropdown change to multiple select because use array as the name

I have problem with my dropdown. I need dropdown that can select only one value and I use the same dropdown to repeat the selection for others row. Here is my page look like
Click here to view My page
My code for the dropdown is
<?php
for($i=0;$i<=$rNumber;$i++){
$occNo = array(
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
'6' => '6',
'7' => '7',
'8' => '8',
'9' => '9',
'10' => '10'
);
echo form_dropdown('roomPax[]',$occNo,set_value('roomPax', isset($databaseData[$i]->roomPax) ? $databaseData[$i]->roomPax : ''),'class="form-control"');
}
?>
I found that, the dropdown change to multiple select because I use roomPax[] (array name). I have to use array name because the same dropdown I use for other row. How to set this dropdown as single select and maintain roomPax[] as their name?
You do not need to define name as like that: roomPax[]
Just replace it with roomPax in first param of form_dropdown()
From Codeigniter User Guide:
Lets you create a standard multiselect field. The first parameter will
contain the name of the field, the second parameter will contain an
associative array of options, and the third parameter will contain the
value or values you wish to be selected. The parameter usage is
identical to using form_dropdown() above, except of course that the
name of the field will need to use POST array syntax, e.g. foo[].
As per user guide, when you need multiselect options than you can use your select name as an array.

CakePHP custom values on select input

I am trying to create a select input field. However I want to set the values of each individual option manually.
in an attempt I tried the following:
echo $this->Form->input('field', array(
'options' => array('Active', 'Blocked', 'Pending', 'Unknown'),
'values' => array(1,2,0,99),
'empty' => '(choose one)'
));
However this did not help (i.e 'Active' was 0, 'Blocked' was 1 etc...)
Does anyone know if it is possible to manually set the values?
values is not the right key, you need to leverage the options array for it, as well:
'options' => array(1 => 'Active', 2 => 'Blocked', 0 => 'Pending', 99 => 'Unknown'),
but that is basic PHP (since non-defined keys are numerically indexed starting off at 0).
You’ll need to use an associative array to set the keys as well:
$options = array(
'1' => 'Active',
'2' => 'Blocked',
'0' => 'Pending',
'99' => 'Unknown'
);
echo $this->Form->input('field', array('options' => $options));
However, I’d advise storing options like this in a separate database table rather than hard-coding them, to keep your views DRY and allowing them to be easily modified in the future.

Cakephp adding option field with array values

I am trying to create a drop down menu (option) and to fill this drop down i have sent an array list to the view:
$country = $this->country_list;
$this->set(compact('country'));
Now my question is does cake have a buildin method for me to set an input field using ($this->Form->input()) with the data of the array list?
take this example
$sizes = array(
's' => 'Small',
'm' => 'Medium',
'l' => 'Large'
);
echo $this->Form->input('size', array('options' => $sizes, 'default' => 'm'));
In the controller, set the value
$this->set('countries', $this->Country->find('list', array('fields' => 'Country.name')));
To show the dropdown box in the view
$this->Form->input('country_id');

How to pass an associative array from smarty templates to jquery

I have assigned two arrays in selection.php to smarty like this
$country = array(
'1' => 'Japan',
'2' => 'Australia',
'3' => 'India'
);
$city = array(
'1' => array(
'10' => 'Tokyo',
'11' => 'Osaka'
),
'2' => array(
'20' => 'Sydney',
'21' => 'Melbourne'
),
'3' => array(
'30' => 'Mumbai',
'31' => 'Delhi'
)
);
$smarty->assign('country_select',$country);
$smarty->assign('city_select',$city);
$smarty->display('selection.tpl');
The code in selection.tpl looks like this.
<div>{html_options id='country_select' options=$country_select}</div>
<div>{html_options id='city_select' options=$city_select}</div>
Now what I want to do is, write a jQuery function that when I select a country in country_select drop-down, the items in the city_select drop-down will be changed in accordance with the country selection. Means, if I select 'Australia' in the country_select drop-down, in the city-select drop-down except 'Sydney' and 'Melbourne' other options will be removed.
Can you please help me how the jQuery code will be. I have been failed to pass the $city_select array to jQuery.
$country = array('1' => 'Japan', '2' => 'Australia', '3' => 'India');
$city = array(
'1' => array('10' => 'Tokyo', '11' => 'Osaka'),
'2' => array('20' => 'Sydney', '21' => 'Melbourne'),
'3' => array('30' => 'Mumbai', '31' => 'Delhi')
);
$data = array('countries' => $country, 'cities' => $city);
echo json_encode($data);
Use json_encode to echo the output to JSON, the result should look like this:
{"countries":{"1":"Japan","2":"Australia","3":"India"},"cities":{"1":{"10":"Tokyo","11":"Osaka"},"2":{"20":"Sydney","21":"Melbourne"},"3":{"30":"Mumbai","31":"Delhi"}}}
Then you should use jQuery ajax to fetch your PHP and using JSON to communicate
$.get("selection.php", function(result) {
//code here
}, "json");
I wrote an example code on Fiddle, the javascript should put into the middle of above jQuery AJAX.
http://jsfiddle.net/sing0920/X3dvG/
Hope this help.
oops, I found out I totally ignore "smarty" here, sorry for that. But still hope this can help you.
You might use ajax request with JSON data serialization as above. On the other hand you may ommit ajax request, and use $.data functiontion, adding the data-<smth> attribute to some specific element or body. the value of this datat element may be something like:
base64_encode(json_encode($data))
and then use $('<elem>').data('<smth>') in your jquery script and decode it with something like http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html and JSON.parse.

Categories