Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Sessions and simplexml_load_file?
Old 05-27-2009, 09:29 PM Sessions and simplexml_load_file?
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
Hey all, right now I'm having some problems combining sessions and the simplexml_load_file function. The error I get?

Warning: session_start() [function.session-start]: Node no longer exists in myLocation on line 1

I Googled it, and found that it's a fairly well-known problem/bug. Apparently sessions don't like storing XML information. Lots of people have their own fixes, but I can't get anything to work. Here's the code I'm using:

PHP Code:
$filename 'products.xml';
$xmlstr file_get_contents($filename);
$xml_data simplexml_load_string($xmlstr);

$products = array();
$_SESSION['cart'] = array();

foreach (
$xml_data->product as $product) {
    
$id $product->id;
    
$name $product->name;
    
$description $product->description;
    
$image $product->image;
    
$price $product->price;

    
$status "";

    if(
$_POST['cart'.$id]) {
        
$status "Product added";
        
        
array_push($_SESSION['cart'],$id);
    }

    
$products[] = "
    <div class='product'>
    <h5>"
.$id."</h5>
    <h3>"
.$name."</h3>
    <p>"
.$description."</p>
    <p>"
.$image."</p>
    <h4>"
.$price."</h4>
    <form action='?' method='POST'>
    <input type='submit' value='Add to cart' name='cart"
.$id."' />
    </form>
    "
.$status."
    </div>"
;
}  

foreach(
$products as $content) {
    echo 
$content;
}
foreach(
$_SESSION['cart'] as $sessions) {
    echo 
$sessions;

And, of course, I have a session_start() at the beginning of the page before the <html> tag. Do you have any fix that would work with this code?

Thanks.
__________________
if($stevej == "helpful") { $talkupation += $user_power; }
stevej is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-28-2009, 11:20 AM Re: Sessions and simplexml_load_file?
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
Well guys, never mind. I did a little more research, a little more tinkering with the code, and I finally got it to work. The error still displays, but @session_start fixed that. I also had to cast the session as a string. Here's my code:

PHP Code:
$xml_data simplexml_load_file('products.xml');

$products = array();

if(!isset(
$_SESSION['cart'])){
    
$_SESSION['cart'] = array();


foreach (
$xml_data->product as $product) {
    
$id $product->id;
    
$name $product->name;
    
$description $product->description;
    
$image $product->image;
    
$price $product->price;

    
$status "";

    if(
$_POST['cart'.$id]) {
        
$status "Product added";
        
$_SESSION['cart'][] = (string)$id;
    }

    
$products[] = "
    <div class='product'>
    <h5>"
.$id."</h5>
    <h3>"
.$name."</h3>
    <p>"
.$description."</p>
    <p>"
.$image."</p>
    <h4>"
.$price."</h4>
    <form action='?' method='POST'>
    <input type='submit' value='Add to cart' name='cart"
.$id."' />
    </form>
    "
.$status."
    </div>"
;
}  

foreach(
$products as $content) {
    echo 
$content;
}
foreach(
$_SESSION['cart'] as $sessions) {
    echo 
$sessions;

BTW, thanks for all the help guys!
__________________
if($stevej == "helpful") { $talkupation += $user_power; }
stevej is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Sessions and simplexml_load_file?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.52427 seconds with 12 queries