4dim arrays ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

4dim arrays ??

Post by ddragas »

:twisted:
Why does always array function stick me in corner of getting solutions for resolving problems?


I'll try to explain as best I could my problem

I'm should get 4 values from my form (grade1, grade2, grade3, user_id)

put them in array and depending on user_id put them in the database


can somebody put me in right direction


p.s. using PHP4

regards ddragas
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Not enough information to really say much other than that isn't a four dimensional array, but two dimensional (at best).
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

how 2dim.?

I've got to put in array 4 values from my form with x recordsets

array1("grade1", "grade2", "grade3", "user_id")
array2("grade1", "grade2", "grade3", "user_id")
array3("grade1", "grade2", "grade3", "user_id")
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That's two dimensional. You have rows (the arrays) and columns (the values.) If it were four dimensional you'd have arrays inside of arrays inside of arrays inside of an array.

Anyways, your example would suggest simply iterating over the recordsets and inserting/updating as appropriate.

Now, if your submission isn't designed to be iterated in a simple fashion, this may be difficult, so it would be nice to see your form and whatever other code directly related to this you have and to know where you are having specific troubles.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

thank you feyd

I'll reply you tomorow

In Croatia is 02:25 AM, and I' infront of monitor from 13:30

A little bit tired


regards

ddragas
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Here is code of getting values from form

Code: Select all

if(isset($_POST['SUBMIT_edit_ocijena']))
	{
		foreach ($_POST['pawn'] as $pawn => $value)
                {
                       $pawn[] = $value;
                }
               foreach ($_POST['verbal_testing'] as $verbal_testing => $value)
                {
                       $verbal_testing[] = $value;
                }
               foreach ($_POST['written_testing'] as $written_testing => $value)
                {
                       $written_testing[] = $value;
                }
               foreach ($_POST['student_id'] as $student_id => $value)
                {
                       $student_id[] = $value;
                }
	}
How to join these 4 arrays in one array?


PHP5 uses array_combine function, but my webhosting service is using PHP4 so I'm forced to use PHP4
AshrakTheWhite
Forum Commoner
Posts: 69
Joined: Thu Feb 02, 2006 6:47 am

Post by AshrakTheWhite »

ok you dont need to foreach anything, because when you hit submit button one array of set parameters gets forwarded into PHP so if you want to insert them into a database all you have to do is:

Code: Select all

if(!empty($_POST))
{
   $array = $_POST;

   $sql = "INSERT INTO *TABLE_NAME* ('pawn', 'verbal_testing', 'written_testing') VALUES($array['pawn'], $array['verbal_testing'], $array['written_testing']) WHERE ID = $array['student_id']";
   mysql_query($sql);
   unset($_POST);
}

SQL might be wrong oh and you actually have to run the SQL too hehe
Last edited by AshrakTheWhite on Sun Oct 15, 2006 10:30 am, edited 6 times in total.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

and this code will insert all grades in db from each array?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Likely, it would not.. but it's difficult to tell how your form works without you posting it.
AshrakTheWhite
Forum Commoner
Posts: 69
Joined: Thu Feb 02, 2006 6:47 am

Post by AshrakTheWhite »

i went on the assumption that theres 4 boxes with values but if theres 16 x 4 boxes in 1 form you need to foreach yes but same query will work, only the values of the array will change :)
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

feyd wrote:Likely, it would not.. but it's difficult to tell how your form works without you posting it.


Here is form

I know that array_combine doesnt work with 4 , but only with 2.

Could this be a solution?

$array1 = array_combine($pawn,$student_id);
$array2= array_combine($written_testing,$verbal_testing);

$array3 = array_combine($array1,$array2);

Code: Select all

<?
if(isset($_POST['SUBMIT_edit_ocijena']))
	{

			   foreach ($_POST['pawn'] as $pawn1 => $value)
                {
                       $pawn[] = $value;
                }
               foreach ($_POST['verbal_testing'] as $verbal_testing1 => $value)
                {
                       $verbal_testing[] = $value;
                }
               foreach ($_POST['written_testing'] as $written_testing1 => $value)
                {
                       $written_testing[] = $value;
                }
               foreach ($_POST['student_id'] as $student_id1 => $value)
                {
                       $student_id[] = $value;
                }
				
			$niz = array_combine($student_id, $pawn, $verbal_testing, $written_testing);

			
			exit();
				
	}



	else

	{

		echo "<form id="form1" name="form1" method="post" action="">";
		for($i=1; $i <= 10; $i++)
			{

			
			echo "<p>" . $i;
			echo "<input type="hidden" name="student_id[]" value="$i"/>";
			echo "<input type="text" name="pawn[]" value="$i"/>";
			echo "<input type="text" name="verbal_testing[]" value="$i"/>";
			echo "<input type="text" name="written_testing[]" value="$i"/>";
			echo "</p>";
			}
		
		  echo "<p>";
		  echo "<input type="submit" name="SUBMIT_edit_ocijena" value="Submit" />";
		echo "</p>";
		echo "</form>";


}
?>
AshrakTheWhite
Forum Commoner
Posts: 69
Joined: Thu Feb 02, 2006 6:47 am

Post by AshrakTheWhite »

that wont work no matter what you do as far as i can tell, since your not identifying each row of the form, can you do a print_r($_POST); and copy paste the results here?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maybe this

Code: Select all

<?php $student_ids = array(3,6,9,11,34,38,57); ?>
<html>
	<head><title>...</title></head>
	<body>
		<pre><?php var_export($_POST); ?></pre>
		<form method="post">
<?php foreach( $student_ids as $id) { ?>
			<fieldset><legend><?php echo $id; ?></legend>
				<input type="text" name="entry[<?php echo $id; ?>][pawn]" value="a <?php echo $id; ?>" />
				<input type="text" name="entry[<?php echo $id; ?>][verbal_testing]" value="b <?php echo $id; ?>" />
				<input type="text" name="entry[<?php echo $id; ?>][written_testing]" value="c <?php echo $id; ?>" />
			</fieldset>
<?php } ?>
			<p><input type="submit" /></p>
		</form>
	</body>
</html>
gives you a hint.
AshrakTheWhite
Forum Commoner
Posts: 69
Joined: Thu Feb 02, 2006 6:47 am

Post by AshrakTheWhite »

adapt volkas code to your needs ;)
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Thank you all for reply

volkas code goves me what I need, and my next problem is how to insert values from form into db ?

I've tried what you've suggested me, but with no result of course with changed sql query

I get error in sql query, and error is
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\Apache Group\Apache2\htdocs\skola\Untitled-2.php on line 14

SQL QUERY WITH ERROR

Code: Select all

$sql = "INSERT INTO ocijene SET
   ocijena_usmeno = '$array['pawn']',
   ocijena_pismeno = '$array['verbal_testing']',
   ocijena_zalaganje = '$array['written_testing']',
   ucenik =  '$array['student_id']'";
Post Reply