dropdown li in codeigniter not fetching proper values from database using id - php

I have a site with a dropdown menu that shows city names and after clicking a particular city it redirects to another page that shows the city details.
My problem is that in this page the menu is showing only the current single city, but should show all of them.
here is the image showing header:
after clicking particular city this is how header is looking:
Header view code:
<ul class="dropdown-menu" role="menu">
<?php foreach ($race_details as $city) {?>
<li>
<a class="dropdown-item <?= ($this->uri->segment(2) == 'id') ? 'active' : ''; ?>" href="<?php echo base_url('city/'. $city->id); ?>">
<?= $city->location ?>
</a>
</li>
<?php
}
?>
</ul>
Controller code:
public function city() {
$data['site'] = $this->Site_model->get_site_details();
$data['page_title'] = $data['site']->site_title;
$city_id = $this->uri->segment(2);
if($city_id){
$data['race_details'] = $this->Site_model->get_race_details_by_city($city_id);
$this->load->view('city_page', $data);
}
}
Modal code:
public function get_race_details_by_city($city_id='') {
$this->db->select('*');
$this->db->where('id', $city_id);
$query = $this->db->get('race_details');
return $query->result();
}

Whenever you need dropdown of all the cities (Home page, Header page) you can use all_cities_list() function, Now whenever you need race details by city you can call get_race_details_by_city() function by passing city id in it so both will work for you
public function all_cities_list() {
$this->db->select('*');
$query = $this->db->get('cities');
return $query->result();
}
public function get_race_details_by_city($city_id='') {
$this->db->select('*');
$this->db->where('id', $city_id);
$query = $this->db->get('race_details');
return $query->result();
}

Using two header for city options it's not valid solution. You can also use below alternate solutions.
Always use all_cities_list() function for get list of all the cities list on all the pages.
Now whenever you click on city page get that city_id and pass it in controllers like
public function city() {
$data['site'] = $this->Site_model->get_site_details();
$data['page_title'] = $data['site']->site_title;
$data['city_id'] = $this->uri->segment(2);
$this->load->view('city_page', $data);
}
Now you can match your city_id in foreach loop data on header page
<ul class="dropdown-menu" role="menu">
<?php foreach ($race_details as $city) {?>
<li>
<a class="dropdown-item <?= ($this->uri->segment(2) == 'id') ? 'active' : ''; ?>" href="<?php echo base_url('city/'. $city->id); ?>" <?= if(isset($city_id)) { if($city->id == $city_id){echo 'selected';}} ?>>
<?= $city->location ?>
</a>
</li>
<?php
}
?>
</ul>
Hope this will helps you.

Related

My route somehow keep redirecting to wrong url

so
i try make a pos(point of sale) program using ci3
im trying add a feature where admin can make new menu and submenu
but something went wrong
when i goto controller "sistem"
my route show "sitename/sistem/sistem/menu" this route supposed to "sitename/sistem/menu". another controller also effected when i goto "dashboard" controller the route show
"sistem/dashboard"
this is sistem controller
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Sistem extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('M_menu');
$this->load->model('M_user');
if (!$this->session->userdata('email')) {
redirect('auth');
}
}
public function index()
{
$this->menu();
}
public function menu()
{
$data['judulpage'] = 'Manajemen Menu';
$data['datamenu'] = $this->M_menu->getmenus()->result_array();
$data['user'] = $this->M_user->getuserdata()->row_array();
$this->form_validation->set_rules('menutambah', 'Menu', 'required');
// $this->form_validation->set_message([
// 'required' => 'Field menu add harus diisi'
// ]);
if ($this->form_validation->run() == false) {
$this->load->view('template/header', $data);
$this->load->view('template/sidebar', $data);
$this->load->view('sistem/menu', $data);
$this->load->view('template/footer');
} else {
$menu['menu'] = $this->input->post('menutambah');
$this->M_menu->insertmenu($menu);
$this->session->set_flashdata('flash','Sukses');
redirect('sistem');
}
}
this is the sidebar.php i think this is the problem but i cant find the solution
<?php
$role_id = $this->session->userdata('role_id');
$queryMenu = "SELECT `user_menu`.`id`, `menu`, `icon_menu`
FROM `user_menu`
JOIN `user_access_menu` ON `user_menu`.`id` = `user_access_menu`.`menu_id`
WHERE `user_access_menu`.`role_id` = $role_id";
$menus = $this->db->query($queryMenu)->result_array();
?>
<!-- BEGIN: Main Menu-->
<div class="main-menu menu-fixed menu-dark menu-accordion menu-shadow" data-scroll-to-active="true">
<div class="main-menu-content">
<ul class="navigation navigation-main" id="main-menu-navigation" data-menu="menu-navigation">
<li class=" navigation-header"><span>MENU</span><i class=" feather icon-minus" data-toggle="tooltip" data-placement="right" data-original-title="Apps"></i>
</li>
<?php foreach($menus as $menu) : ?>
<?php
$menuid = $menu['id'];
$querysubmenu = "SELECT * FROM `user_sub_menu` WHERE `menu_id` = $menuid AND `is_active` = 1";
$submenus = $this->db->query($querysubmenu)->result_array();
?>
<li class=" nav-item"><i class="<?php echo $menu['icon_menu']; ?>"></i><span class="menu-title"><?php echo strtoupper($menu['menu']); ?></span>
<ul class="menu-content">
<?php foreach($submenus as $submenu) : ?>
<li>
<a class="menu-item" href="<?php echo $submenu['url']; ?>"><i class="<?php echo $submenu['icon']; ?>"></i><span><?php echo $submenu['title']; ?></span></a>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
i take the url from database.
u can see from inspect mode, the href redirecting to sistem/menu which is coorect url but when i click it, it goto sistem/sistem/menu
You do not need to call double <?php ?> tag.
just do this
<?php echo base_url($submenu['url']); ?>
or simplify the echo syntax with
<?= base_url($submenu['url']); ?>
fixed href="<?php echo base_url(); ?> i add / this before php tag but somehow it worked but still directing to wrong url localhost/sistem/menu it supposed to localhost/site/sistem/menu, so i add base_url to add my sitename and it work, thx
<li>
<a class="menu-item" href="<?php echo base_url(); ?><?php echo $submenu['url']; ?>">
<i class="<?php echo $submenu['icon']; ?>"></i><span><?php echo $submenu['title']; ?></span></a>
</li>

How to get multiselect checkbox data in update action and save it in junction table

I have two models (Video, Category). In my video form i am fetching the categories (from Category model) and show them as checkboxes (note that: there is no categories field inside video table). each video can have single or multiple categories. I need to save the video categories (which are to be selected inside video form) inside a junction table (video_categories). I will save the categories using afterSave() inside Video model.
My main problem is that how can i fetch my checked categories from video form inside updateAction() of VideoController, and assign them to some public member (of Video model) and then use that member in afterSave() to save the categories inside that junction table (video_categories).
One more thing is that I want to save the categories inside updateAction() only. In createAction() i am saving just the video file. all other details about video needs to be saved inside updateAction().
This is how I displayed Categories inside video form.
<ul class="catgss_list">
<?php
$category = common\models\Category::find()->all();
foreach($category as $cat):
?>
<li>
<div class="chekbox-lg">
<label>
<input type="checkbox" name="category[]" value="<?php echo $cat->id ?>">
<b class="checkmark"></b>
<span><?= $cat->category_name ?></span>
</label>
</div>
</li>
<?php endforeach; ?>
</ul>
this is how my form looks like https://ibb.co/tpZXrVw
i have solved my problem. what I did is written below:-
my Video model
public $categories;// i declare a public attribute.
public function afterSave($insert, $changedAttributes) {
parent::afterSave($insert, $changedAttributes);
$this->deleteExistingCategories();
if (isset($_POST['Video']['categories'])) {
$this->saveVideoCategories($_POST['Video']['categories']);
}
}
public function deleteExistingCategories() {
VideoCategory::deleteAll(['video_id' => $this->video_id]);
}
public function saveVideoCategories($categoriesList) {
foreach ($categoriesList as $category) {
$videoCategory = new VideoCategory();
$videoCategory->video_id = $this->video_id;
$videoCategory->category_id = $category;
$videoCategory->save();
}
}
my video form code
<ul class="catgss_list">
<?php
$checkedCategories = VideoCategory::findAll(['video_id' => $model->video_id]);
if (isset($checkedCategories)) {
foreach ($checkedCategories as $selectedCategory) {
$savedCategories[] = $selectedCategory->category_id;
}
}
$category = Category::find()->all();
foreach ($category as $cat):
?>
<li>
<div class="chekbox-lg">
<label>
<input type="checkbox" name="Video[categories][]" value="<?php echo $cat->id ?>"
<?php
if (isset($savedCategories)) {
$isCategory = in_array($cat->id, $savedCategories);
if (isset($isCategory) && $isCategory == true) {
echo "checked";
}
}
?>
>
<b class="checkmark"></b>
<span><?php echo $cat->category_name ?></span>
</label>
</div>
</li>
<?php endforeach; ?>
</ul>

Display data from three tables with common id in foreach

So I currently have three tables like this given below:
AI = auto increment.
ci_users: user_id(AI), username, slug, email, biography.
ci_terms: term_id(AI), title, slug, body.
ci_relationship: id(AI), term_id, user_id, type.
I'm trying to display all the users from a specific term_id(where type is skill),
for example, let's say term_id 3(where term_id has the title of 'CSS' from ci_terms).
To make it more clear,the above paragraph basically says show me all users with skill of CSS but I want the displayed users to show all of their other skills which are stored in the ci_relationship table.
PARAGRAPHS BELOW ARE AFTER CLICKING A SPECIFIC TERM_ID(SKILL).
This is the function which shows me all of the users that have the term_id as their skill.(from my previous Stackoverflow question. Thanks to pradeep who helped me solve that query:
// All users with specific skill
public function get_user_skill($id)
{
$this->db->select('*, count(ci_relationship.term_id)');
$this->db->from($this->table);
$this->db->join('ci_relationship', 'ci_relationship.user_id = ci_users.id', 'INNER');
$this->db->order_by('ci_relationship.user_id', 'DESC');
$this->db->group_by('ci_relationship.user_id');
$this->db->where('ci_relationship.term_id', $id);
$this->db->where('ci_relationship.type', 'skill');
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}
else
{
return false;
}
}
As I said before the code above just display the users with the specific term_id. Now this is the method in the controller which shows the expected data(users with specific term_id):
This is my controller :
public function skill($id){
// Get data
$data['users'] = $this->User_model->get_user_skill($id);
$term = $this->Terms_model->get($id);
// Get skill name
$data['skill'] = $this->Terms_model->get($id);
// Get skills per foreach --- HERE IS WHERE I NEED HELP
$data['skills'] = $this->User_model->skills($id);
// Meta
$data['title'] = $this->settings->title.' | '. ucfirst($term->title);
// Load template
$this->template->load('public', 'default', 'users/skill', $data);
}
This is the method that I'm trying to create to displayed the term_id per user_id:
// Skill for user foreach
public function skills($id){
$this->db->select('*');
$this->db->from($this->relationship);
$this->db->where('term_id', $id);
$this->db->where('type', 'skill');
$this->db->order_by('id', 'DESC');
$query = $this->db->get();
if($query->num_rows() >= 1){
return $query->result();
} else {
return false;
}
}
The view is the following code:
<?php if($users) : ?>
<div class="row">
<?php foreach($users as $user) : ?>
<div class="col-lg-3 col-sm-6">
<div class="card text-center panel">
<div class="cardheader panel-heading" style="background: url(<?php if($user->user_id) : echo base_url('assets/img/users/covers/'.get_username($user->user_id).'/'.get_username_cover($user->user_id)); else : echo base_url().'assets/img/noavatar.jpg'; endif ?>)no-repeat center center;"></div>
<div class="avatar">
<a target="_self" href="<?= base_url('users/profile/'.get_username($user->user_id)); ?>"><img class="img-circle" alt="<?= get_username($user->user_id); ?> profile picture" title="<?= get_username($user->user_id); ?> profile picture" src="<?php if($user->user_id) : echo base_url('assets/img/users/avatars/'.get_username($user->user_id).'/'.get_username_avatar($user->user_id)); else : echo base_url().'assets/img/noavatar.jpg'; endif ?>"></a>
</div>
<div class="info panel-body">
<div class="title">
<a target="_self" href="<?= base_url('users/profile/'.get_username($user->user_id)); ?>"><?= get_username($user->user_id); ?></a>
</div>
<div class="desc"><?php if(get_occupation($user->user_id)) : ?><?= get_occupation($user->user_id); ?><?php else : echo 'No Job'; endif ?> <?php if(get_company($user->user_id)) : ?> - <b><?= get_company($user->user_id); ?></b><?php else : echo '- <b>No Company</b>'; endif ?></div>
<!-- BEGINNING OF HERE IS WHERE I NEED HELP -->
<?php if($skills) : ?>
<?php foreach($skills as $skill) : ?>
<span class="label label-orange"><?= get_term($skill->term_id) ?></span>
<?php endforeach ?>
<?php endif ?>
<!-- END OF HERE IS WHERE I NEED HELP -->
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php else : ?>
<div class="alert alert-danger">No user with <?= $skill->title ?> skill found</div>
<?php endif; ?>
This is the actual output:
And this is the expected output:
I hope I could explain myself well enough to make it easy to understand,thanks in advance.
Hope this will help you :
Create a helper method get_skill_by_user just like get_username or get_occupation and use it in your view
function get_skill_by_user($user_id, $id)
{
$ci = & get_instance();
$ci->db->select('*');
$ci->db->from('ci_relationship');
$ci->db->where('term_id', $id);
$ci->db->where('user_id', $user_id);
$ci->db->where('type', 'skill');
$ci->db->order_by('id', 'DESC');
$query = $ci->db->get();
if($query->num_rows() > 0)
{
return $query->result();
} else
{
return false;
}
}
In your view your skills loop should be like this :
<?php $user_skills = get_skill_by_user($user->user_id, $user->term_id); if($user_skills) : ?>
<?php foreach($user_skills as $skill) : ?>
<span class="label label-orange"><?= get_term($skill->term_id) ?></span>
<?php endforeach ?>
<?php endif ?>

Category product menu bar not linking

I made a category menu bar on my website so you can click on a category and see products of that category, but its not working for me and I must be doing something wrong.
Some database information:
categories table:
Row 1: id
Row 2: name
In the products table:
Row: category_id
I used a db helper (db_helper.php)
<?php if (!function_exists('get_categories_h')) {
function get_categories_h(){
$CI = get_instance();
$categories = $CI->Product_model->get_categories();
return $categories;
} } ?>
This is the my Product_model file where I made the get_categories function:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product_model extends CI_model {
public function saveProduct($data) {
$this->db->insert('products', $data);
$product_id = $this->db->insert_id();
return $product_id;
}
public function get_product_details($product_id) {
$arrReturn = array();
$this->db->select('*');
$this->db->from('products');
$this->db->where('product_id', $product_id);
$query = $this->db->get();
$result = $query->result_array();
if (!empty($result)) {
$arrReturn = $result[0];
}
return $arrReturn;
}
/*
Get categories
*/
public function get_categories(){
$this->db->select('*');
$this->db->from('categories');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
}
?>
And this is the menu bar in my view file where I'm loading all my categories.
<div class="container-fluid">
<div class="row">
<div class="col-lg-1">
<div id="categorymenu">
<center> <h3>Categorieën</h3> </center>
<ul class="list-group">
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<a href="<?php echo $category->id; ?>"><?php echo $category['name']; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
</div>
</div>
I am able to echo all the categories from my database but the links are not working.
I hope someone can help me,
Thanks!
Looks like you are creating a nonsense URL for the href value. Unless the 'id' field of the 'categories' table contains values that use a "controller/method/id" scheme the links you are creating don't go anywhere.
The 'id' field probably just contains numbers. Yes?
If so, your links probably look something like http://example.com/123 which isn't going to go anywhere unless you have a controller named "123".
You need hrefs that go somewhere. I recommend using the anchor() function of Codeigniter URL Helper. You will need to load that helper before this example will work.
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<?php echo anchor("controller_name/controller_method/{$category->id}", $category['name']); ?>
</li>
<?php endforeach; ?>
Without the anchor() function it would be written
<?php echo $category['name']; ?>
Try this:
<li class="list-group-item">
<a href="<?php echo $category->id;?>">
<?php echo $category['name']; ?>
</a>
</li>

Subcategory inside category in the Codeigniter framework

I want to show subcategory inside the main category. My table looks like this. but when i print_r($cat_id) it returns only the last id of the category. How can I fix this? Here is my controller:
$data['categories'] = $this->courses_model->get_all_categories_for_courses();
foreach($data['categories'] as $ct){
$cat_id = $ct['id'];
}
$data['subcategories'] = $this->courses_model->get_all_subcategories_for_courses($cat_id);
$this->load->view('templates/header');
$this->load->view('courses/courses-grid-fullwidth', $data);
$this->load->view('templates/footer');
}
and here is my model:
public function get_all_categories_for_courses() {
$query = $this->db->get_where('categories', array('parent_id' => NULL));
return $query->result_array();
}
function get_all_subcategories_for_courses($cat_id) {
$query = $this->db->get_where('categories', array('parent_id' => $cat_id));
return $query->result_array();
}
and here is my view page:
<?php foreach($categories as $cat): ?>
<div class="col-md-3 col-sm-6 incat">
<h4>
<a href="#" class="catname"><i class="fa <?= $cat['icon']; ?>" aria-hidden="true"></i>
<?= $cat['name_rus']; ?></a>
</h4>
<ul>
<?php foreach($subcategories as $subcat): ?>
<li><?= $subcat['name_rus']; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
Thanks in advance!
Or you can do this
<?php foreach($categories as $cat): ?>
<div class="col-md-3 col-sm-6 incat">
<h4>
<a href="<?= site_url('/courses/category/'.$cat['category_slug']); ?>" class="catname"><i class="fa <?= $cat['icon']; ?>" aria-hidden="true"></i>
<?= $cat['name_rus']; ?></a>
</h4>
<ul>
<?php
$query = $this->db->get_where('categories', array('parent_id' => $cat['id']));
?>
<?php foreach($query->result() as $subcat): ?>
<li><?= $subcat->name_rus; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
As Ukasyah said in a comment on your question, you're overwriting the $cat_id value each time you loop through your categories.
What you will need to do is get an array populated with the IDs and use $this->db->where_in() to match an array rather than a single value.
Your controller will need to look more like this:
$data['categories'] = $this->courses_model->get_all_categories_for_courses();
$cat_ids = [];
foreach($data['categories'] as $ct){
$cat_ids[] = $ct['id'];
}
$data['subcategories'] = $this->courses_model->get_all_subcategories_for_courses($cat_ids);
$this->load->view('templates/header');
$this->load->view('courses/courses-grid-fullwidth', $data);
$this->load->view('templates/footer');
While you will need to change your model function to look like this:
function get_all_subcategories_for_courses($cat_ids) {
$query = $this->db->where_in('parent_id', $cat_ids)->get('categories');
return $query->result_array();
}
Best of luck.

Categories