Author Topic: Unserializing data  (Read 348 times)

0 Members and 1 Guest are viewing this topic.

Offline bullbreedTopic starter

  • Enthusiast
  • Posts: 109
  • Gender: Male
    • View Profile
Unserializing data
« on: March 18, 2010, 09:26:02 AM »
I have some serializes data in the db that holds the sizes of a product and i'm trying to unserialize it and show it in a menu form field but im having a few problems.

Heres the code

  
<?php
    
        $sql 
"SELECT * FROM `products` WHERE `prod_name` = '".$_GET['nid']."' limit 1 ";
        
$query mysql_query($sql) or die(mysql_error());
            
                    if (
mysql_num_rows($query) > 0) {
            
                            while (
$row mysql_fetch_assoc($query)){
                            
$sizes $row['prod_sizes'];
                            
$sizes stripslashes($sizes); 
                            
$sizes unserialize($sizes);
                            
$itemcount count($sizes);
                                    for (
$i=0;$i<$itemcount;$i++) {
            
    
?>      
    <div class="contwraps">
    <label>Select a size</label>
        <div class="textinput">
            <select name="sizeselect">
                <option><?php echo $sizes[$i]; ?></option>
            </select>
        </div>
    </div>
    
    
    <?php 
                                    
}
                            }
                    }
	
	
	
	
	

    
?>

Heres what it says on screen

Notice: unserialize() [function.unserialize]: Error at offset 18 of 20 bytes in C:\xampp\htdocs\Fight-Stuff\CommunicoreCMS\includes\prodtype.php on line 92

Line 92 is - $sizes = unserialize($sizes);

Whats wrong?
When I die, I want to go peacefully like my Grandfather did, in his sleep -- not screaming, like the passengers in his car.

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Unserializing data
« Reply #1 on: March 18, 2010, 09:35:13 AM »
Your serialized string is broken.

ps: Why are you stripping slashes from the retrieved data?

Offline bullbreedTopic starter

  • Enthusiast
  • Posts: 109
  • Gender: Male
    • View Profile
Re: Unserializing data
« Reply #2 on: March 18, 2010, 10:29:43 AM »
Ah yes, thank you. I set the size of the varchar to 10 (duh)

he result is a little strange though now I changes it because I now get each size displayed in its own menu dropdown so if I have sizes S-M-L-XL they display in their individual drop down menus instead of all being in 1 menu drop down
When I die, I want to go peacefully like my Grandfather did, in his sleep -- not screaming, like the passengers in his car.

Offline bullbreedTopic starter

  • Enthusiast
  • Posts: 109
  • Gender: Male
    • View Profile
Re: Unserializing data
« Reply #3 on: March 18, 2010, 11:24:08 AM »
How do I get them to show in one menu drop down. I remember reading somewhere about array-unique. Would this be used in the script. Im quite lost on this one.
When I die, I want to go peacefully like my Grandfather did, in his sleep -- not screaming, like the passengers in his car.

Offline bullbreedTopic starter

  • Enthusiast
  • Posts: 109
  • Gender: Male
    • View Profile
Re: Unserializing data
« Reply #4 on: March 21, 2010, 08:09:18 AM »
any ideas guyz and girlz  :shrug:

For each size in the database I get an individual menu field so if there are 5 sizws I get 5 menu drop doen boxes
« Last Edit: March 21, 2010, 08:16:36 AM by bullbreed »
When I die, I want to go peacefully like my Grandfather did, in his sleep -- not screaming, like the passengers in his car.

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Unserializing data
« Reply #5 on: March 21, 2010, 08:20:40 AM »
Quote
I remember reading somewhere about ...

Creating code that does what you want, when and where you want it to be, is not a matter of reading somewhere about something, it is a matter of making your code do what you want and when your code does not do what you want it is a matter of troubleshooting what it is doing. Did you look at and read the code that is responsible for producing the output in question.

The for(){} loop that is producing the menu contains the entire code for the <select>...</select> menu inside of it, so of course you will get a complete menu for each pass through the for(){} loop.

Wouldn't the solution be to output just the <option>...</option> choices inside of the for(){} loop?
« Last Edit: March 21, 2010, 08:21:11 AM by PFMaBiSmAd »
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline bullbreedTopic starter

  • Enthusiast
  • Posts: 109
  • Gender: Male
    • View Profile
Re: Unserializing data
« Reply #6 on: March 21, 2010, 09:16:32 AM »
Thanks for that.

Please be aware that I have only been doing PHP for a couple of months now and at 36 yrs old its a bit like teaching an old dog new tricks. lol. Remember I come from a time of Commodore 64 and BBC Master 128.  ;D

I changed the code to this;

<?php
    
        $sql 
"SELECT * FROM `products` WHERE `prod_name` = '".$_GET['nid']."' limit 1 ";
        
$query mysql_query($sql) or die(mysql_error());
            
                    if (
mysql_num_rows($query) > 0) {
            
                            while (
$row mysql_fetch_assoc($query)){
                            
$sizes $row['prod_sizes']; 
                            
$sizes unserialize($sizes);
                            
$itemcount count($sizes);
	
	
	
	
	
	
	
}
                    }
            
    
?>     
    <label>Select a Size</label>
        <div class="textinput">
            <select name="sizeselect">
            <?php for ($i=0;$i<$itemcount;$i++) { ?>
                <option><?php echo $sizes[$i]; ?></option> 
	
	
	
<?php ?>
            </select>


So if I understand correctly, the issue was because the For (){} loop was performing a loop for each size option as an individual, hence individual menu form fields.

But because I put the for(){} loop around only the <option>...</option> code this placed the data in one meny field.

Thanks for your help.
When I die, I want to go peacefully like my Grandfather did, in his sleep -- not screaming, like the passengers in his car.