Breadcrumbs links not showing correct url - codeigniter

I am creating some bread crumbs for my folders. When I click on the 2nd bread crumb link it does not go to the correct URL
The previous URL shows "test"
http://localhost/project/admin/common/test?directory=test/sub
It should show
http://localhost/project/admin/common/test?directory=test
Inspector Output
<ul class="breadcrumb">
<li>Catalog</li>
<li>test</li>
<li>sub</li>
</ul>
On view
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><?php echo $breadcrumb['text']; ?></li>
<?php } ?>
</ul>
Question: How can I make sure the previous URL to shows the before folder correct URL.
I generate my breadcrumbs on my controller
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => 'Catalog',
'href' => base_url('admin/common/test')
);
$directory_names = explode('/', $this->input->get('directory'));
foreach ($directory_names as $directory_name) {
$data['breadcrumbs'][] = array(
'text' => $directory_name,
'href' => base_url('admin/common/test?directory=' . $this->input->get('directory'))
);
}

Thanks to a solution now I found on another forum I will post on here to share
https://forum.codeigniter.com/thread-68653-post-346110.html#pid346110
$directory_names = explode('/', $this->input->get('directory'));
$directory_done = '';
foreach ($directory_names as $directory_name) {
$directory_done .= ($directory_done <> ''? '/':'').$directory_name;
$data['breadcrumbs'][] = array(
'text' => $directory_name,
'href' => base_url('admin/common/test?directory=' . $directory_done)
);
}

Related

How to make sure that foreach loop data shows in correct set position views

I have a controller that shows modules for my each position
Array
(
[0] => Array
(
[layout_module_id] => 1
[layout_id] => 1
[module_id] => 1
[position] => column_left
[sort_order] => 1
)
[1] => Array
(
[layout_module_id] => 2
[layout_id] => 1
[module_id] => 2
[position] => column_left
[sort_order] => 2
)
)
Above currently I have only two modules set and the are in the position of column left.
Because the position views are out side of the foreach loop they are picking up that module even though not set for that position? As shown in image.
Question: How can I make sure that the module will only display in its set position view.
public function index() {
$layout_id = $this->getlayoutID($this->router->class);
$modules = $this->getlayoutsmodule($layout_id);
echo '<pre>';
print_r($modules);
echo "</pre>";
$data['modules'] = array();
foreach ($modules as $module) {
$this->load->library('module/question_help');
$data['modules'][] = $this->load->view('module/question_help', $this->question_help->set(), TRUE);
}
// Position Views
$data['column_left'] = $this->load->view('column_left', $data, TRUE);
$data['column_right'] = $this->load->view('column_right', $data, TRUE);
$data['content_top'] = $this->load->view('content_top', $data, TRUE);
$data['content_bottom'] = $this->load->view('content_bottom', $data, TRUE);
// Main view
$this->load->view('welcome_message', $data);
}
public function getlayoutsmodule($layout_id) {
$this->db->select('*');
$this->db->from('layouts_module');
$this->db->where('layout_id', $layout_id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
}
}
Each of the position views have the same foreach loop
<?php if ($modules) { ?>
<?php foreach ($modules as $module) { ?>
<?php echo $module;?>
<?php } ?>
<?php }?>
main view
<div class="container">
<div class="row">
<?php echo $column_left; ?>
<?php if ($column_left && $column_right) { ?>
<?php $class = 'col-sm-6'; ?>
<?php } elseif ($column_left || $column_right) { ?>
<?php $class = 'col-sm-9'; ?>
<?php } else { ?>
<?php $class = 'col-sm-12'; ?>
<?php } ?>
<div id="content" class="<?php echo $class; ?>">
<?php echo $content_top; ?>
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/Welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
<?php echo $content_bottom; ?>
</div>
<?php echo $column_right; ?></div>
</div>
I would do it like this (not sure if the library needs to be loaded in each iteration but leaving it there):
$tmpdata = array();
foreach ($modules as $module) {
$this->load->library('module/question_help');
$tmpdata[$module['position']]['modules'][] = $this->load->view('module/question_help', $this->question_help->set(), TRUE);
}
$data = array();
foreach ($tmpdata as $pos => $mods) {
$data[$pos] = $this->load->view($pos, $tmpdata[$pos], TRUE);
}
$this->load->view('welcome_message', $data);
Passing $tmpdata[$pos] to each position's view makes the array called $modules available, so in the views, just use
if ($modules) {
foreach ($modules as $module) {
echo $module;
}
}
as before.
This keeps it dynamic and you don't have to modify this code if you change or add positions. I'm using two data arrays just to avoid passing unnecessary data.

Clear or reset the wordpress posts pagination while changing filters

I think it is simple, but I don't get it.
This is my filter:
<form class='post-filters'>
<select name="filter">
<?php
$filter_options = array(
'houses' => 'Houses',
'hotels' => 'Hotels',
);
foreach( $filter_options as $value => $label ) {
echo "<option ".selected( $_GET['filter'], $value )." value='$value'>$label</option>";
}
?>
</select>
<input type='submit' value='Filter!'>
</form>
Related PHP to apply the filter to the wordpress query:
<?php
global $destinations;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$destinations = new WP_Query([
'paged' => $paged,
'location' => $location,
'category_name' => urldecode(get_query_var('filter')),
'posts_per_page' => 6
]);
?>
If I do select my "filter" and the result has more than six entries, I use next_posts_link() to see the next six results. The problem is now, if I'm on page 2 or 3 and the other filter has less than e.g. 6 entries, I will see no results while changing my filter.
How do I clear the get variable (/page/2/) while changing my filter?
Example:
category/subcategory/subsubcategory/page/3/?filter=houses
Now I select "filter" hotels
category/subcategory/subsubcategory/page/3/?filter=hotels
and the "/page/3" will not be cleared. So I can not see some posts.
It has been anwsered here:
https://wordpress.stackexchange.com/a/264266
function get_nopaging_url() {
$current_url = $_SERVER[REQUEST_URI];
$pattern = '/page\\/[0-9]+\\//i';
$nopaging_url = preg_replace($pattern, '', $current_url);
return $nopaging_url;
}
You could use this function to remove the pagination in your urls with filters.
See example:
<a href="<?php echo get_nopaging_url(); ?>?filter=houses">

Saving data from a drop down list in CodeIgniter

I created a menu page where it has a drop down menu with a list of menus from the database and it also has a textbox to enter new menus.
The problem I'm having is that I can't seem to figure out how to save my dropdown. So for example I have a menu called "About Us" in the drop down list and I want to create a new menu called "Team", and "Team" is a child of "About Us"
So in my table I would have something like this
id | parent | title
------------------------
1 | NULL | About Us
2 | 1 | Team
Menu Controller
function get_data_from_post()
{
$data['title'] = $this->input->post('title', TRUE);
$data['parent'] = $this->input->post('parent', TRUE);
if(!isset($data)){
$data = '';
}
return $data;
}
function get_data_from_db($update_id)
{
$query = $this->get_where($update_id);
foreach($query->result() as $row){
$data['title'] = $row->title;
$data['parent'] = $row->parent;
}
return $data;
}
function create()
{
$update_id = $this->uri->segment(3);
$submit = $this->input->post('submit', TRUE);
if($submit == "Submit"){
//person has submitted the form
$data = $this->get_data_from_post();
}else{
if(is_numeric($update_id)){
$data = $this->get_data_from_db($update_id);
}
}
if(!isset($data)){
$data = $this->get_data_from_post();
}
//$titles = array();
$query = $this->get('title');
foreach($query->result() as $row){
$titles[] = $row->title;
}
$data['titles'] = $titles;
$data['update_id'] = $update_id;
$data['view_file'] = "create";
$this->load->module('templates');
$this->templates->admin_template($data);
}
function submit()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
if($this->form_validation->run($this) == FALSE){
$this->create();
}else{
$data = $this->get_data_from_post();
$update_id = $this->uri->segment(3);
if(is_numeric($update_id)){
$this->_update($update_id, $data);
}else{
$this->_insert($data);
}
redirect('menus/manage');
}
}
create.php view
<div class="row">
<div class="col-md-12">
<h2>Create Menus</h2>
<h5>Welcome Jhon Deo , Need to make dynamic. </h5>
</div>
</div>
<hr />
<?php
echo validation_errors("<p style='color: red;'>", "</p>");
echo form_open('menus/submit/'.$update_id);
?>
<div class="row">
<div class="col-md-12">
<form role="form">
<div class="form-group">
<select name="menus">
<?php
foreach($titles as $title){
echo "<option value=".$title.">".$title."</option>";
}
?>
</select>
</div>
<div class="form-group">
<label>Title</label>
<!-- <input class="form-control" /> -->
<?php
$data = array(
'name' => 'title',
'id' => 'title',
'value' => $title,
'class' => 'form-control',
);
echo form_input($data);
?>
</div>
<?php
$data = array(
'name' => 'submit',
'id' => 'submit',
'value' => 'Submit',
'class' => 'btn btn-success',
'style' => 'width: 100%',
);
echo form_submit($data);
?>
</form>
</div>
</div>
<?php
echo form_close();
?>
UPDATE:
this is what I have when I print_r($titles)
Array
(
[0] => About Us
[1] => Home
)
If there is anything you don't understand or if you need me to give more information please let me know.
You should have declared a model. From there, you can create a function that will save the values in the database that you initialize via controller. You should utilize the MVC pattern of it. CodeIgniter has a great documentation to read about what I am pointing out.. https://codeigniter.com/user_guide/overview/mvc.html?highlight=model

How to load database into helper and display in view with specific column

footer_helper
<?php
function get_gadget_footer() {
//the database functions can not be called from within the helper
//so we have to explicitly load the functions we need in to an object
//that I will call ci. then we use that to access the regular stuff.
$ci=& get_instance();
$ci->load->database();
//select the required fields from the database
//$ci->db->select('name, type, setting');
//tell the db class the criteria
$ci->db->where('display', 'Footer');
//supply the table name and get the data
$query = $ci->db->get('gadgets');
foreach($query->result() as $row):
$type['name'] = $row->name;
$type['type'] = $row->type;
var_dump($type);
endforeach;
return $type;
}
hi this is my helper page. var_dump($type) is returning me required result in form of array as:
var_dump($type)
array (size=2)
'name' => string 'Quick Links' (length=11)
'type' => string '<a href='www.facebook.com'>Facebook</a><br>
<a href='#'>Twitter</a><br>
<a href='#'>Salyani</a><br>
<a href='#'>B&W</a><br>' (length=123)
array (size=2)
'name' => string 'Social Network' (length=14)
'type' => string '<a href='#'>Facebook</a><br>
<a href='#'>Facebook</a><br>
<a href='#'>Facebook</a><br>
<a href='#'>Facebook</a><br>' (length=115)
Now in my view page. I want display specific record in div. Like record of name
record of type .
Since now i had done following:
view
<?php
$this->load->helper('footer_helper');
$name = get_gadget_footer();
?>
<div id="footer">
<div id="footer_gadget_collection">
<?php
foreach ($name as $dat){
?>
<?php
?>
<div id="footer_subgadget">
<div id='title'><?php echo $dat['name']; ?></div>
<div id='description'><?php $dat['type']; ?> </div>
</div>
<?php
}
?>
But, its giving me error. Please help me.
In your helper you are restting the name/type of the array, not building it.
It should be something like;
<?php
//supply the table name and get the data
$query = $ci->db->get('gadgets');
$type = array();
foreach ($query->result() as $row) {
$type[] = array('name' => $row->name, 'type' => $row->type);
}
return $type;

Opencart how to display only 5 subcategory in a column

I'm new to opencart. I'm using category module to display category in left sidebar. I'm trying to add subcategory limit (say 5 in a column) same like ebay.in left categories.
My .../module/category.php file is
<?php class ControllerModuleCategory extends Controller {
protected function index($setting) {
$this->language->load('module/category');
$this->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])) {
$this->data['category_id'] = $parts[0];
} else {
$this->data['category_id'] = 0;
}
if (isset($parts[1])) {
$this->data['child_id'] = $parts[1];
} else {
$this->data['child_id'] = 0;
}
if (isset($parts[2])) {
$this->data['sisters_id'] = $parts[2];
} else {
$this->data['sisters_id'] = 0;
}
$this->load->model('catalog/category');
$this->load->model('catalog/product');
$this->data['categories'] = array();
$categories = $this->model_catalog_category->getCategories(0);
foreach ($categories as $category) {
$children_data = array();
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
$sister_data = array();
$sisters = $this->model_catalog_category->getCategories($child['category_id']);
if(count($sisters) > 1) {
foreach ($sisters as $sisterMember) {
$sister_data[] = array(
'category_id' =>$sisterMember['category_id'],
'name' => $sisterMember['name'],
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']. '_' . $sisterMember['category_id'])
);
}
$children_data[] = array(
'category_id' => $child['category_id'],
'sister_id' => $sister_data,
'name' => $child['name'],
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}else{
$children_data[] = array(
'category_id' => $child['category_id'],
'sister_id' =>'',
'name' => $child['name'],
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}
}
$data = array(
'filter_category_id' => $category['category_id'],
'filter_sub_category' => true
);
$product_total = $this->model_catalog_product->getTotalProducts($data);
$this->load->model('tool/image');
$this->data['categories'][] = array(
'category_id' => $category['category_id'],
'name' => $category['name'],
'children' => $children_data,
'sister' => $sister_data,
'category_image' => $this->model_tool_image->resize($category['image'], 20, 20),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category.tpl')) {
$this->template = $this->config->get('config_template') . '/template/module/category.tpl';
} else {
$this->template = 'default/template/module/category.tpl';
}
$this->render();
}}?>
And my ...module/category.tpl file is
<div id="menu_box">
<div class="box-heading"><?php echo $heading_title; ?></div>
<ul>
<?php foreach ($categories as $category) { ?>
<li>
<?php if ($category['category_id'] == $category_id) { ?>
<?php echo $category['name']; ?>
<?php } else { ?>
<?php echo $category['name']; ?>
<span style="float:right; font-weight:bold;">ยป</span>
<a class="menuimg" ><img src="<?php echo $category['category_image']; ?> " /></a>
<?php } ?>
<?php if ($category['children']) { ?>
<ul class="second-level" style="position: absolute;left: 166px;top:-2px;padding: 5px;margin: 0px;width:350px; background-color: whitesmoke; border:1px solid#ececec; z-index:10;">
<?php foreach ($category['children'] as $child) { ?>
<li id="second-li">
<?php if ($child['category_id'] == $child_id) { ?>
<a style="border-bottom:1px solid #5d5d5d;" href="<?php echo $child['href']; ?>" class="active"> <?php echo $child['name']; ?></a>
<?php } else { ?>
<a style="border-bottom:1px solid #5d5d5d;" href="<?php echo $child['href']; ?>"> <?php echo $child['name']; ?></a>
<?php } ?>
<?php if($child['sister_id']){ ?>
<ul>
<?php foreach($child['sister_id'] as $sisters) { ?>
<li>
<?php if ($sisters['category_id'] == $sisters_id) { ?>
<?php echo $sisters['name']; ?>
<?php } else { ?>
<?php echo $sisters['name']; ?>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>
Any suggestion to do this is much appreciated.
For main category
Ugly/Quick fix
You can add this line
$categories = array_slice($categories, 0, 5);
just below
$categories = $this->model_catalog_category->getCategories(0);
in catalog/controller/module/category.php
Better Solution
In catalog/model/catalog/category.php replicate the function getCategories($parent_id = 0) and add a second parameter limit to it. Use this parameter inside the query to limit the results returned
To limit the results in children/sub-category
Ugly-Fix
Add this line
$children = array_slice($children, 0, 5);
Just below
$children = $this->model_catalog_category->getCategories($category['category_id']);
in catalog/controller/module/category.php
Better Solution
Replicate the function getCategoriesByParentId($category_id) in catalog/model/catalog/category.php, add a second paramter limit to it, use this parameter in the SQL query.
Hope that helps!

Resources