For some reason my status and date added are not displaying on my table in codeigniter. I have got the username working fine but should be able to see date added and status both are working fine in database. image of my user http://s20.postimg.org/7719crcl9/user.png
Status in the database when enabled is "1" But should show text enabled.
How do I get both my status and date added working?
Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->helper('date');
}
public function getTotalUsers() {
$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . $this->db->dbprefix . "user`");
return $query->row('total');
}
public function getUsers($data = array()) {
$sql = "SELECT * FROM `" . $this->db->dbprefix . "user`";
$sort_data = array(
'username',
'status',
'date_added'
);
if (trim($data['sort']) && in_array($data['sort'], $sort_data)) {
$sql .= " ORDER BY " . $data['sort'];
} else {
$sql .= " ORDER BY username";
}
if (trim($data['order']) && ($data['order'] == 'DESC')) {
$sql .= " DESC";
} else {
$sql .= " ASC";
}
$query = $this->db->query($sql);
return $query->result_array();
}
}
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends Controller {
private $error = array();
public function index() {
$this->lang->load('user/user', 'english');
$this->load->model('user/user_model');
$this->getList();
}
public function getList() {
if (null !==($this->input->get('sort'))) {
$sort = $this->input->get('sort');
} else {
$sort = 'username';
}
if (null !==($this->input->get('order'))) {
$order = $this->input->get('order');
} else {
$order = 'ASC';
}
if (null !==($this->input->get('page'))) {
$page = $this->input->get('page');
} else {
$page = 1;
}
$url = '';
if (null !==($this->input->get('sort'))) {
$url .= '&sort=' . $this->input->get('sort');
}
if (null !==($this->input->get('order'))) {
$url .= '&order=' . $this->input->get('order');
}
if (null !==($this->input->get('page'))) {
$url .= '&page=' . $this->input->get('page');
}
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('text_home'),
'href' => site_url('dashboard')
);
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('heading_title'),
'href' => site_url('user')
);
$data['insert'] = site_url('user/insert');
$data['delete'] = site_url('user/delete');
$data['users'] = array();
$filter_data = array(
'sort' => $sort,
'order' => $order,
);
$user_total = $this->user_model->getTotalUsers();
$results = $this->user_model->getUsers($filter_data);
foreach ($results as $result) {
$data['users'][] = array(
'user_id' => $result['user_id'],
'username' => $result['username'],
'status' => ($result['status'] ? $this->lang->line('text_enabled') : $this->lang->line('text_disabled')),
'date_added' => date($this->lang->line('date_format_short'), strtotime($result['date_added'])),
'edit' => site_url('user/update', '&user_id=' . $result['user_id'] . $url)
);
}
$data['heading_title'] = $this->lang->line('heading_title');
$data['text_no_results'] = $this->lang->line('text_no_results');
$data['text_confirm'] = $this->lang->line('text_confirm');
$data['column_username'] = $this->lang->line('column_username');
$data['column_status'] = $this->lang->line('column_status');
$data['column_date_added'] = $this->lang->line('column_date_added');
$data['column_action'] = $this->lang->line('column_action');
$data['button_insert'] = $this->lang->line('button_insert');
$data['button_edit'] = $this->lang->line('button_edit');
$data['button_delete'] = $this->lang->line('button_delete');
if (null !==($this->input->post('selected'))) {
$data['selected'] = (array)$this->input->post('selected');
} else {
$data['selected'] = array();
}
$url = '';
if ($order == 'ASC') {
$url .= '&order=DESC';
} else {
$url .= '&order=ASC';
}
if (null !==($this->input->get('page'))) {
$url .= '&page=' . $this->input->get('page');
}
$data['sort_username'] = site_url('user', '&sort=username' . $url);
$data['sort_status'] = site_url('user', '&sort=status' . $url);
$data['sort_date_added'] = site_url('user', '&sort=date_added' . $url);
$url = '';
if (null !==($this->input->get('sort'))) {
$url .= '&sort=' . $this->input->get('sort');
}
if (null !==($this->input->get('order'))) {
$url .= '&order=' . $this->input->get('order');
}
$data['sort'] = $sort;
$data['order'] = $order;
$this->load->view('user/user_list', $data);
}
}
View
<?php if ($users) { ?>
<?php foreach ($users as $user) { ?>
<tr>
<td class="text-center"><?php if (in_array($user['user_id'], $selected)) { ?>
<input type="checkbox" name="selected[]" value="<?php echo $user['user_id']; ?>" checked="checked" />
<?php } else { ?>
<input type="checkbox" name="selected[]" value="<?php echo $user['user_id']; ?>" />
<?php } ?></td>
<td class="text-left"><?php echo $user['username']; ?></td>
<td class="text-left"><?php echo $user['status']; ?></td>
<td class="text-left"><?php echo $user['date_added']; ?></td>
<td class="text-right"><i class="fa fa-pencil"></i></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td class="text-center" colspan="5"><?php echo $text_no_results; ?></td>
</tr>
<?php } ?>
I found my problem I had to load my English language file I for got it.
public function index() {
$this->lang->load('user/user', 'english');
$this->lang->load('english', 'english'); // Had to add this
$this->load->model('user/user_model');
$this->getList();
}
Related
I am using a select dropdown menu to filter clinical trials from a database. Below the select is code that calls clinical_trial() class. Problem is that no results are being displayed when $_GET variable ?cid= is appended to the url.
clinical_trials.php
<?php if($_GET['cid']) $cid = $_GET['cid']; ?>
<?php
$query = "SELECT * FROM `category` ORDER BY category_name";
$categories = $_db->get_results($query, ARRAY_A); ?>
<select id="dynamic_select">
<option value="clinical_trials.php" selected>All Categories</option>
<?php
foreach($categories as $row):
extract($row);
echo '<option ' . ($cid == $category_id ? "selected" : "") . ' value="clinical_trials.php?cid='.$category_id.'">' . $category_name . '</option>';
endforeach; ?>
</select>
<script>
jQuery(document).ready(function($){
$('#dynamic_select').on('change', function () {
var url = $(this).val();
if (url) window.location = url; // redirect
return false;
});
});
</script>
<?php
$ct = new clinical_trial();
$params = array();
if($cid != '') $params['category_id'] = $cid;
$results = $ct->search($params);
$file_path = CLINICAL_TRIALS_REL_PATH;
$ts = strtotime($file_date);
if(count($results) > 0):
$html = '';
$html .= '<table id="current-clinicals">';
foreach($results as $row):
extract($row);
$html .= '<tr>';
$html .= '<td valign="top">'.$trial_name.'</td>';
$html .= '<td valign="top">'.$category_name.'</td>';
$html .= '<td valign="top">'.date("m/d/Y").'</td>';
$html .= '<td width="80" valign="top" align="center"><strong>View Here</strong></td>';
$html .= '</tr>';
endforeach;
else:
$html .= '<p>No clinical trials in this category.</p>';
endif;
$html .= '</table>';
echo $html; ?>
Below code is stored in clinical_trial.php
<?php
class clinical_trial{
public function validate($post, $file, &$errors, $action='create'){
global $_db;
cleanup_arr($post);
extract($post);
$errors = array();
$rules[] = array( 'rule'=>'', 'val'=>$lst_category_id, 'minlen'=>0, 'maxlen'=>0, 'required'=>true, 'friendly_name'=>'Category', 'var'=>'lst_category_id');
$rules[] = array( 'rule'=>'', 'val'=>$txt_trial_name, 'minlen'=>0, 'maxlen'=>0, 'required'=>true, 'friendly_name'=>'Trial name', 'var'=>'txt_trial_name');
$rules[] = array( 'rule'=>'', 'val'=>$txt_file_date, 'minlen'=>0, 'maxlen'=>0, 'required'=>true, 'friendly_name'=>'File date', 'var'=>'txt_file_date');
$flag_validated = true;
foreach($rules as $r){
$ret = validate($r);
$varname = $r['var'];
if($ret != VALIDATE_SUCCESS){
$flag_validated = false;
$errors[$varname] = $ret;
}
}
if ($action == 'create'){
if(!is_uploaded_file($file['file_filename']['tmp_name'])){
$flag_validated = false;
$errors['file_filename'] = 'Please upload a file.';
}
}
return $flag_validated;
}
function create($post, $file){
global $_db;
cleanup_arr($post);
extract($post);
$ts = strtotime($txt_file_date);
$file_date = date("Y-m-d", $ts);
$query = "INSERT INTO `clinical_trial` (trial_name, file_date, file_name, category_id) VALUES ('$txt_trial_name', '$file_date', '', $lst_category_id)";
$_db->query($query);
$clinical_trial_id = $_db->insert_id;
//$filename = md5(time());
$filename = $file_date . '-' . make_file_name($txt_trial_name);
$filename = handle_file_upload($file['file_filename'], $filename, CLINICAL_TRIALS_ABS_PATH);
$_db->query("UPDATE `clinical_trial` SET file_name='$filename' WHERE clinical_trial_id=$clinical_trial_id");
}
function update($post, $file){
global $_db;
cleanup_arr($post);
extract($post);
$ts = strtotime($txt_file_date);
$file_date = date("Y-m-d", $ts);
$query = "UPDATE `clinical_trial` SET trial_name='$txt_trial_name', category_id=$lst_category_id, file_date='$file_date' WHERE clinical_trial_id=$hdn_clinical_trial_id";
$_db->query($query);
if(is_uploaded_file($file['file_filename']['tmp_name'])){
#unlink(CLINICAL_TRIALS_ABS_PATH . $_db->get_var("SELECT file_name FROM clinical_trial WHERE clinical_trial_id=$hdn_clinical_trial_id"));
$filename = $file_date . '-' . make_file_name($txt_trial_name);
$filename = handle_file_upload($file['file_filename'], $filename, CLINICAL_TRIALS_ABS_PATH);
$_db->query("UPDATE `clinical_trial` SET file_name='$filename' WHERE clinical_trial_id=$hdn_clinical_trial_id");
}
}
function delete($clinical_trial_id){
global $_db;
cleanup_var($clinical_trial_id);
#unlink(CLINICAL_TRIALS_ABS_PATH . $_db->get_var("SELECT file_name FROM clinical_trial WHERE clinical_trial_id=$clinical_trial_id"));
$_db->query("DELETE FROM `clinical_trial` WHERE clinical_trial_id=$clinical_trial_id");
}
function search($params, $order_by=''){
global $_db;
if($params){
cleanup_arr($params);
extract($params);
}
if($category_id != '') $where = " AND ct.category_id=$category_id ";
$order_by = $order_by == "" ? "file_date DESC" : $order_by;
$query = "SELECT * FROM `clinical_trial` ct, `category` c
WHERE ct.category_id=c.category_id
$where
ORDER BY $order_by";
return $_db->get_results($query, ARRAY_A);
}
public function get($id)
{
global $_db;
cleanup_var($id);
$query = "SELECT * FROM `clinical_trial` ct WHERE ct.clinical_trial_id=$id";
$r = $_db->get_row($query, ARRAY_A);
if(count($r) == 0)
return false;
foreach ( $r as $key => $val ){
$this->$key = stripslashes($val);
}
return true;
}
} // class
You are not retrieving the $_GET variable?
Assuming this line is where you think you are retrieving it:
if($cid != '') $params['category_id'] = $cid;
From you code that condition will always be false.
Correct use would be:
if($_GET['cid'] != '') $params['category_id'] = $_GET['cid'];
I'm using php CRM that the following code exports the list of all users in database (this list is shown by admin users)
foreach ($rResult as $aRow) {
$row = array();
for ($i = 0; $i < count($aColumns); $i++) {
if (strpos($aColumns[$i], 'as') !== false && !isset($aRow[$aColumns[$i]])) {
$_data = $aRow[strafter($aColumns[$i], 'as ')];
} else {
$_data = $aRow[$aColumns[$i]];
}
if ($aColumns[$i] == 'last_login') {
if ($_data != null) {
$_data = time_ago($_data);
} else {
$_data = 'Never';
}
} elseif ($aColumns[$i] == 'active') {
$checked = '';
if ($aRow['active'] == 1) {
$checked = 'checked';
}
$_data = '<div class="onoffswitch">
<input type="checkbox" data-switch-url="'.admin_url().'staff/change_staff_status" name="onoffswitch" class="onoffswitch-checkbox" id="c_'.$aRow['staffid'].'" data-id="'.$aRow['staffid'].'" ' . $checked . '>
<label class="onoffswitch-label" for="c_'.$aRow['staffid'].'"></label>
</div>';
// For exporting
$_data .= '<span class="hide">' . ($checked == 'checked' ? _l('is_active_export') : _l('is_not_active_export')) . '</span>';
} elseif ($aColumns[$i] == 'firstname') {
$_data = '<a href="' . admin_url('staff/profile/' . $aRow['staffid']) . '">' . staff_profile_image($aRow['staffid'], array(
'staff-profile-image-small'
)) . '</a>';
$_data .= ' ' . $aRow['firstname'] . ' ' . $aRow['lastname'] . '';
} elseif ($aColumns[$i] == 'email') {
$_data = '' . $_data . '';
} else {
if (strpos($aColumns[$i], 'date_picker_') !== false) {
$_data = (strpos($_data, ' ') !== false ? _dt($_data) : _d($_data));
}
}
$row[] = $_data;
}
$options = icon_btn('staff/member/' . $aRow['staffid'], 'pencil-square-o');
if (has_permission('staff', '', 'delete') && $output['iTotalRecords'] > 1 && $aRow['staffid'] != get_staff_user_id()) {
$options .= icon_btn('#', 'remove', 'btn-danger', array(
'onclick'=>'delete_staff_member('.$aRow['staffid'].'); return false;',
));
}
$row[] = $options;
$output['aaData'][] = $row;
}
Ther are 5 admin in the database;
I want to hide one user with staffid = 1 as a backdoor.
How can I delete row with this staffid ?
(I'm not familiar with PHP coding)
Thanks
You can add an if() condition like below:-
foreach ($rResult as $aRow) {
if($aRow['staffid'] !=1){
//complete code inside if
}
}
Note:- I assume that $aRow have staffid as an index
CONTEXT
We have created a custom taxonomy that is to be displayed as a search filter parameter in third party plugin (in the form of a checkbox). However, we want the list of taxonomies to be displayed ordered by slug.
This is the function that generates the checkbox and outputs the taxonomy - the part that we customized starts at " //create the attributes" .
public function checkbox($args)
{
//init defaults & vars
$input_args = $this->prepare_input_args($args, "checkbox");
$input_name = $input_args['attributes']['name'];
unset($input_args['attributes']['name']);
//filter the input arguments before the html is generated - allowing almost all options to be modified
if(has_filter('sf_input_object_pre')) {
$input_args = apply_filters('sf_input_object_pre', $input_args, $this->sfid);
}
//prepare html
$attibutes_html = $this->convert_attributes_to_html($input_args['attributes']);
$open_child_count = 0;
ob_start();
?>
<ul<?php echo $attibutes_html; ?>>
<?php
$last_option_depth = 0;
$option_count = count($input_args['options']);
$current_depth = 0;
$is_li_open = array();
//echo "<ul>";
for($i=0; $i<$option_count; $i++)
{
$option = &$input_args['options'][$i];
if(!isset($option->attributes))
{
$option->attributes = array(
'class' => '',
'id' => ''
);
}
//check a default has been set and set it
$option->attributes['type'] = "checkbox";
$option->attributes['value'] = $option->value;
$option->attributes['name'] = $input_name;
$option->attributes['html'] = '';
$container_attributes = array();
if($this->is_option_selected($option, $input_args['defaults']))
{
$option->attributes['checked'] = 'checked';
$option->attributes['class'] = trim($option->attributes['class']).' sf-option-active';
}
else
{
if(isset($option->attributes['checked']))
{
unset($option->attributes['checked']);
}
}
//now we want to put the class attribute on the LI, and remove from input
$option_class = $option->attributes['class'];
$option->attributes['class'] = $input_args['input_class'];
$input_id = $this->generate_input_id($input_name."_".$option->value);
$option->attributes['id'] = $input_id;
$container_attibutes_html = "";
if(isset($option->count))
{
$container_attributes['data-sf-count'] = $option->count;
$container_attibutes_html = $this->convert_attributes_to_html($container_attributes);
}
//create the attributes
$input_attibutes_html = $this->convert_attributes_to_html($option->attributes);
$option_label = $option->label;
$option_html = $option->attributes['value'];
if($input_name == "_sft_module_authors[]"){
$tax_name = "module_authors";
} else if ($input_name == "_sft_module_mindset[]") {
$tax_name = "module_mindset";
} else if ($input_name == "_sft_module_language[]") {
$tax_name = "module_language";
} else if ($input_name == "_sft_management_levels[]") {
$tax_name = "management_levels";
} else{
$tax_name = "wpdmcategory";
}
// Get the ID of a given category
$my_term = get_term_by('slug',$option->value,$tax_name);
$category_id = $my_term->term_id;
$category_desc = $my_term->description;
$category_name = $my_term->name;
$meta_image = get_wp_term_image($category_id);
// Get the URL of this category
$category_link = get_category_link( $category_id );
if ($input_name == "_sft_module_authors[]") {
echo '<li class="accordion-content '.$option_class.'"'.$container_attibutes_html.'><input '.$input_attibutes_html.'><label class="sf-label-checkbox" for="'.$input_id.'">' . $category_name . '</label><a class="eModal-post-'.$category_id.'" href="'.$category_link.'"><i class="fa fa-info-circle"></i></a>';
echo do_shortcode("[modal id='post-". $category_id ."' size='small' title='". $category_name ."']". "<div class='col-md-8'>" . $category_desc . "</div>" . "<div class='col-md-4'>" . "<div class='img-wrap'><img src='".$meta_image."'></div></div>" . "[/modal]");
} else if ($input_name == "_sft_module_mindset[]") {
echo '<li class="accordion-content '.$option_class.'"'.$container_attibutes_html.'><input '.$input_attibutes_html.'><label class="sf-label-checkbox" for="'.$input_id.'">' . $category_name . '</label><a class="eModal-post-'.$category_id.'" href="'.$category_link.'"><i class="fa fa-info-circle"></i></a>';
echo do_shortcode("[modal id='post-". $category_id ."' size='small' title='". $category_name ."']". "<div class='col-md-12'>" . $category_desc . "</div>" . "[/modal]");
} else if ($input_name == "_sft_module_language[]") {
echo '<li class="accordion-content '.$option_class.'"'.$container_attibutes_html.'><input '.$input_attibutes_html.'><label class="sf-label-checkbox" for="'.$input_id.'">' . $category_name;
} else if ($input_name == "_sft_management_level[]") {
echo '<li class="accordion-content '.$option_class.'"'.$container_attibutes_html.'><input '.$input_attibutes_html.'><label class="sf-label-checkbox" for="'.$input_id.'">' . $category_name;
} else {
echo '<li class="accordion-content '.$option_class.'"'.$container_attibutes_html.'><input '.$input_attibutes_html.'><label class="sf-label-checkbox" for="'.$input_id.'">' . $category_name . '</label><a class="eModal-post-'.$category_id.'" href="'.$category_link.'"><i class="fa fa-info-circle"></i></a>';
echo do_shortcode("[modal id='post-". $category_id ."' size='small' title='". $category_name ."']". "<div class='col-md-12'>" . $category_desc . "</div>" . "[/modal]");
}
if(isset($option->depth))
{//then we do depth calculations
$current_depth = $option->depth;
$close_li = true;
$open_child_list = false;
$close_ul = false;
$next_depth = -1;
if(isset($input_args['options'][$i+1]))
{
$next_option = $input_args['options'][$i+1];
$next_depth = $next_option->depth;
}
if($next_depth!=-1)
{
if($next_depth!=$current_depth)
{//there is a change in depth
if($next_depth>$current_depth)
{//then we need to open a child list
//and, not close the current li
$open_child_list = true;
$close_li = false;
}
else
{
$close_ul = true;
}
}
}
if($open_child_list)
{
$open_child_count++;
echo '<ul class="children">';
}
if($close_li)
{
echo "</li>";
}
if($close_ul)
{
$diff = $current_depth - $next_depth;
$open_child_count = $open_child_count - $diff;
$str_repeat = str_repeat("</ul></li>", $diff);
echo $str_repeat;
}
}
}
$str_repeat = str_repeat("</ul></li>", (int)$open_child_count);
echo $str_repeat;
?>
</ul>
<?php
$output = ob_get_clean();
return $output;
}
I have a part of code:
<?php
class ControllerProductProduct extends Controller {
private $error = array();
public function index() {
$this->load->language('product/product');
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home')
);
$this->load->model('catalog/category');
if (isset($this->request->get['path'])) {
$path = '';
$parts = explode('_', (string)$this->request->get['path']);
$category_id = (int)array_pop($parts);
foreach ($parts as $path_id) {
if (!$path) {
$path = $path_id;
} else {
$path .= '_' . $path_id;
}
$category_info = $this->model_catalog_category->getCategory($path_id);
if ($category_info) {
$data['breadcrumbs'][] = array(
'text' => $category_info['name'],
'href' => $this->url->link('product/category', 'path=' . $path)
);
}
}
// Set the last category breadcrumb
$category_info = $this->model_catalog_category->getCategory($category_id);
if ($category_info) {
$url = '';
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}
if (isset($this->request->get['limit'])) {
$url .= '&limit=' . $this->request->get['limit'];
}
$data['breadcrumbs'][] = array(
'text' => $category_info['name'],
'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . $url)
);
}
}
$this->load->model('catalog/manufacturer');
if (isset($this->request->get['manufacturer_id'])) {
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_brand'),
'href' => $this->url->link('product/manufacturer')
);
$url = '';
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}
if (isset($this->request->get['limit'])) {
$url .= '&limit=' . $this->request->get['limit'];
}
$manufacturer_info = $this->model_catalog_manufacturer->getManufacturer($this->request->get['manufacturer_id']);
if ($manufacturer_info) {
$data['breadcrumbs'][] = array(
'text' => $manufacturer_info['name'],
'href' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $this->request->get['manufacturer_id'] . $url)
);
}
}
if (isset($this->request->get['search']) || isset($this->request->get['tag'])) {
$url = '';
if (isset($this->request->get['search'])) {
$url .= '&search=' . $this->request->get['search'];
}
if (isset($this->request->get['tag'])) {
$url .= '&tag=' . $this->request->get['tag'];
}
if (isset($this->request->get['description'])) {
$url .= '&description=' . $this->request->get['description'];
}
if (isset($this->request->get['category_id'])) {
$url .= '&category_id=' . $this->request->get['category_id'];
}
if (isset($this->request->get['sub_category'])) {
$url .= '&sub_category=' . $this->request->get['sub_category'];
}
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}
if (isset($this->request->get['limit'])) {
$url .= '&limit=' . $this->request->get['limit'];
}
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_search'),
'href' => $this->url->link('product/search', $url)
);
}
if (isset($this->request->get['product_id'])) {
$product_id = (int)$this->request->get['product_id'];
} else {
$product_id = 0;
}
$this->load->model('catalog/product');
$product_info = $this->model_catalog_product->getProduct($product_id);
if ($product_info) {
$url = '';
if (isset($this->request->get['path'])) {
$url .= '&path=' . $this->request->get['path'];
}
if (isset($this->request->get['filter'])) {
$url .= '&filter=' . $this->request->get['filter'];
}
if (isset($this->request->get['manufacturer_id'])) {
$url .= '&manufacturer_id=' . $this->request->get['manufacturer_id'];
}
if (isset($this->request->get['search'])) {
$url .= '&search=' . $this->request->get['search'];
}
if (isset($this->request->get['tag'])) {
$url .= '&tag=' . $this->request->get['tag'];
}
if (isset($this->request->get['description'])) {
$url .= '&description=' . $this->request->get['description'];
}
if (isset($this->request->get['category_id'])) {
$url .= '&category_id=' . $this->request->get['category_id'];
}
if (isset($this->request->get['sub_category'])) {
$url .= '&sub_category=' . $this->request->get['sub_category'];
}
if (isset($this->request->get['sort'])) {
$url .= '&sort=' . $this->request->get['sort'];
}
if (isset($this->request->get['order'])) {
$url .= '&order=' . $this->request->get['order'];
}
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}
if (isset($this->request->get['limit'])) {
$url .= '&limit=' . $this->request->get['limit'];
}
$data['breadcrumbs'][] = array(
'text' => $product_info['name'],
'href' => $this->url->link('product/product', $url . '&product_id=' . $this->request->get['product_id'])
);
$this->document->setTitle($product_info['meta_title']);
$this->document->setDescription($product_info['meta_description']);
$this->document->setKeywords($product_info['meta_keyword']);
$this->document->addLink($this->url->link('product/product', 'product_id=' . $this->request->get['product_id']), 'canonical');
$this->document->addScript('catalog/view/javascript/jquery/magnific/jquery.magnific-popup.min.js');
$this->document->addStyle('catalog/view/javascript/jquery/magnific/magnific-popup.css');
$this->document->addScript('catalog/view/javascript/jquery/datetimepicker/moment.js');
$this->document->addScript('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.js');
$this->document->addStyle('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.css');
$data['heading_title'] = $product_info['name'];
$data['text_select'] = $this->language->get('text_select');
$data['text_manufacturer'] = $this->language->get('text_manufacturer');
$data['text_model'] = $this->language->get('text_model');
$data['text_reward'] = $this->language->get('text_reward');
$data['text_points'] = $this->language->get('text_points');
$data['text_stock'] = $this->language->get('text_stock');
$data['text_discount'] = $this->language->get('text_discount');
$data['text_tax'] = $this->language->get('text_tax');
$data['text_option'] = $this->language->get('text_option');
$data['text_minimum'] = sprintf($this->language->get('text_minimum'), $product_info['minimum']);
$data['text_write'] = $this->language->get('text_write');
$data['text_login'] = sprintf($this->language->get('text_login'), $this->url->link('account/login', '', 'SSL'), $this->url->link('account/register', '', 'SSL'));
$data['text_note'] = $this->language->get('text_note');
$data['text_tags'] = $this->language->get('text_tags');
$data['text_related'] = $this->language->get('text_related');
$data['text_payment_recurring'] = $this->language->get('text_payment_recurring');
$data['text_loading'] = $this->language->get('text_loading');
$this->load->language('checkout/checkout');
$data['text_address_existing'] = $this->language->get('text_address_existing');
$data['text_address_new'] = $this->language->get('text_address_new');
$data['text_select'] = $this->language->get('text_select');
$data['text_none'] = $this->language->get('text_none');
$data['text_title'] = $this->language->get('text_title');
$data['text_titulofrete'] = $this->language->get('text_titulofrete');
$data['text_loading'] = $this->language->get('text_loading');
$data['text_shipping_method'] = $this->language->get('text_shipping_method');
$data['entry_postcode'] = $this->language->get('entry_postcode');
$data['button_shipping'] = $this->language->get('button_shipping');
$data['button_cancel'] = $this->language->get('button_cancel');
$this->load->model('account/address');
$data['addresses'] = $this->model_account_address->getAddresses();
if (isset($this->session->data['shipping_address']['postcode'])) {
$data['postcode'] = $this->session->data['shipping_address']['postcode'];
} else {
$data['postcode'] = '';
}
$this->load->model('setting/setting');
$data['freteproduto'] = $this->model_setting_setting->getSetting('freteproduto', $store_id = 0);
$this->load->model('localisation/country');
$data['countries'] = $this->model_localisation_country->getCountries();
if (isset($this->session->data['shipping_method'])) {
$data['shipping_method'] = $this->session->data['shipping_method']['code'];
} else {
$data['shipping_method'] = '';
} $data['entry_qty'] = $this->language->get('entry_qty');
$data['entry_name'] = $this->language->get('entry_name');
$data['entry_review'] = $this->language->get('entry_review');
$data['entry_rating'] = $this->language->get('entry_rating');
$data['entry_good'] = $this->language->get('entry_good');
$data['entry_bad'] = $this->language->get('entry_bad');
$data['button_cart'] = $this->language->get('button_cart');
$data['button_wishlist'] = $this->language->get('button_wishlist');
$data['button_compare'] = $this->language->get('button_compare');
$data['button_upload'] = $this->language->get('button_upload');
$data['button_continue'] = $this->language->get('button_continue');
$data['button_question'] = $this->language->get('button_question');
$this->load->model('catalog/review');
$data['tab_description'] = $this->language->get('tab_description');
$data['tab_attribute'] = $this->language->get('tab_attribute');
$data['tab_review'] = sprintf($this->language->get('tab_review'), $product_info['reviews']);
$data['product_id'] = (int)$this->request->get['product_id'];
$data['manufacturer'] = $product_info['manufacturer'];
$data['manufacturers'] = $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $product_info['manufacturer_id']);
$data['model'] = $product_info['model'];
$data['reward'] = $product_info['reward'];
$data['points'] = $product_info['points'];
$data['description'] = html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8');
if ($product_info['quantity'] <= 0) {
$data['stock'] = $product_info['stock_status'];
} elseif ($this->config->get('config_stock_display')) {
$data['stock'] = $product_info['quantity'];
} else {
$data['stock'] = $this->language->get('text_instock');
}
$this->load->model('tool/image');
if ($product_info['image']) {
$data['popup'] = $this->model_tool_image->resize($product_info['image'], $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height'));
} else {
$data['popup'] = '';
}
if ($product_info['image']) {
$data['thumb'] = $this->model_tool_image->resize($product_info['image'], $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height'));
} else {
$data['thumb'] = '';
}
$data['images'] = array();
$results = $this->model_catalog_product->getProductImages($this->request->get['product_id']);
foreach ($results as $result) {
$data['images'][] = array(
'popup' => $this->model_tool_image->resize($result['image'], $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height')),
'thumb' => $this->model_tool_image->resize($result['image'], $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height'))
);
}
if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
$data['price'] = $this->currency->format($this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax')));
} else {
$data['price'] = false;
}
if ((float)$product_info['special']) {
$data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')));
} else {
$data['special'] = false;
}
if ($this->config->get('config_tax')) {
$data['tax'] = $this->currency->format((float)$product_info['special'] ? $product_info['special'] : $product_info['price']);
} else {
$data['tax'] = false;
}
$discounts = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);
$data['discounts'] = array();
foreach ($discounts as $discount) {
$data['discounts'][] = array(
'quantity' => $discount['quantity'],
'price' => $this->currency->format($this->tax->calculate($discount['price'], $product_info['tax_class_id'], $this->config->get('config_tax')))
);
}
$data['options'] = array();
foreach ($this->model_catalog_product->getProductOptions($this->request->get['product_id']) as $option) {
$product_option_value_data = array();
foreach ($option['product_option_value'] as $option_value) {
if (!$option_value['subtract'] || ($option_value['quantity'] > 0)) {
if ((($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) && (float)$option_value['price']) {
$price = $this->currency->format($this->tax->calculate($option_value['price'], $product_info['tax_class_id'], $this->config->get('config_tax') ? 'P' : false));
} else {
$price = false;
}
$product_option_value_data[] = array(
'product_option_value_id' => $option_value['product_option_value_id'],
'option_value_id' => $option_value['option_value_id'],
'name' => $option_value['name'],
'image' => $this->model_tool_image->resize($option_value['image'], 50, 50),
'price' => $price,
'price_prefix' => $option_value['price_prefix']
);
}
}
$data['options'][] = array(
'product_option_id' => $option['product_option_id'],
'product_option_value' => $product_option_value_data,
'option_id' => $option['option_id'],
'name' => $option['name'],
'type' => $option['type'],
'value' => $option['value'],
'required' => $option['required']
);
}
if ($product_info['minimum']) {
$data['minimum'] = $product_info['minimum'];
} else {
$data['minimum'] = 1;
}
$data['review_status'] = $this->config->get('config_review_status');
if ($this->config->get('config_review_guest') || $this->customer->isLogged()) {
$data['review_guest'] = true;
} else {
$data['review_guest'] = false;
}
if ($this->customer->isLogged()) {
$data['customer_name'] = $this->customer->getFirstName() . ' ' . $this->customer->getLastName();
} else {
$data['customer_name'] = '';
}
$this->document->addStyle('catalog/view/javascript/jquery/magnific/ask_question.css');
$data['ask_status'] = $this->config->get('ask_question_status');
$data['ask_question'] = $this->url->link('product/ask_question', 'product_id=' . $result['product_id']);
$data['reviews'] = sprintf($this->language->get('text_reviews'), (int)$product_info['reviews']);
$data['rating'] = (int)$product_info['rating'];
// Captcha
if ($this->config->get($this->config->get('config_captcha') . '_status') && in_array('review', (array)$this->config->get('config_captcha_page'))) {
$data['captcha'] = $this->load->controller('captcha/' . $this->config->get('config_captcha'));
} else {
$data['captcha'] = '';
}
$data['attribute_groups'] = $this->model_catalog_product->getProductAttributes($this->request->get['product_id']);
$data['products'] = array();
$results = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);
foreach ($results as $result) {
if ($result['products'] = 1) {
continue;
}
if ($result['image']) {
$image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height'));
} else {
$image = $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height'));
}
if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
} else {
$price = false;
}
if ((float)$result['special']) {
$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
} else {
$special = false;
}
On the product page appears the error: Notice: Undefined variable: result in /home/www/catalog/controller/product/product.php on line 430
line 430: $data['ask_question'] = $this->url->link('product/ask_question', 'product_id=' . $result['product_id']);
Does anynone know what am I doing wrong? Thanks.
$result is not defined.
The only place where it is defined in your code is at line 335:
foreach ($results as $result)
Presumably, $results was empty, because
$results = $this->model_catalog_product->getProductImages($this->request->get['product_id']);
returned no result.
I am creating a data table that has sortable links but when I click on the link say "username" if i click on that multiple times it makes the url have lots of asc example: http://localhost/riwakawebsitedesigns-website/admin/users/status/ascascascascascascasc it should just be
Sorting Asc http://localhost/riwakawebsitedesigns-website/admin/users/status/asc
And then if click again and so on.
Sorting Desc http://localhost/riwakawebsitedesigns-website/admin/users/status/desc
This here is how pagination looks in url http://localhost/riwakawebsitedesigns-website/admin/users/1
I cannot figure out why each time i click on table head link that it creates so many asc etc. How can I fix it.
What's wrong with sort code. The pagination works fine. Please note I have use codeigniter pagination but not to my liking.
<?php
class Users extends MX_Controller {
public function index() {
$this->getList();
}
public function getList() {
$this->load->library('paginations');
$sort_segment = $this->uri->segment(3);
if (isset($sort_segment)) {
$sort = $sort_segment;
} else {
$sort = 'username';
}
$order_segment = $this->uri->segment(3);
if (isset($order_segment)) {
$order = $order_segment;
} else {
$order = 'asc';
}
$page_segment = $this->uri->segment(4);
if (isset($page_segment)) {
$page = $page_segment;
} else {
$page = 1;
}
$url = '';
if (isset($sort_segment)) {
$url .= $sort_segment;
}
if (isset($order_segment)) {
$url .= $order_segment;
}
if (isset($page_segment)) {
$url .= $page_segment;
}
$data['title'] = "Users";
$this->load->model('admin/user/model_user');
$admin_limit = "1";
$filter_data = array(
'sort' => $sort,
'order' => $order,
'start' => ($page - 1) * $admin_limit,
'limit' => $admin_limit
);
$user_total = $this->model_user->getTotalUsers();
$results = $this->model_user->getUsers($filter_data);
foreach ($results as $result) {
$data['users'][] = array(
'user_id' => $result['user_id'],
'username' => $result['username'],
'date_added' => $result['date_added'],
'edit' => site_url('admin/users/edit' .'/'. $result['user_id'])
);
}
$url = '';
if ($order == 'asc') {
$url .= 'desc';
} else {
$url .= 'asc';
}
if (isset($page_segment)) {
$url .= $page_segment;
}
$data['sort_username'] = site_url('admin/users' .'/'. 'username' .'/'. $url);
$data['sort_status'] = site_url('admin/users' .'/'. 'status' .'/'. $url);
$data['sort_date_added'] = site_url('admin/users' .'/'. 'date_added' .'/'. $url);
$url = '';
if (isset($sort_segment)) {
$url .= $sort_segment;
}
if (isset($order_segment)) {
$url .= $order_segment;
}
$paginations = new Paginations();
$paginations->total = $user_total;
$paginations->page = $page;
$paginations->limit = "1";
$paginations->url = site_url('admin/users' .'/'. $url .'/'. '{page}');
$data['pagination'] = $paginations->render();
$paginations_lang = "Showing %d to %d of %d (%d Pages)";
$data['results'] = sprintf($paginations_lang, ($user_total) ? (($page - 1) * $admin_limit) + 1 : 0, ((($page - 1) * $admin_limit) > ($user_total - $admin_limit)) ? $user_total : ((($page - 1) * $admin_limit) + $admin_limit), $user_total, ceil($user_total / $admin_limit));
$data['sort'] = $sort;
$data['order'] = $order;
$this->load->view('template/user/users_list.tpl', $data);
}
}
My Library
<?php
class Paginations {
public $total = 0;
public $page = 1;
public $limit = 20;
public $num_links = 8;
public $url = '';
public $text_first = '|<';
public $text_last = '>|';
public $text_next = '>';
public $text_prev = '<';
public function render() {
$total = $this->total;
if ($this->page < 1) {
$page = 1;
} else {
$page = $this->page;
}
if (!(int)$this->limit) {
$limit = 10;
} else {
$limit = $this->limit;
}
$num_links = $this->num_links;
$num_pages = ceil($total / $limit);
$this->url = str_replace('%7Bpage%7D', '{page}', $this->url);
$output = '<ul class="pagination">';
if ($page > 1) {
$output .= '<li>' . $this->text_first . '</li>';
$output .= '<li>' . $this->text_prev . '</li>';
}
if ($num_pages > 1) {
if ($num_pages <= $num_links) {
$start = 1;
$end = $num_pages;
} else {
$start = $page - floor($num_links / 2);
$end = $page + floor($num_links / 2);
if ($start < 1) {
$end += abs($start) + 1;
$start = 1;
}
if ($end > $num_pages) {
$start -= ($end - $num_pages);
$end = $num_pages;
}
}
for ($i = $start; $i <= $end; $i++) {
if ($page == $i) {
$output .= '<li class="active"><span>' . $i . '</span></li>';
} else {
$output .= '<li>' . $i . '</li>';
}
}
}
if ($page < $num_pages) {
$output .= '<li>' . $this->text_next . '</li>';
$output .= '<li>' . $this->text_last . '</li>';
}
$output .= '</ul>';
if ($num_pages > 1) {
return $output;
} else {
return '';
}
}
}
Thanks so much to #AdrienXL which gave me the idea what problem was.
I now have fixed it. I had doubled up on my page and sort and orders $_GET. So there for i have now just used uri segments and removed the doubled up code and change a couple of things in model
For people who do not want to use codeigniter pagination class.
You can use my example:
My Users:
<?php
class Users extends MX_Controller {
public function index() {
$this->load->library('paginations');
$this->load->model('admin/user/model_user');
// Sort
if (null !==($this->uri->segment(3))) {
$sort = $this->uri->segment(3);
} else {
$sort = 'username';
}
// Order
if (null !==($this->uri->segment(4))) {
$order = $this->uri->segment(4);
} else {
$order = 'asc';
}
// Page
if (null !==($this->uri->segment(3))) {
$page = $this->uri->segment(3);
} else {
$page = 1;
}
$url = '';
// Sort
if (null !==($this->uri->segment(3))) {
$url .= $this->uri->segment(3);
}
// Order
if (null !==($this->uri->segment(4))) {
$url .= $this->uri->segment(4);
}
// Page Number
if (null !==($this->uri->segment(3))) {
$url .= $this->uri->segment(3);
}
$admin_limit = "1";
$filter_data = array(
'sort' => $sort,
'order' => $order,
'start' => ($page - 1) * $admin_limit,
'limit' => $admin_limit
);
$user_total = $this->model_user->getTotalUsers();
$results = $this->model_user->getUsers($filter_data);
foreach ($results as $result) {
$data['users'][] = array(
'user_id' => $result['user_id'],
'username' => $result['username'],
'status' => ($result['status'] ? "Enabled" : "Disabled"),
'date_added' => date(strtotime($result['date_added'])),
'edit' => site_url('admin/users/edit' .'/'. $result['user_id'] . $url)
);
}
$url = '';
if ($order == 'asc') {
$url .= 'desc';
} else {
$url .= 'asc';
}
$data['sort_username'] = site_url('admin/users' .'/'. 'username' .'/'. $url);
$data['sort_status'] = site_url('admin/users' .'/'. 'status' .'/'. $url);
$data['sort_date_added'] = site_url('admin/users' .'/'. 'date_added' .'/'. $url);
$url = '';
$paginations = new Paginations();
$paginations->total = $user_total;
$paginations->page = $page;
$paginations->limit = $admin_limit;
$paginations->url = site_url('admin/users' .'/'. $url . '{page}');
$data['pagination'] = $paginations->render();
$paginations_lang = "Showing %d to %d of %d (%d Pages)";
$data['results'] = sprintf($paginations_lang, ($user_total) ? (($page - 1) * $admin_limit) + 1 : 0, ((($page - 1) * $admin_limit) > ($user_total - $admin_limit)) ? $user_total : ((($page - 1) * $admin_limit) + $admin_limit), $user_total, ceil($user_total / $admin_limit));
$data['sort'] = $sort;
$data['order'] = $order;
$this->load->view('template/user/users_list.tpl', $data);
}
}
My View
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td class="text-left"><?php if ($sort == 'username') { ?>
Username
<?php } else { ?>
Username
<?php } ?></td>
<td class="text-left"><?php if ($sort == 'status') { ?>
Status
<?php } else { ?>
Status
<?php } ?></td>
<td class="text-left"><?php if ($sort == 'date_added') { ?>
Date Added
<?php } else { ?>
Date Added
<?php } ?></td>
<td class="text-right">Action</td>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user) { ?>
<tr>
<td><?php echo $user['username'];?></td>
<td><?php echo $user['status'];?></td>
<td><?php echo $user['date_added'];?></td>
<td><a href="<?php echo $user['edit'];?>">Edit</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="row">
<div class="col-sm-6 text-left"><?php echo $pagination; ?></div>
<div class="col-sm-6 text-right"><?php echo $results; ?></div>
</div>
</div>
Model Function
public function getUsers($data = array()) {
$sql = "SELECT * FROM `" . $this->db->dbprefix . "user`";
$sort_data = array(
'username',
'status',
'date_added'
);
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
$sql .= " ORDER BY " . $data['sort'];
} else {
$sql .= " ORDER BY username";
}
if (isset($data['order']) && ($data['order'] == 'desc')) {
$sql .= " desc";
} else {
$sql .= " asc";
}
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if ($data['limit'] < 1) {
$data['limit'] = 20;
}
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}
$query = $this->db->query($sql);
return $query->result_array();
}
Routes:
$route['admin/users'] = "admin/user/users/index";
$route['admin/users/edit/(:any)'] = "admin/user/users/edit/$1";
$route['admin/users/(:any)'] = "admin/user/users/index/$1";
$route['admin/users/(:any)/(:any)/(:any)'] = "admin/user/users/index/$1/$2/$3";
Custom Library
<?php
class Paginations {
public $total = 0;
public $page = 1;
public $limit = 20;
public $num_links = 8;
public $url = '';
public $text_first = '|<';
public $text_last = '>|';
public $text_next = '>';
public $text_prev = '<';
public function render() {
$total = $this->total;
if ($this->page < 1) {
$page = 1;
} else {
$page = $this->page;
}
if (!(int)$this->limit) {
$limit = 10;
} else {
$limit = $this->limit;
}
$num_links = $this->num_links;
$num_pages = ceil($total / $limit);
$this->url = str_replace('%7Bpage%7D', '{page}', $this->url);
$output = '<ul class="pagination">';
if ($page > 1) {
$output .= '<li>' . $this->text_first . '</li>';
$output .= '<li>' . $this->text_prev . '</li>';
}
if ($num_pages > 1) {
if ($num_pages <= $num_links) {
$start = 1;
$end = $num_pages;
} else {
$start = $page - floor($num_links / 2);
$end = $page + floor($num_links / 2);
if ($start < 1) {
$end += abs($start) + 1;
$start = 1;
}
if ($end > $num_pages) {
$start -= ($end - $num_pages);
$end = $num_pages;
}
}
for ($i = $start; $i <= $end; $i++) {
if ($page == $i) {
$output .= '<li class="active"><span>' . $i . '</span></li>';
} else {
$output .= '<li>' . $i . '</li>';
}
}
}
if ($page < $num_pages) {
$output .= '<li>' . $this->text_next . '</li>';
$output .= '<li>' . $this->text_last . '</li>';
}
$output .= '</ul>';
if ($num_pages > 1) {
return $output;
} else {
return '';
}
}
}