codeigniter retrieve parameter values passed from view to controller - php

i've passed some parameters from a view to a controller function. i see them in the url like : .../view/a1312014031
but i'm not able to use them in the function
here is the view
<a href="<?php echo base_url() ?>visiteur/view/<?php echo $id; echo $mois; ?>" class='btn btn-primary'>Mettre en paiement </a>
here is the controller function
public function view($id = '', $mois = '') {
......
$page = 'visiteur_liste';
$this->load->view('visiteur/' . $page, $data);
....
}
after clicking the link i get url with the parameters.../view/a1312014031
is there a solution to use those parameters in the function without using the session?
thanks for your time

Ok, then
You'l need to separate your parameter value in your hyperlink by "/", like:
HTML
<a href="<?php echo base_url() ?>visiteur/view/<?php echo $id.'/'.$mois; ?>" class='btn btn-primary'>Mettre en paiement </a>
PHP
public function view($id = '', $mois = '') { //function in CodeIgniter Controller (visiteur)
......
$page = 'visiteur_liste';
$this->load->view('visiteur/' . $page, $data);
....
}
ANOTHER WAY: PASS DATA USING GET PARAMETERS:
<a href="<?php echo base_url() ?>visiteur/view?id=<?php echo $id.'&mois='.$mois; ?>" class='btn btn-primary'>Mettre en paiement </a>
GET IN CodeIgniter
public function view() { //function in CodeIgniter Controller (visiteur)
$id=$this->input->get("id"); // or, $id=$_GET["id"];
$mois=$this->input->get("mois"); // or, $mois=$_GET["mois"];
}

try this
<a href="<?php echo base_url() ?>visiteur/view/<?php echo $id;?>/<?php echo $mois; ?>"
class='btn btn-primary'>Mettre en paiement </a>

Related

undefined variable: id while clicking a dynamic image in homepage using CodeIgniter

Controller:
public function latestnews()
{
$data['news'] = $this->New_model->getById($id);
$this->load->view('news',$data);
}
Model:
public function getById($id)
{
return $this->db->get_where($this->_table, ["new_id" => $id])->row();
}
View:
<?php
if (isset($news) and $news) {
foreach($news as $new) {
?>
<div class="col-sm-12">
<div class="section">
<img src="<?php echo site_url('uploads/'.$new->image); ?>" />
</div>
<p><?php echo $new->description;?> </p>
</div>
<?php
}
}
?>
How and where to define the variable id?
While clicking a dynamic image,it should be opened in another page containing details but instead showing Undefined variable: id
In the latestnews() function you have no $id defined.
Try to call the latestnews() function with the right $id parameter.
you can write function like this,
function latestnews($id)
{
Controller:
function single_news($id) { // This $id variable comes from URL Ex: example.com/single_news/15. So $id = 15
$this->data['news_data'] = $this->new_model->getNewsByID($id);
$this->load->view('single-news', $this->data); // Create New View file named single-news.php
}
Model:
function getNewsByID($id) {
$this->db->where('id', $id);
$q = $this->db->get('news');
if($q->num_rows() > 0) {
return $q->row();
}
return false;
}
View: (Single News Page)
<div>
<img src="<?php echo site_url('uploads/'.$news_data->image); ?>" />
</div>

Ajax for changing displayed image in php

Currently, if no images have been clicked, I get my selected image like this and it's also what's shown on first page load.
$pictures = Picture::getPictures($album->getId());
$selectedPicture = $pictures[0];
It selects the first picture and display it like this:
<img src="<?php echo $selectedPicture->getOriginalFilePath($user->getId()); ?>" >
I am trying to change the selected image when a user clicks on a different thumbnail because this is just one of many images of an album.
Attempt with a ajax, I'm not really experienced with it so I couldn't get it to work
<script type="text/javascript">
function Func() {
$.ajax({
type: "POST",
url: "RefreshImage.php",
success: function (json) {}, error: function () {}
})
}
</script>
Then the php portion also has something like to create the thumbnails so that a user can clicks on one, it displays the image. But I'm not sure how to do that.
foreach ($pictures as $pic) {
$output.= "<a href='" . $pic->getOriginalFilePath($user->getId()) . "'> <img style='width:120px;height:120px' "
. "src='" . $pic->getThumbnailFilePath($user->getId()) . "'"
. "name='" . $pic->getFileName() . "' "
. "onClick='Func'></a>";
}
RefreshImage.php content
session_start();
$user = $_SESSION['user'];
//$albums = Album::getAlbums($user->getId());
if (isset($_POST["chosenAlbid"])) {
$album_id = $_POST["chosenAlbid"];
$user = $_SESSION['user'];
$album = Album::getAlbumById($user->getId(), $album_id);
$pictures = Picture::getPictures($album->getId());
$selectedPicture = $pictures[1];
}
?>
<div id="imagecontainer">
<div>
<p style="float: left;"><img style="width:500px;height:400px" src="<?php echo $selectedPicture->getOriginalFilePath($user->getId()); ?>" ></p>
<p><span>
<h4> Description</h4>
</span><?php echo $selectedPicture->getDescription() ?></p>
</div>
</div>
If you rewrite your function into jQuery it will be more easy.
From this
function Func() {
$.ajax({
type: "POST",
url: "RefreshImage.php",
success: function (json) {}, error: function () {}
})
}
To jQuery
$('a[data-refresh-img]').click(function(e) {
e.preventDefault(); // block ahref redirect
$('#imagecontainer img').attr('src', $(this).attr('href')); // replace image
);
Then activate function add data-refresh-img attribute on a tag.
And all will work for you.
JSFIDDLE
** UPDATE **
If you need replace any data with image you can do this
<a data-refresh-img href="original_img_path">
<img
title="picture one"
src="thumbnail_img_path" height=50>
<span style="display: none"><?php echo $selectedPicture->getDescription() ?> AAA</span>
</a>
And change function to
$('a[data-refresh-img]').click(function(e) {
e.preventDefault(); // block ahref redirect
$('#imagecontainer img').attr('src', $(this).attr('href')); // replace image
$('#imagecontainer img').attr('title', $(this).find('img').attr('title')); // replace title
$('#imagecontainer span[data-description]').html($(this).find('span').html()); // replace description
});
JSFIDDLE 2

can't delete record in codeigniter using href

I am trying to delete particular record using href. But it's not working properly when i click on button its passing in URL like this
http://localhost/project_x/Organisation/Organisation/%3C?php%20echo%20base_url();?%3EOrganisation/delete_org?id=3
I don't know where is the mistake can anyone help me.It's passing id in URL is correctly.
Here is my controller:
function delete_org() {
// Pass the $id to the org_delete() method
$this->Org_model->org_delete($id);
redirect('Organisation/organisation');
}
here is my redirect organisation function
public function organisation() {
$data['organisations'] = $this->Org_model->getOrganisations();
$this->load->view('template/header');
$this->load->view("organisation", $data);
$this->load->view('template/footer');
}
Here is my model:
function org_delete($id) {
$this->db->where('id', $id);
$this->db->delete('organisation');
}
And Here is my view buttons:
<?php
echo "<td class='text-center'><div class='btn-group btn-group-xs'><a href='#' data-logid='" . $org->id . "' data-target='#editGroups' data-toggle='modal' title='Edit' class='open_modal btn btn-default'><i class='fas fa-edit'></i></a>";
echo "<a href='<?php echo base_url();?>/Organisation/delete_org?id=$org->id' class='btn btn-danger confirmation'><i class='fas fa-times'></i></a></div></td></tr>";
?>
Can anyone help me where i did mistake.
Thanks in advance.
Issue is you are adding php tag again in echo. change your delete anchor tag as below:
echo "<a href='".base_url()."/Organisation/delete_org?id=$org->id' class='btn btn-danger confirmation'><i class='fas fa-times'></i></a></div></td></tr>";
Also in controller define $id
function delete_org() {
// Pass the $id to the org_delete() method
$id = $_GET['id'];
$this->Org_model->org_delete($id);
redirect('Organisation/organisation');
}
Hope this will help you :
Change view buttons like this:
<?php
echo "<td class='text-center'><div class='btn-group btn-group-xs'><a href='#' data-logid='" . $org->id . "' data-target='#editGroups' data-toggle='modal' title='Edit' class='open_modal btn btn-default'><i class='fas fa-edit'></i></a>";
echo "<a href='".base_url('Organisation/delete_org?id='.$org->id)."' class='btn btn-danger confirmation'><i class='fas fa-times'></i></a></div></td></tr>";
In controller :
function delete_org() {
$id = $this->input->get('id');
$this->Org_model->org_delete($id);
redirect('Organisation/organisation');
}
function delete_org($id= NULL) {
// Pass the $id to the org_delete() method
$id = $this->input->post('id');
$this->Org_model->org_delete($id);
redirect('Organisation/organisation');
}
hope this helps you
You may try this deleting record using codeigniter framework by data-attribute as bellow.
html
<a class="deletedata" data-id="<?php echo $data['id] ?>">delete</a>
js
$(".deletedata").on("click",function(){
var id=$(this).data('id');
var baseurl = $("#base_url").val();
$.post(baseurl+"Products/dltProduct/"+id,function(id){
window.location.reload();
});
});
Here my Controller name is Products. you can define your name and
than after define your method name.
Controller
public function dltProduct($id) {
$this->db->where("id",$id);
$this->db->delete("tbl_name");
}

Session Not setting after Login [Codeigniter]

When i logged in to the site the session is not setting. It was working fine earlier but somehow it is not working now.
My controller code:
public function login_subadmin()
{
$email=$this->input->post('Email');
$password=sha1($this->input->post('Password'));
$this->sb->login_sub_admin($email,$password);
}
My Model code:
public function login_sub_admin($email,$password)
{
$this->load->library('session');
$query=$this->db->where(['Email'=>$email,'Password'=>$password])->get('dc_user');
$res=$query->row();
$this->session->set_userdata('user',$res->Employee_Name);
if(!isset($this->session->userdata['user']))
{
$this->session->set_userdata('user',"No session");
}
redirect(base_url());
}
My view Navigation:
<?php
if(!isset($this->session->userdata['user']))
{
$this->session->set_userdata('user',"No session");?>
<!-- Login -->
<li class="nav-item">
<a href="sub_Admin" class="nav-item-child radius-3">
Login
</a>
</li>
<!-- End Login -->
<?php }
if (isset($this->session->userdata['user'])) {$user=$this->session->userdata['user'];?>
<!-- Home -->
<li class="nav-item">
<a href="#" class="user-profile dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<?php echo $user;?> <img style="width: 30px; height: 30px;" class="image-circle" src="<?php echo base_url(); ?>profileimages/<?php if (isset($this->session->userdata['profile'])) {
echo $this->session->userdata['profile'];
}?>" alt="">
When i load the page / login into the site the it says no session. i use session library in autoload, and individually also but i don't know why it suddenly stops working, It was working fine the last time i close the project, i also clear my browser cache but still did not able to fix it, i have searched a lot and applied many steps / procedures on it but did not get the issue reason.
Any help will be appreciated.
try below code
Errors:
To access profile in session you need to set it
wrong declaration of the session and few codes have been modified.
As well Session setting part should move to the controller.
In Controller
public function login_subadmin()
{
$this->load->library('session');
$email=$this->input->post('Email');
$password=sha1($this->input->post('Password'));
$result = $this->sb->login_sub_admin($email,$password);
if (empty($result)) {
redirect(base_url());
}
else {
$newdata = array(
'user' => $result[0]['Employee_Name'],
'profile' => $result[0]['Profile_Image'], # New Element
'logged_in' => TRUE
);
if (!$this->session->set_userdata($newdata)) {
echo "Cannot set session";
} else {
redirect(base_url());
}
}
}
In Model
public function login_sub_admin($email,$password)
{
$query = $this->db->query("SELECT * FROM dc_user WHERE email = '$email' AND password= '$password' ");
$result = $query->result_array();
return $result;
}
In View
<?php
$user = $this->session->userdata['user'];
if(empty($user))) # or can check logged_in == TRUE (recommended personally)
{
?>
<!-- Login -->
<li class="nav-item">
<a href="sub_Admin" class="nav-item-child radius-3">
Login
</a>
</li>
<!-- End Login -->
<?php
}
if (!empty($user))
{
?>
<!-- Home -->
<li class="nav-item">
<a href="#" class="user-profile dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<?php echo $user;?> <img style="width: 30px; height: 30px;" class="image-circle"
src="<?php echo base_url(); ?>profileimages/<?php echo $this->session->userdata['Profile_Image'];?>" alt="">
</a>
</li>
<?php
}

Change Text and href of a tag after login

<li>Log In</li>
This link will go to the login page and there I will login but after login it should change the text to "profile" and and href= "userwork.php".
<?php
session_start();
if (isset($_SESSION['email']))
{
echo "<script type='text/javascript'>";
echo "$('.changelogin').text('Profile')";
echo "$('.changelogin').attr('href', 'userwork.php')";
echo "</script>";
}
?>
Why not doing something like this ?
<?php
if (isset($_SESSION['email'])) {
echo '<li>Profile</li>';
} else {
echo '<li>Log In</li>';
}
?>
try this.
<li>
<a href="<?php echo isset($_SESSION['email']? 'userwork.php' : 'login.php'; ?>"
class="changelogin"
title="<?php echo isset($_SESSION['email']?'profile':'Log In'; ?>"
><?php echo isset($_SESSION['email']?'profile':'Log In'; ?></a>
</li>
#Damien solution is simple, if you still want to achieve this by using jQuery than you use like:
<?php
// your session start
session_start();
?>
<input type="hidden" name="is_session" id="is_session" value="<?=(isset($_SESSION['email']) ? 1 : 0)?>">
<li>Log In</li>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var is_session = $("#is_session").val();
if(is_session == 1){
$('.changelogin').text('Profile');
$('.changelogin').attr('href', 'userwork.php');
}
});
</script>
In this example you can store session value into a hidden input field and use it after page load by using jQuery.

Categories