Controller
public function show_sku() {
$this->load->view('templates/header');
$this->load->view('check_sku');
$this->load->view('templates/footer');
}
public function retrieve_data() {
$data['product'] = $this->sku->find_sku();
$this->load->view('templates/header');
$this->load->view('check_sku', $data);
$this->load->view('templates/footer');
}
Routes
$route['check_sku/show_sku'] = 'check_sku/show_sku';
$route['check_sku/retrieve_data'] = 'check_sku/retrieve_data';
View
<?php echo form_open('/check_sku/retrieve_data'); ?>
<div class="form-group">
<?php echo form_label('SKU', 'sku', ''); ?>
<?php echo form_input('sku', '', $options['sku']); ?>
<?php echo form_submit('', 'Check', 'class="btn btn-primary"') ?>
</div>
<?php echo form_close(); ?>
Model
public function find_sku() {
$this->load->library('get_api');
$sku = $this->input->post('sku');
$get_data = $this->get_api->get_data("GET", "http://89.201.137.7:8082/datasnap/rest/artikli/sifra/" . $sku, false);
$response = json_decode($get_data, true);
return $response['result'][0]['artikli'][0];
}
What is the proper way to structure your controller so I don't load the views two times and I don't submit the form by just hitting the route, and so that I pass the data to the view when the form is submitted by clicking the submit.
Related
I need help fetching data from database and display them in view.
My DB name is directory and table usernames that includes:id,title,body,slug,username,platform,gender,age and created_at.
I used foreach to display all posts from database, but i want to display now a specific post based on platform.
How can i do it?
This is the code i use to display all usernames from the database ordered by id:
<div class="col-md-6 mb-4">
<div class="card">
<div class="card-body" style="height: 130px;">
<h5 class="card-title"><?php echo $username_data['title']; ?></h5>
<p class="card-text"><?php echo word_limiter($username_data['body'], 24); ?></p>
</div>
<div class="card-footer text-muted">
<div class="row">
<div class="col-6">
<small>Created at: <?php echo $username_data['created_at'];?></small><br>
<strong>Is <?php echo $username_data['gender'];?></strong>
</div>
<div class="col-6 text-right">
View full message
</div>
</div>
</div>
</div>
</div>
This is my model:
<?php
class Usernames extends CI_Controller{
public function index(){
$data['title'] = 'Lastest Posts';
$data['posts'] = $this->post_model->get_usernames();
$this->load->view('templates/header');
$this->load->view('usernames/index', $data);
$this->load->view('templates/footer');
}
public function view($slug = NULL){
$data['post'] = $this->post_model->get_usernames($slug);
$query = $this->db->get('usernames');
if(empty($data['post'])){
show_404();
}
$data['title'] = $data['post']['title'];
$this->load->view('templates/header');
$this->load->view('usernames/view', $data);
$this->load->view('templates/footer');
}
public function create(){
$data['title'] = 'Create Post';
$this->form_validation->set_rules('title','Title','required');
$this->form_validation->set_rules('body','Message','required');
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('age','Age','required');
$this->form_validation->set_rules('gender','Gender','required');
$this->form_validation->set_rules('platform','Platform','required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('usernames/create', $data);
$this->load->view('templates/footer');
} else {
$this->post_model->create_username();
redirect('usernames');
}
}
public function snapchat(){
$data['title'] = 'Lastest Posts';
$data['posts'] = $this->post_model->get_usernames();
$this->load->view('templates/header');
$this->load->view('usernames/snapchat', $data);
$this->load->view('templates/footer');
}
}
Model:
<?php
class Post_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_usernames($slug = FALSE){
if($slug === FALSE){
$this->db->order_by('usernames.id', 'DESC');
$query = $this->db->get('usernames');
return $query->result_array();
}
$query = $this->db->get_where('usernames', array('slug' => $slug));
return $query->row_array();
}
public function create_username(){
$slug_link = substr(str_replace(['+', '/', '='], '', base64_encode(random_bytes(16))), 0, 16);
$slug = url_title($this->input->post('title').'-'.$slug_link);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body'),
'platform' => $this->input->post('platform'),
'age' => $this->input->post('age'),
'gender' => $this->input->post('gender'),
'username' => $this->input->post('username')
);
return $this->db->insert('usernames', $data);
}
}
Everything is working fine, but i don't know really how to fetch those data.
I need that in this specific page ('telegram.php'), show all rows that includes Telegram from platform.
As mentioned before, my database is structured like this:
directory(DB Name)
usernames(table name)
->id->title->body->slug->username->platform->gender->age->created_at
I need help fetching data from database and display them in view.
My DB name is directory and table usernames that includes:id,title,body,slug,username,platform,gender,age and created_at.
I used foreach to display all posts from database, but i want to display now a specific post based on platform.
How can i do it?
This is the code i use to display all usernames from the database ordered by id:
<div class="col-md-6 mb-4">
<div class="card">
<div class="card-body" style="height: 130px;">
<h5 class="card-title"><?php echo $username_data['title']; ?></h5>
<p class="card-text"><?php echo word_limiter($username_data['body'], 24); ?></p>
</div>
<div class="card-footer text-muted">
<div class="row">
<div class="col-6">
<small>Created at: <?php echo $username_data['created_at'];?></small><br>
<strong>Is <?php echo $username_data['gender'];?></strong>
</div>
<div class="col-6 text-right">
View full message
</div>
</div>
</div>
</div>
</div>
This is my model:
<?php
class Usernames extends CI_Controller{
public function index(){
$data['title'] = 'Lastest Posts';
$data['posts'] = $this->post_model->get_usernames();
$this->load->view('templates/header');
$this->load->view('usernames/index', $data);
$this->load->view('templates/footer');
}
public function view($slug = NULL){
$data['post'] = $this->post_model->get_usernames($slug);
$query = $this->db->get('usernames');
if(empty($data['post'])){
show_404();
}
$data['title'] = $data['post']['title'];
$this->load->view('templates/header');
$this->load->view('usernames/view', $data);
$this->load->view('templates/footer');
}
public function create(){
$data['title'] = 'Create Post';
$this->form_validation->set_rules('title','Title','required');
$this->form_validation->set_rules('body','Message','required');
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('age','Age','required');
$this->form_validation->set_rules('gender','Gender','required');
$this->form_validation->set_rules('platform','Platform','required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('usernames/create', $data);
$this->load->view('templates/footer');
} else {
$this->post_model->create_username();
redirect('usernames');
}
}
public function snapchat(){
$data['title'] = 'Lastest Posts';
$data['posts'] = $this->post_model->get_usernames();
$this->load->view('templates/header');
$this->load->view('usernames/snapchat', $data);
$this->load->view('templates/footer');
}
}
Model:
<?php
class Post_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_usernames($slug = FALSE){
if($slug === FALSE){
$this->db->order_by('usernames.id', 'DESC');
$query = $this->db->get('usernames');
return $query->result_array();
}
$query = $this->db->get_where('usernames', array('slug' => $slug));
return $query->row_array();
}
public function create_username(){
$slug_link = substr(str_replace(['+', '/', '='], '', base64_encode(random_bytes(16))), 0, 16);
$slug = url_title($this->input->post('title').'-'.$slug_link);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body'),
'platform' => $this->input->post('platform'),
'age' => $this->input->post('age'),
'gender' => $this->input->post('gender'),
'username' => $this->input->post('username')
);
return $this->db->insert('usernames', $data);
}
}
Everything is working fine, but i don't know really how to fetch those data.
I need that in this specific page ('telegram.php'), show all rows that includes Telegram from platform.
As mentioned before, my database is structured like this:
directory(DB Name)
usernames(table name)
->id->title->body->slug->user
public function get_usernames_from_platform($platform){
if(!empty($platform)){
$this->db->where('platform',$platform);
$this->db->order_by('usernames.id', 'DESC');
$query = $this->db->get('usernames');
return $query->result_array();
}
return array();
}
Add the method, to your Model and call it to fetch the corresponding data in the corresponding method in your controller.
It seems to be missing its constructor $this->load->model('post_model');
I am facing problem in function profile(). In this method I am fetching data from email using session.
When I login, displaying current data. But when I click on profile it displays previous session data and after refresh it become current session data.
last session data
after refresh display current session
my controller file is admin.php.
admin.php
<?php
class Admin extends CI_Controller{
function __construct(){
parent::__construct();
// Load form helper library
$this->load->helper('form');
//Load foam valodation library
$this->load->library('form_validation');
// Load the model
$this->load->model('adminmodel');
}
public function index(){
// Load our view to be displayed
$this->load->view('admin/login');
}
// Check for Admin login process
public function dashboard(){
// Validate the user can login
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE) //checking validation
{
if(isset($this->session->userdata['logged_in']))
{
$this->load->view('admin/dashboard');
}
else
{
$this->load->view('admin/login');
}
}
else
{ //validation are true
if (isset($_POST['login']))
{
$email = $this->input->post('email');
$password =$this->input->post('password');
// Validate the admin can login
$result = $this->adminmodel->validate($email,$password);
if ($result) {//if the user credidential is validated
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => true
);
print_r($data);
}
// Add user data in session
$this->session->set_userdata($data);
//load dashboard and passing value
$this->load->view('admin/dashboard',$result);
}
}
} //end of dashboard
public function profile(){
if($this->session->userdata('is_logged_in')){
$email = $this->session->userdata('email');
$result = $this->adminmodel->fetchdata($email);
print_r($result);
$this->load->view('admin/profile',$result);
}
else
{
echo "failed profile";
}
} // end of profile
// Logout from admin page
public function logout() {
// Removing session data
$sess_array = array(
'email' => '',
'is_logged_in'=>false
);
$this->session->unset_userdata($sess_array);
$data['message_display'] = 'Successfully Logout';
$this->load->view('admin/login', $data);
}
}
?>
model file
adminmodel.php
<?php
class Adminmodel extends CI_Model{
public function validate($email,$password)
{
$query = $this->db->where(['email'=>$email,'password'=>$password])
->from('login')
->get();
$result = $query->row();
return $result;
}
public function fetchdata($email)
{
$query = $this->db->where(['email'=>$email])
->from('login')
->get();
$result = $query->row();
return $result;
}
}
profile.php
<?php
echo " I am in Profile " . $email;
?>
<!DOCTYPE html>
<html>
<head>
<title>Profile</title>
</head>
<body>
<p>Name : <input type = "text" value="<?php echo $name; ?>" /><p>
<p>Mobile : <input type = "text" value="<?php echo $mobile; ?>" /> </p>
<p>Address : <input type = "text" value="<?php echo $address; ?>" /> </p>
<p>Email : <input type = "text" value=" <?php echo $email;?>" /> </p>
</body>
</html>
Try the following, after logount instead of unset data, destroy the session entirely
$this->session->sess_destroy();
Don't get this.
I have to validate basic form. Im using CI form_validation library, form validation rules are set just to "required"...if form validation fails it just need to echo validation error in login form. Im using by default <?php echo validation_errors(); ?> Helper is autoloaded. Form functionality is OK it works how it should, only problem is that framework dont display error messagges if dont fill required fields?
login_form.php - VIEW
<?php echo validation_errors(); ?>
<?php echo form_input('email' , 'email'); ?> <br>
<?php echo form_input('password' , 'password'); ?> <br>
<?php echo form_submit('submit' , 'submit'); ?>
login.php - CONTROLLER
<?php
Class Login extends CI_Controller {
function index () {
$data ['content'] = "login_form";
$this->load->view("template", $data);
}
function validate() {
$this->load->library("form_validation");
$this->form_validation->set_rules('email' , 'email' , 'trim|xss_clean|required');
$this->form_validation->set_rules('password' , 'password' , 'md5|trim|required');
if( $this->form_validation->run() == FALSE) {
redirect('login/index');
}
else {
redirect("user/profile");
}
}
}
?>
Why don't you use code in controller like this hope will help you just set form action to login.php
login.php - CONTROLLER
function index () {
$data ['content'] = "login_form";
$this->load->library("form_validation");
$this->form_validation->set_rules('email' , 'email' , 'trim|xss_clean|required');
$this->form_validation->set_rules('password' , 'password' , 'md5|trim|required');
if( $this->form_validation->run() == FALSE) {
$this->load->view("template", $data);
}
else {
redirect("user/profile");
}
}
login_form.php - VIEW
<?php echo validation_errors(); ?>
<?php echo form_input('email' , 'email'); ?> <br>
<?php echo form_input('password' , 'password'); ?> <br>
<?php echo form_submit('submit' , 'submit'); ?>
if( $this->form_validation->run() == FALSE) {
redirect('login/index');
}
Don't redirect, else error will not be available. Instead of that keep
if( $this->form_validation->run() == FALSE) {
$data ['content'] = "login_form";
$this->load->view("template", $data);
}
Check the following link for more details:
Form Validation : CodeIgniter User Guide
I'm trying to make alert with cross sign. Unfortunately cross sign not working. I have included jquery before bootstrap.js. Here is my model. Thanks in Advance
public function create(){
$data = array('name'=> $this->input->post('name'));
$this->db->insert('dbname', $data);
echo'
<div class="alert alert-success">
×You have successfully posted
</div>
';
exit;
}
Change this to
×You have successfully posted
this
×You have successfully posted
And as well as use Model to write data into database. Use follows
In controller
public function create(){
$this->load->model('Model_name');
$result = $this->Model_name->insert_data();
if($result == 1)
{
?>
<div class="alert alert-success">
×You have successfully posted
</div>
<?php
}
else
{
?>
<div class="alert alert-error">
×Failed to poste
</div>
<?php
}
}
In Model
public function insert_data()
{
$data = array(
'name'=> $this->input->post('name')
);
if(!$this->db->insert('dbname', $data))
{
return $log = 0;
}
else
{
return $log = 1;
}
}