phpretard Posted May 8, 2008 Share Posted May 8, 2008 How can I Give this array a name? $Cnumber=$_GET['cn']; $invoice= $_POST['file']; foreach($_POST as $key => $val){ $_SESSION[$invoice][$key] = $val; } //CLOSE FOR EACH This Makes and Array Like This: $_SESSION:Array ( [] => Array //CAN I REFERENCE THIS ARRAY BY NAME AND IF SO HOW DO I NAME IT? ( [customer] => ENS Metals and Jewelry, Inc. [category] => Marketing Material [product] => Tri-Fold Brochure [Cnumber] => 156882 [Qty] => [item] => Marketing Material [Description] => Tri-Fold Brochure | Full Color [Cost] => [Date] => 05-08-2008 [Paid] => ) ) OR...Does It Already Have a name and I am missing it? Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/ Share on other sites More sharing options...
BlueSkyIS Posted May 8, 2008 Share Posted May 8, 2008 the name of the array is $_SESSION Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536166 Share on other sites More sharing options...
phpretard Posted May 8, 2008 Author Share Posted May 8, 2008 Could I insert this into as database table? Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536167 Share on other sites More sharing options...
BlueSkyIS Posted May 8, 2008 Share Posted May 8, 2008 it's essentially the same as any other array, so you can do with it whatever you can do with any other array. Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536169 Share on other sites More sharing options...
phpretard Posted May 8, 2008 Author Share Posted May 8, 2008 So to insert I guess wouold be: mysql_query("INSERT INTO table (id, invoice) VALUES ('', '$_SESSION' )"); ?? Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536173 Share on other sites More sharing options...
BlueSkyIS Posted May 8, 2008 Share Posted May 8, 2008 that would simply insert the string 'array' into your database. you need to specify each value of the array. $an_array = array('val1'=>'something1','val2'=>'something2'); $sql = "INSERT INTO sometable (somefield) VALUES ('{$an_array['val1']}')"; mysql_query($sql) or die(mysql_error()); $sql2 = "INSERT INTO sometable (somefield) VALUES ('{$an_array['val2']}')"; mysql_query($sql2) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536178 Share on other sites More sharing options...
phpretard Posted May 8, 2008 Author Share Posted May 8, 2008 I serialized it and inserted it that way. Well it worked and all of the data in in the database and now I can't get it back out in the form of an array? ROW1 now = a:1:{s:0:"";a:10:{s:8:"customer";s:28:"ENS Metals and Jewelry, Inc.";s:8:"category";s:18:"Marketing Material";s:7:"product";s:17:"Tri-Fold Brochure";s:7:"Cnumber";s:6:"156882";s:3:"Qty";s:0:"";s:4:"Item";s:18:"Marketing Material";s:11:"Description";s:30:"Tri-Fold Brochure | Full Color";s:4:"Cost";s:0:"";s:4:"Date";s:10:"05-08-2008";s:4:"Paid";s:0:"";}} How can I get it back out in the form of an array again? $result = mysql_query("SELECT * FROM table"); while($row = mysql_fetch_array($result)) { $ROW1=$row['ROW1']; echo "ARRAY HERE"; } Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536199 Share on other sites More sharing options...
BlueSkyIS Posted May 8, 2008 Share Posted May 8, 2008 can you unserialize() the stored value? Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536201 Share on other sites More sharing options...
phpretard Posted May 8, 2008 Author Share Posted May 8, 2008 I am testing this: $result = mysql_query("SELECT * FROM invoices"); while($row = mysql_fetch_array($result)) { $test=unserialize($row['description']); echo $test; } And all I get is: Array Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536203 Share on other sites More sharing options...
BlueSkyIS Posted May 8, 2008 Share Posted May 8, 2008 yes, $test is now the array you stored. reference it's contents just like any array: $a_value = $test['val1']; echo $a_value; Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536205 Share on other sites More sharing options...
DarkWater Posted May 8, 2008 Share Posted May 8, 2008 Are you sure you know what an array is? No offense. Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536210 Share on other sites More sharing options...
phpretard Posted May 8, 2008 Author Share Posted May 8, 2008 I do struggle with it. Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536225 Share on other sites More sharing options...
DarkWater Posted May 8, 2008 Share Posted May 8, 2008 An array is a collection of variables. You can't just "echo" an array, because it doesn't know what you actually want from the array. Read up on it in the manual on arrays. Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536226 Share on other sites More sharing options...
phpretard Posted May 8, 2008 Author Share Posted May 8, 2008 I can work with arrays. I am trying to get it unserialzed back into the array format I am familiar with. Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536242 Share on other sites More sharing options...
wildteen88 Posted May 8, 2008 Share Posted May 8, 2008 It looks like you havn't setup any keys in your serialized array. If you haven't defined any keys you'll have to use $arr[0] to get the first item from the array $arr[1] to get the second ... etc... $arr[5] to get the sixth item. Remember arrays start at zero and not one Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536248 Share on other sites More sharing options...
phpretard Posted May 8, 2008 Author Share Posted May 8, 2008 I really appreiciate your patients with my ignorance. Sometime the answers given are clear to me and sometimes they are not. Beleive me I have read many tutotials on arrays and the one problem I have is pulling just one value from an array. I truly follow the terms of this sight. Please Let me give you the full detail of this project... I am using a form and posting it to a page to build an array and store that array into a databse. Later it will be called to populate cells (<td>) in a table (<table>). MY FORM: <input type=text name='Cnumber' value='$cn'> <input type=text name='Qty' value=''> <input type=text name='Item' value='$p'> <input type=text name='Description' value='$I | Full Color'> <input type=text name='Cost' value=''> <input type=text name='Date' value='$today'> <input type=text name='Paid' value=''> POSTED TO: $Cnumber=$_GET['cn']; $invoice = $_POST['file']; foreach($_POST as $key => $val){ $_SESSION[$invoice][$key] = $val; } //CLOSE FOR EACH $cost=serialize($_SESSION); include 'connect.php'; mysql_query("INSERT INTO invoices (id, description) VALUES ('', '$cost')"); THIS ALL WORKS. My prblem is getting them later into the cells mentioned earlier. MY LASTEST ATEMPT: $result = mysql_query("SELECT * FROM invoices WHERE Cnumber='123456'"); while($row = mysql_fetch_array($result)) { $test=unserialize($row['description']); print_r(array($test)); } My Attempt Yields: Array ( [0] => Array ( [] => Array ( [customer] => ENS Metals and Jewelry, Inc. [category] => Marketing Material [product] => Tri-Fold Brochure [Cnumber] => 156882 [Qty] => [item] => Marketing Material [Description] => Tri-Fold Brochure | Full Color [Cost] => [Date] => 05-08-2008 [Paid] => ) ) ) I only need the items such as :"ENS Metals and Jewelry, Inc" AND "Marketing Material" Basically the values. I could be setting up the array wrong in the first place (like you said). Can you show me a better way. It seems the tutorials and scripts I find don't seem to apply to what I am doing am therfore confuse me. If you choose to help please explain as well. I appreciate it !! -Anthony Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536265 Share on other sites More sharing options...
wildteen88 Posted May 8, 2008 Share Posted May 8, 2008 Yes it seem your array is not being made correctly. Can you post more code of your form. And explain what information gets provided in the form and where the form gets submitted too. Then post the PHP code which process the form. Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536270 Share on other sites More sharing options...
phpretard Posted May 8, 2008 Author Share Posted May 8, 2008 Here is what my inexperience has come up with: MY FORM: <form action='POST PAGE' method=post> <input type=text name='Cnumber' value='123456'> <input type=text name='Qty' value='10'> <input type=text name='Item' value='Marketing Material'> <input type=text name='Description' value='Two Sides | Full Color'> <input type=text name='Cost' value='125.00'> <input type=text name='Date' value='May 8, 2008'> <input type=text name='Paid' value='0'> <input type=submit name=submit> </form> POST PAGE: //Basically I Would Like to store all of the posted information into an array and insert it into the database. I think the code goes something like this but I know it's probably way off. if (isset($_POST['submit'])){ $Cnumber=$_POST['Cnumber']; foreach($_POST as $key => $val){ $invoice= array($key => $val); } include '../../../../connect.php'; mysql_query("INSERT INTO invoices (id, Cnumber, invoice) VALUES ('', '$Cnumber', '$invoice')"); } Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536304 Share on other sites More sharing options...
phpretard Posted May 8, 2008 Author Share Posted May 8, 2008 Did this help in understanding my problem? Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536350 Share on other sites More sharing options...
wildteen88 Posted May 9, 2008 Share Posted May 9, 2008 Change this code: if (isset($_POST['submit'])){ $Cnumber=$_POST['Cnumber']; foreach($_POST as $key => $val){ $invoice= array($key => $val); } to: if (isset($_POST['submit']) && is_numeric($_POST['Cnumber'])) { $Cnumber = $_POST['Cnumber']; $invoice = mysql_real_escape_string(serialize($_POST)); include '../../../../connect.php'; mysql_query("INSERT INTO invoices (Cnumber, invoice) VALUES ('$Cnumber', '$invoice')"); } Quote Link to comment https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536993 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.