I have included session in autoload, and its working on all other places.
I'm having problem in retrieving session variable it return null result. While in the controller where i have set the session there it's working fine.
Here is the code where i'm setting it in else condition:
class Controller_catagory extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('generic_model');
$this->load->model('backend/model_post');
$this->load->model('backend/model_permissions');
}
public function index($param1='',$param2='')
{
$id=$this->generic_model->getAllRecords('dramas',array(
'drama_slug' => $param2 ),'drama_id','DESC');
// print_r($id);
if (!empty($id))
{
foreach ($id as $key)
{
$id = $key['drama_id'];
}
}
$data;
if (!empty($param1) && empty($param2))
{
$data["page"] = 'frontend/includes/view_alldramas';
$id = $this->generic_model->getAllRecords('channel',array('channel_slug' => $param1 ),'channel_id','DESC');
if (!empty($id))
{
foreach ($id as $key)
{
$id = $key['channel_id'];
$ch_slug= $data['ch_slug'] = $key['channel_slug'];
$this->session->set_userdata('ch_slug',$ch_slug);
}
}
$this->session->set_userdata('channel_capture',$id);
$data['dramas_pagination'] = $this->model_post->get_specific_channel_pagination(0,12,$id);
$data["get_dramas"]=$this->model_post->get_all_dramas();
$data['channels'] = $this->generic_model->getAllRecords('dramas', array('channel_fk' => $id ),'drama_id','DESC');
}
else
{
// The id is printing right result
echo $id;
// Here i'm setting session, if i retrieve here its working
$this->session->set_userdata('drama_episode',$id);
$data['episodes_pagination'] = $this->model_post->get_specific_post_pagination(0,12,$id);
$data["get_episodes"]=$this->model_post->get_all_dramas();
$data['dramas'] = $this->generic_model->getAllRecords('post',$arr = array(
'dramas_fk' => $id ),'id','DESC');
$data["page"] = 'frontend/includes/view_allposts';
}
$data['title'] = 'GLOBAL VIDEOS';
$data['heading'] = 'Dramas List';
$data["top"] = 'frontend/includes/top_home';
$this->load->view('frontend/index',$data);
}
}
}
Now here is another class where i'm trying to get the value of set session but its not retrieving the data and i'm getting empty record.
Note: i am doing the same thing with 'channel_capture' and i'm successfully getting its value
class Home extends CI_Controller {
public function __construct()
{
$data = array();
parent::__construct();
$this->load->model('backend/model_post');
$this->load->model('generic_model');
}
public function ajax_posts()
{
$start= $_GET['start'];
// it gives empty result here don't know why
$id = $this->session->userdata('drama_episode');
//prints nothing
echo "This key: ".$id;
$post_pagination=$this->model_post->get_specific_post_pagination($start, 12, $id);
var_dump($post_pagination);
$str='';
$base_url=base_url();
if (empty($post_pagination))
{
return false;
}
foreach($post_pagination as $post)
{
$str.= '<div class="col-lg-3 col-sm-6 col-md-4 epi_height" >';
$str.= '<a href='.$base_url.$post['slug'].'>';
$str.= '<img class="img-responsive" src='.$base_url.$post['thumbnail'].' alt="recent dramas" />';
$str.= $post['title'];
$str.= '</a>';
$str.= '</div>';
}
echo $str;
}
}
Try to set session before if else condition like:
$this->session->set_userdata('drama_episode',$id);
your if else goes here and then retrieve it.
Auto load session library from autoload.php as follows:
$autoload['libraries'] = array('session');
and you can use session variables wherever you like!
Or you may need to use flash explained here:
https://www.codeigniter.com/user_guide/libraries/sessions.html#flashdata
Please do these troubleshooting :-
Inside index() function add
echo $this->session->userdata('drama_episode'); die();
Now call the controller i believe URL/category.
Hope you can see the correct session value.
Inside ajax_posts() function print the entire session
print_r($_SESSION); die();
Call the URL to access the function ajax_posts()
Let me know what is the output u r getting?
====================================
Alternatively check, before you call the function ajax_posts(), are you some where deleting the session value for drama_episode ?
Related
i am a newbie having difficulty in this. i did not getting a value of ID In A box for Update Inside A Box.
update code
<?php
require_once (__DIR__ . '/../../config.php');
require_once($CFG->dirroot.'/local/message/classes/form/edit.php');
global $DB;
$PAGE->set_url(new moodle_url('/local/message/update.php'));
$PAGE->set_context(\context_system::instance());
$PAGE->set_title('Edit');
//here display form
$mform=new edit();
$table='local_message';
$id=required_param('id', PARAM_INT);
$info=$DB->get_records($table,array('id' => $id));
if ($mform->is_cancelled()) {
print_r("1");
redirect($CFG->wwwroot.'/local/message/manage.php', 'You Cancelled The Form');
} else if ($fromform = $mform->get_data()) {
print_r("2");
$record = new stdClass();
$record->id=$id;
$record->messagetext = $fromform->messagetext;
$record->messagetype = $fromform->messagetype;
$DB->update_record($table, $record);
redirect($CFG->wwwroot . '/local/message/manage.php', 'you created a message with a title ' . $fromform->messagetext);
}
echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();
Form/moodle/Code
<?php
require_once("$CFG->libdir/formslib.php");
class edit extends moodleform {
public function definition() {
global $CFG;
$mform = $this->_form; // Don't forget the underscore!
$mform->addElement('text', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('text', 'messagetext', get_string('message_text', 'local_message')); // Add elements to your form
$mform->setType('messagetext', PARAM_NOTAGS); //Set type of element
$mform->setDefault('messagetext', get_string('enter_message', 'local_message')); //Default value
$choices = array();
$choices['0'] = \core\output\notification::NOTIFY_WARNING;
$choices['1'] = \core\output\notification::NOTIFY_SUCCESS;
$choices['2'] = \core\output\notification::NOTIFY_ERROR;
$[enter image description here][1]choices['3'] = \core\output\notification::NOTIFY_INFO;
$mform->addElement('select', 'messagetype', get_string('message_type', 'local_message'), $choices);
$mform->setDefault('messagetype', '3');
$this->add_action_buttons();
}
//Custom validation should be added here
function validation($data, $files) {
return array();
}
}
You need to pass the id to the form before displaying the form
$mform->set_data($info);
$mform->display();
Also, you should use the single get_record function not the multiple get_records function
$info = $DB->get_record($table, array('id' => $id));
Also, you might want to use the hidden element for the id
$mform->addElement('hidden', 'id');
I want to make html file that while be a ip adress and insert what client do via $_SERVER
My problem is that i cant make table in that file so code is this
FTP
public static function Write($Wfile, $Wtext)
{
$open = fopen($Wfile, "w+");
fwrite($open, $Wtext);
}
File to create log
public function __construct()
{
chdir("Log");
$this->_file = $_SERVER['REMOTE_ADDR'] .".html";
if(!is_file($this->_file)){
ftpFile::Write($this->_file,$this->Standards());
}
public function Standards()
{
$html = "<html>\r\n <body>\r\n <table cellpadding='10'>";
return $html;
}
AND WHAT I WANT TO INSERT NOW
public function Set()
{
$indicesServer = array(
'PHP_SELF',
'argv',
'argc',
'GATEWAY_INTERFACE',
'SERVER_ADDR',
'SERVER_NAME',
......
foreach ($indicesServer as $arg){
return '<tr><td>'.$arg.'</td><td>' . $_SERVER[$arg] . '</td></tr>';
}
so i try return, echo, print, file put content and i only get one result and that is last in my array.
ONCE Again i want to create log for user that come to my site and everthing inside $_SERVER write one time when sesion_id active and every where client go i want to insert and what $_POST insert in that file. Many of that i do only this is need to be fixed... TNX all
Your bottom code snippet doesn't work because you are attempting to return inside foreach loop: you can only return from a function once. Try this:
public function Set()
{
$indicesServer = array(
'PHP_SELF',
'argv',
'argc',
'GATEWAY_INTERFACE',
'SERVER_ADDR',
'SERVER_NAME',
......
$ret = "";
foreach ($indicesServer as $arg)
$ret .= '<tr><td>'.$arg.'</td><td>' . $_SERVER[$arg] . '</td></tr>';
return $ret;
}
Am getting a variable called $msisdn from a view using post in one function (search_results). After doing the processing, I would like to use the same variable in another function(assign_role) currently,I am unable to do that since am getting this error
Severity: Notice Message: Undefined variable: msisdn
. Below is my search_result function where am getting the post data:
public function search_results(){
$msisdn = $this->input->post('search_data');//getting data from the view
if ($msisdn == false){
$this->load->view('add_publisher');
} else{
$this->assign_role($msisdn); //passing the variable to another function
$this->load->model('search_model');
$data['result'] = $this->search_model->search_user($msisdn);
$data1['box'] = $this->search_model->search_select($msisdn);
$result = array_merge($data, $data1);
$this->load->view('add_publisher',$result);
echo json_encode($result);
}
}
I want to use $msisdn from above function in this function below:
public function assign_role($msisdn){
//echo $msisdn['numm'];
$publisher = $this->input->post('select');
if ($publisher == false) {
$this->load->view('add_publisher');
} else {
$data = array(
'publisher' => true,
);
$this->load->model('insert_model');
$data1 = $this->insert_model->search_group($msisdn, $data);
if ($data1 == true) {
echo "Recorded updated succesfully";
$this->load->view('add_publisher');
} else {
echo "Recorded not updated";
$this->load->view('add_publisher');
}
}
}
Please help me to achieve this.
Passing variables from one function to another in the same controller can be achieved by using sessions. In Codeigniter, one can use flashdata like:
$this->session->set_flashdata('info', $MyVariable);
Remember, flashdata is only available for the next server request then it will be automatically cleared.
Then it can be retrieved as:
$var = $this->session->flashdata('info');
If you want to keep the variable for one more server request, you can do this:
$this->session->keep_flashdata('info');
Hope this will help someone faced with the problem.
Please help!!
I am trying to search database to produce results for my search query but it does not output any result even when the searched term exist in the database
Here is my controller
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Search extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model("profile_model");
$this->load->model("model_home");
}
public function index(){
$data = array();
$search_term = $this->input->post('search');
if($query = $this->model_home->car_search())
{
$data['car'] = $query;
}
$this->load->view('search_results', $data);
}
}
Here is my model
public function car_search($search_term='default')
{
$this->db->select('*');
$this->db->like('car_make',$search_term);
$this->db->or_like('car_model',$search_term);
$this->db->or_like('car_year',$search_term);
$this->db->or_like('registration_number',$search_term);
$this->db->or_like('engine_number',$search_term);
$this->db->or_like('chasis_number',$search_term);
$query = $this->db->get('cars');
return $query->result_array();
}
My search form
<?php
echo form_open('search');
echo form_input(array('name'=>'search'));
echo form_submit('search_submit','submit');
?>
My search result page
<?php
if(isset($car)) :
foreach($car as $row) {
echo $row['car_make'];
} //<-- moved this line
else :
echo '<h1> Not working </h1>';
endif; ?>
Its always outputting "Not Working"
This code is a bit of a mess, if you tidy it up you will see the error
Your code without all the <?php and ?>
<?php
$count = 1;
if(isset($car)) :
foreach($car as $row) {
$count++;
}
echo $row['car_make']
else :
echo '<h1> Not working </h1>';
endif;
?>
So you should see the } in the wrong place ending the foreach loop, quite easily now, so change it to
<?php
$count = 1;
if(isset($car)) :
foreach($car as $row) {
$count++;
echo $row['car_make'];
} //<-- moved this line
else :
echo '<h1> Not working </h1>';
endif;
?>
As you are getting the Not Working message I have to assume there is a problem with your querying of the database.
Probably as you are not passing the search term into the car_search method call. So try passing the paramter you get from the POSTed data.
public function index(){
$data = array();
if($query = $this->model_home->car_search($this->input->post('search'))) {
$data['car'] = $query;
}
$this->load->view('search_results', $data);
}
You should also look at the default value used in the car_search method as the string 'default' will almost definitely cause the query to return no results.
I have a class with a couple of methods
deleteUploadedFile() and currentUploadedFiles().
currentUploadedFiles(), basically loops over a session array and displays it on screen, simple as. Code sample:
function currentUploadedFiles()
{
if(isset($_SESSION['fileArray']) && $this->count > 0)
{
echo '<p style="clear:both">Current files uploaded list:</p>';
echo '<ol>';
foreach($_SESSION['fileListing'] as $key => $value )
{
echo '<li>'. $value .' [Remove File]</li>';
}
echo "</ol>\n\r";
echo "<p> Current file size allowance: ". $this->_returnRemainingSessionFileSize() ." of 8 MB";
} else {
echo '<p style="clear:both">No files have been uploaded yet</p>';
}
if($this->deleteUploadedFile() === true)
{
echo '<p>File has now been deleted from our records.</p>';
}
}
the deleteUploadedFile() method, basically when form is submitted it deletes file from the server and removes the entry from the session array. Sample code:
function deleteUploadedFile()
{
(int) $id = $_GET['id'];
(bool) $deleted = false;
if (file_exists($this->target_path.'/'.$_SESSION['fileArray'][$id]))
{
$_SESSION['fileSize'] -= $this->_checkSessionFileSize($id);
if (unlink($this->target_path.'/'.$_SESSION['fileArray'][$id]))
{
$deleted = true; //'<p>File has now been deleted from our records.</p>';
unset($_SESSION['fileArray'][$id]);
unset($_SESSION['fileListing'][$id]);
}
}
return $deleted;
}
my controller, basically checks if file id# isset, then checks if the array id# isset, then calls the deleteUploadedFile() method and then calls the currentUploadedFiles() method.
Question is, why when I var_dump $deleted var in deleteUploadedFile() I get bool(true) but inside the currentUploadedFiles() method I get bool(false). Sounds like I'm messing up the scope somehow?
Looks like $deleted is in the local scope of the delete function.
Something like the following should work.
class theClass
{
function __construct()
{
$this->deleted = false
}
function delete()
{
$this->deleted = true;
}
function upload()
{
var_dump($this->deleted);
}
}