Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic array

Status
Not open for further replies.

cogdev

MIS
Nov 30, 2001
85
US
I am trying to create an array dynamically: here is the scenario.
Code:
<?
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
 echo("<tr>\n <td>".$row["fac_name"]. "</td>");
 
 echo("<td>" .$row["dept"] ."</td>");
 
  if ($row["dept"]== "cs") {
 $numcs = $numcs + 1;
 }


instead of the line if ($row["dept"]== "cs") I would like to create an array that would use ' as an index. For example, I have a list that has 'CS', 'math', eng, sci etc.
What I want is to have an array that looks like this:

-------------------------------------
Cs | math | eng|
------------------------------------
and the value in array should be the number of times 'i' occurs in the list.

 
If you want to create the count buckets as you go:

Code:
<?
$counts = array();
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
	echo("<tr>\n <td>".$row["fac_name"]. "</td>");

	echo("<td>" .$row["dept"] ."</td>");

	if (isset ($counts[$row["dept"]]))
	{
		$counts[$row["dept"]]++;
	}
	else
	{
		$counts[$row["dept"]] = 1;
	}
}
?>

If you want to pre-create the count buckets:

Code:
<?
$categories = array('cs', 'math', 'eng');

$counts = array();
foreach ($categories as $key => $value)
{
	$counts[$key] = 1;
}

while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
	echo("<tr>\n <td>".$row["fac_name"]. "</td>");

	echo("<td>" .$row["dept"] ."</td>");

	$counts[$row["dept"]]++;
}
?>


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top