session data won't show - php

I don't know the problem.
The data is saved in the session but it only shows the last input.
$_SESSION['data'][] = $_POST;
$_SESSION['data']['lengtezijde'] = $_POST['lengtezijde'];
$_SESSION['data']['kleur'] = $_POST['kleur'];
$_SESSION['data']['hoogte'] = $_POST['hoogte'];
?><tr><?
?><th><?echo $_SESSION['data']['lengtezijde'];?></th><?
?><th><?echo $_SESSION['data']['kleur'];?></th><?
?><th><?echo $_SESSION['data']['hoogte'];?></th><?
?></tr><?

I have tried your code also and it work me. Since it is an array, you must loop trough it to display the values and that is why I think it is giving you only the last value.
//Declare your variables
$lengtezijde = $_POST['lengtezijde'];
$kleur = $_POST['kleur'];
$hoogte = $_POST['hoogte'];
//Store it in session
$_SESSION['data'] = array(
'lengtezijde' => $lengtezijde,
'kleur' => $kleur,
'hoogte' => $hoogte,
);
Now you can loop through your data and display it. I hope this helps.

if i understand what you looking for you must check your $_POST
i test this code
$_SESSION['data'][] = "Test";
$_SESSION['data']['lengtezijde'] = 'test1';
$_SESSION['data']['kleur'] = 'test2';
$_SESSION['data']['hoogte'] = 'test3';
echo $_SESSION['data']['hoogte'];
br();
echo $_SESSION['data']['kleur'];
br();
echo $_SESSION['data']['lengtezijde'];
result :
test2
test1
test3

Make sure you have mentioned session_start() in First Line of your code ,
session_start() too, before you assign values to $_SESSION
Rest of things seems ok in your code.
Never use below kind of assignment it will ruin your machine memory . If you run your script you will see that on each refresh of the page this size will be increasing. that is not advisable.
$_SESSION['data'][] = $_POST;

Related

struggling to get cookie information to print in PHP

For some reason, I can't get the value of "price" from a cookie.
Here's the cookie info.
[{"label":"300 HOME ","price":150},{"label":"Yes, my phone line is (1234567890)","price":40},{"label":"internet 2","price":0},{"price":-25}]
And here's the code I'm using. What am I doing wrong?
<?php
$cookie = $_COOKIE["MonthlyOptions"];
$obj = json_decode($cookie);
print $obj->{'price'};
?>
Thanks to both #waterloomatt and #Ravi Patel for their suggestions. In the end, what was stopping me from progressing was the need to add stripslashes.
$cookie = (stripslashes( $_COOKIE["MonthlyOptions"] ));
$obj = json_decode($cookie);
print $obj[0]->price;

JSON array unset

First of all, I really don't know if that kind of topic exist. But I searched a lot and now I am here.
My question about parsing. For example I would like to unset some items.
$now = array();
$now[0]['name'] = "Hello1";
$now[0]['si'] = "BumBum1";
$now[1]['name'] = "Hello2";
$now[1]['si'] = "BumBum2";
$now[2]['name'] = "Hello3";
$now[2]['si'] = "BumBum3";
$now[3]['name'] = "Hello4";
$now[3]['si'] = "BumBum4";
echo json_encode($now)."<br>";
unset($now[0]);
echo json_encode($now);
And the output:
[{"name":"Hello1","si":"BumBum1"},{"name":"Hello2","si":"BumBum2"},{"name":"Hello3","si":"BumBum3"},{"name":"Hello4","si":"BumBum4"}]
{"1":{"name":"Hello2","si":"BumBum2"},"2":{"name":"Hello3","si":"BumBum3"},"3":{"name":"Hello4","si":"BumBum4"}}
And my the JSON file turns to messy code. Appears numbers and etc.
Any ideas how to solve this.
You need to "reindex" the array (use array_values() function).
//unset..
unset($now[0]);
//reindex
$now = array_values($now);
//display as before
echo json_encode($now);

Using Simple HTML DOM to extract an 'a' URL

I have this code for scraping team names from a table
$url = 'http://fantasy.premierleague.com/my-leagues/303/standings/';
$html = #file_get_html($url);
//Cut out the table
$FullTable = $html->find('table[class=ismStandingsTable]',0);
//get the text from the 3rd cell in the row
$teamname = $FullTable->find('td',2)->innertext;
echo $teamname;
This much works.. and gives this output....
Why Always Me?
But when I add these lines..
$teamdetails = $teamname->find('a')->href;
echo $teamdetails;
I get completely blank output.
Any idea why? I am trying to get the /entry/110291/event-history/33/ as one variable, and the Why Always Me? as another.
Instead do this:
$tdhtml = DOMDocument::loadHTML($teamdetails);
$link = $tdhtml->getElementsByTagName('a');
$url = $link->item(0)->attributes->getNamedItem('href')->nodeValue;
$teamdetails = $teamname->find('a')->href;
^^^^^^^^^---- never defined in your code
I also fail to see how your "works" code could possibly work. You don't define $teamname in there either, so all you'd never get is the output of a null/undefined variable, which is...no output all.
Marc B is right, I get that you don't have to initialize a variable, but he is saying you are trying to access a property of said variable:
$teamdetails = $teamname->find('a')->href;
^^^^^^^^^---- never defined in your code
This is essentially:
$teamname = null;
$teamname->find('a')->href;
The problem in your example is that $teamname is a string and you're treating it like a simple_html_dom_node

Forward a JSONencoded string to another page

I'm having trouble taking a string (called $name) from one page and outputing that string to another page.
This is a snippet of my code from where I think it is relevant:
if ($_POST['to'])
{
// Get all relevant messages
$get_msgs = mysql_query("SELECT * FROM messages WHERE touser='$from'");
$mailbox = array();
while($row = mysql_fetch_assoc($get_msgs))
{
$mailbox[] = $row;
}
// Make a string with the JSON array in it
$name = "{ \"mailbox\":".json_encode($mailbox)." }";
// now here, how do I simply forward this string to a page called page2.php
}
A. What should the code be for page2.php? (even though all I want it to do is really just echo $name)
B. If I run the above code multiple times, would page2.php be cleared, and refreshed each time with the new $name?
Thank you in advance for the help guys.
I tried this:
session_start();
$_SESSION['myValue']=$name;
And in page2 used:
session_start();
echo $_SESSION['myValue'];
But no data was forwarded to page 2
you have 3 way :
some one set session to passing array to new page :
sesstion_strat() ;
$_SESSION['name'] = $name ;
-> in page2
sesstion_strat() ;
$name = $_SESSION['name'] ;
2en way :
pass array with get method , this method not good for long arrays and spcial chars.
header("Location: page2.php?name=".$name);
-> in page2
$name = $_GET['name'] ;
or use curl for post data :
you see sample here :
http://php.net/manual/en/book.curl.php

Losing a session variable between one page with codeigniter session library

In my code i am trying to store a variable between two pages but i either get a string return "images" or 0... or nothing- tried casting it.. echoing in on one page works and displays correct number but as soon as you click through the view to the next page- its lost- i tried turning on cs_sessions in the db and made no difference
<?php
public function carconfirm($car_id = '')
{
if(empty($car_id))
{
redirect('welcome');
}
$this->load->model('mcars');
$car = $this->mcars->getCar($car_id);
$data['car'] = $car->row_array();
$car_id = (int) $car_id;
$this->session->set_userdata('flappy', $car_id);
echo $car_id;
//insert details
//display details
$this->load->view('phps/carconfirm',$data);
}
function confirm()
{
//get vars
$time_slot = $this->session->userdata('slot');
$person_id = $this->session->userdata('person_id');
$car_id = $this->session->userdata('flappy');
$insert_array = array( 'slot'=> $time_slot ,
'car'=> $car_id,
'person_id'=> $person_id
);
print_r($insert_array);
$this->load->model('mbooking');
$result = $this->mbooking->addbooking($insert_array);
if($result)
{
redirect('welcome/options');
}
}
?>
the variable I'm losing is flappy- i changed the name to see if that was the problem
Finally, I fixed this. Using this answer in SO too : codeigniter setting session variable with a variable not working, I scan my js/css for missing resources. Turn out that, my thickbox.js refer to loadingAnimation.gif in, yes, images folder. That's where the images come. Having fix the missing file, the sesion variabel working just fine.
Not sure, why CI replace (maybe) last added session variabel using this, but maybe it's because CI is not using $_SESSION global variabel. CMIIW. I use this article as a hint : http://outraider.com/frameworks/why-your-session-variables-fail-in-codeigniter-how-to-fix-them/

Categories