Dropdown is empty when using foreach from DB - database

My program brings a list of a teacher from a table.
Then I want to show the names in the drop-down but for some reason, it opens an empty dropdown with the number of empty lines like the records in the table.
This is my code for this problem
<div>
<label for="teacher_name"> שם המורה: </label>
<select name="teacher_name" id="teacher_name">
<?php if (empty($teachers)): ?>
<p> No teachers found</p>
<?php else: ?>
<?php if($status == 'new'): ?>
<option value="please_select">נא לבחור</option>
<?php foreach ($teachers as $teacher): ?>
<option value=<?=$teacher['teacher_name']; ?> name="teacher_name" id="teacher_name"></option>
<?php endforeach; ?>
<?php endif; ?>
<?php if($status == 'edit'): ?>
<p> edit</p>
<input type="text" name="teacher_name" id="teacher_name" value="<?= htmlspecialchars($lesson->teacher_name); ?>" >
<?php endif; ?>
<?php endif; ?>
</select>
</div>

If you "view source" of your output I think you will see that the option tags do have data in them - but you haven't set the value to display, it should be more like <option ....>Show this in the list</option>.
Try this (the same code as you were using, but adding the teacher name variable again where it will display)
<option value=<?=$teacher['teacher_name']; ?> name="teacher_name" id="teacher_name"><?=$teacher['teacher_name']; ?></option>

Related

How to get the data of a "multiselect option" coming from a cell

I created a cell to assemble a "multiple selection option" and inserted it into a view for the user.
Fill in the fields like this:
<select class="form-control" multiple id="product">
<?php foreach ($json["data"] as $product):?>
<option value="<?php echo $product["id"]; ?>"><?php echo $product["name"]; ?></option>
<?php endforeach; ?>
</select>
I added this cell to a user registration form along with other fields!
<?= $this->cell('Products');?>
How can I get the information selected by the user and send it to the controller after submitting the form?
Can I solve this using the cell?

How to call language from database in codeigniter and call with foreach from language library message?

How can I call language id from database with print message from language library
in codeigniter
This is the image:
And I was to implement in this code and call in login form for call which language is select from database
<div class="form-group valid-form">
<h4><?php echo $this->lang->line('select'); ?></h4>
<select class="form-control" name="language" onchange="javascript:window.location.href='<?php echo base_url(); ?>Language/index/'+this.value;">
<option name="english" value="english" <?php if($this->session->userdata('site_lang') == 'english') echo 'selected="selected"'; ?>>English</option>
<option name="french" value="french" <?php if($this->session->userdata('site_lang') == 'french') echo 'selected="selected"'; ?>>French</option>
</select>
<span class="text-danger"><?php echo form_error("select");?></span>
</div>
//controller
function function_name(){
$data=array();
$this->load->model('model_name');
$data['languageArray']=$this->model_name->get_language();
//load view file
$this->load->view('folder/file_name',$data);
}
//model
function get_language(){
return $this->db->get('table_name')->result_array();
}
//view
<select class="form-control" name="language_id" id ="language_id">
<option value="">Select Language</option>
<?php foreach ($languageArray as $language) { ?>
<option value="<?php echo $language['id'] ?>">
<?php echo $language['lang'] ?></option>
<?php } ?>
</select>

Issue with Check box update or Edit to database in Codeigniter

My function is when I will check then 1 will insert and if not then by default the field will fill with 0.At the time of insert problem is not happen, it store '1' to db but at the time of update with check box it not able to send '1' again to db, it sending 0.
AT THE INSERT TIME
View
.
<?php echo form_checkbox("feature_opd","1", set_checkbox("feature_opd","1")); ?>
<?php echo form_checkbox("feature_gyane","1", set_checkbox("feature_gyane","1")); ?>
<?php echo form_checkbox("feature_iui","1", set_checkbox("feature_iui","1")); ?>
Model
public function add_article($array)
{
return $this->db->insert('table_name',$array);
}
AT THE UPDATE TIME
View
<div>
<?php
if($article->feature_opd == 1)
{?>
<input type="checkbox" name="feature_opd" checked >
<?php
}
else
{?>
<input type="checkbox" name="feature_opd" >
<?php
}
?>
</div>
<div>
<?php
if($article->feature_gyane == 1)
{?>
<input type="checkbox" name="feature_gyane" checked>
<?php
}
else
{?>
<input type="checkbox" name="feature_gyane" >
<?php
}
?>
</div>
<div>
<?php
if($article->feature_iui == 1)
{?>
<input type="checkbox" name="feature_iui" checked>
<?php
}
else
{?>
<input type="checkbox" name="feature_iui" >
<?php
}
?>
</div>
Model
public function update_article($article_id , array $article)
{
return $this->db
->where('id',$article_id)
->update('ivf_productsettings', $article );
}
My Query is how I will update db with check box. I am facing problem to update with check box in Codeigniter, Please suggest me , if my code going to tough then please suggest me in simple way how to update with check box in codeigniter .I am new in ci .
You can create your update checkboxes like so:
<?php echo form_checkbox("feature_iui","1", set_checkbox("feature_iui","1", $article->feature_iui)); ?>
The default value will apply from the db.
Aside from that there is nothing aside from user input preventing the checkboxes from being properly sent. Thus, any code that is causing a 0 being put in the db is probably due to how you are processing the checboxes in your controller update function. As I've said in the comments, checkboxes only submit 1's never 0's.

Form submit button losing value attribute in CakePHP

We have baked a crud with validations. All seems ok.
However, when we hit the submit button,
<?php echo $this->Form->submit('go for it'); ?>
and if we get validation errors, the HTML submit button loses is value attribute.
Before hitting submit we get this output:
<input type="submit" value="go for it"/>
After hitting submit we get this output:
<input type="submit" value></input>
Here's the full ctp code:
<?php echo $this->Form->create('ScheduleAccess'); ?>
<?php echo $this->Form->input('id'); ?>
<fieldset class="short">
<legend>Bla bla</legend>
<?php echo $this->Form->input('schedule_es', array('label' => 'Bla bla (es)')); ?>
<?php echo $this->Form->input('schedule_en', array('label' => 'Ble ble (en)')); ?>
</fieldset>
<fieldset class="short">
<legend>Last Bla bla</legend>
<?php echo $this->Form->input('last_entry', array(
'dateFormat' => 'YMD',
'timeFormat' => '24',
'label'=>'until:'
)); ?>
</fieldset>
<fieldset class="full">
<legend>terms</legend>
<div class="medium wysihtml5">
<?php echo $this->Form->input('free_entry_day_time_es', array('label' => 'Things (es)')); ?>
<?php echo $this->Form->input('free_entries_and_discounts_es', array('label' => 'More things (es)')); ?>
</div>
<div class="medium wysihtml5">
<?php echo $this->Form->input('free_entry_day_time_en', array('label' => 'Things (en)')); ?>
<?php echo $this->Form->input('free_entries_and_discounts_en', array('label' => 'More things (en)')); ?>
</div>
</fieldset>
<?php //echo $this->Element('admin/save'); ?>
<?php echo $this->Form->submit('go for it'); ?>
<?php echo $this->Form->end(); ?>
Has anyone found a similar issue?
just for grin's try this
<?php echo $this->Form->end('Go for it'); ?>
end can provide the submit button. If this works then you probably have something overwriting the value of the submit. I'd start looking in your $this->request->data to see what its sending over to the view.
Hope this helps.

Populate a checkbox when using a viewScript decorator in Zend Form

I have a zend_form with a checkbox:
$horz = new Zend_Form_Element_Checkbox('horizontal');
$horz->setLabel('Display horizontally?');
$horz->setDecorators(array(array('ViewScript', array('viewScript' => 'partials/forms/checkbox.phtml'))));
My custom viewScript checkbox.phtml looks like this:
<?php
$name = $this->element->getName();
$attrs = $this->element->getAttribs();
$options = $attrs['options'];
?>
<dt id="<?= $name ?>-element">
<input type="hidden" value="<?= $options['uncheckedValue'] ?>" name="<?= $name ?>" />
<label>
<input type="checkbox" class="checkbox" value="<?= $this->element->getValue() ?>" id="<?= $this->element->getId() ?>" name="<?= $name ?>">
<?= $this->element->getLabel(); ?>
</label>
</dt>
It renders beautifully, but when I populate the form from a database record:
$record = array('horizontal'=>1);
$form->populate($record);
The checkbox is not checked. Is there something I need to set in my viewScript to allow it to populate?
You have to check the box in your html view script after the value is populated.
<input type="checkbox" class="checkbox" value="<?= $this->element->getValue() ?>" id="<?= $this->element->getId() ?>" name="<?= $name ?>" <?php echo $this->element->isChecked() ? " checked=\"checked\"" : "" ?> />

Resources