I need to create a drop-down menu in Codigniter and the values will pulled from a database table. The data contains a group of movies, I need to be able to populate the data into the menu.
How should I do this?
just iterate through your data and create a select list
<select name="movie_id">
<?php foreach ($rows as $row): ?>
<option value="<?= $row['id']; ?>"><?= $row['name']; ?></option>
<?php endforeach; ?>
</select>
This will obviously be different based on your table schema
According to Codeigniter documentation
see this url
Code Igniter - form_dropdown selecting correct value from the database
Codeigniter form_helper getting database rows to be values in select menu
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 you wish to be selected. You can also pass an array of multiple items through the third parameter, and CodeIgniter will create a multiple select for you.
Your admin controller should have something like
$data['selected'] = $this->salary_expectation->get_salary_selected();
According to this, the admin view should be like this
<?php echo form_dropdown('salaries', $salaries, $selected_value); ?>
Related
hello i have two tables joined , and i'm trying to fetch some values into a select box, making it so that when a user selects one of the options , when submitting the form it will post the other values associated
<select name="matricula[]">
<?php
// use a while loop to fetch data
// from the $all_categories variable
// and individually display as an option
while ($matricula = mysqli_fetch_array(
$result,MYSQLI_ASSOC)):;
?>
<option value="<?php echo $matricula["matricula"];
// The value we usually set is the primary key
?>">
<?php echo $matricula["matricula"];
// To show the category name to the user
?>
</option>
<?php
endwhile;
// While loop must be terminated
?>
</select>
in this code i need to add the value automatically and insert on form submission : "tipocomb" it has already got a table named "combustivel" created with the values and when i use json encode it displays the two arrays connected in the view source
it's just a question of how to echo the "tipocomb"?
If you want to do something like that you need js to post form that has hidden inputs with related data. Simpler solution would be when you read the post in your controller fetch related data and do further logic there.
I want to create a form with two fields. The first field lists all pages and the second field lists all child pages (if they exist) of the chosen page from the first field. When the form is submitted the site redirects the user to the chosen page. How can I implement this form?
You're going to need to do an AJAX request to get the second drop down list.
There is a function built into WordPress which gets the pages and you can specify parent ID's and also to show only top level pages.
Heres an example of the code but you'll need to then implement it in a way that best suits your requirements
<?php
$top_level_pages = get_pages(array('parent'=> 0));
?>
<select>
<?php
foreach($top_level_pages as $top_level_page) {
echo '<option>'.$top_level_page['post_title'].'</option>';
}
?>
</select>
You will then need to determine what option the user has clicked and run an AJAX request to fill the second select.
The second select should look something like this:
<?php
$id = $_GET['page_id']; // get the id of the page from the first select
$child_pages = get_pages(array('child_of'=>$id));
?>
<select>
<?php
foreach($child_pages as $child_page) {
echo '<option>'.$child_page['post_title'].'</option>';
}
?>
</select>
Read more about the get_pages() function here: http://codex.wordpress.org/Function_Reference/get_pages
I've written this code:
<select id="board_id" name="board_id" class="select_class">
<?php $userId = $this->session->userdata('login_user_id');?>
<?php $userBoards = getUserBoard($userId);?>
<?php foreach ($userBoards as $boardKey => $boardValues):?>
<option value="<?php echo $boardValues->id;?>"><?php echo $boardValues->board_name;?> </option>
<?php endforeach;?>
</select>
It logs the user and displays a dropdown list of that users boards.
I have sub-boards too. It displays them all.
I'm trying to modify this code to display just sub-boards, or at least mark sub-boards differently so the user knows which is which. Currently it lists them all by time of creation, regardless of board/sub-board status.
The column in SQL which tells me if the board is sub or not is sub. If the board is sub the id of the parent board is the value in this column $boardValues->id.
I originally wanted chained select to do this but it was too difficult to grasp and wouldn't work on my website.
you can use optgroup html tag to differentiate between boards and sub-boards.
see example
In other way you can use text-indent style for sub-boards option to display differently.
i created a zend form which is able to generate subform with the elements like: items[0][price], items[0][count]; items[1][price] etc.
in each row of a table i need to display certain data. so, in my view i created a html table and tried to iterate through the 'items' and insert certain form element in certain column of a table.
<table>
<?php foreach($this->form->getSubform('items') as $item) :?>
<tr>
<td>just some data like product name</td>
<td><?php echo $item->getElement('price');?></td>
<td><?php echo $item->getElement('count');?></td>
</tr>
<?php endforeach;?>
</table>
after that, each form element item do NOT have array name anymore, as i was expecting, but just like 'items' or 'count' in each row, so multidimensional array will not be returned.
if i generate the form in view (echo $this->form;), then form element names are ok, but how should i add some data in the table then..? preferably it should be done in view.
Instead of adding elements always with price and count, since you're generating the field dinamically, try to name them price1, count1, price2, count2, and so on. Then you can retrieve them in your table using their new name (with the count appended).
Prepend your form decorator stack with the PrepareElements decorator. This decorator will loop through your form and set the appropriate belongsTo names.
I have two <select> one is category and the second is subcategory.
here is the first <select> for category.
<select name="category" size="10">
<?php foreach($categories->fetch(array('table' => 'categories')) as $category) { ?>
<option value="<?php echo $category['id']; ?>"><?php echo $category['name']; ?></option>
<?php } ?>
</select>
now the second <select> i.e subcategory should be hidden initially and when a user click on category <select> based on the value it should populate the value in subcategory.
One way of doing this is via AJAX by passing categoryId as POST Request and getting HTML as response.
however i would like to know if there is any other alternative so that it automatically pass the categoryId value to PHP and unhide the second <select> here is the code of second <select>
<select name="subcategory" size="10">
<?php foreach($categories->fetch(array('table' => 'subCategories', 'categoryId' => $categoryId)) as $subCategory) { ?>
<option value="1"><?php echo $subCategory['name']; ?></option>
<?php } ?>
</select>
the only thing i need here is $categoryId to be populated dynamically. is there any way of doing this?
thank you..
No, there is no way to do what you are suggesting. PHP is only run on the server, so by the time the page is rendered on the client the PHP has already been run.
Your best bet would be what you already suggested, running some AJAX after the first select is changed, sending back the category ID to the server and retrieving what you need to build the second select.
Is there a reason why you don't want to do it this way?
Sukumar has probably suggested the best and most intuitive solution to make it appear as if the data is being loaded dynamically to the user.
The other alternative would be to submit the form when the select box is changed. Once the form has been submitted PHP would pick up the ID from the POST array and then re-populate the sub-category select box. This is often used as a fallback in case the user doesn't have JavaScript enabled.
Structurally, there are three choices to solve this problem:
Use an ajax call to fetch the required data when a user selection is made as jbruno has described.
Submit the whole page to the server, let your PHP see the newly selected option and fill in the newly desired data in a returned page. This will cause the page to refresh so is less ideal than option 1.
Pre-populate the page with all possible data in a javascript data structure so you can use Javascript to just look up the desired category ID in a local data structure, modify the page and never have to talk to the server in order to update the page.
In my opinion, option 3) is the most desirable if the data set required for local lookup is not too large (say under 100k) and it's not too expensive on the server to collect all that data for inclusion in the original page and if the data doesn't change real-time or having data as of the page load time is OK.
If option 3) isn't feasible for any reason, then option 1) is next best. Option 2) is not as good a user experience so it should only be the last resort if you really can't implement options 1) or 3).
You asked more specifically about option 3. I don't really yet understand what the whole data you need looks like. If you really only have four total data types residential_plot, residential_apartment, office_space and showroom, then you can just make those be four keys on an object and store their data that way:
var data = {
"residential_plot": 1,
"residential_apartment": 2,
"office_space": 3,
"showroom": 4
};
The 1, 2, 3 and 4 are just whatever data you want to store for that type. It can be numbers, strings, arrays of data, other objects of data, anything.
To access this, you would do like this:
var id = data.residential_plot;
or
var index = "residential_plot";
var id = data[index];
If you wanted to store the notion of categories and sub-categories, you would need an extra level of objects:
var data = {
"residential": {"residential_plot": 1, "residential_apartment": 2},
"commercial": {"office_space": 3, "showroom": 4}
};
Then, you would access it like this:
var id = data.residential.residential_plot;
or like this:
var category = "residential";
var catType = "residential_plot";
var id = data[category][catType];