Jump to content

array help part 3


imarockstar

Recommended Posts

Thanks again for the help guys !!! I think this is the last step that I need help with then I can take it the rest of the way ...

 

the code below pulls the POST data from the GROUPS of questions, then puts them into an array ...

 

working code :

 

<?php

include_once('');

if ( $_POST['submit'] ){

   for($i=1;$i<4;$i++){
   
      foreach($_POST["group$i"] as $key=>$val){
      
         echo "$key: $val";
         
         }//end foreach
         
         echo "<br><br>";

      }//end for
             
   }//end if
?>



<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" ?>



<!-- group 1 -->
field 1 : <input name="group1[text1]" size="20" > <br>
field 2 : <input name="group1[text2]" size="20" > <br>
field 3 : <input name="group1[text3]" size="20" > <br><br>


<!-- group 2 -->
field 4 : <input name="group2[text1]" size="20" > <br>
field 5 : <input name="group2[text2]" size="20" > <br>
field 6 : <input name="group2[text3]" size="20" > <br><br>

<!-- group 3 -->
field 7 : <input name="group3[text1]" size="20" > <br>
field 8 : <input name="group3[text2]" size="20" > <br>
field 9 : <input name="group3[text3]" size="20" > <br><br>





<input type="submit" name="submit" value="test">

</form>

 

 

my problem is that when completed .. the form will be dynamic, so each of the questions group name will be different. and I do not know how to change the FOREACH code at the top to reflect that ...

 

below is the code that I have put in place, however it does not work since  the FOREACH is not recognizing that the group name is dynamic ..

 

this is the part I cant figure out :  foreach($_POST["group$i"] as $key=>$val)

 

this : name="<?=$rows['edu_id']; ?>[text1]" will be pulled from a DB and the EDU_ID will be the name of the different arrays.

 

***I do have a DB connection in place, its just not being shown here..

 

 

non working code

 

<?php

include_once('settings.php');

if ( $_POST['submit'] ){

   for($i=1;$i<4;$i++){
   
      foreach($_POST["group$i"] as $key=>$val){
      
         echo "$key: $val";
         
         }//end foreach
         
         echo "<br><br>";

      }//end for
             
   }//end if
?>



<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" ?>


<?php
$sql="SELECT * FROM edu ORDER BY edu_id ASC";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>


<!-- group 1 -->
field 1 : <input name="<?=$rows['edu_id']; ?>[text1]" size="20" > <br>
field 2 : <input name="<?=$rows['edu_id']; ?>[text2]" size="20" > <br>
field 3 : <input name="<?=$rows['edu_id']; ?>[text3]" size="20" > <br><br>





<? } ?>



<input type="submit" name="submit" value="test">

</form>

 

 

 

 

 

Link to comment
Share on other sites

you can try recreating the query in the save

 

<?php

include_once('settings.php');

if ( $_POST['submit'] ){

$sql="SELECT * FROM edu ORDER BY edu_id ASC";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
$name=$rows['edu_id'];

   for($i=1;$i<4;$i++){
   
      foreach($_POST["$name$i"] as $key=>$val){
      
         echo "$key: $val";
         
         }//end foreach
         
         echo "<br><br>";

      }//end for
             
   }//end if
   
   
}//end while
   
?>

 

obv you'll have to re connect to the db, or just move your connection up to the top of the page.

 

and you may have to fiddle with the ["$name$i"] part. it may be [$name.$i] or something like that.

 

this is also dependent on there being 3 for each entry in the db.

 

lemme know

Link to comment
Share on other sites

here is the code thats getting the error :

 

<?php

include_once('settings.php');

if ( $_POST['submit'] ){

$sql="SELECT * FROM edu ORDER BY edu_id ASC";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
$name=$rows['edu_id'];

   for($i=1;$i<4;$i++){
   
      foreach($_POST["$name$i"] as $key=>$val){
      
         echo "$key: $val";
         
         }//end foreach
  
         
         echo "<br><br>";

      } //end for
             
   } } //end if
?>



<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" ?>


<?php
$sql="SELECT * FROM edu ORDER BY edu_id ASC";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>


<!-- group 1 -->
field 1 : <input name="<?=$rows['edu_id']; ?>[text1]" size="20" > <br>
field 2 : <input name="<?=$rows['edu_id']; ?>[text2]" size="20" > <br>
field 3 : <input name="<?=$rows['edu_id']; ?>[text3]" size="20" > <br><br>





<? } ?>



<input type="submit" name="submit" value="test">

</form>

 

 

Link to comment
Share on other sites

it's looking like you'll need a multi-dimensional array here. use the group number as one key, and the input number as the second key:

 

<input name="group[1][1]" type="text" />
<input name="group[1][2]" type="text" />
<input name="group[1][3]" type="text" />

<input name="group[2][1]" type="text" />
<input name="group[2][2]" type="text" />
<input name="group[2][3]" type="text" />

<input name="group[3][1]" type="text" />
<input name="group[3][2]" type="text" />
<input name="group[3][3]" type="text" />

 

to traverse through each group, you can use one foreach(), and to traverse through the inputs in each group, you can use a second foreach():

 

foreach ($_POST['group'] AS $group_number => $input_array)
{
  echo 'this is group '.$group_number.':<br />';
  foreach ($input_array AS $input_number => $input_text)
  {
    echo 'input number '.$input_number.' has a value of '.$input_text.'<br />';
  }
}

 

give that a shot - just running that code should give you an idea of how foreach() and arrays work.

Link to comment
Share on other sites

well the problem is the the 'group' will be different for every group of questions ...

 

 

basically i have say 20 questions, and each question has 3 sub questions. well when i post that data i need it all in one array, and the group will be the question id of that question, so. ... the sub questions will have the same question id as the main question .. .

 

um i think i just confused myself ..

Link to comment
Share on other sites

it will all be in one gigantic array with the method i suggested. the first key will be the question number, the second key will be the sub-question:

 

echo $_POST['group'][1][2]; // shows the response to subquestion #2 of question #1

 

the first foreach() will traverse through each individual question, and the foreach() embedded within the first will traverse through each subquestion in each question.

Link to comment
Share on other sites

yes yes .. that much i understand lol ... but I probably explained it wrong ..

 

this part :

 

<input name="group[1][1]" type="text" />

<input name="group[1][2]" type="text" />

<input name="group[1][3]" type="text" />

 

<input name="group[2][1]" type="text" />

<input name="group[2][2]" type="text" />

<input name="group[2][3]" type="text" />

 

<input name="group[3][1]" type="text" />

<input name="group[3][2]" type="text" />

<input name="group[3][3]" type="text" />

 

I put that as an example, probably a bad one ... since the form is dynamic it will really look like this :

 

 

<input name="10[1][1]" type="text" />

<input name="10[1][2]" type="text" />

<input name="10[1][3]" type="text" />

 

<input name="25[2][1]" type="text" />

<input name="25[2][2]" type="text" />

<input name="25[2][3]" type="text" />

 

<input name="30[3][1]" type="text" />

<input name="30[3][2]" type="text" />

<input name="30[3][3]" type="text" />

 

the 10,25 and 30 .. those are the question ID's.

 

i have a huge DB of questions with each with each question having its own id .. so i need to group questions based on the main questions id ..

 

i will try your code though .. so i can at least understand it !!

 

 

 

 

Link to comment
Share on other sites

is there a reason for having the extra key then? it looks entirely redundant, if you could just go:

 

<input name="question[10][1]" type="text" />
<input name="question[10][2]" type="text" />
<input name="question[10][3]" type="text" />

<input name="question[25][1]" type="text" />
<input name="question[25][2]" type="text" />
<input name="question[25][3]" type="text" />

 

perhaps i'm missing something entirely here.

Link to comment
Share on other sites

ya my bad man .... i put your code in there and it works just fine ... but here is where i am having the trouble ... this id your code WITH my additions .. i will explain after the code what I did ..

 

url page : http://franklinspirko.com/sites/questions/scripts/testing.php

 

<?php

include_once('settings.php');

if ( $_POST['submit'] ){



   for($i=1;$i<4;$i++){
   
      foreach($_POST["$name$i"] as $key=>$val){
      
         echo "$key: $val";
         
         }//end foreach
  
         
         echo "<br><br>";

      } //end for
             
   }  //end if
?>



<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" ?>


<?php
$sql="SELECT * FROM edu ORDER BY edu_id ASC";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>


This will be the main question : <BR>

subquestion 1 : <input name="<? echo $rows['udu_id']; ?>[1][1]" type="text" /> <br>
subquestion 2 : <input name="<? echo $rows['udu_id']; ?>[1][2]" type="text" /> <br>
subquestion 3 : <input name="<? echo $rows['udu_id']; ?>[1][3]" type="text" /> <br><br>







<? } ?>



<input type="submit" name="submit" value="test">

</form>

 

 

when all the questions get printed out, i have them grouped by the main QUESTION ID "<input name="<? echo $rows['udu_id']; ?>[1][1]" type="text" />"

 

so the group name will be changing every time since its looping through the database.

 

so in your code :

 

foreach ($_POST['group'] AS $group_number => $input_array)
{
  echo 'this is group '.$group_number.':<br />';
  foreach ($input_array AS $input_number => $input_text)
  {
    echo 'input number '.$input_number.' has a value of '.$input_text.'<br />';
  }
}

 

this part :

 

foreach ($_POST['group'] AS $group_number => $input_array)

 

will not work since

 

$_POST['group']

 

will be different for each array ...

 

does that make better since to what I am trying to do ?

 

 

Link to comment
Share on other sites

we're going in circles here. when echoing the input tag to the browser, DON'T give them a variable variable name. this:

 

"<input name="<? echo $rows['udu_id']; ?>[1][1]" type="text" />"

 

should be:

 

"<input name="question[<? echo $rows['udu_id']; ?>][1]" type="text" />"

 

this will result in the structure i explained above, with output like this:

 

<input name="question[10][1]" type="text" />

Link to comment
Share on other sites

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.