I was wrote this function in my model :
public function get_writers()
{
$this->db->select('writer');
$query = $this->db->get('news');
return $query->result_array();
}
And I want to run a foreach loop on result of this array in my view file :
<label for="writer">Writer</label>
<select name="writer" id="writer">
<?php foreach (??? as $each): ?>
<option value="<?php echo $each['id'] ?>"><?php echo $each['writer'] ?></option>
<?php endforeach; ?>
</select>
But I don't know what should I write in the first parameter in foreach loop.Can anybody help me with this ?
Thanks
You should call the function get_writers() in controller like :-
$writers = $this->your_model->get_writers();
Then you need to assign this $result variable data in view section like :-
$this->view->load('view_page',array('writers'=>$writers));
After that you can use that variable data in form in view section.
<label for="writer">Writer</label>
<select name="writer" id="writer">
<?php foreach ($writers as $each): ?>
<option value="<?php echo $each['id'] ?>"><?php echo $each['writer'] ?></option>
<?php endforeach; ?>
</select>
Get the value on controller and pass to view like this:
MODEL
public function get_writers()
{
$this->db->select('writer');
$query = $this->db->get('news');
return $query->result_array();
}
CONTROLLER (inside of your method)
$this->view->load('your_page_in_view_path', array('result' => $this->model->get_writers()));
VIEW
<label for="writer">Writer</label>
<select name="writer" id="writer">
<?php foreach ($result as $each): ?>
<option value="<?php echo $each['id'] ?>"><?php echo $each['writer'] ?></option>
<?php endforeach; ?>
</select>
Related
In need some help on this.
I have a form with a select who works perfectly and add all the values selected in my database. But now I want to do my update form, and I want that when I click to modify a user and arrive on the form, the values are already selected.
For example if in my first form, if I had selected the values 1 and 2, I would like 1 and 2 to be already selected when i go to the modification form. I've already did that :
<?php
$nomLogiciel = getDemandeNomLogiciel($id_demande)[0];
$nom = explode(", ", $nomLogiciel);
foreach ($nom as $item) {
$profilsTmp = getProfilByLogiciel($item); ?>
<div class="form-floating mb-3">
<select class="form-control selectpicker" id="id_profil" name="id_profil[]" multiple data-live-search="true">
<?php foreach ($profilsTmp as $item2) { ?>
<option value="<?php echo $item2['id_profil']; ?>"><?php echo $item2['profil']; ?></option>
<?php } ?>
</select>
<label for="id_profil">Profil <?php echo $item ?></label>
</div>
<?php } ?>
I already managed to do it with a simple select like this :
<select class="form-control selectpicker" id="nomPole" name="nomPole">
<option value="Ambulatoire" <?php if ($one['pole'] == "Ambulatoire") { echo ' selected="selected"'; } ?>>Ambulatoire</option>
<option value="Habitat et Vie Sociale" <?php if ($one['pole'] == "Habitat et Vie Sociale") { echo ' selected="selected"'; } ?>>Habitat et Vie Sociale</option>
</select>
But I would like to know how to do it with multiple values in a foreach ?
If you need my SQL function :
function getDemandeNomLogiciel($id_demande) {
global $bd;
$stmt = $bd->prepare('SELECT nom_logiciel FROM demandes WHERE id_demande = :id_demande');
$stmt->bindParam(ID_DEMANDE, $id_demande);
$stmt->execute();
return $stmt->fetch();
}
function getProfilByLogiciel($nomLogiciel) {
global $bd;
$stmt = $bd->prepare('SELECT * FROM profils_logiciels WHERE nom_logiciel = :nomLogiciel');
$stmt->bindParam(NOM_LOGICIEL, $nomLogiciel);
$stmt->execute();
return $stmt->fetchAll();
}
Thanks for your help !
I tried this and it worked very well. I just check if the values of my previous request can be found in the database. If yes I select them :
<?php
foreach ($structures as $item) { ?>
<option value="<?php echo $item['id_structure']; ?>"
<?php if ($item['id_structure'] == $structurePrincipaleID['id_structure']) { echo ' selected="selected"'; } ?>>
<?php echo $item['nom_structure'] ?></option>
<?php } ?>
I want to get multiselsect values in PHP. This is my code:
<select id="tableset" class="form-control select2 table_id" name="table_id[]">
<option value="">Table</option>
<?php foreach ($tables as $tbls) { ?>
<option value="<?php echo $tbls->id; ?>"><?php echo $tbls->name; ?></option>
<?php } ?>
</select>
now to receive the values this is what i am doing
foreach ($_GET['table_id'] as $selectedOption)
{
echo $selectedOption;
}
I get an error with invalid argument for foreach. Please help me. How can I sort the issue?
First for a multiple select you need to indicate it by adding multiple tag to the select
<select id="tableset" multiple="multiple" class="form-control select2 table_id" name="table_id[]">
<option value="">Table</option>
<?php
foreach ($tables as $tbls) {
?>
<option value="<?php echo $tbls->id; ?>"><?php echo $tbls->name; ?>
</option>
<?php } ?>
now you can get them
foreach ($_GET['table_id'] as $selectedOption){
echo $selectedOption;
}
I am trying to populate a drop-down list of the database. In my view file I have the following code
Here is my controller
$query = $this->interprete_model->interpreteID($this->session->userdata('user_id'));
print_r($query);
$data['interprete'] = $query;
Aqui esta mi vista, usa set_select.
<select class="form-control" name="regionI" id="regionI">
<option value="">- Select -</option>
<?php foreach($result as $row):?>
<option value="<?php echo $row->id;?>"
<?php echo set_select('regionI', $row->id, TRUE); ?>><?php echo $row->name;?></option>
<?php endforeach; ?>
</select>
Result:
enter image description here
Many selected, I need one selected to modify (update) the data.
You can try this :
<select class="form-control" name="regionI" id="regionI">
<option value="">- Select -</option>
<?php foreach($users as $row):
$selected = FALSE;
// 1 is the id u want to be selected u can change it according to you
if ($row->id == 1){
$selected = TRUE;
}
?>
<option value="<?php echo $row->id;?>"
<?php echo set_select('regionI', $row->id, $selected); ?>><?php echo $row->name;?></option>
<?php endforeach; ?>
</select>
You can also use form_dropdown as
// FOR ids
$ids = array(1,2,3,4); // array of user ids
echo form_dropdown('regionI',$ids,1,array('class'=>'form-control'));
// FOR name
$names= array('name1','name2','name4','name3'); // array of user names
echo form_dropdown('regionI',$names,'name1',array('class'=>'form-control'));
For More :
https://www.codeigniter.com/user_guide/helpers/form_helper.html
i write this way for edit time selection
<?php foreach ($select_single as $select_single_show):?>
<select class="form-control" name="regionI">
<?php foreach ($users as $row):?>
<option <?php if($row->id==$select_single_show->regionI)echo "selected";?> value="<?php echo $all_branch_show->id?>"><?php echo $row->name?>
</option>
<?php endforeach;?>
</select>
<?php endforeach;?>
I am new to codeigniter and I am working on some project and I happen to need to display a drop down but filled with data from two or more tables. I don't know if join will solve the problem. Here is my view code:
<div class="form-group">
<label class="control-label">Store Name</label>
<select class="form-control" id="store" name="store">
<?php foreach($dataget as $val)
{
?>
<option value="<?php echo $val->Store;?>"><?php echo $val->Store;?></option>
<?php
}
foreach($storename as $value)
{
?>
<option value="<?php echo $value['StoreName'];?>"><?php echo $value['StoreName'];?></option>
<?php
}
?>
</select>
</div>
and here is my model:
function get_store()
{
$this->db->select('StoreName');
$this->db->from('store');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
and here is my controller:
public function poedit()
{
$id = $this->uri->segment(3);
$data['dataget'] = $this->wip_model->getByPOId($id);
$datadrop['storename'] = $this->wip_model->get_store();
$this->load->view('header');
$this->load->view('editpo',$data);
$this->load->view('footer');
}
I am working on the edit page of the project can someone please help. Thanks in advance.
In your controller
public function poedit()
{
$id = $this->uri->segment(3);
$data['dataget'] = $this->wip_model->getByPOId($id);
$data['storename'] = $this->wip_model->get_store();
$this->load->view('header');
$this->load->view('editpo',$data);
$this->load->view('footer');
}
To stop duplicate rows you can use in your view
<div class="form-group">
<label class="control-label">Store Name</label>
<select class="form-control" id="store" name="store">
<?php
$store_name = array();
foreach($dataget as $val)
{
$store_name[] = $val->Store;
?>
<option value="<?php echo $val->Store;?>"><?php echo $val->Store;?></option>
<?php
}
foreach($storename as $value)
{
if(!in_array($value['StoreName'],$store_name))//it will stop duplication of rows...
{
?>
<option value="<?php echo $value['StoreName'];?>"><?php echo $value['StoreName'];?></option>
<?php
}
}
?>
</select>
</div>
Well, looks like you put incorrect view variable
$datadrop['storename'] = $this->wip_model->get_store();
Should be changed to
$data['storename'] = $this->wip_model->get_store();
And I'd recommend to merge 2 arrays ($dataget/$storename) into one in controller, then use one foreach in the view, the code must be better
Student looking for help.
Trying to populate a dropdown menu from a mysql database, no data being showed in dropdown. Using getCategory function to get data from tables categories. Database is connecting and the particular table is being accessed through another function. I'm lost on this one, been looking and googling for the answer for last number of nights, still no luck. Maybe I've been looking at it too long that my brains fried?
Function Code
public function getCategory()
{
echo $query = "SELECT * FROM `categories`";
$results = mysqli_query($this->_con, $query) or die(mysqli_error());
$categories = array();
foreach ($results as $result){
$categories[$result['id']] = $result['category_name'];
}
mysqli_close($this->_con);
return $categories;
echo "<pre>"; print_r($categories); echo "</pre>";
}
Dropdown Code
<select class="form-control" name="category" id="category">
<option value="">Choose your category</option>
<?php foreach ($categories as $key=>$category){ ?>
<option value="<?php echo $key; ?>"><?php echo $category; ?></option>
<?php } ?>
</select>
Table called categories
Columns are id & category_name
please try the below code in your code in your function
$nums=mysql_num_rows($results);
for($i=0;$i<$nums;$i++)
{
$id=mysql_result($results,$i,'id');
$category=mysql_result($results,$i,'category_name');
$categories[$id] = $category;
}
$categories =getCategory() ;
I am not sure what you are struggling with, your code is missing a function call to getCategory (should be called getCategories because you are getting plural of objects, not one. Just a sidenote).
Following code is tested and works (save as index.php in your documentRoot to test), please be aware that I cannot declare a function as public when not in a class context, also I could not use $this->_con without a class with _con var, so I used $con instead. You need to adapt it to your context and needs:
index.php:
<?php
$category = filter_input(INPUT_POST,'category');
if($category!=FALSE && $category!=NULL){
$selectedCategory = $category;
}
function getCategory() {
$con = mysqli_connect("localhost","root","","categories") or die("Error " . mysqli_error($con));
echo $query = "SELECT * FROM `categories`";
$results = mysqli_query($con, $query) or die(mysqli_error());
$categories = array();
foreach ($results as $result){
$categories[$result['id']] = $result['category_name'];
}
mysqli_close($con);
return $categories;
}
?>
<html>
<head>
<title>Categories Test</title>
</head>
<body>
<?php
if(isset($selectedCategory)){
echo "last selected category ID: ".$selectedCategory;
}
?>
<form action="index.php" method="POST">
<select class="form-control" name="category" id="category">
<option value="">Choose your category</option>
<?php
$categories = getCategory();
foreach ($categories as $key=>$category): ?>
<option value="<?php echo $key; ?>"><?php echo $category; ?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="send" />
</form>
</body>
Here is the example i did it in this way. Hope! it will helps you.
public function GetStates()
{
$tempgetstates = mysql_query("SELECT * FROM `states`") or die(mysql_error());
echo '<select name="state" class="sel_reg_form" required />';
echo '<option value="">...Select...</option>';
while($getstates = mysql_fetch_object($tempgetstates))
{
echo '<option value="'.$getstates->state_code.'">'.$getstates->state.'</option>';
}
echo '</select>';
}