Drop down does not show the option selected in codeigniter - html

I used a drop down button to select an option from the given options. But when I selected an option it does not display that in the box. And I it gives chance only once to select an option. For the drop down I retrieve data from the database.
This is the view code (Its inside Assign_Inquiries)
<select id = "counsellorname" name="counsellornamename" class="btn btn-default dropdown-toggle">
<option value = "0"> Select Category Name</option>
<?php
foreach($result as $row){
echo "<option value = ".$row['email'].">".$row['fname']." ".$row['lname']."</option>";
}
?>
</select>
Its really great if someone can help me. Thanks inadvance
This is the controller code
<?php
class Assign_Inquiries_Controller extends CI_Controller{
function index(){
$this->load->model('Assign_Inquiries_Model');
$data['result'] = $this->Assign_Inquiries_Model->index();
//print_r($data);
$this->load->view('Assign_Inquiries',$data);
}
}
?>
This is the model code
<?php
class Assign_Inquiries_Model extends CI_Model{
function index(){
$this->db->select('first_name,last_name,email');
$where = "status =3";
$this->db->where($where);
$query = $this->db->get('user');
foreach ($query -> result() as $row) {
$data[] = array(
'fname' => $row->first_name,
'lname' => $row->last_name,
'email' => $row->email
);
}
return $data;
}
}
?>

Change your model code as below: use result_array() which returns the result in array format.
function index(){
$this->db->select('first_name,last_name,email');
$this->db->where('status',3);
$query = $this->db->get('user');
return $query ->result_array();
}
In view you loop must be like this....
foreach($result as $row){
echo "<option value = ".$row['email'].">".$row['first_name']." ".$row['last_name']."</option>";
}

Related

Dependent Dropdown in Yii2 lost values on Update

I created a form with a dropdownlist dependent on another (without extensions) and it ran fast.
But when you try to change the same form, the value of the second drop-down list is lost (type_id). Basically the Type field depends on the selected Area. See my code:
_form.php
<?= $form->field($model, 'area_id')->dropDownList(ArrayHelper::map(Department::find()->where(['helpdesk' => 1])->all(),'id','name'),['prompt'=>'Selecione a Área',
'onchange' => '
$.post("index.php?r=helpdesk/solicitation/lists&id=' . '"+$(this).val(),function(data){
$("select#solicitation-type_id").html(data);
});']) ?>
<?php
echo $form->field($model, 'type_id')->dropDownList(['prompt'=>'Selecione a Área']);
?>
Controller
public function actionLists($id)
{
$countType = Type::find()
->where(['area_id' => $id])
->count();
$types = Type::find()
->where(['area_id' => $id])
->orderBy(['name' => SORT_ASC])
->all();
if($countType > 0 )
{
foreach($types as $type ){
echo "<option value='".$type->id."'>".$type->name."</option>";
}
}
else{
echo "<option> - </option>";
}
}
I think it's the same problem at this topic, but I did not understand how it worked.
you need to provide the default set of options from which it should select the saved option whereas you are not providing any data
<?php
$types = Type::find()
->where(['area_id' => $model->area_id])
->orderBy(['name' => SORT_ASC])
->all();
echo $form->field($model, 'type_id')->dropDownList(ArrayHelper::map($types,'id','name'),['prompt'=>'Selecione a Área']);
?>
Now when you will use findOne() and select the model the saved value for the type_id will be automatically selected.
You can move the above query in the controller/action and pass the $types to the view.
also you can make you method actionLists D.R.Y by using count($types) and remove the first query
public function actionLists($id)
{
$types = Type::find()
->where(['area_id' => $id])
->orderBy(['name' => SORT_ASC])
->all();
if(count($types) > 0 )
{
foreach($types as $type ){
echo "<option value='".$type->id."'>".$type->name."</option>";
}
}
else{
echo "<option> - </option>";
}
}

Checkbox values in form update doesn´t work

I have one little problem with checkboxes in yii. I have modal popup, where i have form for update.
Here is my controller:
public function actionUpdateroomrow(){
$model = Rooms::findOne($_POST['id']);
$model -> room_number_of_people = $_POST['room_number_of_people'];
$model -> room_name = $_POST['room_name'];
if(isset($_POST['room_air_conditioning']))
$model -> room_air_conditioning = $_POST['room_air_conditioning'];
else $model -> room_air_conditioning = false;
$model->update();
if (isset ($_POST['room_multimedia_id'])){
foreach($_POST['room_multimedia_id'] as $key => $value){
$model2 = RoomMultimediaData::findOne($value);
$model2 -> room_multimedia_data_value = $key;
var_dump($model2 -> errors);
}
}
$this->redirect('index');
}
Where is problem? In this row.
<div class="form-group">
<?php foreach($room_multimedia as $rm){ ?>
<label><?= $rm->room_multimedia_title ?></label><input type="checkbox" name="room_multimedia_id[<?= $rm->Checkmultimediadata($room->room_id,$rm->room_multimedia_id)->room_multimedia_data_id ?>]" value="<?= $rm->room_multimedia_id ?>"/>
<?php } ?>
</div>
This is row from update formular and im not really sure, where i failed.
In RoomMultimedia.php, modul i have this:
public function Checkmultimediadata($room_id,$room_multimedia){
return RoomMultimediaData::find()->where('room_id='.$room_id.' and room_multimedia_id='.$room_multimedia.'')->one();
}
Warning from yii:
PHP Warning – yii\base\ErrorException
Creating default object from empty value
$model2 -> room_multimedia_data_value = $key;
Can you help me solve this?
Thank you all! :)

Select Query from two tables in codeigniter

I have 2 tables:
Events and Registration.
Events Fields: EventID and EventName
Registration Field: MemberID and Name
I want to pull the EventName and the Name- of the person. How do I do this with a model?
The code I have:
Model
function getAll()
{
$query = $this->db->get('tblRegistration');
if( $query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
}
$query->free_result();
return $data;
}
controller
public function getallevents() {
$this->load->model('events_model');
$data['records'] = $this->events_model->getAll();
$this->load->view('view-events', $data);
}
View
foreach ($records as $row):
echo "<tr>";
echo "<td> $row->Name</td>";
// echo "<td> $row->intLocationID</td>";
echo "</tr>";
endforeach;
Edit
OK I have added the data that I want out of the table.
model
function getAll()
{
// get all the records from the schools table
$this->db->select('*');
$this->db->from('tblRegistration as reg');
$this->db->join('tblMeetRace as met', 'reg.intMeetRaceID = met.intEventID');
$query = $this->db->get();
// if the number of rows returned is more than 0
if( $query->num_rows() > 0) {
// loop through each record (row) and place that
// record into an array called $data
foreach ($query->result() as $row) {
$data[] = $row;
}
}
$query->free_result();
// return the array to the controller
return $data;
}
view
foreach ($records as $row):
echo "<tr>";
echo "<td> $row->intMeetRaceID/td>";
echo "<td> $row->intEventID</td>";
echo "</tr>";
endforeach;
but I get an error of:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/view-events.php
Line Number: 89
First of all you need to add another column in your events table for storing the MemberId, say memberid.
Once you have that, when ever a member registers for an event, you can store his ID in the events table and relate it.
Then you can use Joins to fetch the datas
$this->db->select('*');
$this->db->from('registration as reg');
$this->db->join('events as evt', 'reg.id = evt.memberid');
$query = $this->db->get();

where am i going wrong in my extraction of value from mysql database?

where am i going wrong in my extraction of country name from mysql database? (codeigniter: activerecord enabled)
Its printing out "default country" instead of expected "Australia".
Pages Controller
public function view($page)
{
$page = strtoupper($page); // Capitalize the first letter
$data = array(
'title' => 'My Title',
'country' => 'default country'
);
$this->getCountryName($page, $data);
if(!is_null($data['country'])){
$page = "country";
}
$this->load->helper('url');
$this->load->helper('utility');
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
if(!is_null($data['country'])){
$this->load->view('templates/infobox', $data);
}
$this->load->view('templates/footer', $data);
}
function getCountryName(&$page, &$data){
$this->db->select('name')->from('pv_country')->where('code', $page);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data['country'] = $row;
echo $row;
}
}
}
Country View
<div style =" position:absolute;top:90%; background: red;">
<?php echo $country; ?></div> // prints default country.
</div>
A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass could not be converted to string
Filename: pages/country.php
Line Number: 25
so i put in some more debugging and found the correct syntax/method to call to get the result,
which then returns an object with attributes that need to be accessed in the view.
function getCountryName(&$page, &$data){
$this->db->select('name')->from('pv_country')->where('code', $page);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$data['country'] = $query->row();
}
}
<?php echo $country->name; ?>

Query to database and display

I use CodeIgniter 2.1.3, PHP and MySQL.
Hello, I want to display data from database. Always I display by foreach($results as $data), but now I want do display all data in few step. Display first record and when user click next then display next row from database. I now that I must use mysql_fetch_row() but I don't know how I can do it...
This is my model:
public function play($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("quiz");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
controler:
public function index()
{
$config = array();
$config["base_url"] = base_url() . "index.php/main_menu/Quiz/index";
$config["total_rows"] = $this->quiz_model->record_count();
$config["per_page"] = 11;
$config["uri_segment"] = 4;
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data["results"] = $this->quiz_model->play($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view('left_column/quiz_open', $data);
}
Pagination is not important.
and view:
<form>
<?php
if (empty($results)) {
}
else {
foreach($results as $data) { ?>
<label style='width:450px;'> <b> &nbsp <?php echo $data->pytanie?> </b> </label>
<label style='width:300px;'> <input type="radio" name="Wiek" value=<?php echo $data->odp1 ?> /> <?php echo $data->odp1 ?> </label>
<label style='width:300px;'> <input type="radio" name="Wiek" value=<?php echo $data->odp2 ?> /> <?php echo $data->odp2 ?> </label>
<label style='width:300px;'> <input type="radio" name="Wiek" value=<?php echo $data->odp3 ?> /> <?php echo $data->odp3 ?> </label>
<?php }
}?>
<label style='width:300px;'> <input type="submit" name="Wyslij" id="Wyslij" value="&nbsp Wyślij &nbsp"/> </label>
</form>
There is an inbuilt Pagination class provided by codeIgniter. You can find it in user guide.
Define a start index variable in the function where u want to use pagination as zero.
public function pagination($start_index = 0)
{
$result = $this->model->get_results($data); //this $data is the argument which you are passing to your model function. If you are using database to get results array.
$items_per_page = 10; //this is constant variable which you need to define
$filtered_result = array_splice($result, $start_index, ITEM_PER_PAGE_USERS);
$model['$filtered_result'] = $filtered_result;
$total_rows = count($result);
$model['page_links'] = create_page_links (base_url()."/controlelr_name/pagination",ITEM_PER_PAGE_USERS, $total_rows);
$this->load->view('controller_name/view_file_name', $model);
}
This is a generalised function for pagination. You can keep this function in one helper file where you keep all generalised functions.
function create_page_links($base_url, $per_page, $total_rows)
{
$CI = & get_instance();
$CI->load->library('pagination');
$config['base_url'] = $base_url;
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$CI->pagination->initialize($config);
return $CI->pagination->create_links();
}
This create page links function is a generic function.............for more explanation check pagination class from user guide......