Jump to content

Name and Array


phpretard

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/104743-name-and-array/
Share on other sites

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536178
Share on other sites

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

  }

Link to comment
https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536199
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536248
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536265
Share on other sites

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


}

Link to comment
https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536304
Share on other sites

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

}

Link to comment
https://forums.phpfreaks.com/topic/104743-name-and-array/#findComment-536993
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.