Codeigniter Bootstrap Alert not Working - php

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;
}
}

Related

Proper controller structure

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.

Fetch data from DB with CodeIgniter

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');

Codeigniter save form_multiselect into database and set_value

i try to save a form_multiselect but it dont save and the value dont select. I hope you can help me please.
Edit: Its working now but only with a single select. I cant select more as one options.
I try this
view
<div class="form-group">
<label class="col-sm-2 control-label" for="field-1"> Team</label>
<div class="col-sm-4">
<?= form_multiselect('teams_id[]', dd2menu('teams', array('teams_id' => 'title')), set_value('teams_id[]', $item->teams_id), 'class="form-control"') ?>
</div>
</div>
Controller
public function manage($id = NULL) {
$data = array();
if ($id) {
$this->{$this->model}->{$this->_primary_key} = $id;
$data['item'] = $this->{$this->model}->get();
if (!$data['item'])
show_404();
} else {
$data['item'] = new Std();
}
$this->load->library("form_validation");
$this->{$this->model}->custom_select = 'users.*, teams.title as teams';
$this->{$this->model}->joins = array( 'teams' => array('teams.teams_id = users.teams_id', 'inner'));
if ($this->form_validation->run() == FALSE)
$this->load->view($this->module . '/manage', $data);
else {
$this->users_model->teams_id = $this->input->post('teams_id');
$this->{$this->model}->save();
redirect('admin/' . $this->module);
}
}
Model
class Users_model extends CI_model
{
public $_table = 'users';
public $_primary_keys = array('user_id');
}
I cant find the error because its working if i one select. But i can select more as one options.
If found the fix in the other code from NikuNi.
Now its save 1 or 1,2 in the Database.
Controller
if( is_array( $this->input->post('teams_id') ) ) {
$this->{$this->model}->teams_id = join(",", $this->input->post('teams_id'));
} else {
$this->{$this->model}->teams_id = $this->input->post('teams_id');
}
$this->form_validation->set_rules('teams_id[]', 'teams_id', 'trim');

How to retrieve an array in Simple MVC Framework?

How can I retrieve an array in Simple MVC Framework to show error messages if some input fields are not valid?
Controller:
public function index($error)
{
$data ['title'] = $this->language->get('welcome_text');
View::renderTemplate('header', $data);
View::render('insert/form', $data);
View::renderTemplate('footer', $data);
}
public function save()
{
if (isset($_POST)) {
$data ['is_valid'] = \Helpers\Gump::is_valid($_POST, array(
'firstname' => 'required|min_len,2'
));
if ($data ['is_valid'] === true) {
// continue
} else {
$this->index($data ['is_valid']); // show error messages
die();
}
}
}
View:
<div class="alert alert-danger"><?php var_dump($error); ?></div>
... <button type="submit">Save (calls save())</button>
var_dump($error) always shows bool(false).
Basically you wasn't sent error to render, as i see.
public function index($error)
{
$data ['title'] = $this->language->get('welcome_text');
$data ['error'] = $error;
View::renderTemplate('header', $data);
View::render('insert/form', $data);
View::renderTemplate('footer', $data);
}
View
<div class="alert alert-danger"><?php var_dump($data['error']); ?></div>

undefined variable error in codeigniter model

I am working on a registration form
When i submit the form after the entering details the registration data get stored in the database but the the page shows the following error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: dbRet
Filename: models/registration_model.php
Line Number: 47
I am not getting the prolem the same code is working good at the localhost but what is the problem at server??
This is the model Code
class Registration_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function index()
{
$data['page_title'] = 'Registration';
$this->load->view('client/general/head',$data);
$this->load->view('client/general/header');
$this->load->view('registration');
$this->load->view('client/general/footer');
}
function busregister()
{
$data['page_title'] = 'Registration';
$this->load->view('business/registration1');
}
public function save_registration($un,$email,$pwd,$utype)
{
try
{
$data = array(
'username' => $un,
'password' => $pwd,
'email' => $email
);
//print_r($data);
//die();
$this->load->database();
$this->db->reconnect();
if($utype=='buss')
{
$dbRet=$this->db->insert('business', $data);
}
else
{
$dbRet-$this->db->insert('user', $data);
}
if (!$dbRet) {
$ret['ErrorMessage'] = $this->db->_error_message();
$ret['ErrorNumber'] = $this->db->_error_number();
log_message('error', "DB Error: (".$ret['ErrorNumber'].") ".$ret['ErrorMessage']);
$this->db->close();
return $ret['ErrorNumber'];
}
else{
$rid=$this->db->insert_id();
$this->db->close();
return $rid;
}
}
catch (Exception $e){
return -1;
}
}
}?>
This is the controller code
class Registration extends CI_Controller {
public function index()
{
$this->load->helper(array('registration','date','country'));
$this->load->helper('client');
$this->load->model('Registration_model');
if ($this->input->post("submit_registration",TRUE))
{
if($this->form_validation->run("stud_registration_rules"))
{
$this->load->library(array('encrypt'));
$stud_first_name = $this->input->post('stud_user_name');
$stud_email = $this->input->post('stud_email');
$stud_pass = $this->input->post('stud_password');
$retval=$this->Registration_model->save_registration($stud_first_name,$stud_email,$this->encrypt->encode($stud_pass),"user");
var_dump($this->encrypt->encode($stud_pass));
if($retval!==-1){ //SAVE SUCCESS
$this->session->set_flashdata('flashSuccess', 'Record saved successfully.');
redirect('/');
}
else{
$this->session->set_flashdata('flashError', 'Error Saving Record');
redirect('/registration');
}
}
else{
// FORM IS NOT VALIDATED
$this->session->set_flashdata('flashError', 'Validation fields error');
redirect('/registration');
}
}
$this->Registration_model->index();
}
}
This is the view code
<div class="content">
<div class="container_12">
<div class="grid_12">
<?php if($this->session->flashdata('flashSuccess')) : ?>
<div class="alert alert-success">
×
<p><?php print_r($this->session->flashdata('flashSuccess'));
?></p>
</div>
<?php endif; ?>
<?php if($this->session->flashdata('flashError')) : ?>
<div class="alert alert-danger">
×
<p><?php print_r($this->session->flashdata('flashError'));
?></p>
</div>
<?php endif; ?>
<?php echo registration_form();?>
</div>
</div>
In your Model code Registration_model , Else part assignment operator is wrong
change the following line
$dbRet-$this->db->insert('user', $data);
to
$dbRet = $this->db->insert('user', $data);

Categories