dropfaith Posted October 22, 2008 Share Posted October 22, 2008 So unserilize is a new function to me completly never played with arrays in a dbs before(newb) was wondering if someone could explain how i can do this http://lawrenceguide.org/recipes/profile.php?Name=Beer%20nuts i want the array to unserilize and loop the rows like so Quantity1 ingredient1 Quantity2 ingredient2 Quantity3 ingredient3 i cant fiqure out how the sysntax or how this would be done is <?php // includes include("../template/conf.php"); // open database connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // Get all the data from the "Recipe" table $result = mysql_query("SELECT * FROM Recipes") or die(mysql_error()); echo "<table border='1'>"; echo "<tr> <th>Quantity</th> <th>Ingredients</th> </tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['Quantity']; echo "</td><td>"; echo $row['Ingredients']; echo "</td></tr>"; echo "</table>"; } ?> Link to comment https://forums.phpfreaks.com/topic/129588-solved-i-feel-dumb-unserilize-issues/ Share on other sites More sharing options...
aximbigfan Posted October 22, 2008 Share Posted October 22, 2008 $input = array( 'one', 'two'); $ser = serialize($input); //$ser is now a string $output = unserialize($ser); //$output is now the array echo $output[0]; echo $output[1]; That should give you some explanation. Chris Link to comment https://forums.phpfreaks.com/topic/129588-solved-i-feel-dumb-unserilize-issues/#findComment-671812 Share on other sites More sharing options...
dropfaith Posted October 22, 2008 Author Share Posted October 22, 2008 not really i cant seem to get it working from a select from the database also the two fields are currently varchar if that makes a difference Link to comment https://forums.phpfreaks.com/topic/129588-solved-i-feel-dumb-unserilize-issues/#findComment-671839 Share on other sites More sharing options...
dropfaith Posted October 22, 2008 Author Share Posted October 22, 2008 below is the relavant form code inserts thru this $Quantity = mysql_real_escape_string(trim(stripslashes(serialize($_POST['Quantity'])))); $Ingredients = mysql_real_escape_string(trim(stripslashes(serialize($_POST['Ingredients'])))); <?php for($i=0; $i<=15; $i=$i+1) { if($i % 2 == 0) { echo "<tr>"; echo "<td valign=\"top\" style=\"width:50px\">"; echo "<select name=\"Quantity[]\"><option value=\"1 cup\">1 cup</option></select>"; echo "</td>"; echo "<td valign=\"top\" style=\"width:75px\"><input type=text name=\"Ingredients[]\" size=\"25\"></td>"; echo "</tr>"; } else { echo "<tr>"; echo "<td valign=\"top\" style=\"width:50px\">"; echo "<select name=\"Quantity[]\"><option value=\"1 cup\">1 cup</option></select>"; echo "</td>"; echo "<td valign=\"top\" style=\"width:75px\"><input type=text name=\"Ingredients[]\" size=\"25\"></td>"; echo "</tr>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/129588-solved-i-feel-dumb-unserilize-issues/#findComment-671842 Share on other sites More sharing options...
Zane Posted October 22, 2008 Share Posted October 22, 2008 I don't quite understand what you're doing $Quantity = mysql_real_escape_string(trim(stripslashes(serialize($_POST['Quantity'])))); Only arrays are meant to be serialized...really unless you want to serialize every item in the array and then serialize the array again which is pointless. and also..on this line $Quantity = mysql_real_escape_string(trim(stripslashes(serialize($_POST['Quantity'])))); serialize would be the last thing you want to do, because you can't stripslashes, trim, and mysqlify a serialized string. If that makes sense This is what you'd change it to $Quantity = serialize(mysql_real_escape_string(trim(stripslashes($_POST['Quantity'])))); Link to comment https://forums.phpfreaks.com/topic/129588-solved-i-feel-dumb-unserilize-issues/#findComment-671846 Share on other sites More sharing options...
dropfaith Posted October 22, 2008 Author Share Posted October 22, 2008 http://lawrenceguide.org/user/addrecipe.php username phpfreaks password phpfreaks basicly im making a database for recipes i need that form submitting it creates the arrays for quantity and ingred i need to pull it back out and display it in a table like this http://lawrenceguide.org/recipes/profile.php?Name=Beer%20nuts repeating each item in a new table row Side note when i use $Ingredients = serialize(mysql_real_escape_string(trim(stripslashes($_POST['Ingredients'])))); $Quantity = serialize(mysql_real_escape_string(trim(stripslashes($_POST['Quantity'])))); it doesnt insert correctly and creates this s:5:"Array"; Link to comment https://forums.phpfreaks.com/topic/129588-solved-i-feel-dumb-unserilize-issues/#findComment-671852 Share on other sites More sharing options...
Zane Posted October 22, 2008 Share Posted October 22, 2008 it doesnt insert correctly and creates this s:5:"Array"; that's just what a serialized array looks like Why are you wanting to use serialize in the first place? If you're looking for simple encryption then look at base64_encode() and base64_decode...along with serialize $array = base64_encode(serialize($array)); Link to comment https://forums.phpfreaks.com/topic/129588-solved-i-feel-dumb-unserilize-issues/#findComment-671859 Share on other sites More sharing options...
dropfaith Posted October 22, 2008 Author Share Posted October 22, 2008 Qunatity a:16:{i:0;s:5:"1 cup";i:1;s:5:"1 cup";i:2;s:5:"1 cup";i:3;s:5:"1 cup";i:4;s:5:"1 cup";i:5;s:5:"1 cup";i:6;s:5:"1 cup";i:7;s:5:"1 cup";i:8;s:5:"1 cup";i:9;s:5:"1 cup";i:10;s:5:"1 cup";i:11;s:5:"1 cup";i:12;s:5:"1 cup";i:13;s:5:"1 cup";i:14;s:5:"1 cup";i:15;s:5:"1 cup";} Ingreds a:16:{i:0;s:7:"chicken";i:1;s:4:"beef";i:2;s:7:"lettuce";i:3;s:8:"chhessee";i:4;s:5:"dgewg";i:5;s:3:"weg";i:6;s:3:"weg";i:7;s:3:"weg";i:8;s:3:"weg";i:9;s:3:"ewg";i:10;s:0:"";i:11;s:0:"";i:12;s:0:"";i:13;s:0:"";i:14;s:0:"";i:15;s:0:"";} okay i have this serilized in my db i just need a way to display this <tr><td>Quantity </td><td>Chicken</td></tr> <tr><td>Quantity </td><td>beef</td></tr> <tr><td>Quantity </td><td>lettuce</td></tr> <tr><td>Quantity </td><td>Cheese</td></tr> <tr><td>Quantity </td><td>And so on</td></tr> Link to comment https://forums.phpfreaks.com/topic/129588-solved-i-feel-dumb-unserilize-issues/#findComment-671860 Share on other sites More sharing options...
aximbigfan Posted October 23, 2008 Share Posted October 23, 2008 You nee to unserialize it first. Chris Link to comment https://forums.phpfreaks.com/topic/129588-solved-i-feel-dumb-unserilize-issues/#findComment-672518 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.