我的餐桌成分w / c包含(ingredient_id,name)。我可以完美地添加一种成分。但我想添加禁止添加现有成分的条件。我有这个条件的代码,但我认为这是错误的,因为条件不起作用,我仍然可以添加现有的成分。请帮帮我
这是我的代码:
查看:
import main #File where class lives.
def funcA():
#do something
print ("In A...") #Helps us observe scope.
funcB()
def funcB():
#do something
print("In B...") #scope
funcC()
def funcC():
#here i want to execute script main.py
#My attempt 1 :
print("In C...") #scope
main() #Reached C, lets run main now...
#This is one way to do allow a function to be accessible to your class.
def get_details(a,b,c):
#do something else as a function of ¬[class] test.py operating on
#a RepeatEvery object...
pass
def main(): #Function main is separate from class main. It houses our opening code.
thread = main.RepeatEvery(30, get_details,"arg1","arg2","arg3")
print ("starting")
thread.start()
thread.join(21) # allow thread to execute a while...
funcA()
控制器:
<?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(); ?>
MODEL:
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');
} }
答案 0 :(得分:0)
将模型更改为
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;
}
应该这样做。只需更改$查询行。
如果这不起作用,请在该行上方的$ value上执行var_dump,这样您就可以看到是否需要调整您所定位的值。
答案 1 :(得分:0)
使用以下代码。
控制器:
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];
}
}