-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeignitor dropdown.txt
91 lines (75 loc) · 2.66 KB
/
codeignitor dropdown.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Creating a dropdown in CodeIgniter involves using the form_dropdown() helper function, which simplifies the process of generating HTML <select> elements.
Here’s a step-by-step guide to creating a dropdown in CodeIgniter:
1. Controller
Create a method in your controller to load the data for the dropdown and pass it to the view.
php
Copy code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class DropdownController extends CI_Controller {
public function index() {
// Example data for the dropdown
$data['options'] = [
'1' => 'Option 1',
'2' => 'Option 2',
'3' => 'Option 3',
'4' => 'Option 4',
];
// Selected option
$data['selected_option'] = '2'; // Preselect Option 2
// Load the view and pass the data
$this->load->view('dropdown_view', $data);
}
}
2. View
Create a view file (e.g., dropdown_view.php) to display the dropdown.
php
Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dropdown Example</title>
</head>
<body>
<h1>Dropdown Example</h1>
<?php
// Load the form helper
$this->load->helper('form');
// Dropdown attributes
$dropdown_attributes = [
'id' => 'example_dropdown',
'class' => 'form-control'
];
// Create the dropdown
echo form_open();
echo form_label('Choose an option:', 'example_dropdown');
echo form_dropdown('example_dropdown', $options, $selected_option, $dropdown_attributes);
echo form_close();
?>
</body>
</html>
3. Explanation
Data ($options):
The $options array defines the key-value pairs for the dropdown. The key represents the value attribute of each <option>, and the value is the visible text.
Selected Option ($selected_option):
This specifies which option is preselected when the page loads.
Attributes:
You can pass attributes like id, class, etc., to customize the dropdown.
Helper Functions:
form_open(): Starts the form.
form_label(): Adds a label for the dropdown.
form_dropdown(): Generates the dropdown.
form_close(): Closes the form.
4. Output
The output HTML will look like this:
html
Copy code
<form action="" method="post">
<label for="example_dropdown">Choose an option:</label>
<select name="example_dropdown" id="example_dropdown" class="form-control">
<option value="1">Option 1</option>
<option value="2" selected>Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>