bullbreed Posted March 18, 2010 Share Posted March 18, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/195682-unserializing-data/ Share on other sites More sharing options...
trq Posted March 18, 2010 Share Posted March 18, 2010 Your serialized string is broken. ps: Why are you stripping slashes from the retrieved data? Quote Link to comment https://forums.phpfreaks.com/topic/195682-unserializing-data/#findComment-1028084 Share on other sites More sharing options...
bullbreed Posted March 18, 2010 Author Share Posted March 18, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/195682-unserializing-data/#findComment-1028104 Share on other sites More sharing options...
bullbreed Posted March 18, 2010 Author Share Posted March 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195682-unserializing-data/#findComment-1028139 Share on other sites More sharing options...
bullbreed Posted March 21, 2010 Author Share Posted March 21, 2010 any ideas guyz and girlz 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 Quote Link to comment https://forums.phpfreaks.com/topic/195682-unserializing-data/#findComment-1029498 Share on other sites More sharing options...
PFMaBiSmAd Posted March 21, 2010 Share Posted March 21, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/195682-unserializing-data/#findComment-1029501 Share on other sites More sharing options...
bullbreed Posted March 21, 2010 Author Share Posted March 21, 2010 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. 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. Quote Link to comment https://forums.phpfreaks.com/topic/195682-unserializing-data/#findComment-1029535 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.