Jump to content

array in array but not wanted


freelance84

Recommended Posts

OK apologies for the length of this one.

 

Below is some php code which generates all the possible combinations from the result of user input in a table using radio buttons.

 

An example of the $_POST result from the user selecting one of all of the radio buttons is as follows:

Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [all_possible] => All Possible Results )

1-7 are the names of the radio buttons, and all_possible is the name of one of the buttons in the form.

 

The form relates to a table which looks something like:

a_number    |g_number    |option_number    |comment    |

101at1

201sldfhjlsd

301at3a

The table goes on...

 

The php code:

if (isset($_POST['all_possible']))
{
//getting users matrix answers
$r_number = array();	//[0]=at1 [1]=at2....
for($an = 1 ; $an < 8 ; ++$an)
	{
		$g_number = get_post($an);
		array_push($r_number, $g_number);
	}
//getting the number of options per g
$count = array();
for($op = 1 ; $op < 8 ; ++$op)
	{
		$rn = $op - 1;
		$query_option_quant = "SELECT option_number FROM matrix WHERE a_number='$op' AND g_number='$r_number[$rn]'";
		$option_result = mysql_query($query_option_quant);
		$rows = mysql_num_rows($option_result);
		array_push($count,$rows);
	}
//getting all possible combinations
$all_combinations = array();
for($a1 = 0 ; $a1 < $count[0] ; ++$a1)
	{
		for($a2 = 0 ; $a2 < $count[1] ; ++$a2)
			{
				for($a3 = 0 ; $a3 < $count[2] ; ++$a3)
					{
						for($a4 = 0 ; $a4 < $count[3] ; ++$a4)
							{
								for($a5 = 0 ; $a5 < $count[4] ; ++$a5)
									{
										for($a6 = 0 ; $a6 < $count[5] ; ++$a6)
											{
												for($a7 = 0 ; $a7 < $count[6] ; ++$a7)
													{
														array_push($all_combinations,$a1.",".$a2.",".$a3.",".$a4.",".$a5.",".$a6.",".$a7);
													}
											}
									}
							}
					}
			}
	}
$counted = count($all_combinations);
for($ctd = 0 ; $ctd < $counted ; ++$ctd)
	{
		$combination = explode(",",$all_combinations[$ctd]);
		for($atn = 1 ; $atn < 8 ; ++$atn)
			{
				$rpn = $atn - 1;
				$query_get_comment = "SELECT comment FROM matrix WHERE a_number = '$atn' AND g_number = '$r_number[$rpn]'";
				$get_result = mysql_query($query_get_comment);
				$number_comments = mysql_num_rows($get_result);
				$comment = array();     //this is array in question
				for($i = 0 ; $i < $number_comments ; ++$i)
					{
						$com = mysql_fetch_row($get_result);
						array_push($comment,$com);
					}
				echo $rpn."<br />";
				print_r($comment);
				echo "<br />";
			}
		echo "<br />";
		echo "<br />";
	}
}

 

 

The result of the above is:

0

Array ( [ 0] => Array ( [ 0] => at1 ) )

1

Array ( [ 0] => Array ( [ 0] => sldfhjlsd ) )

2

Array ( [ 0] => Array ( [ 0] => at3a ) )

3

Array ( [ 0] => Array ( [ 0] => at4a ) )

4

Array ( [ 0] => Array ( [ 0] => at5a ) )

5

Array ( [ 0] => Array ( [ 0] => at6a ) )

6

Array ( [ 0] => Array ( [ 0] => at7a ) [1] => Array ( [ 0] => at7b ) )

 

 

0

Array ( [ 0] => Array ( [ 0] => at1 ) )

1

Array ( [ 0] => Array ( [ 0] => sldfhjlsd ) )

2

Array ( [ 0] => Array ( [ 0] => at3a ) )

3

Array ( [ 0] => Array ( [ 0] => at4a ) )

4

Array ( [ 0] => Array ( [ 0] => at5a ) )

5

Array ( [ 0] => Array ( [ 0] => at6a ) )

6

Array ( [ 0] => Array ( [ 0] => at7a ) [1] => Array ( [ 0] => at7b ) )

 

 

 

The array in question I have commented out in the php code above. I can't understand why it isn't just creating a single dimension array?

Link to comment
Share on other sites

I can't believe it. After typing out all that thread I found i'de missed off a tiny little thing in my code:

 

$comment = array();
				for($i = 0 ; $i < $number_comments ; ++$i)
					{
						$com = mysql_fetch_row($get_result);
						array_push($comment,$com[0]);   // i left out the [0]
					}

 

Now it works  ::)

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.