prohibit adding existing value in a multidimensional array - php

I have a table ingredient w/c consist of(ingredient_id,name). I can perfectly add an ingredient. But i want to add a condition that prohibit on adding existing ingredient. I have the code for that condition but i think it's wrong because the condition didn't work and i can still add an existing ingredient. please help me
HERE'S MY CODE:
VIEW:
<?php echo form_open('dashboard/uploadIngredients', 'class="form-horizontal" enctype="multipart/form-data"'); ?>
<div class="form-group">
<div class="col-sm-10">
<select required class="form-control" name="ingredient_category">
<option value="" selected disabled>Select Ingredient Category</option>
<option value="All">All</option>
<?php foreach($this->products_model->getCategory() as $row): ?>
<option value="<?php echo $row->category_id ?>"><?php echo $row->name; ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<textarea class="form-control" name="ingredients" rows="5" placeholder="Ingredients (EX. onion, oil, pasta)" required></textarea>
</div>
</div>
<div class='form-group'>
<div class="col-sm-10">
<button class="btn btn-lg btn-positive" type="submit"><i class="glyphicon glyphicon-ok"></i> Save Ingredient</button>
</div>
</div>
<?php echo form_close(); ?>
CONTROLLER:
public function uploadIngredients()
{
foreach(explode(',', $this->input->post('ingredients')) as $key => $value) {
$saveData[] = array('ingredient_id' => null,
'name' => trim($value)
);
}
$ingredient_id = $this->products_model->saveIngredients($saveData);
redirect('dashboard/add_ingredients');
} }
MODEL:
public function saveIngredients($ingredient_id)
{
foreach($ingredient_id as $row => $value) {
$query=$this->db->where('ingredient_id', $value->ingredient_id);
if($query->num_rows > 0) {
echo '<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>';
echo "Ingredient already taken";
echo '</strong></div>';
} else {
$this->db->insert('ingredient', $value);
$insert_id[] = $this->db->insert_id();
}
}
return $insert_id;
}

Change your model to
public function saveIngredients($ingredient_id)
{
foreach($ingredient_id as $row => $value) {
$query=$this->db->where('name', $value->name);
if($query->num_rows > 0) {
echo '<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>';
echo "Ingredient already taken";
echo '</strong></div>';
} else {
$this->db->insert('ingredient', $value);
$insert_id[] = $this->db->insert_id();
}
}
return $insert_id;
}
That should do it. Just changing the $query line.
If that doesn't work, do a var_dump on $value just above that line, so you can see if you need to adjust the value you're targeting.

Use below code.
CONTROLLER:
public function uploadIngredients()
{
foreach(explode(',', $this->input->post('ingredients')) as $key => $value) {
$result = this->products_model->saveIngredients(trim($value));
if($result['status'])
{
$ids[]=$result['id'];
}else{
echo '<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>';
echo "Ingredient already taken";
echo '</strong></div>';
}
}
redirect('dashboard/add_ingredients');
} }
MODEL:
public function saveIngredients($data)
{
$query=$this->db->where('name', $data);
if($query->num_rows > 0) {
return ['status'=>FALSE];
} else {
$this->db->insert('ingredient', array('name'=>$data));
$insert_id = $this->db->insert_id();
return ['status'=>TRUE,'id'=>$insert_id];
}
}

Related

Problems receiving data from a record for update operation (Trying to access array offset on value of type bool)

I can't get my form_update_manga.php page to receive the values from the records that the form_read_manga.php page sends.
None of the fields return any value from the record, making it impossible to update the form. By the way, there is no way to do anything in this form. I can't find a logical solution.
The error that occurs in the form_update_manga.php page is shown in the figure below:
Image here
Error in most form fields: Trying to access array offset on value of type bool
Manga.php
<?php
require_once '../model/BD.php';
class Manga extends BD {
private $id_manga, $title, $publisher, $volumes, $discount, $value;
public function getId() { return $this->id_manga; }
public function getTitle() { return $this->title; }
public function getPublisher() { return $this->publisher; }
public function getVolumes() { return $this->volumes; }
public function getDiscount() { return $this->discount; }
public function getValue() { return $this->value; }
public function setId($id_manga) { $this->id_manga = $id_manga; }
public function setTitle($title) { $this->title = $title; }
public function setPublisher($publisher) { $this->publisher = $publisher; }
public function setVolumes($volumes) { $this->volumes = $volumes; }
public function setDiscount($discount) { $this->discount = $discount; }
public function setValue($value) { $this->value = $value; }
}
?>
MangaDAO.php
protected $table = 'manga';
public function readManga() {
try {
$sql = "SELECT * FROM $this->table WHERE id_manga = :id_manga";
$stm = BD::getInstance()->prepare($sql);
$stm->bindValue(':id_manga', $this->getId());
$stm->execute();
return $stm->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo "Error PDO" . $e->getMessage();
die();
} catch (Exception $e) {
echo "Error" . $e->getMessage();
die();
}
}
public function updateManga() {
try {
$sql = "UPDATE $this->table SET title = :title, publisher = :publisher,
volumes = :volumes, discount = :discount, value = :value WHERE id_manga = :id_manga";
$stm = BD::getInstance()->prepare($sql);
$stm->bindValue(':id_manga', $this->getId());
$stm->bindValue(':title', $this->getTitle());
$stm->bindValue(':publisher', $this->getPublisher());
$stm->bindValue(':volumes', $this->getVolumes());
$stm->bindValue(':discount', $this->getDiscount());
$stm->bindValue(':value', $this->getValue());
return $stm->execute();
} catch (PDOException $e) {
echo "Error PDO" . $e->getMessage();
die();
} catch (Exception $e) {
echo "Error ". $e->getMessage();
die();
}
}
form_read_manga.php
<div class="container" style="margin-top: 40px;">
<h4 class="text-center">Manga list</h4>
<br>
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Publisher</th>
<th>Volumes</th>
<th>Discount</th>
<th>Value collection</th>
<th>Register date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
require_once "../model/MangaDAO.php";
$manga = new MangaDAO();
foreach ($manga->readAllMangas() as $value){
echo '<tr>';
echo '<td>'.$value['id_manga'].'</td>';
echo '<td>'.$value['title'].'</td>';
echo '<td>'.$value['publisher'].'</td>';
echo '<td>'.$value['volumes'].'</td>';
echo '<td>'.$value['discount'].'</td>';
echo '<td>R$'.$value['value'].'</td>';
echo '<td>'.date('d/m/Y H:i:s', strtotime($value['register_date'])).'</td>';
echo '<td>';
echo '<a class="btn btn-info" href="/mangas/view/form_update_manga.php?id_manga='.$value['id_manga'].'"role="button" ><i class="fa fa-edit"></i> Update</a>';
echo " ";
echo '<a class="btn btn-danger" href="/mangas/controller/delete_manga.php?id_manga='.$value['id_manga'].'"role="button"><i class="fa fa-trash"></i> Delete</a>';
echo '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div>
form_update_manga.php
<?php
require_once "../model/MangaDAO.php";
$mangaDAO = new MangaDAO();
if (!isset($_GET['id_manga'])) {
echo '<p><button>Redo operation</button></p>';
die();
} else {
$data = $mangaDAO->readManga($_GET['id_manga']);
}
?>
<div class="container" id="t_container" style="margin-top: 20px;">
<h4 class="text-center">Update collection</h4>
<small class="form-text text-muted text-center">All fields are mandatory</small>
<br>
<form method="POST" action="/mangas/controller/update_manga.php">
<div class="form-group">
<label>ID:</label>
<input type="number" class="form-control" name="id_manga" required value="<?php echo $data['id_manga'] ?>" disabled>
</div>
<div class="form-group">
<label>Title:</label>
<input type="text" class="form-control" name="title" required maxlength="50" value="<?php echo $data['title'] ?>">
</div>
<div class="form-group">
<label>Publisher:</label>
<select class="custom-select" name="publisher" required value="<?php echo $data['publisher'] ?>">
<option selected value="">Choose the publisher</option>
<option value="Astral Comics" title="Astral Comics">Astral Comics</option>
<option value="Clamp" title="Clamp">Clamp</option>
<option value="Conrad" title="Conrad">Conrad</option>
<option value="Darkside Books" title="Darkside Books">Darkside Books</option>
<option value="Devir" title="Devir">Devir</option>
<option value="JBC" title="JBC">JBC</option>
<option value="NewPop" title="NewPop">NewPop</option>
<option value="Nova Sampa" title="Nova Sampa">Nova Sampa</option>
<option value="Panini" title="Panini">Panini</option>
<option value="Pipoca & Nanquim" title="Pipoca & Nanquim">Pipoca & Nanquim</option>
<option value="Veneta" title="Veneta">Veneta</option>
<option value="Other" title="Other">Other</option>
</select>
</div>
<div class="form-group">
<label>Volumes amount:</label>
<input type="text" class="form-control" name="volumes" required onkeypress="$(this).mask('000', {reverse: true});" value="<?php echo $data['volumes'] ?>">
</div>
<div class="form-group">
<label>Discount:</label>
<input type="text" class="form-control" name="discount" required onkeypress="$(this).mask('000.00', {reverse: true});" value="<?php echo $data['discount'] ?>">
</div>
<div class="form-group">
<label>Value collection:</label>
<input type="text" class="form-control" name="value" required value="<?php echo $data['value'] ?>" onkeypress="$(this).mask('000.00', {reverse: true});">
</div>
<button type="submit" name="btn_update" class="btn btn-info btn-lg btn-block"><i class="fa fa-edit"></i> Update</button>
</form>
</div>
Replace your code with this in MangaDAO.php
protected $table = 'manga';
public function readManga($mangaId) {
try {
$sql = "SELECT * FROM $this->table WHERE id_manga = :id_manga";
$stm = BD::getInstance()->prepare($sql);
$stm->bindValue(':id_manga', $mangaId);
$stm->execute();
return $stm->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo "Error PDO" . $e->getMessage();
die();
} catch (Exception $e) {
echo "Error" . $e->getMessage();
die();
}
}
You have to put the selected attr to a specific value please refer to the below code.
<div class="form-group">
<label>Publisher:</label>
<select class="custom-select" name="publisher" required value="<?php echo $data['publisher'] ?>">
<option value="" <?php if($data['publisher']==''){echo 'selected';} ?>>Choose the publisher</option>
<option value="Astral Comics" title="Astral Comics" <?php if($data['publisher']=='Astral Comics'){echo 'selected';} ?>>Astral Comics</option>
<option value="Clamp" title="Clamp" <?php if($data['publisher']=='Clamp'){echo 'selected';} ?>>Clamp</option>
<option value="Conrad" title="Conrad" <?php if($data['publisher']=='Conrad'){echo 'selected';} ?>>Conrad</option>
<option value="Darkside Books" title="Darkside Books" <?php if($data['publisher']=='Darkside Books'){echo 'selected';} ?>>Darkside Books</option>
<option value="Devir" title="Devir" <?php if($data['publisher']=='Devir'){echo 'selected';} ?>>Devir</option>
<option value="JBC" title="JBC" <?php if($data['publisher']=='JBC'){echo 'selected';} ?>>JBC</option>
<option value="NewPop" title="NewPop" <?php if($data['publisher']=='NewPop'){echo 'selected';} ?>>NewPop</option>
<option value="Nova Sampa" title="Nova Sampa" <?php if($data['publisher']=='Nova Sampa'){echo 'selected';} ?>>Nova Sampa</option>
<option value="Panini" title="Panini" <?php if($data['publisher']=='Panini'){echo 'selected';} ?>>Panini</option>
<option value="Pipoca & Nanquim" title="Pipoca & Nanquim" <?php if($data['publisher']=='Pipoca & Nanquim'){echo 'selected';} ?>>Pipoca & Nanquim</option>
<option value="Veneta" title="Veneta" <?php if($data['publisher']=='Veneta'){echo 'selected';} ?>>Veneta</option>
<option value="Other" title="Other" <?php if($data['publisher']=='Other'){echo 'selected';} ?>>Other</option>
</select>
</div>

Codeigniter Searchbar

Searchbar is working in codeigniter website but it does not load suggestion products. As I want if someone write 'dell' in searchbay he should get dell laptops suggestions below. and one another thing 'enter' button does not work for search one have to click the search icon to search the product.code is below
Controller
function text_search(){
if ($this->crud_model->get_settings_value('general_settings','vendor_system') !== 'ok') {
$search = $this->input->post('query');
$category = $this->input->post('category');
redirect(base_url() . 'index.php/home/category/'.$category.'/0-0/0/0/'.$search, 'refresh');
}else{
$type = $this->input->post('type');
$search = $this->input->post('query');
$category = $this->input->post('category');
if($type == 'vendor'){
redirect(base_url() . 'index.php/home/store_locator/'.$search, 'refresh');
} else if($type == 'product'){
redirect(base_url() . 'index.php/home/category/'.$category.'/0-0/0/0/'.$search, 'refresh');
}
}
}
Model
function get_type_name_by_id($type, $type_id = '', $field = 'name')
{
if ($type_id != '') {
$l = $this->db->get_where($type, array(
$type . '_id' => $type_id
));
$n = $l->num_rows();
if ($n > 0) {
return $l->row()->$field;
}
}
}
function get_settings_value($type, $type_name = '', $field = 'value')
{
if ($type_name != '') {
return $this->db->get_where($type, array('type' => $type_name))->row()->$field;
}
}
View
<div class="header-search">
<?php
echo form_open(base_url() . 'index.php/home/text_search/', array(
'method' => 'post'
));
?>
<input class="form-control" type="text" name="query" placeholder="<?php echo translate('what_are_you_looking_for');?>?"/>
<select
class="selectpicker header-search-select cat_select hidden-xs" data-live-search="true" name="category"
data-toggle="tooltip" title="<?php echo translate('select');?>">
<option value="0"><?php echo translate('all_categories');?></option>
<?php
$categories = $this->db->get('category')->result_array();
foreach ($categories as $row1) {
if($this->crud_model->if_publishable_category($row1['category_id'])){
?>
<option value="<?php echo $row1['category_id']; ?>"><?php echo $row1['category_name']; ?></option>
<?php
}
}
?>
</select>
<?php
if ($this->crud_model->get_type_name_by_id('general_settings','58','value') == 'ok') {
?>
<select
class="selectpicker header-search-select" data-live-search="true" name="type" onchange="header_search_set(this.value);"
data-toggle="tooltip" title="<?php echo translate('select');?>">
<option value="product"><?php echo translate('product');?></option>
<option value="vendor"><?php echo translate('vendor');?></option>
</select>
<?php
}
?>
<button class="shrc_btn"><i class="fa fa-search"></i></button>
</form>
</div>
<!-- /Header search -->

CODEIGNITER: Check if exist

I have table ingredient w/c consist of(ingredient_id,name),category w/c consist of(category_id,name) and category_ingredients w/c consist of(ingredient_id,category_id). I created a form for adding many ingredients by category and It check if 1 or more ingredients already exists in that category then I will only have to get the id's of the ingredient and then the other ingredients that don't exist will be inserted in my DB. Please help me i really need your help :(
VIEW:
<?php echo form_open('dashboard/uploadIngredients', 'class="form-horizontal" enctype="multipart/form-data"'); ?>
<div class="form-group">
<div class="col-sm-10">
<select required class="form-control" name="ingredient_category">
<option value="" selected disabled>Select Ingredient Category</option>
<option value="All">All</option>
<?php foreach($this->products_model->getCategory() as $row): ?>
<option value="<?php echo $row->category_id ?>"><?php echo $row->category_name; ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<textarea class="form-control" name="ingredients" rows="5" placeholder="Ingredients (EX. onion, oil, pasta)" required></textarea>
</div>
</div>
<div class='form-group'>
<div class="col-sm-10">
<button class="btn btn-lg btn-positive" type="submit"><i class="glyphicon glyphicon-ok"></i> Save Ingredient</button>
</div>
</div>
<?php echo form_close(); ?>
CONTROLLER:
public function uploadIngredients(){
foreach(explode(',', $this->input->post('ingredients')) as $key => $value)
{
if (!$this->products_model->getIngredientByName($value)) {
$saveData[] = array(
'ingredient_id' => null,
'name' => trim($value)
);
}
}
$ingredient_id = $this->products_model->saveIngredients($saveData);
foreach (explode(',', $this->input->post('ingredient_category')) as $key => $value)
{
foreach ( $ingredient_id as $key => $str ){
$joinData[] = array(
'ingredient_id' => $str,
'category_id' => intval($value)
);
}
$this->products_model->saveCategoryIngredients($joinData);
redirect('dashboard/add_ingredients');
}
MODEL:
public function saveIngredients($ingredient_id)
{
foreach($ingredient_id as $row => $value) {
$query=$this->db->where('ingredient_id', $value->ingredient_id);
$this->db->insert('ingredient', $value);
$insert_id[] = $this->db->insert_id();
}
return $insert_id;
}
public function getIngredientByName($name) {
$this->db->select('ingredient_id');
$this->db->limit(1);
$this->db->where('category_id', $category_id);
$ingredientQuery = $this->db->get('category_ingredient');
$ingredient_id = $ingredientQuery->row()->ingredient_id;
$this->db->select('name');
$this->db->where('ingredient_id', $ingredient_id);
$query = $this->db->get('ingredient');
foreach($query->result() as $row)
{
return $row->name;
}
}

CODEIGNITER: check if ingredient exist

I have table ingredient w/c consist of(ingredient_id,name),category w/c consist of(category_id,name) and category_ingredients w/c consist of(ingredient_id,category_id). I create a form for adding many ingredients by category and i want to check if 1 or more ingredients already exist then i will only have to get the id's of the ingredient and then the other ingredients that don't exist will be inserted in my db. can u guys help me pls?
Here's my code:
VIEW:
<?php echo form_open('dashboard/uploadIngredients', 'class="form-horizontal" enctype="multipart/form-data"'); ?>
<div class="form-group">
<div class="col-sm-10">
<select required class="form-control" name="ingredient_category">
<option value="" selected disabled>Select Ingredient Category</option>
<option value="All">All</option>
<?php foreach($this->products_model->getCategory() as $row): ?>
<option value="<?php echo $row->category_id ?>"><?php echo $row->category_name; ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<textarea class="form-control" name="ingredients" rows="5" placeholder="Ingredients (EX. onion, oil, pasta)" required></textarea>
</div>
</div>
<div class='form-group'>
<div class="col-sm-10">
<button class="btn btn-lg btn-positive" type="submit"><i class="glyphicon glyphicon-ok"></i> Save Ingredient</button>
</div>
</div>
<?php echo form_close(); ?>
CONTROLLER:
public function uploadIngredients()
{
foreach(explode(',', $this->input->post('ingredients')) as $key => $value)
{
$saveData[] = array('ingredient_id' => null,
'name' => trim($value)
);
}
$ingredient_id = $this->products_model->saveIngredients($saveData);
foreach (explode(',', $this->input->post('ingredient_category')) as $key => $value)
{
foreach ( $ingredient_id as $key => $str ){
$joinData[] = array(
'ingredient_id' => $str,
'category_id' => intval($value)
);
}
//var_dump($joinData); die();
$this->products_model->saveCategoryIngredients($joinData);
redirect('dashboard/add_ingredients');
}
}
MODEL:
public function saveIngredients($ingredient_id)
{
foreach($ingredient_id as $row => $value) {
$query=$this->db->where('ingredient_id', $value->ingredient_id);
$this->db->insert('ingredient', $value);
$insert_id[] = $this->db->insert_id();
}
return $insert_id;
}
public function saveCategoryIngredients($data)
{
foreach($data as $row => $value)
{
$this->db->insert('category_ingredient', $value);
$insert_id[] = $this->db->insert_id();
}
return $insert_id;}
}
You just have to add a function to your model, like this :
<?php
public function getIngredientByName($name) {
return $this->db
->from('ingredient I')
->where('I.name', $name)
->get()->row(); //Will return the row of ingredient if ingredient exists, else null
}
And in your controller :
<?php
foreach(explode(',', $this->input->post('ingredients')) as $key => $value)
{
if (!$this->products_model->getIngredientByName($value)) {
$saveData[] = array(
'ingredient_id' => null,
'name' => trim($value)
);
}
}
Thanks Gwendal for answering this question i am modifying this answer a bit to prevent duplicate inserts e.g.:- if user inserts SuGar CaNe but we have in our database Sugar Cane then the with answer's code we will have 2 sugar cane to avoid these type of inserts we can use this code for model
<?php
public function getIngredientByName($name) {
return $this->db
->from('ingredient I')
-> where("( REPLACE( LOWER(I.name), ' ', '') LIKE '".strtolower(preg_replace('/\s+/', '', $name)) ."%')")
->get()->row(); //Will return the row of ingredient if ingredient exists, else null
}
for controller same as
<?php
foreach(explode(',', $this->input->post('ingredients')) as $key => $value)
{
if (!$this->products_model->getIngredientByName($value)) {
$saveData[] = array(
'ingredient_id' => null,
'name' => trim($value)
);
}
}

How do i retrieve the value of dropdown when editing in codeigniter

OK, so I have looked everywhere for a solution to my problem but found none so far.My code looks like this.I have created a dynamic view page in which edit and add views are loaded dynamically.But the problem arises when i try to retain the value of select dropdown during editing.I would be grateful if someone could help me out.
View
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<?php if(#$patient_info){
echo '<h1 class="page-header">Edit Patient</h1>';
}else{
echo '<h1 class="page-header">Add Patient</h1>';
}
?>
<?php if($this->session->flashdata('error')){ ?>
<div class="alert alert-danger"><?php echo $this->session->flashdata('error'); ?></div>
<?php } ?>
<?php if($this->session->flashdata('erroredit')){ ?>
<div class="alert alert-danger"><?php echo $this->session->flashdata('erroredit'); ?></div>
<?php } ?>
<?php //echo validation_errors('<div class="alert alert-danger">','</div>'); ?>
<form role="form" method="post" action="<?php echo isset($patient_info) ? site_url('home/patient/edit') .'/' .$patient_info->patientID : site_url('home/patient/new'); ?>">
<div class="form-group">
<?php echo form_error('pname', '<div class="alert alert-danger">', '</div>'); ?>
<label for="pname">Patient Name</label>
<input class="form-control" placeholder="Enter Patient Name" name="pname" value="<?php echo isset($patient_info) ? $patient_info->patientName : ''; ?>">
</div>
<!-- Dropdown menu for selecting clinic -->
<div class="form-group">
<label for="select">Select Clinic Name</label>
<select class="form-control" name="selectClinic">
<option value="none">Select Clinic Below</option>
<?php foreach($allclinic as $key=>$clinic){ ?>
<!--<?php //foreach($clinicByPatient as $clin): ?>-->
<option value="<?php $clinic->clinicID; ?>"
<?php if(isset($patient_info)){
echo 'selected="selected"';
}
?>
>
<?php echo $clinic->clinicName; ?>
</option>
<?php //endforeach; ?>
<?php } ?>
</select>
</div>
<!-- Select Clinic ends-->
<div class="form-group">
<label for="select">Select Dentist</label>
<select class="form-control" name="selectDentist">
<option value="">Select Dentist</option>
<?php foreach($dentistdet as $key=>$dentist){ ?>
<option value="<?php echo $did = $dentist->dentistID;?>"><?php echo $dentist->dentistName; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary btn-lg btn-block" name="submit" value="<?php echo isset($patient_info) ? 'Update Patient' : 'Add Patient'; ?>" >
</div>
</form>
<div class="form-group">
<input type="submit" class="btn btn-danger btn-lg btn-block" value="Cancel">
</div>
Edit controller
public function edit(){
$id = $this->uri->segment(4);
$this->load->library('form_validation');
$this->load->model('clinic_model');
$this->load->model('dentist_model');
$data['patient_info'] = $this->patient_model->getPatientById($id);
$data['clinicByPatient'] = $this->patient_model->getPatientByClinic();
$data['allclinic'] = $this->clinic_model->getAllClinics();
// $data['clinicdet'] = $this->patient_model->getPatientByClinic();
if($_POST){
$this->form_validation->set_rules('pname', 'Patient Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('paddress', 'Patient Address', 'trim|required|xss_clean');
$this->form_validation->set_rules('pcontact', 'Patient Contact', 'trim|required|xss_clean');
if($this->form_validation->run()== FALSE){
$data['subview'] = 'patient/patient_new';
$this->load->view('includes/layout', $data);
}else{
$patientname = $this->input->post('pname');
$patientaddress = $this->input->post('paddress');
$patientcontact = $this->input->post('pcontact');
$select = $this->input->post('selectClinic');
$option = $this->input->post('selectDentist');
$edited = $this->patient_model->editpatient($id, $patientname, $patientaddress, $patientcontact, $select, $option);
if($edited){
$this->session->set_flashdata('successedit', 'Successfully updated the record');
redirect('home/patient');
}
}
}else{
$data['subview'] = 'patient/patient_new';
$this->load->view('includes/layout',$data);
}
}
Model looks like this
<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Patient_model extends CI_Model{
public function countPatients(){
$countPatient = $this->db->count_all('patient');
return $countPatient;
}
public function getallpatients(){
$query = $this->db->get('patient');
if($query->num_rows()>0){
return $query->result();
}
else{
return FALSE;
}
}//getallpatients function ends
public function getPatientByClinic(){
$this->db->select('*');
$this->db->from('patient');
$this->db->join('clinic', 'patient.clinicID = clinic.clinicID', 'left');
$this->db->join('dentist', 'patient.dentistID = dentist.dentistID', 'left');
$query = $this->db->get();
if($query->num_rows>0){
return $query->result();
}
}
public function addPatientByClinic($patientname, $patientadd, $patientcontact, $select, $option){
$data = array(
'patientName' => $patientname,
'patientAddress' => $patientadd,
'patientContact' => $patientcontact,
'clinicID' => $select,
'dentistID' => $option
);
return $this->db->insert('patient',$data);
}// method ends
public function deletePatient($id){
$verifyID = array('patientID' => $id);
// $affRows = $this->db->affected_rows();
// $obj = new Patient_model;
if($verifyID){
$this->db->where($verifyID);
$this->db->delete('patient');
if($this->db->affected_rows()){
return TRUE;
}
}
}
public function editpatient($id, $patientname, $patientaddress, $patientcontact, $select, $option){
$data = array(
'patientName' => $patientname,
'patientAddress' => $patientaddress,
'patientContact' => $patientcontact,
'clinicID' => $select,
'dentistID' => $option
);
$query = $this->db->where('patientID', $id)
->update('patient', $data);
if($query){
return true;
}
}//method ends
public function getPatientById($id){
$query = $this->db->where('patientID', $id)
->get('patient');
return $query->row();
}
}//class ends
?>
In the code for your select box, you aren't actually echoing $clinic->clinicID. Thus, when the form is submitted, the value will be empty.
You also need to be careful with how you are choosing which select element will be selected by default - you aren't comparing with anything that will change within the loop. Should you be checking against $clinic->clinicID?

Categories