Pick from a list and save it to database - php

I am creating a website with Codeigniter 3 HMVC style for a client and he wants have a function on this page where the logged user has the choice of picking their own news. For example if a user chooses Juventus then he will recieve all the news added where Juventus is named. Basically like a add to basket or cart thing and I have never done something like this so here is what I have created:
Controller
class Usernews extends MY_Controller{
function __construct() {
parent::__construct();
$this->load->model('model_usernews');
$this->load->module('template');
}
function search(){
$data['pageTitle'] = "User news";
$search_name = $this->input->post('band');
$data['bands'] = $this->model_usernews->get_clubs($search_name);
$data['module'] = "usernews";
$data['view_file'] = "usernews";
$this->template->user_news($data);
}
}
Model
class Model_usernews extends CI_Model {
function get_table(){
$table = "bands";
return $table;
}
function get_clubs($keyword) {
$table = $this->get_table();
$this->db->like('band_name', $keyword, 'both');
$this->db->order_by('band_name');
$query=$this->db->get($table);
return $query;
}
and part of the View
<div class="row">
<div class="col-lg-8 ">
<!-- USER BAND LIST -->
<!-- LIST OF ARTICLES -->
</div>
<div class="col-lg-4">
<div class="row">
<?php $attributes = array('id' => 'list_of_bands');
echo form_open('usernews/search',$attributes); ?>
<input type="text" class="form-control" name="band"
placeholder="Search">
<button type="submit" class="btn btn-default btn-lg" >OK</button>
<?php echo form_close(); ?>
<?php $num_rows = $listing->num_rows();
if($num_rows > 0){ ?>
<ul id="the_bands" class="list-unstyled">
<li> <?php foreach ($listing->result() as $row){?>
<li><?php echo $row->band_name; ?>" ><button id="add_band"
class="btn btn-default btn-sm" >Add to list</button></li>
<?php }
} else{
echo "<p>No bands with that name</p>";
} ?>
</div>
</div>
I can search for the bands in the database but now I am clueless.

In your view use $bands instead of $listing.

Related

i want to fetch data from db get by name not by is id by clicking the hyper link

i am new in code igniter i am trying to fetch the data of all jobs of same location in my view
i write some code but when i try to get the data its gives me single data i want to fetch all data of same location
here is my controler
public function location($location){
$data['jobs'] = $this->edituser_model->getBlogBylocation($location);
$this->load->view('locwise' , ['data'=>$data]);
}
and model is
public function getBlogBylocation($location){
$this->db->select('*');
$this->db->like('location', $location);
$query = $this->db->get('jobs');
if($query->num_rows() > 0){
return $query->row();
}else {
return false;
}
}
and my view is
<?php foreach ($data as $key => $data): ?>
<div class="job-box">
<div class="company-logo">
</div>
<div class="row">
<div class="col-lg-12">
<div class="alert alert-success alert-2" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
Recently Posted Jobs in <?php echo $data->location?>
Citi</div>
<div class="row">
<div class="job-box">
<div class="description">
<div class="float-left">
<h5 class="title"><?php echo $data->job_title?></h5>
<div class="candidate-listing-footer">
<ul>
<li><i class="flaticon-pin"> </i><?php echo $data->location?></li>
<li><i class="flaticon-money"> </i><?php echo $data->salary?></li>
<li><i class="flaticon-work"> </i><?php echo $data->timing?></li>
<!--<li><i class="flaticon-honor"> </i><?php echo $jobs->qualification?></li>
<li><i class="flaticon-notepad"> </i><?php echo $jobs->experience?></li>-->
</ul>
<h6>Deadline:<?php echo $data->deadline?></h6>
</div>
</div>
<div class="div-right">
Apply Now
<!--<i class="flaticon-heart favourite"></i>-->
</div>
</div>
</div>
</div>
</div><!--end row-->
</div>
</div>
<?php endforeach ?>
</div>
i want to show multi record not single by clicking the hyper link
Many Thanks in advance
Junaid Amin
Please update your Controller method, Model method and View as follows:
Controller::Method:
public function location($location){
$data['jobs'] = $this->edituser_model->getBlogBylocation($location);
$this->load->view('locwise' , $data);
}
Model::Method:
public function getBlogBylocation($location){
$this->db->select('*');
$this->db->like('location', $location);
$query = $this->db->get('jobs');
if($query->num_rows() > 0){
return $query->result();
} else {
return false;
}
}
View:
<?php foreach ($jobs as $row): ?>
//Your staff goes here (i.e. $row->name_of_properties)
<?php endforeach ?>
pass name instead of id in you a tag and make route for that inside config/routes file
$route['location/(:any)'] = 'controller_name/function_name/$1';
public function location($location){
$data['jobs'] = $this->edituser_model->getBlogBylocation($location);
$this->load->view('locwise' , ['data'=>$data]);
}

How i can echo php code to codeigniter view

I want to filter data when user select click on the button all data is filtered correctly but tag not work properly.It will show two size but i will need one.
Here is pic of error:
And here is my model:
$result=NULL;
$this->db->select('*');
$this->db->from('sales');
$query = $this->db->get();
if($query->num_rows()>0)
{
foreach($query->result() as $all_prod)
{
$prod_size=NULL;
$make_option=NULL;
$flag=false;
$i=0;
$prod_size=$all_prod->sizes;
if (strpos($prod_size, ',') !== false)
{
$flag=true;
}
else
{
$flag=false;
}
$result.='<div class="col-lg-4 men-one">
<img src='.base_url($all_prod->img).' class="img-responsive">
<div class="row">
<p class="col-lg-10" style="margin-top:5px;">'.($all_prod->description).'
</p>
<span class="btn btn-light glyphicon glyphicon-heart col-lg-1"style="color:#C9302C"></span>
</div>
<div class="row">
<p class="col-lg-3 text-center">Rs:'.($all_prod->sale_price).'
</p>
<p class="col-lg-3 text-center"><strike>'. ($all_prod->orignal_price).'</strike>
</p>
<?php
if('.$flag.'=='.false.')
{
?>
<form class="col-lg-5">
<select class="size_color">
<option>Size</option>
<option>'.$prod_size.'</option>
</select>
</form>
<?php
}
else
{
if('.$flag.'=='.true.')
{
?>
<form class="col-lg-5">
<select class="size_color">
<option>Size</option>
<?php
while('.$prod_size.'['.$i.']!='.NULL.')
{
if('.$prod_size.'['.$i.']!='.",".')
{
'.$make_option.'='.$make_option.'
.'.$prod_size.'['.$i.'];
}
else
{
if('.$prod_size.'['.$i.']=='.",".')
{
?>
<option>'.$make_option.'</option>
<?php
'.$make_option.'='.NULL.';
}
}
'.$i.'='.$i.'+1;
if('.$prod_size.'['.$i.']=='.NULL.')
{
?>
<option>'.$make_option.'</option>
<?php
'.$make_option.'=NULL;
'.$flag.'='.false.';
}
}
?>
</select>
</form>
<?php
}
}
?>
</div>
<button class="btn btn-danger col-lg-12" onclick="add_to_cart(id='.$all_prod->id.')">ADD TO CART</button>
</div>';
}
echo $result;
}
else
{
return false;
}
Thanks In Advance help me i spend too much time tried all possible method that i know but i'm not successful.

Link to a user profile page in codeigniter

I'm trying to make a function in the CodeIgniter framework so users can click on product detail page, and see which user uploaded that certain product and they must be able to click on the product owner to go to his profile page.
Now, I am already able to echo owner information of the product owner on the details page. But the link to the owner profile page is not working some reason and the name isn't echoed anymore since I tried to make it a link.
This is my product details page (details.php) foreach loop function where I'm trying to echo the username of the owner of a product and link it to their profile page:
<?php include_once ('templates/header.php'); ?>
<div class="container">
<h3>Email van de eigenaar:</h4>
<?php
foreach($userdetail_list as $row)
{
?>
<tr>
<td><?php echo $row['email'];?></td>
</tr>
<?php } ?>
</div>
<div class="container">
<div class="row">
<div class="col-md-4" style="margin-top:24px;">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto']; ?>" class="img-responsive">
</div>
<div class="col-md-5">
<h1> <div class="product_naam"> <?php echo $product['product_naam']; ?> </div> </h1>
<h3>Over dit cadeau</h3>
<div class="product_beschrijving"><?php echo $product['product_beschrijving']; ?> </div>
</div>
<div class="col-md-3">
<button type="button" class="btn btn-default">Cadeau aanvragen</button>
<div class="aangeboden_door"> Aangeboden door: <?php
foreach($userdetail_list as $row)
{
?>
<tr>
<td><?=$row['username']; ?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php } ?>
</div>
</div>
</div>
</div>
<div class="container">
<footer>
<p>© kadokado 2017, Inc.</p>
</footer>
<hr>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
When I load the view page I do not see the username and no link to the users profile.
Here is my controller (User.php):
<?php
class User extends CI_Controller
{
public function index()
{
$this->load->view('profile', $data);
}
public function __construct()
{
parent::__construct();
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
}
public function userdetails($user_id)
{
//load the User_model
$this->load->model('User_model');
//call function getdata in de Product_model
$data['userdata_list'] = $this->User_model->getdata();
//get product details
$data['user'] = $this->User_model->get_user_info($user_id);
//laad view
$data['main_content'] = 'profiel_user';
$this->load->view('profiel_user',$data);
}
public function profile()
{
$this->load->model('User_model');
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
$this->load->view('profile');
}
}
full model file (User_model.php)
<?php
class User_model extends CI_Model {
public function getUserInfoByEmail($email)
{
$q = $this->db->get_where('users', array('email' => $email), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$email.')');
return false;
}
}
public function getUserInfo($user_id)
{
$q = $this->db->get_where('users', array('user_id' => $user_id), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$user_id.')');
return false;
}
}
public function getdata()
{
$this->db->where('user_id', $id);
$this->db->from('users');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
}
?>
Problably because of this line:
<?=$row['username']; ?>
Maybe your server does not support short hand tags?
try this:
<?php echo $row['username']; ?>
You also missed a semicolon here at the end:
echo base_url() . 'User/profiel_user?id='.$row['user_id']
and also here:
<a href="<?php echo base_url() ?>/Cadeauaanvragen">
You also use table row and table data tags without a surrounding table tag. Or remove all tr and td tags.
See: How do browsers analyze <tr> <td> without <table>?

PHP MVC Send object variable with href

Building a school management system using only PHP and PDO - college project.
Now I'm using MVC, and I'm having trouble sending information from one view to another.
This is the View I want information from
<?php
include_once 'model/student.php';
include_once 'model/courses.php';
$selected = new Student();
$students = Student::getAllStudents();
$selected2 = new Course();
$courses = Course::getAllCourses();
?>
<div>
<div class="col-lg-6">
<span style="font-size: 24px;"> <u>Students</u> - <button style="color:black;width: 30px;height: 35px;">+</button></span>
<br>
<hr>
<?php /* this is where I have the problem, the href attribute - I'm not sure I am sending the information correctly */
foreach($students as $stu){
?> <div> Student Name: <a href='? controller=student&action=showstudent&student_id=<?php echo $stu->id ?>'> <?php echo $stu->name ?> </a></div><br>
<div> Student Phone: <?php echo $stu->phone ?> </div><br> <hr> <?php
}
?>
</div>
<div class="col-lg-6">
<span style="font-size: 24px;"> <u>Courses</u> - <button style="color:black;width: 30px;height: 35px;">+</button></span>
<br>
<hr>
<?php
foreach($courses as $cur){
echo "<span><a href='#' ><b><u><i>" .$cur->name . "</i></u></b></a></span></br><hr>";
}
?>
</div>
</div>
These are the Controller and Model
<?php
class StudentController {
public $student_model;
public function __construct() {
$this->student_model= new Student();
}
public function showstudent($id) {
$find = $this->student_model->findStudentById($id);
if($find==TRUE){
require_once 'views/pages/container/student_into_container.php';
}
else{
echo "error";
}
}
}
Model
<?php
require_once 'connection.php';
class Student{
public $name;
public $phone;
public $email;
public $img;
public $id;
public function __construct() {
}
public static function getAllStudents(){
$students = [];
$db = Db::getInstance();
$req = $db->query('SELECT id, name, phone, email, image FROM students');
$students_info = $req->fetchAll();
foreach ($students_info as $student_info){
$stu = new Student;
$stu->id = $student_info['id'];
$stu->name = $student_info['name'];
$stu->phone = $student_info['phone'];
$stu->email = $student_info['email'];
$stu->img = $student_info['image'];
array_push($students, $stu);
}
return $students;
}
public function findStudentById($id){
$db = Db::getInstance();
$req = $db->prepare('SELECT * FROM student WHERE id = :id');
$req->execute(array('id' => $id));
$student = $req->fetch();
return $student;
}
}
And this is the view I want to get the $id from a student I found
and show all his value fields - this is the student_into_container.php from the StudentController page
<?php
require_once 'connection.php'
?>
<div style="" class="">
<div class="col-lg-4 ">
<?php
require_once 'views/pages/container/student_courses_aside.php';
?>
</div>
<div style="color: black;" class="col-lg-8 blackboard ">
<div class="blackboard-container" style="margin-top:10px;font-size:36px;">
<?php foreach($students as $stu){
echo "<span> Student Name: <a href='#'>". $stu->name . "</a></span></br>";
echo "<span> Student Phone: " . $stu->phone . "</span></br> <hr>";
}
?>
</div>
</div>
</div>
Totally lost here, any help would help.
In short?
Show in once view an information sent from another view with an href attribute

Unable to load the requested file: category.php

I have the unable to load the requested file error When the site is visited. The product which is the first page to be displayed shows the above error so as all product related functions like the product details and the product category respectively.
Below is my products class showing the method that gets all the product, product details and categories.
defined('BASEPATH') OR exit('No direct script access allowed');
class Products extends CI_Controller {
public function index()
{
// Get all products
$data['products'] = $this->Product_model->get_products();
// Load View
$data['main_content'] = 'products';
$this->load->view('layouts/main',$data);
}
public function details($id)
{
// Get all products details
$data['product'] = $this->Product_model->get_product_details($id);
// Load View
$data['main_content'] = 'details';
$this->load->view('layouts/main',$data);
}
public function category($id)
{
// Get all products details
$data['products'] = $this->Product_model->get_category_details($id);
// Load View
$data['main_content'] = 'category';
$this->load->view('layouts/main',$data);
}
}
And below is the Product View
<?php if($this->session->flashdata('registered')) : ?>
<div class="alert alert-success panel-body">
<?php echo $this->session->flashdata('registered');?>
</div>
<?php endif;?>
<?php if($this->session->flashdata('pass_login')) : ?>
<div class="alert alert-success panel-body">
<?php echo $this->session->flashdata('pass_login');?>
</div>
<?php endif;?>
<?php if($this->session->flashdata('fail_login')) : ?>
<div class="alert alert-danger panel-body">
<?php echo $this->session->flashdata('fail_login');?>
</div>
<?php endif;?>
<?php foreach($products as $product): ?>
<div class="col-md-4 game">
<div class="game-price"> <?php echo $product->price;?></div>
<a href="<?php echo base_url();?>products/details/<?php echo $product->id;?>">
<img src=" <?php echo base_url();?>assets/images/products/<?php echo $product->image;?>" />
</a>
<div class="game-title">
<?php echo $product->title;?>
</div>
<div class="game-add">
<form method="post" action="<?php echo base_url();?>cart/add">
QTY:<input class="qty" type="text" name="qty" value="1"/><br>
<input type="hidden" name="item_number" value="<?php echo $product->id;?>"/>
<input type="hidden" name="price" value="<?php echo $product->price;?>"/>
<input type="hidden" name="title" value="<?php echo $product->title;?>"/>
<button class="btn btn-primary" type="submit">Add To Cart</button>
</form>
</div>
</div>
<?php endforeach ?>
Here's my view folder structure
I see you have your view file with upper case Category.php.
There for on controller
public function category($id)
{
// Get all products details
$data['products'] = $this->Product_model->get_category_details($id);
// Load View
$data['main_content'] = 'Category'; // match filename
$this->load->view('layouts/main',$data);
}
Just make sure that you are loading views correctly.
you can share your views folder structure with us to lookup this problem

Categories