foreach($Portfolio_cat as $row_2)
{
#if($row['cid']==$row_2['cid'])
echo '<option value="'.$row_2->cid.'" selected>'.$row_2->cname.'</option>';
}
Try like this
foreach($Portfolio_cat as $row_2){ ?>
<option <?php if($row['cid']==$row_2['cid']){ echo selected="selected";} ?> value="<?= $row_2->cid ?>"><?= $row_2->cname ?></option>;
OR
foreach($Portfolio_cat as $row_2)
{
if($row['cid']==$row_2['cid']){
$selected = 'selected="selected"';
}else{
$selected = '';
}
echo '<option '.$selected.' value="'.$row_2->cid.'" selected>'.$row_2->cname.'</option>';
}
Related
I'm trying to make a select option in CodeIgniter, and want to make it autocomplete fill. How do I do that?
This is my code:
Model:
public function get_nama_customer(){
$data = array();
$query = $this->db->get('tabel_customer');
if($query->num_rows() > 0){
foreach($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
Controller:
public function add_data()
{
$this->load->model('model_tabel_material');
$data['tabel_customer'] = $this->model_tabel_material->get_nama_customer();
$this->load->view('datamaster/tabel_material/v_add_material', $data);
}
public function do_add()
{
$nama_customer = $_POST['nama_customer'];
$data_add = array(
"nama_customer" => $nama_customer,
);
$temp = $this->model_tabel_material->insert_data('tabel_material', $data_add);
if($temp >=1){
redirect('tabel_material/controller_tabel_material/index');
}
}
View:
<select class="form-control" name="nama_customer" id="nama_customer" required>
<?php if(count($tabel_customer)){ ?>
<option value=''>--Pilih Customer--</option>
<?php foreach ($tabel_customer as $list){ ?>
<?php
echo "<option value='".$list['nama_customer']."'>".$list['nama_customer']."</option>";
?>
<?php } ?>
<?php } ?>
</select>
view part where the error occured
<select name="standard" class="form-control">
<option <?php if($course->medium =='E'){ echo "selected";} ?> value="English" >English</option>
<option <?php if($course->medium =='M'){ echo "selected";} ?> value="Malayalam">Malayalam</option>
</select>
model part
public static function getStandard($id){
$this->db->select("*");
$this->db->from('standard');
$this->db->where('id',$id);
$res=$this->db->get();
foreach($res->result() as $value) {
return $value;
}
}
controller part
public function add_standard($action='',$id='') {
$this->load_syles();
$course = array();
if ($action == 'edit' and $id != '' and is_numeric($id)) {
$course = $this->news_model->getStandard($id);
}
$this->data['course'] = $course ;
$this->data['title'] = 'Add Standard';
$this->data['page_header'] = "Add Standard";
$this->data['page_header_desc'] = "Add Standard";
$details=$this->news_model->viewAllStandard();
$this->data['details']=$details;
$this->data['count']=count($details);
$this->data['is_edit']=$action;
You wrote
$course = array()
and then used
$course->medium
as if it was an object
you should use $course['medium'] in the view(html)
Change your model like
public static function getStandard($id)
{
$this->db->select("*");
$this->db->from('standard');
$this->db->where('id',$id);
$res=$this->db->get();
return $res->row_array();
}
Change $course->medium to $course['medium']
<select name="standard" class="form-control">
<option <?php if($course['medium'] =='E'){ echo "selected";} ?> value="English" >English</option>
<option <?php if($course$course['medium'] =='M'){ echo "selected";} ?> value="Malayalam">Malayalam</option>
I want to make a search form in Codeigniter using two fields.
But I'm confused about making the controller and model.
View:
echo form_open("home/pencarian","class='form-inline navbar-search'");
<input id="srchFld" class="srchTxt" name="keyword" type="text" value=" echo $this->session->userdata("keyword");" />
<select class="srchTxt" name="kategori">
<option>All</option>
foreach ($kat as $kategori){
$id = $kategori['id'];
$nama_kategori = $kategori['nama_kategori'];
$created_at = $kategori['created_at'];
<option value=" echo $id; ?>"><?php echo $nama_kategori; </option>
}
</select>
echo form_submit('action', 'Cari', "class='btn btn-primary'");
echo form_close();
How do I make the controller and model?
Model example_model.php
public function search($search_txt, $search_category) {
$this->load->database();
$sql = "select * from search_table where search_field = ? and search_category = ?;";
$query = $this->db->query($sql, array($search_txt, $search_category));
return $query->result();
}
Controller home
public function pencarian() {
$this->load->model('example_model', 'm');
$search_txt = $this->input->post('srchFld');
$search_category = $this->input->post('kategori');
$data['search_result'] = $this->m->search($search_txt, $search_category);
$this->load->view('example_view', $data);
}
View example_view
<?php foreach($search_result as $r): ?>
<div><?php echo $r->field1 . ' ' . $r->field2; ?></div>
<?php endforeach; ?>
I am new with codeigniter.I want to make a select dorpdown that gets its value and title from database.I tried some codes but it did not work.There are my codes:
Model
function get_sec_info(){
$records=$this->db->query('SELECT sec_id,name FROM section');
if($records->num_rows() > 0)
return $records->result();}
Controller
function sec_ifo(){
$data['rec']=$this->mymodel->get_sec_info();
$this->load->view('article',$data);}
View
<select name="section">
<?php foreach($rec as $row) {?>
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>"
<?php } ?>
It does not show any error and any option to show
In the controller you set "red" $data['red'] and in the view you access "rec" foreach($rec
Model:
function get_sec_info(){
$this->db->select('sec_id,name');
$records = $this->db->get('section');
return $records->result();
}
Controller:
function sec_ifo(){
$this->load->model('mymodel');
$this->data['red'] = $this->mymodel->get_sec_info();
$this->load->view('article',$this->data);
}
View:
<select name="section">
<?php foreach($red as $row) { ?>
<option value="<?php echo $row->sec_id; ?>"><?php echo $row->name; ?></option>
<?php } ?>
Model
public function getClasse() {
$query(`enter code here`);
$result = $this->db->query($query)->result_array();
foreach ($result as $key => $rows) {
$resultado[] = $rows['DescricaoClasse'];
}
return $resultado;
}
Controller:
public function getClasse() {
$this->load->model('Decisao_monocratica_model');
return $this->Decisao_monocratica_model->getClasse();
}
View
<select id="ClasseProcesso" class="input-xlarge" name="classeProcesso">
<option value="0">Todos os Tipos</option>
<? foreach ($classeProcesso as $key => $classe) { ?>
<option value="<? echo $classe ?>"><? echo $classe ?></option>
<? } ?>
</select>
I ask this https://stackoverflow.com/a/14277726/1670630 on other post but my problem still exist.
In codeigniter 2.1 I'm trying to display channels by category. So if i have a category called Film, i should see a list of Channels within Film. I tried a nested foreach loop to accomplish this but can't seem to get it to work in the slidshow and limit by number of row.
My model:
<?php
class Pages_model extends CI_Model {
function get_channels_by_categ_tv()
{
$this->db->select('categories.category_name, channels.channel_name');
$this->db->from('type_categ');
$this->db->join('categories', 'categories.category_id = type_categ.category_id');
$this->db->join('channels', 'channels.channel_id = type_categ.channel_id');
$this->db->order_by('categories.category_id');
//$this->db->group_by(array('categories.category_id'));
$query = $this->db->get();
if($query->num_rows() == 0)
{
#no channels
return false;
}
return $query->result_array();
}
}
I have this in the view:
<ul class="slides">
<li>
<?php $cat_shown = ''; ?>
<div class="programe-tv_link">
<?php $cat_show = ''; $cnl_show = '';?>
<?php foreach ($category_chaneels as $category): ?>
<?php
if ($cat_show != $category['category_name']) {
$cat_show = $category['category_name'];
echo '<p>' . $cat_show . '</p>';
}
$cnl_show = $category['channel_name'];
echo '<dd> >>' . $cnl_show . '</dd> ';
?>
<?php endforeach; ?>
</div>
</li>
<li>
<div class="programe-tv_link">
<p>Arte</p>
<dd> >> Acasa</dd>
<dd> >> Antena 1</dd>
<dd> >> Pro TV</dd>
</div>
<div class="programe-tv_link">
<p>Music Box</p>
<dd> >> Acasa</dd>
<dd> >> Antena 1</dd>
<dd> >> Pro TV</dd>
<dd> >> TLC</dd>
</div>
</li>
</ul>
I atache image with ilustration,
sorry for my english and if you don't understund me please write here. THX in advance.
My finale code is this.
<div id="programe-tv-slide" class="flexslider">
<strong>Programe TV</strong>
<div class="redLine"></div>
<?php $cat_cnl = array();
$list = array();
$i=1;
foreach ($category_chaneels as $option) {
$catname = $option['category_name'];
$chlname = $option['channel_name'];
$cat_cnl[$catname][$i] = $chlname;
$list[$i] = $catname;
$i++;
};
?>
<?php
$rows = array_chunk($cat_cnl, 4, TRUE);
foreach ($rows as $row) { //var_dump($rows);
?>
<ul class="slides">
<?php
echo ('<li>');
foreach ($row as $category => $channels) {
echo '<div class="programe-tv_link">';
echo '<p>' . $category . '</p>';
foreach ($channels as $channel) {
echo '<dd>' . $channel . '</dd> ';
};
echo '</div>';
};
echo ('</li>');
?>
</ul>
<?php }; ?>
</div>