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 -->
Related
I was wondering if is there anything pre-made to compare a single image folder on the server against a field on a mysql database to unlink/remove files not needed from the server and keep it clean.
Enough comparing image filename, but if come with thumnbnails/dates great.
Thanks
Top of the file
if (isset($_GET['delete'])) {
unlink($_GET['delete']);
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
function getImagesFromDir($path) {
$images = array();
if ( $img_dir = #opendir($_SERVER['DOCUMENT_ROOT'].$path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}
Custom Form to select mysql TABLE and FIELD name as well as SERVER FOLDERS
<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<select name="tablename">
<option value="<?php if (isset($_GET['tablename'])) { echo $_GET['tablename']; } ?>"><?php if (isset($_GET['tablename'])) { echo $_GET['tablename']; } ?></option>
<option value="TABLENAME1">TABLENAME1</option>
<option value="TABLENAME2">TABLENAME2</option>
</select>
<select name="fieldname">
<option value="<?php if (isset($_GET['fieldname'])) { echo $_GET['fieldname']; } ?>"><?php if (isset($_GET['fieldname'])) { echo $_GET['fieldname']; } ?></option>
<option value="FIELDNAME1">FIELDNAME1</option>
<option value="FIELDNAME2">FIELDNAME2</option>
</select>
<select name="folder">
<option value="<?php if (isset($_GET['folder'])) { echo $_GET['folder']; } ?>"><?php if (isset($_GET['folder'])) { echo $_GET['folder']; } ?></option>
<option value="RELATIVE_PATH_1"</option>
<option value="RELATIVE_PATH_2"</option>
</select>
<input type="submit" value="Send" class="btn btn-primary">
</div>
</form>
Now let's retrieve the images from the FOLDER that DON'T EXISTS ON THE DATABASE TABLE AS PER REQUEST ON THE FORM:
<?php if ((isset($_GET['tablename'])) && (isset($_GET['fieldname'])) && (isset($_GET['folder'])) ) {
$pdo = new PDO("mysql:host=$servername;dbname=$database", $username, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
if (isset($_GET['tablename'])) {
$table = $_GET['tablename'];
}
$query = $pdo->prepare("SELECT * FROM $table");
$query->execute();
$dbimages = $query->fetchAll(PDO::FETCH_ASSOC);
if ((!$query->rowCount() == 0)) {
$arraydbimg = array();
foreach ($dbimages as $row => $result) {
$arraydbimg[] = $result['img'];
}
}
if (isset($_GET['folder'])) {
$path = $_GET['folder'];
} else {
$path = '/your-default/images-folder/';
}
$imgList = getImagesFromDir($path);
$i=0;
foreach ($imgList as &$img) {
if (!in_array($img, $arraydbimg)) { ?>
<br clear="all">
<a href="admin.php?delete=<?php echo urlencode($_SERVER['DOCUMENT_ROOT']. $path . $img); ?>">
<img src="<?php echo $path . $img ?>" width="150px" height="auto" style="border-radius:0; margin:10px; border:1px #333 solid" />
</a><?php echo $img; ?>
<? } } } ?>
CLICK ON THE IMAGE JUST IF YOU WANT TO ERASE!
I want to show student from selected school, class and section. if I delete 'class_id' => $class_id, 'section_id' => $section_id, from controller then it show all student from selected school otherwise it show nothing. any solution please. where am wrong ?
This is file from where I have to select value
<div class="form-group">
<label class="col-sm-3 control-label" >School<span class="required">*</span></label>
<div class="col-sm-5">
<select name="school_id" id="school_id" class="js-example-basic-multiple form-control" onchange="get_student_by_school_class_section_id()">
<option value="" >Select School...</option>
<?php if (!empty($all_school_info)): foreach ($all_school_info as $v_school): ?>
<option value="<?php echo $v_school->school_id; ?>"
<?php if (!empty($all_student_complain_info->school_id)) {
echo $v_school->school_id == $all_student_complain_info->school_id ? 'selected ' : ''; } ?>>
<?php echo $v_school->school_name ; ?>
</option>
<?php
endforeach;
endif;
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" >Class<span class="required">*</span></label>
<div class="col-sm-5">
<select name="class_id" id="class_id" class="js-example-basic-multiple form-control" onchange="get_student_by_school_class_section_id()">
<option value="" >Select Class...</option>
<?php if (!empty($all_classes_info)): foreach ($all_classes_info as $v_class): ?>
<option value="<?php echo $v_class->class_id; ?>"
<?php if (!empty($all_student_complain_info->class_id)) {
echo $v_class->class_id == $all_student_complain_info->class_id ? 'selected ' : ''; } ?>>
<?php echo $v_class->classes_name ; ?>
</option>
<?php
endforeach;
endif;
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" >Section<span class="required">*</span></label>
<div class="col-sm-5">
<select name="section_id" id="section_id" class="js-example-basic-multiple form-control" onchange="get_student_by_school_class_section_id()">
<option value="" >Select Section...</option>
<?php if (!empty($all_section_info)): foreach ($all_section_info as $v_section): ?>
<option value="<?php echo $v_section->section_id; ?>"
<?php if (!empty($all_student_complain_info->section_id)) {
echo $v_section->section_id == $all_student_complain_info->section_id ? 'selected ' : ''; } ?>>
<?php echo $v_section->section_name ; ?>
</option>
<?php
endforeach;
endif;
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label" >Student<span class="required">*</span></label>
<div class="col-sm-5">
<select name="student_id" id="student" class="js-example-basic-multiple form-control" >
<option value="" >Select Student...</option>
<?php if (!empty($student_info)): foreach ($student_info as $v_student): ?>
<option value="<?php echo $v_student->student_id; ?>"
<?php if (!empty($all_student_complain_info->student_id)) {
echo $v_student->student_id == $all_student_complain_info->student_id ? 'selected ' : ''; } ?>>
<?php echo $v_student->student_id.' '.$v_student->student_name.' ('.$v_student->student_father_name.')' ; ?>
</option>
<?php
endforeach;
endif;
?>
</select>
</div>
</div>
This is ajax.php
function get_student_by_school_class_section_id() {
var school_id = document.getElementById('school_id').value;
var class_id = document.getElementById('class_id').value;
var section_id = document.getElementById('section_id').value;
var base_url = '<?= base_url() ?>';
var strURL = base_url + "admin/global_controller/get_student_by_school_class_section_id/" + school_id + "/" + class_id + "/" + section_id;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
var result = req.responseText;
$("#student").html("<option value='' >Select Student...</option>");
$("#student").append(result);
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("POST", strURL, true);
req.send(null);
}
}
this is controller.php
public function get_student_by_school_class_section_id($school_id, $class_id, $section_id) {
$HTML = NULL;
$this->studentrecord_model->_table_name = 'tbl_studentrecords';
$this->studentrecord_model->_order_by = 'student_id';
$student_info = $this->studentrecord_model->get_by(array('school_id' => $school_id, 'class_id' => $class_id, 'section_id' => $section_id, 'status' => '1'), FALSE);
if (!empty($student_info)) {
foreach ($student_info as $v_student_info) {
$HTML.="<option value='" . $v_student_info->student_id . "'>" .$v_student_info->student_id.' '.$v_student_info->student_name.' ('.$v_student_info->student_father_name.')'. "</option>";
}
}
echo $HTML;
}
I want to show student from selected school, class and section. if I delete 'class_id' => $class_id, 'section_id' => $section_id, from controller then it show all student from selected school otherwise it show nothing. any solution please. where am wrong ?
this is studentrecord_model.php
public function all_student_record_info_by_scs($school_id, $class_id, $section_id) {
$this->db->select('tbl_studentrecords.*', FALSE);
$this->db->select('tbl_school.*', FALSE);
$this->db->select('tbl_class.*', FALSE);
$this->db->select('tbl_section.*', FALSE);
$this->db->select('tbl_fee_department.*', FALSE);
$this->db->from('tbl_studentrecords');
$this->db->join('tbl_school', 'tbl_school.school_id = tbl_studentrecords.school_id', 'left');
$this->db->join('tbl_class', 'tbl_class.class_id = tbl_studentrecords.class_id', 'left');
$this->db->join('tbl_section', 'tbl_section.section_id = tbl_studentrecords.section_id', 'left');
$this->db->join('tbl_fee_department', 'tbl_fee_department.fee_department_id = tbl_studentrecords.fee_department_id', 'left');
$this->db->where('tbl_studentrecords.school_id', $school_id);
$this->db->where('tbl_studentrecords.class_id', $class_id);
$this->db->where('tbl_studentrecords.section_id', $section_id);
$query_result = $this->db->get();
$result = $query_result->result();
return $result;
}
I have done by changing global_controller.php code like this
public function get_student_by_school_class_section_id($school_id, $class_id, $section_id) {
$HTML = NULL;
$school = $school_id;
$class = $class_id;
$section = $section_id;
$this->studentrecord_model->_table_name = 'tbl_studentrecords';
$this->studentrecord_model->_order_by = 'student_id';
$student_info = $this->studentrecord_model->get_by(array('school_id' => $school, 'class_id' => $class, 'section_id' => $section, 'status' => '1'), FALSE);
if (!empty($student_info)) {
foreach ($student_info as $v_student_info) {
$HTML.="<option value='" . $v_student_info->student_id . "'>" .$v_student_info->student_id.' '.$v_student_info->student_name.' ('.$v_student_info->student_father_name.')'. "</option>";
}
}
echo $HTML;
}
I am having a blog page which needs to be updated but unable to fetch the dropdown value from database.Here is the code for fetching dropdown value from database.While adding the data it is inserting successfully but while fetching the data to edit it is not working.Thanks In Advance
View:
<?php if(isset($records) && is_array($records) && count($records)>0): ?>
<?php foreach($records as $r):?>
<?php
$form_attributes = array('name'=>'edit', 'id'=>'edit', 'enctype' => "multipart/form-data");
echo form_open('blogs/editblogs',$form_attributes);
echo form_hidden('blog_id',$r->blog_id);
?>
<div class="element">
<label for="positions"><font color="black">Position</font></label>
<select name="position">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">Select none</option>
</select>
</div>
<?php echo form_close();?>
<?php endforeach;endif;?>
Controller:
function editblogs()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="message error"> ','</span>');
$this->form_validation->set_rules('position','Position');
if($this->form_validation->run()== FALSE)
{
$data['records']=$this->blogs_model->getblogsdata($this->input->post('blog_id'));
$data['mainpage']='blogs';
$data['mode']='edit';
$this->load->view('templates/template',$data);
}
else
{
$result = $this->blogs_model->update($this->input->post('blog_id'));
if(is_array($result))
{
$data['errors']=$result;
$data['records']=$this->blogs_model->getblogsdata($this->uri->segment('blog_id'));
$data['mainpage']='blogs';
$data['mode']='edit';
$this->load->view('templates/template',$data);
}
else
$this->flash->success('<h2>Successfully Updated the record.<h2>');
redirect('blogs');
}
}
Model:
function update($id)
{
$data=array(
'position'=>$this->input->post('position')
);
$this->db->where(array('blog_id'=>$id));
$this->db->update('blogs', $data);
return true;
}
Can you try something like this below one
Please try the below code
<?php if(isset($records) && is_array($records) && count($records)>0): ?>
<?php foreach($records as $r):?>
<?php
$form_attributes = array('name'=>'edit', 'id'=>'edit', 'enctype' => "multipart/form-data");
echo form_open('blogs/editblogs',$form_attributes);
echo form_hidden('blog_id',$r->blog_id);
?>
<div class="element">
<label for="positions"><font color="black">Position</font></label>
<select name="position">
<?php for($i=1;i<3;i++)
$option = '<option value="'.$i.'"';
if($i === $r->position ){
$option .= 'selected';
}
$option .= '>'.$i.'</option>';
<?php?>
<option value="4">Select none</option>
</select>
</div>
<?php echo form_close();?>
<?php endforeach;endif;?>
Fetching the value from database for static dropdown. Here is the answer.
<div class="element">
<label for="positions"><font color="black">Position</font></label>
<?php
$selected = ($this->input->post('position')) ? $this->input->post('position') : $r->position; ;
$position = array("1" => "1", "2" => "2", "3" => "3", "4" => "Select none");
echo form_dropdown('position', $position, $selected);
?>
</div>
I was building a custom plugin for one of my project and I am facing this issue with custom fields that I have created, but once I was trying to save the fields. I am not getting the data displayed in the admin editor. screenshot of issue page
I am adding the code below Please find it.
<?php
function apt_add_fields_metabox()
{
add_meta_box(
'apt_college_fields',
__('College Fields'),
'apt_college_fields_callback',
'college',
'normal',
'default'
);
}
add_action('add_meta_boxes','apt_add_fields_metabox');
//Display Fields Metabox Content
function apt_college_fields_callback($post)
{
wp_nonce_field(basename(__FILE__),'wp_college_nonce');
$apt_college_stored_meta = get_post_meta($post->ID);
?>
<div class="wrap college-form">
<div class="form-group">
<label for="issue_check"><?php esc_html_e('Issue Date Available','apt_domain'); ?></label>
<select name="issue_check" id="issue_check">
<?php
$option_value = array('Yes','No');
foreach($option_value as $key => $value)
{
if($value == $apt_college_stored_meta['issue_check'][0])
{ ?>
<option selected><?php echo $value; ?></option>
<?php
}
else
{
?>
<option ><?php echo $value; ?></option>
<?php
}
}
?>
</select>
</div>
<div class="form-group">
<label for="college-details"><?php esc_html_e('College Details','apt_domain'); ?></label>
<?php
$content = get_post_meta($post->ID,'college-details',true);
$editor = 'college-details';
$settings = array(
'textarea_rows' => 5,
'media_buttons' => true
);
wp_editor($content,$editor,$settings);
?>
</div>
<div class="form-group">
<label for="application_date"><?php esc_html_e('Application Available Date','apt_domain'); ?></label>
<input type="date" name="application_date" id="application_date" value="<?php if(!empty($apt_college_stored_meta['application_date'])) echo esc_attr($apt_college_stored_meta['application_date'][0]); ?>" />
</div>
</div>
<?php
}
function apt_college_save($post_id)
{
$is_autosave = wp_is_post_autosave($post_id);
$is_revision = wp_is_post_revision($post_id);
if(isset($_REQUEST['wp_college_nonce']) && wp_verify_nonce($_REQUEST['wp_college_nonce'],basename(__FILE__)))
{
$is_valid_nonce = true;
}
else
{
$is_valid_nonce = false;
}
if($is_autosave || $is_revision || !$is_valid_nonce)
{
return;
}
if(isset($_REQUEST['issue_check']))
{
update_post_meta($post_id,'issue_check',sanitize_text_field(['issue_check']));
}
if(isset($_REQUEST['college-details']))
{
update_post_meta($post_id,'college-details',sanitize_text_field(['college-details']));
}
if(isset($_REQUEST['application_date']))
{
update_post_meta($post_id,'application_date',sanitize_text_field(['application_date']));
}
}
add_action('save_post','apt_college_save');
?>
There is an error with your code block.you missed out $_REQUEST['issue_check']
update_post_meta($post_id,'issue_check',sanitize_text_field(['issue_check']));
Corrected Code
update_post_meta($post_id,'issue_check',sanitize_text_field($_REQUEST['issue_check']));
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];
}
}