CodeIgniter session value as images - php

In my project, I am using pagination and I used these statements to get the page number detail:
$page=$this->uri->segment(3);
$this->session->set_userdata('page',$page);
echo $this->session->userdata('page');
When I print this session value in that page itself, I get the value correctly and when I click on the particular link and then print that data, I am getting the value like 'images'.
Why is this happening?
However, when I write the statements like
$page=$this->uri->segment(2);
$this->session->set_userdata('page',$page);
echo $this->session->userdata('page');
it's working fine.
My URL is: http://localhost/CI/user/index/4

I have the same problem with you, the session variable always get value 'images'. Then i realize that: if we made a session variable, then we must call redirect() directly.
This is my example:
$page=$this->uri->segment(3);
$this->session->set_userdata('page',$page);
redirect('control/function2');
Then, you can get continue your code in the 'function2'.

Solved the issue by writing another function, setting the session and redirecting to above function like this:
function set_session(){
$id=$this->uri->segment(3);
$this->session->set_userdata('id',$id);
redirect('set_uri_session');
}
function set_uri_session(){
$id=$this->uri->segment(3);
$this->session->set_userdata('id',$id);
echo $this->session->userdata('id');
}

Related

PHP: Delete Value of variable after 5 min

I have problem in print PDF from forms.
When new user go to my site and fill the forms and click submit to send data, i need to get this data in PDF file, So i can get the data for this user from :-
$lastid = mysql_insert_id();
But this i think is not good, When any other user go in this form and click print PDF, he get the last id.
So what can i do to delete the last id and cannot print any data for any user else have fill forms ??
I assume that you are getting data into $lastid because of mysql query execution if that so your query will be executed every time and the result will change depending upon users and their requirement.
But if you still want to do so you can use unset() function of php.
unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.
To unset() a global variable inside of a function, then use the $GLOBALS array to do so:
<?php
function foo()
{
unset($GLOBALS['bar']);
}
$bar = "something";
foo();
?>
Complete documentation can be found over here

echo function call 2 variables

Ok so I have the code for a form that is called and works but it needs two varibles grabbed from the string of a url. I have the first and the second is the same for what im doing on any page that I am creating which is alot. Here is the code at the url: collabedit.com/9g99j
Question if Get <?php echo $_GET['id']; ?> is grabbing my id string from the url how do I use this in the echo of my function I just defined at the bottom of the code? Instead of having this: echo DescriptionGet(1256124, 50874); can someone tell me how to put something like this: echo DescriptionGet(1256124, $id);
This would make it so i dont' have to enter that id value for every page I want to create.
Thanks,
Thanks everyone for your replies and I was able to figure it out on my own and actually used exactly what the first reply was.
Now I have a new question about this function. How do I make it grab the image from that same page its grabbing the form code from? I can't figure this part out and its keeping me from doing mass automation for this site.
Anyone help?
Try this:
$id = $_GET['id'];
echo DescriptionGet(1256124, $id);
You can change your function definition from:
function DescriptionGet($c, $id)
to
function DescriptionGet($c, $id=50874)
Each time when you will call DescriptionGet($c) it will behave as you passed $id=50874 but also if you need you can call DescriptionGet($c, 20) and $id in the function will be set to 20.
And in case you want to simple use $_GET['id'] as function parameter you can simple run
echo DescriptionGet(1256124, intval($_GET['id']));
you don't even need to use extra variable.

Create a variable in one function and pass it to another in PHP

Let me first say I've spent a day reading three google pages of articles on this subject, as well as studied this page here.
Ok, here's my dilemma. I have two functions. Both called upon via AJAX. This first one assigns a value to the variable and the second one uses that variable. Both functions are triggered by two separate buttons and need to stay that way. The AJAX and the firing off of the functions work fine, but the variable isn't passed. Here is my code:
if( $_REQUEST["subjectLine"] ) //initiate first function
{
$CID = wpCreateChimpCampaign();
echo $CID; //this works
}
if( $_REQUEST["testEmails"] ) //initiate second function
{
echo $CID; //does not return anything but should contain "apple"
wpSendChimpTest($CID);
}
function wpCreateChimpCampaign () //first function
{
$CID = "apple";
return $CID;
}
function wpSendChimpTest ($CID) //second function
{
echo $CID; //does not return anything but should contain "apple"
}
I'm open to using a class but I haven't had much luck there either. I was hoping to solve this issue without using classes. Thanks for the help in advance!
If you are making 2 separate calls to this file, it may be helpful for you to visualise this as being 2 functions in 2 totally separate files. Although they exist in the same PHP file, because they used called in different calls, they don't retain the value of the variable $CID. Once the file has run, the variable is destroyed and when you call the file again, the value is null again.
So you need to store that variable between calls. You can either store it in a database or store it in a session variable.
So call session_start(); at the beginning of the file, then rather than use $CID, just use $_SESSION['CID'];
I'm not sure where the hold up is. The code you have will work:
$CID = wpCreateChimpCampaign(); // returns 'apple'
wpSendChimpTest($CID); // echos 'apple'
The code looks fine, but are you certain that all requirements are being met so both functions execute?
In other words are you supplying values for both $_REQUEST["subjectLine"] and $_REQUEST["testEmails"]?

What if a view is an empty value while loading it in codeigniter?

I am building an application which requires loading view dynamically. The value of view is a variable and it depends on what the controller sets it. Sometimes my value is returned as empty or null value, that is I don't want any view to load. But then my code breaks at the same place and half of the screen goes blank. Is there any workaround for this?
Check for the value in the controller, if the value is empty don't load the view?
if($variable== something)
{
$this->load->view('myView');
} else {
do something else;
}
I found a solution for this myself. What I was doing earlier in my view was:
$this->load->view($viewname);
The value of the viewname used to come from controller as:
$data['viewname'] = 'dynamicview';
$this->load->vars($data);
What I am doing right now is, in my controller:
$data['viewname'] = $this->load->view('dynamicview',null,TRUE);
$this->load->vars($data);
And now in my view, I simply write:
echo $viewname;
Actually this passes view as data in the controller and then parses it in the view where we actually print it.
I hope this helps someone out there. Saved my day atleast...

How to store parameters from $_REQUEST for later use

I have an html page with a form. In the form I have a search function and a delete function that will be available in each results’ row, in this way:
..<td><img src="delete.gif" style="cursor: pointer" onClick="javascript:delete();"></td>..
<input type="submit" value="search" onClick="javascript:search();">
In a . js file I have both functions passing the parameters to a php file via jquery’s ajax function.
Let’s go to the php file:
There are no problems with the search function: I get the parameters by $_REQUEST and I do a select query.
By clicking delete.gif in the form, I get to the delete function. The query deletes the selected row and right after I need to call the search function so I can show the results without the deleted row.
And here comes the trouble: both functions use $_REQUEST to build the queries so when calling search function after deleting I have the delete parameters stored in $_REQUEST.
How can I recover the search parameters I had in $_REQUEST in the first search so I can do that search again after deleting??
Here’s a glimpse of the delete function in the php file:
function deleteResult($_REQUEST['param1'],$_REQUEST['param2'], $_REQUEST['param3'])
{
$strSqlDelete = "DELETE FROM …"; // query working in database
//here the connecting to the database code
$result = search($_REQUEST['param1'],$_REQUEST['param2'], $_REQUEST['param3']);
echo utf8_encode($result);
exit();
}
Thanks a lot!!
It's not too safe to use $_REQUEST superglobal. Use $_POST or $_GET depending on your method. To store something for later use use $_SESSION suberglobal array.
session_start(); // In the beginning of your script
...
$_SESSION['param1'] = $_REQUEST['param1'];
Then you can use $_SESSION['param1'] anywhere else within your site even after page reloading.
Store it in the local variables or you can store them into the cookies. So that you can use it as many times you want.
you can also do that by javascript
it's prefer to delete this row by deleting it instead of calling server side again to render the result again
now you save unwanted hit to server side
FOR EXAMPLE
// in JS
function delete(this){
$.ajax({}); // call ajax to delete from server side (php)<br>
delete this row (tr);<br>
}

Categories