问题
I want to dynamically populate data in Facebook open tags. For this, I need to pass a variable from my controller file to a file which is outside the CI model. How to do this?
public function index()
{
$data['clients'] = $this->qua_model->list_clients();
$data['clientst'] = $this->qua_model->list_clientst();
$id = $this->uri->segment(3);
//GET RECORD BY ID
$data['result']=$this->qua_model->get_record_by_id($id);
// print_r($data['result']);
$data['url']= urlencode(base_url().'intermediate.php?data=$result');
// print_r($data['url']);
// return;
//echo $data['url'];
//exit();
//$data['url'] = base_url().'Testimonial';
$this->load->view("header",$data);
$this->load->view("testimonial",$data);
$this->load->view("footer");
}
The above code is the index function of my controller. I want to pass the variable $result to intermediate.php page.
Below is the intermediate.php page.
<!DOCTYPE html>
<html>
<head>
<!--<meta property="fb:app_id" content="535219850163370" />
<meta property="og:site_name" content="meta site name"/>
-->
<meta property="og:url" content="http://www.quanutrition.com/Dotcom/new/"/>
<meta property="og:type" content="website"/>
<meta property="og:title" content="title"/>
<meta property="og:image" content="http://quanutrition.com/Dotcom/new//admin/assets/uploads/clients_image/shikhar-dhawan-759.jpg"/>
<meta property="og:description" content="My description"/>
<script type="text/javascript">
<!--
function Redirect() {
window.location="http://www.quanutrition.com/Dotcom/new/";
}
//-->
</script>
</head>
;
<body onload="Redirect()" >
</body>
</html>
Below is the view page where I am setting the session. $clients is the variable that contains the array.
<?php
session_start();
$_SESSION['clients']=$clients;
print_r ($_SESSION);
exit();
?>
Now in the other page I am doing this.
<?php
session_start();
print_r ($_SESSION);
$var = $_SESSION['clients'];
echo $var;
exit();
?>
It is throwing this error - Undefined index: clients. What am I doing wrong?
This is how a single data in my array in $clients look like
Array ( [clients] => Array ( [0] => stdClass Object ( [client_id] => 1 [name] => Shikhar Dhawan [image] => shikhar-dhawan-759.jpg [video] => [content1] => "Nutrition plays a key role in my recovery and performance. Using Sports Gene testing and advance nutritional blood biochemistry helps me. Eating with a plan gets more scientific and result oriented this way."
回答1:
Passing data using $_SESSION in PHP
Sessions follow a simple workflow. When a session is started, PHP will either retrieve an existing session using the ID passed (usually from a session cookie) or if no session is passed it will create a new session. PHP will populate the $_SESSION superglobal with any session data after the session has started.
See here: http://php.net/manual/en/session.examples.basic.php
Every time you want to store or retrieve data from your session you need to open the session. session_start();
So you need to use this in your controler.php:
<?php
session_start();
...
at the very top. This will start your session and now you can simply store your variables/data in the session you just started. To do so:
$_SESSION['var_name'] = "value";
or
$_SESSION['foo'] = bar;
On your next page you start all over again with:
<?php
session_start();
...
at the very top. An then you simply call the variable:
$new_page_var = $_SESSION['foo']; // contains now 'bar'
If you don't need the variable anymore you can follow this by:
unset($_SESSION['foo']);
and the variable will be deleted.
Once you're done and no session is needed anymore you also can detroy the session using: session_destroy();
- This will delete the session and all the stored data in there!
Hope this helps, cheers :)
With your example here:
<?php
session_start();
$_SESSION['clients']=$clients;
print_r ($_SESSION);
exit();
?>
You're missing $clients variable! Look here:
<?php
session_start();
$clients=array("foo", "bar");
$_SESSION['clients']=$clients;
print_r ($_SESSION);
exit();
?>
Cheers
More detailed solution
<?php
session_start();
$object1 = new stdClass();
$object1->client_id = 1;
$object1->name = 'Shikhar Dhawan';
$object1->content1 = 'Some content';
$object2 = new stdClass();
$object2->client_id = 2;
$object2->name = 'Florian Foo';
$object2->content1 = 'Bar some content';
$clients=array($object1, $object2);
$_SESSION['clients']=$clients;
foreach ($_SESSION['clients'] as $client) {
echo $client->name ." wrote:".$client->content1."\n";
}
exit();
?>
Which will output:
Shikhar Dhawan wrote:Some content
Florian Foo wrote:Bar some content
See here: https://ideone.com/CYDcVa
Cheers
来源:https://stackoverflow.com/questions/46069895/passing-a-variable-from-one-page-to-another-in-php-to-dynamically-populate-faceb