error is showing in my view codeigniter - php

I have a error on my view which I am trying to work out why but not know how to fix.
A PHP Error was encountered
Severity: Warning
Message: in_array() expects parameter 2 to be array, boolean given
What would be the best method to fix this I think it has something to do with my post permission[access] on controller that's effecting view.
View
<div class="form-group">
<label class="col-sm-2 control-label">Access Permission</label>
<div class="col-sm-10">
<div class="well well-sm" style="height: 200px; overflow: auto;">
<?php foreach ($permissions as $permission) { ?>
<div class="checkbox">
<label>
<?php if (in_array($permission, $access)) { ?>
<input type="checkbox" name="permission[access][]" value="<?php echo $permission; ?>" checked="checked" />
<?php echo $permission; ?>
<?php } else { ?>
<input type="checkbox" name="permission[access][]" value="<?php echo $permission; ?>" />
<?php echo $permission; ?>
<?php } ?>
</label>
</div>
<?php } ?>
</div>
<a onclick="$(this).parent().find(':checkbox').prop('checked', true);">Select All</a> / <a onclick="$(this).parent().find(':checkbox').prop('checked', false);">Unselect All</a></div>
</div>
Controller Function
public function getForm() {
$data['title'] = "Users Group";
$this->load->model('admin/user/model_user_group');
$id = $this->uri->segment(4);
if (isset($id) && $this->input->server('REQUEST_METHOD') != 'POST') {
$user_group_info = $this->model_user_group->getUserGroup($id);
}
$ignore = array(
'admin',
'dashboard',
'filemanager',
'login',
'menu',
'register',
'online',
'customer_total',
'user_total',
'chart',
'activity',
'logout',
'footer',
'header'
);
$data['permissions'] = array();
$files = glob(FCPATH . 'application/modules/admin/controllers/*/*.php');
foreach ($files as $file) {
//$part = explode('/', dirname($file));
$permission = basename(strtolower($file), '.php');
if (!in_array($permission, $ignore)) {
$data['permissions'][] = $permission;
}
}
if (null !==($this->input->post('permission[access]'))) {
$data['access'] = $this->input->post('permission[access]');
} elseif ($user_group_info['permission']['access']) {
$data['access'] = $user_group_info['permission']['access'];
} else {
$data['access'] = array();
}
$this->load->view('template/user/users_group_form.tpl', $data);
}

I belive the problem lies in your controller, like you initially said. Try this:
In your controller, replace this:
if (null !== $this->input->post('permission[access]')) {
$data['access'] = $this->input->post('permission[access]');
} elseif ($user_group_info['permission']['access']) {
$data['access'] = $user_group_info['permission']['access'];
} else {
$data['access'] = array();
}
with this:
$permission_access = $this->input->post('permission');
if (isset($permission_access)) {
if (isset($permission_access['access'])) {
$data['access'] = $permission_access['access'];
} elseif ($user_group_info['permission']['access']) {
$data['access'] = $user_group_info['permission']['access'];
} else {
$data['access'] = array();
}
}

Related

Display 'same level' categories in Opencart:2.3.0.2

Here are the changes applied according to #daniel's Answer.
In \catalog\view\theme\default\template\extension\module\sibling_category.tpl
<div class="box box-with-categories">
<div class="box-heading check_2"><?php echo $heading_title; ?></div>
<div class="strip-line"></div>
<div class="box-content box-category">
<ul class="accordion" id="accordion-category">
<?php $i = 0; foreach ($categories as $category) { $i++; ?>
<li class="panel">
<?php if ($category['category_id'] == $category_id) { ?>
<?php echo $category['name']; ?>
<?php } else { ?>
<?php echo $category['name']; ?>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>
</div>
\catalog\controller\extension\module\sibling_category.php
<!--Have changed the controller logic, like you said -->
And In \catalog\language\en-gb\extension\module\sibling_category.php
<?php
// Heading
$_['heading_title'] = 'Sibling Categories';
So, Now what to do for appearing on list -> inside Category Layout.
And is there any way we can do this in coding so this appear in product/category.tpl file, so we don't need to create all files in admin folder.
EDIT: I've rewritten this answer to make it simpler to follow.
Your best bet here is to create a new Module rather than including this in the template. There are two stages: creating admin files & creating front-end files.
FRONT END
Start by duplicating all the front-end files of the standard "Category" module which consist of the following files, rename them according to your own definition e.g. sibling_category wherever you have category:
\catalog\view\theme\default\template\extension\module\category.tpl
\catalog\controller\extension\module\category.php
\catalog\language\en-gb\extension\module\category.php
We don't need a model here as the model for this as the ModelCatalogCategory class has the desired ability already.
So now you need to change the controller logic, edit the newly created sibling_category.php controller file. What we're doing here is getting the current category data ($current_id & $current_category), extracting the Parent category's id (parent_id) and then getting all children of the parent_id aka "siblings"
<?php
class ControllerExtensionModuleSiblingCategory extends Controller {
public function index() {
$this->load->language('extension/module/category');
$data['heading_title'] = $this->language->get('heading_title');
if (isset($this->request->get['path'])) {
$parts = explode('_', (string)$this->request->get['path']);
} else {
$parts = array();
}
if (isset($parts[0])) {
$data['category_id'] = $parts[0];
} else {
$data['category_id'] = 0;
}
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$data['categories'] = array();
$current_id = $parts[count($parts) - 1];
$current_category = $this->model_catalog_category->getCategory($current_id);
$siblings = $this->model_catalog_category->getCategories($current_category['parent_id']);
foreach ($siblings as $category) {
$data['sibling_categories'][] = array(
'category_id' => $category['category_id'],
'name' => $category['name'] ,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
$data['current_id'] = $current_id;
return $this->load->view('extension/module/sibling_category', $data);
}
}
Here's the default OC template's category.tpl modules file adjusted (edit for your template file accordingly replacing "$category_id" with "$current_id":
<div class="list-group">
<?php foreach ($sibling_categories as $category) { ?>
<?php if ($category['category_id'] == $current_id) { ?>
<?php echo $category['name']; ?>
<?php if ($category['children']) { ?>
<?php foreach ($category['children'] as $child) { ?>
<?php if ($child['category_id'] == $child_id) { ?>
- <?php echo $child['name']; ?>
<?php } else { ?>
- <?php echo $child['name']; ?>
<?php } ?>
<?php } ?>
<?php } ?>
<?php } else { ?>
<?php echo $category['name']; ?>
<?php } ?>
<?php } ?>
</div>
ADMIN AREA
In order to manage the module in the admin area, you will need to duplicate the language file and create a controller and the view:
\admin\language\en-gb\extension\module\category.php
Create a new file in the path \admin\controller\extension\module\sibling_category.php and insert this code:
<?php
class ControllerExtensionModuleSiblingCategory extends Controller {
private $error = array();
public function index() {
$this->load->language('extension/module/sibling_category');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('extension/module');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
if (!isset($this->request->get['module_id'])) {
$this->model_extension_module->addModule('sibling_category', $this->request->post);
} else {
$this->model_extension_module->editModule($this->request->get['module_id'], $this->request->post);
}
$this->session->data['success'] = $this->language->get('text_success');
$this->response->redirect($this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true));
}
$data['heading_title'] = $this->language->get('heading_title');
$data['text_edit'] = $this->language->get('text_edit');
$data['text_enabled'] = $this->language->get('text_enabled');
$data['text_disabled'] = $this->language->get('text_disabled');
$data['entry_status'] = $this->language->get('entry_status');
$data['button_save'] = $this->language->get('button_save');
$data['button_cancel'] = $this->language->get('button_cancel');
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_extension'),
'href' => $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true)
);
if (!isset($this->request->get['module_id'])) {
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('extension/module/html', 'token=' . $this->session->data['token'], true)
);
} else {
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('extension/module/html', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], true)
);
}
if (!isset($this->request->get['module_id'])) {
$data['action'] = $this->url->link('extension/module/sibling_category', 'token=' . $this->session->data['token'], true);
} else {
$data['action'] = $this->url->link('extension/module/sibling_category', 'token=' . $this->session->data['token'] . '&module_id=' . $this->request->get['module_id'], true);
}
$data['cancel'] = $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true);
if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
$module_info = $this->model_extension_module->getModule($this->request->get['module_id']);
}
if (isset($this->request->post['name'])) {
$data['name'] = $this->request->post['name'];
} elseif (!empty($module_info)) {
$data['name'] = $module_info['name'];
} else {
$data['name'] = '';
}
if (isset($this->request->post['module_description'])) {
$data['module_description'] = $this->request->post['module_description'];
} elseif (!empty($module_info)) {
$data['module_description'] = $module_info['module_description'];
} else {
$data['module_description'] = array();
}
$this->load->model('localisation/language');
$data['languages'] = $this->model_localisation_language->getLanguages();
if (isset($this->request->post['status'])) {
$data['status'] = $this->request->post['status'];
} elseif (!empty($module_info)) {
$data['status'] = $module_info['status'];
} else {
$data['status'] = '';
}
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('extension/module/sibling_category', $data));
}
protected function validate() {
if (!$this->user->hasPermission('modify', 'extension/module/category')) {
$this->error['warning'] = $this->language->get('error_permission');
}
return !$this->error;
}
}
Finally create the view file at the path \admin\view\template\extension\module\sibling_category.tpl and insert the following:
<?php echo $header; ?><?php echo $column_left; ?>
<div id="content">
<div class="page-header">
<div class="container-fluid">
<div class="pull-right">
<button type="submit" form="form-category" data-toggle="tooltip" title="<?php echo $button_save; ?>" class="btn btn-primary"><i class="fa fa-save"></i></button>
<i class="fa fa-reply"></i></div>
<h1><?php echo $heading_title; ?></h1>
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><?php echo $breadcrumb['text']; ?></li>
<?php } ?>
</ul>
</div>
</div>
<div class="container-fluid">
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-pencil"></i> <?php echo $text_edit; ?></h3>
</div>
<div class="panel-body">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-html" class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="input-status"><?php echo $entry_status; ?></label>
<div class="col-sm-10">
<input type="hidden" name="name" value="<?php echo $heading_title; ?>" id="input-name" class="form-control" />
<select name="status" id="input-status" class="form-control">
<?php if ($status) { ?>
<option value="1" selected="selected"><?php echo $text_enabled; ?></option>
<option value="0"><?php echo $text_disabled; ?></option>
<?php } else { ?>
<option value="1"><?php echo $text_enabled; ?></option>
<option value="0" selected="selected"><?php echo $text_disabled; ?></option>
<?php } ?>
</select>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<?php echo $footer; ?>
Once you've done this, you may need to update your admin permissions. Navigate to "Settings=>Users=>UserGroups" and edit your Admin user type. Make sure you either "Select All" for read and modify permission or select it manually.
You need to then "Install" this module and then edit it, set it to enabled and Save. You'll notice a "Sub-module" listed below the "Sibling Category" this is to get the module in the 'Layouts' page. Do not create multiple instances of the module as it may cause confusion on the layouts - unless you understand what you're doing.
NB When duplicating the language files, remember to find & replace all instances of "Category" with "Sibling Category"

How to retrieve the array values from multiples checkbox?

How to retrieve the array values from multiples checkbox? I have problem when I retrieve value array and echo the value. I get this error
Severity: Warning
Message: in_array() expects parameter 2 to be array, string given
Filename: page/update.php.
Please, could you help me again? Thank you.
You find three files: View, Model and Controller. Please check where I wrong...
UPDATE.PHP:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<div class="row">
<div class="col-sm-12">
<?php echo form_open('page_controller/update_page_post'); ?>
<div class="form-group">
<div class="row">
<div class="col-sm-3 col-xs-12">
<label><?php echo trans('subcategory'); ?></label>
</div>
</div><div class="col-xm-12">
<div class="table-responsive">
<table class="table table-bordered table-striped" role="grid">
<tbody>
<tr>
<?php $valuesub = ($page->subcat_recip_id); ?>
<?php $array_of_values = explode(",", $valuesub);
//if ($item['parent_id'] != "0" && $item['subcat_recip_id'] == "0") :
foreach ($array_of_values as $item) {
if(in_array($valuesub,$item)): { ?>
<td>
<input type="checkbox" name="subcat_recip_id[]" class="square-purple" value="<?php echo html_escape($item["title"]); ?>" CHECKED> <?php echo html_escape($item["title"]);
} ?>
<?php else: { ?>
<input type="checkbox" name="subcat_recip_id[]" class="square-purple" value="<?php echo html_escape($item["title"]); ?>"> <?php echo html_escape($item["title"]);
}
endif; }?>
</td>
<?php echo html_escape($valuesub); ?></tr>
</tbody>
</table>
</div>
</div>
</div>
PAGE_CONTROLLER.PHP
public function update_page_post()
{
//validate inputs
$this->form_validation->set_rules('title', trans("title"), 'required|xss_clean|max_length[500]');
if ($this->form_validation->run() === false) {
$this->session->set_flashdata('errors', validation_errors());
$this->session->set_flashdata('form_data', $this->page_model->input_values());
redirect($this->agent->referrer());
} else {
//get id
$id = $this->input->post('id', true);
$redirect_url = $this->input->post('redirect_url', true);
if (!$this->page_model->check_page_name()) {
$this->session->set_flashdata('form_data', $this->page_model->input_values());
$this->session->set_flashdata('error', trans("msg_page_slug_error"));
redirect($this->agent->referrer());
exit();
}
if ($this->page_model->update($id)) {
$this->session->set_flashdata('success', trans("msg_updated"));
if (!empty($redirect_url)) {
redirect($redirect_url);
} else {
redirect(admin_url() . 'pages');
}
} else {
$this->session->set_flashdata('form_data', $this->page_model->input_values());
$this->session->set_flashdata('error', trans("msg_error"));
redirect($this->agent->referrer());
}
}
}
PAGE_MODEL.PHP
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Page_model extends CI_Model
{
//input values
public function input_values()
{
$checkbox = implode(',', $this->checkBox());
$data = array(
'lang_id' => $this->input->post('lang_id', true),
'title' => $this->input->post('title', true),
'slug' => $this->input->post('slug', true),
'page_description' => $this->input->post('page_description', true),
'page_keywords' => $this->input->post('page_keywords', true),
'page_content' => $this->input->post('page_content', false),
'page_order' => $this->input->post('page_order', true),
'parent_id' => $this->input->post('parent_id', true),
'page_active' => $this->input->post('page_active', true),
'title_active' => $this->input->post('title_active', true),
'breadcrumb_active' => $this->input->post('breadcrumb_active', true),
'right_column_active' => $this->input->post('right_column_active', true),
'need_auth' => $this->input->post('need_auth', true),
'howmany_people' => $this->input->post('howmany_people', true),
'difficulty' => $this->input->post('difficulty', true),
'howmany_time' => $this->input->post('howmany_time', true),
'location' => $this->input->post('location', true),
'subcat_recip_id' => $checkbox
);
return $data;
}
public function checkBox(){
$count = count($_POST['subcat_recip_id']);
for($n=0; $n<$count; $n++){
$checkbox[$n] = $_POST['subcat_recip_id'][$n];
}
return $checkbox;
}
//update page
public function update($id)
{
//set values
$data = $this->page_model->input_values();
if (empty($data["slug"])) {
//slug for title
$data["slug"] = str_slug($data["title"]);
if (empty($data["slug"])) {
$data["slug"] = "page-" . uniqid();
}
}
$page = $this->get_page_by_id($id);
if (!empty($page)) {
$this->db->where('id', $id);
return $this->db->update('pages', $data);
}
return false;
}
in your you view,
<tr>
<?php $valuesub = ($page->subcat_recip_id); ?>
<?php $array_of_values = explode(",", $valuesub);
foreach ($array_of_values as $item) {
if(in_array($subcat_recip_id,$item)): { ?>
<td>
<input type="checkbox" name="subcat_recip_id[]" class="square-purple" value="<?php echo html_escape($item["title"]); ?>" CHECKED> <?php echo html_escape($item["title"]);
} ?>
<?php else: { ?>
<input type="checkbox" name="subcat_recip_id[]" class="square-purple" value="<?php echo html_escape($item["title"]); ?>"> <?php echo html_escape($item["title"]);
}
endif; }?>
</td>
<?php echo html_escape($valuesub); ?>
</tr>
change to :
<tr>
<?php $valuesub = ($page->subcat_recip_id);
$array_of_values = explode(",", $valuesub); ?>
<td>
<?php foreach ($array_of_values as $item) :?>
<input type="checkbox" name="subcat_recip_id[]" class="square-purple" value="<?php echo html_escape($item["title"]); ?>" <?=(in_array($subcat_recip_id,$item))?"CHECKED":""?>> <?=html_escape($item["title"]);?>'
<?php endforeach; ?>
</td>
<?=html_escape($valuesub)?>
</tr>
in model :
public function update($id){
//set values
$data = $this->page_model->input_values();
if (empty($data["slug"])) {
//slug for title
$data["slug"] = str_slug($data["title"]);
if (empty($data["slug"])) {
$data["slug"] = "page-" . uniqid();
}
}
$page = $this->get_page_by_id($id);
if (!empty($page)) {
$this->db->where('id', $id);
return $this->db->update('pages', $data);
}
return false;
}
change to :
public function update($id){
$data = $this->input_values();
if (empty($data["slug"])) {
$data["slug"] = str_slug($data["title"]);
if (empty($data["slug"])) {
$data["slug"] = "page-" . uniqid();
}
}
$page = $this->get_page_by_id($id);
if (!empty($page)) {
$this->db->where('id', $id);
return $this->db->update('pages', $data);
}
return false;
}
please update if you getting error,
tq
Well, it remains a bug but with some changes yr last code works. The great problem is the loop, it re-load the $valuesub for how many times is the value. Ie. If the array is composed of three values, it reads three times the same value. Pls see the two screenshots: https://www.screencast.com/t/MfiEDhdFuX
I can't get this loop off !!
Code
<td>
<?php foreach ($menu_links as $item) :
$valuesub = ($page->subcat_recip_id);
$array_of_values = explode("," , $valuesub);
if ($item['parent_id'] != "0" && $item['subcat_recip_id'] == "0") :
foreach($array_of_values as $dt): ?>
<?php //if(in_array($dt,$item)): ?>
<input type="checkbox" name="subcat_recip_id" class="square-purple" value="<?php echo html_escape($item["title"]); ?>" <?=(in_array($dt,$item))?"CHECKED":""?>> <?=html_escape($item["title"]);?>
<?php endforeach; endif;?>
<?php endforeach; ?>
</td><?php echo html_escape($valuesub); ?>

Fatal error: Uncaught ArgumentCountError: Too few arguments to function Admincategory::deletecategory(),

I know there were some questions related to this, but there are c ++ or other languages. I get this error and I'm not sure what's wrong with my function.
This is my Error
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Admincategory::deletecategory(), 0 passed in F:\xampp\htdocs\digikalamvc\core\app.php on line 34 and exactly 1 expected in F:\xampp\htdocs\digikalamvc\controllers\admincategory.php:50 Stack trace: #0 F:\xampp\htdocs\digikalamvc\core\app.php(34): Admincategory->deletecategory() #1 F:\xampp\htdocs\digikalamvc\index.php(6): App->__construct() #2 {main} thrown in F:\xampp\htdocs\digikalamvc\controllers\admincategory.php on line 50
And my function is:
<?php
class Admincategory extends Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$category = $this->model->getChildren(0);
$data = ['category' => $category];
$this->view('admin/category/index', $data);
}
function showchildren($idcategory)
{
$categoryInfo = $this->model->categoryInfo($idcategory);
$children = $this->model->getChildren($idcategory);
$parents = $this->model->getParents($idcategory);
$data = ['categoryInfo' => $categoryInfo, 'category' => $children, 'parents' => $parents];
$this->view('admin/category/index', $data);
}
function addcategory($categoryId = 0, $edit = '')
{
if (isset($_POST['title'])) {
$title = $_POST['title'];
$parent = $_POST['parent'];
$this->model->addCategory($title, $parent, $edit, $categoryId);
}
$category = $this->model->getCategory();
$categoryInfo = $this->model->categoryInfo($categoryId);
$data = ['category' => $category, 'parentId' => $categoryId, 'edit' => $edit, 'categoryInfo' => $categoryInfo];
$this->view('admin/category/addcategory', $data);
}
function deletecategory($parentId)
{
$ids = $_POST['id'];
$this->model->deletecategory($ids);
header('location:'.URL.'admincategory/showchildren/'.$parentId);
}
}
?>
app.php file
<?php
class App
{
public $controller = 'index';
public $method = 'index';
public $params = [];
function __construct()
{
if (isset($_GET['url'])){
$url = $_GET['url'];
$url = $this->parseUrl($url);
$this->controller = $url[0];
unset($url[0]);
if (isset($url[1])){
$this->method = $url[1];
unset($url[1]);
}
$this->params = array_values($url);
}
$controllerUrl = 'controllers/' . $this->controller . '.php';
if (file_exists($controllerUrl)) {
require($controllerUrl);
$object = new $this->controller;
$object->model($this->controller);
if (method_exists($object, $this->method)) {
call_user_func_array([$object, $this->method], $this->params);
}
}
}
function parseUrl($url)
{
$url = filter_var($url, FILTER_SANITIZE_URL);
$url=rtrim($url,'/');
$url = explode('/', $url);
return $url;
}
}
?>
and index.php
<?php
require('views/admin/layout.php');
$category = $data['category'];
$categoryInfo = [];
if (isset($data['categoryInfo'])) {
$categoryInfo = $data['categoryInfo'];
}
$parents = [];
if (isset($data['parents'])) {
$parents = $data['parents'];
$parents = array_reverse($parents);
}
?>
<style>
.w400 {
width: 600px;
}
</style>
<div class="left">
<p class="title">
مدیریت دسته ها
(
<?php foreach ($parents as $row) { ?>
<a href="admincategory/showchildren/<?= $row['id']; ?>">
<?= $row['title']; ?>
</a>
-
<?php } ?>
<a href="admincategory/<?php if (isset($categoryInfo['id'])) {
echo 'showchildren/' . $categoryInfo['id'];
} else {
echo 'index';
} ?>">
<?php
if (isset($categoryInfo['title'])) {
echo $categoryInfo['title'];
} else {
echo 'دسته های اصلی';
}
?>
</a>
)
</p>
<a class="btn_green_small" href="admincategory/addcategory/<?= #$categoryInfo['id']; ?>">
افزودن
</a>
<a class="btn_red_small" onclick="submitForm();">
حذف
</a>
<form action="admincategory/deletecategory/<?= #$categoryInfo['id']; ?>" method="post">
<table class="list" cellspacing="0">
<tr>
<td>
ردیف
</td>
<td>
عنوان دسته
</td>
<td>
مشاهده زیر دسته ها
</td>
<td>
ویرایش
</td>
<td>
انتخاب
</td>
</tr>
<?php
foreach ($category as $row) {
?>
<tr>
<td>
<?= $row['id']; ?>
</td>
<td class="w400">
<?= $row['title']; ?>
</td>
<td>
<a href="admincategory/showchildren/<?= $row['id']; ?>">
<img src="public/images/view_icon.png" class="view">
</a>
</td>
<td>
<a href="admincategory/addcategory/<?= $row['id']; ?>/edit">
<img src="public/images/edit_icon.ico" class="view">
</a>
</td>
<td>
<input type="checkbox" name="id[]" value="<?= $row['id']; ?>">
</td>
</tr>
<?php } ?>
</table>
</form>
</div>
</div>
============
Thank you for trying to help!
Sometimes it happen that AdminCategory::deletecategory($parentId) is called without a parameter but prototype do not have a default value for it and therefore exception is thrown. Since you get data from a post request and there is always a possibility that a category do not have a parent you can refactor your method to looks like:
function deletecategory($parentId = null)
{
$ids = $_POST['id'];
$this->model->deletecategory($ids);
if (null !== $parentId) {
header('location:'.URL.'admincategory/showchildren/'.$parentId);
}
// PUT MORE OF YOUR LOGIC HERE, I DO NOT KNOW WHAT SHOULD HAPPEN
}
If you are using typing hints more appropriate would be to make method looks like
function deletecategory(string $parentId = ''): void //void is for php7.1
{
$ids = $_POST['id'];
$this->model->deletecategory($ids);
if ('' !== $parentId) {
header('location:'.URL.'admincategory/showchildren/'.$parentId);
}
// AGAIN LOGIC HERE
}
If you REALLY expect that parentId MUST be passed then instead wrap method caller with try catch
if (method_exists($object, $this->method)) {
try {
call_user_func_array([$object, $this->method], $this->params);
} catch (\Exception $ex) {
// HANDLE EXCEPTION HERE
}
}

Zend Search Lucene searching whole product description

What is below is my code of search engine on the website. Right now is only searching what is refered as ProductName and ProductNumber. I didn't know what need to be changed to searching whole ProductDescription
Here is Search.php file
protected $_index;
protected $_indexed = array();
/**
*
* #var Zend_Http_Client
*/
protected $_httpClient;
public function __construct()
{
try {
$indexDir = realpath($_SERVER['DOCUMENT_ROOT'] . '/../tmp/search');
$this->_index = Zend_Search_Lucene::open($indexDir);
} catch (Zend_Search_Lucene_Exception $e) {
$this->_index = Zend_Search_Lucene::create($indexDir);
}
$this->_httpClient = new Zend_Http_Client();
$this->_httpClient->setConfig(array('timeout' => 10));
Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
}
public function indexUrl($url)
{
if (is_array($url)) {
foreach ($url as $uri) {
$this->_indexUrl($uri);
}
} else if (is_string($url)) {
$this->_indexUrl($url);
}
}
public function indexWholePage()
{
$pageUrl = $this->_getHostName();
$this->_indexUrl($pageUrl . '/');
}
protected function _indexUrl($url)
{
if (in_array($url, $this->_indexed))
return;
$log = Zend_Registry::get('Zend_Log');
$log->log($url, Zend_Log::NOTICE);
$this->_httpClient->setUri($url);
$response = $this->_httpClient->request();
$this->_indexed[] = $url;
if ($response->isSuccessful()) {
$body = $response->getBody();
$doc = Zend_Search_Lucene_Document_Html::loadHTML($body, true);
foreach ($doc->getLinks() as $link) {
if ($this->_isValidPageLink($link) && !in_array($this->_getHostName() . $link, $this->_indexed)) {
$this->_indexUrl($this->_getHostName() . $link);
}
}
$t = new Zend_Search_Lucene_Index_Term($url, 'url');
$q = new Zend_Search_Lucene_Search_Query_Term($t);
$hits = $this->_index->find($q);
foreach ($hits as $hit) {
if ($hit->md5 == md5($body)) {
return;
} else {
$this->_index->delete($hit->id);
}
}
$doc->addField(Zend_Search_Lucene_Field::Keyword('url', $url));
$doc->addField(Zend_Search_Lucene_Field::UnIndexed('md5', md5($body)));
$this->_index->addDocument($doc);
$log = Zend_Registry::get('Zend_Log');
$log->log('done', Zend_Log::NOTICE);
}
}
public function search($query)
{
return $this->_index->find($query);
}
public function deleteIndex()
{
}
protected function _getHostName()
{
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
$proto = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== "off") ? 'https' : 'http';
$port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : 80;
$uri = $proto . '://' . $host;
if ((('http' == $proto) && (80 != $port)) || (('https' == $proto) && (443 != $port))) {
$uri .= ':' . $port;
}
return $uri;
}
protected function _isValidPageLink($url)
{
$hostName = $this->_getHostName();
if (substr($url, 0, strlen($hostName)) == $hostName ||
substr($url, 0, 1) == '/' || substr($url, 0, 1) == '?') {
if (#preg_match('#^(.+)\.(jpg|gif|png|pdf|doc|xls)$#i', $url)) {
return false;
}
return true;
}
return false;
}
And here is php form to generate search results. Lucene implementations that I found after searching where completly different than what here is. This is my first time with ZendFramework.
<form method="get" action="/search.html" class="searchForm" enctype="application/x-www-form-urlencoded" id="searchForm">
<fieldset>
<input type="text" id="search_text" name="q" value="<?php echo $this->escape($this->query) ?>"><br>
<input type="submit" value="search" id="search" name="search">
</fieldset>
</form>
<h1>Search results</h1>
<?php if(empty($this->searchString)): ?>
<p><strong>Please write text of minimal lenght of<?php echo $this->minimumLength ?></strong></p>
<?php else: ?>
<?php if(count($this->products)){ ?>
<?php foreach ($this->products as $product): ?>
<?php $link = '/'.$this->permalink($product->product_name).','.$product->product_id.','.$product->category_id.',p.html'; ?>
<div class="productlist clearfix">
<a href="<?= $link; ?>" class="clearfix">
<div class="txt">
<h2><?= $product->product_name ?><?php if(strlen($product->product_number) > 2){ echo '<small> [ '.$product->product_number.' ]</small>'; } ?></h2>
<p><?= stripslashes($product->product_intro2) ?></p>
</div>
<div class="pic">
<?php if($product->has_media): ?>
<?php echo $this->thumb($product->media_src, 110, 110) ?>
<?php endif; ?>
<p style="text-align: center;">More</p>
</div>
</a>
</div>
<hr/>
<?php endforeach; ?>
<?php }else{ ?>
<p>0 product was found</p>
<?php } ?>
<div style="clear: both;">
<?php echo $this->products; ?>
</div>
<?php endif ?>

Error message not displaying in CodeIgniter

I want to edit some data after a POST in CodeIgniter.
The problem is when I make an edit I want to show a message that tells me the edit is done. I tried to do the following, but no message is displayed. Why?
Controller
function edit($id = 0) {
$this->load->model('Event_Model');
if ($id > 0) {
$this->data['eventinfo'] = $this->Event_Model->getInfo($id);
}
$this->data['pagetitle'] = "Edit";
$this->data['formID'] = $this->uri->segment(2)."-".$this->uri->segment(3);
$this->template->build('admin/events/add',$this->data);
}
function do_edit() {
$this->load->model('Event_Model');
$id = $this->input->post('hID');
$data = array(
'ev_text' => $this->input->post('ev_text'),
'ev_pic' => $this->input->post('ev_pic'),
);
$this->Event_Model->update($id, $data);
$this->data['success'] = "Done";
$this->data['errors'] = $this->errors;
$this->template->build('admin/events/add',$this->data);
}
Model
function update($id, $data) {
$this->db->where('ev_id', $id);
$this->db->update('events', $data);
}
View (where I want the message)
<?php
if (isset($success)) { ?>
<div class="alert alert-success normal-alert" style="display: block;">
<p><span class="ico-text ico-alert-success" ></span><?= $success; ?></p>
</div>
<?php
}
if (isset($errors)) { ?>
<div class="alert alert-error normal-alert" style="display: block;">
<div>
<span class="ico-text ico-alert-error"></span>
<?php if (count($errors) > 0) { ?>
<ul>
<?php
foreach ($errors as $error) {
echo "<li>$error</li>";
}
?>
</ul>
<?php } ?>
<div class="clear"></div>
</div>
</div>
<?php } ?>
and the second error, update data with picture not work, its get me error in dubg that call me the $_FILES['choose-file']['name'] not define
Try This:
Controller
function do_edit() {
$this->load->model('Event_Model');
$id = $this->input->post('hID');
$data = array(
'ev_text' => $this->input->post('ev_text'),
'ev_pic' => $this->input->post('ev_pic'),
);
$result = $this->Event_Model->update($id, $data);
if($result) {
$this->data['success'] = "Done";
$this->template->build('admin/events/add',$this->data);
}
$this->data['errors'] = $this->errors;
$this->template->build('admin/events/add',$this->data);
}
Model:
function update($id, $data) {
$this->db->where('ev_id', $id);
$this->db->update('events', $data);
return $this->db->affected_rows();
}

Categories