Jump to content

Recommended Posts

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

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>";
	}
}

?>


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']))));

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";

 

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));

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.