Jump to content

Array using foreach.


claro

Recommended Posts

I got this already, my problem is I can't get all values of my $prog array because of '=>' operator.

Is there any way to get rid of this?

 

foreach ($_POST['course'] as $val =>$course)
{
	$prog = $_POST['program'][$val]	;
	echo $course;
	echo $prog;
}

Link to comment
Share on other sites

What?! You must have no idea what you are doing because what you've stated makes absolutely no sense. The => operator does not prevent you in any way from accessing all the values of the array.

 

If I had to guess, I would say that the arrays $_POST['course'] and $_POST['program'] do not have the same keys. So, why are you trying to use the key of one array to access the values of a different array? Since I do not understand the relationship of $_POST['course'] and $_POST['program'] in your application I cannot provide any advice on what you could do to solve your problem.

 

Post what the typical values of the two arrays will hold and explain what you want to achieve

Link to comment
Share on other sites

You will need to do a foreach on each array separately. However, if the two arrays have values that should be "associated" with each other and the arrays can have different number of elements you need to explain how they should be associated so we can help you. Again, providing an example of the contents of those arrays and an example of how you want them to be output would help.

Link to comment
Share on other sites

thank you for your time. I did a research about array. I found a code that can get the value of my arrays and it went well. My problem is how to put it in my queries. Below is are my codes.

 

$n=0;
foreach ($_POST['course'] as $course) {
$bigar[$n][1] = $course;
$n++;
}
$n=0;
foreach ($_POST['program'] as $program) {
$bigar[$n][2] = $program;
$n++;
}

foreach ($bigar as $part) 
{
$course =  $part[1];
$program = $part[2];
//echo $course;
//echo $program;

	$stud = mysql_query ("SELECT * FROM vw_report_instructor WHERE user_Program = '$program' AND course_Id = '$course' ") or die (mysql_error());	

	if (mysql_num_rows($stud))
	{
		while ($row = mysql_fetch_array ($stud))
		{
				echo "<tr><td>".$row['user_Fname']." ".$row['user_Lname']."</td>";
				echo "<td>".$row['course_Title']."</td>";
				echo "<td>".$row['se_Yearlevel']."</td>";
				echo "<td>".$row['user_Program']."</td></tr>";
		}
	}
	else
	{	
		echo "<table><br/><br/>";
		echo "<p style = 'text-indent: 29em;'>no match found</p>";
		echo "</table>";
	}
}

}


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.