Page 1 of 2
4dim arrays ??
Posted: Sat Oct 14, 2006 5:42 pm
by ddragas
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
Posted: Sat Oct 14, 2006 5:57 pm
by feyd
Not enough information to really say much other than that isn't a four dimensional array, but two dimensional (at best).
Posted: Sat Oct 14, 2006 6:10 pm
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")
Posted: Sat Oct 14, 2006 7:12 pm
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.
Posted: Sat Oct 14, 2006 7:24 pm
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
Posted: Sun Oct 15, 2006 9:27 am
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
Posted: Sun Oct 15, 2006 10:18 am
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
Posted: Sun Oct 15, 2006 10:22 am
by ddragas
and this code will insert all grades in db from each array?
Posted: Sun Oct 15, 2006 10:57 am
by feyd
Likely, it would not.. but it's difficult to tell how your form works without you posting it.
Posted: Sun Oct 15, 2006 11:23 am
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

Posted: Sun Oct 15, 2006 11:28 am
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>";
}
?>
Posted: Sun Oct 15, 2006 11:47 am
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?
Posted: Sun Oct 15, 2006 11:59 am
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.
Posted: Sun Oct 15, 2006 12:11 pm
by AshrakTheWhite
adapt volkas code to your needs

Posted: Sun Oct 15, 2006 2:32 pm
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']'";