Creating a Matrix table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeremydt
Forum Newbie
Posts: 1
Joined: Tue Sep 07, 2010 1:30 am

Creating a Matrix table

Post by jeremydt »

Hi,

I'm brand new to the forum; just introduced myself here :
viewtopic.php?f=6&t=120872

I'm having a bit of trouble trying to create a matrix; I'm still coding proceduraly as I haven't really ventured into OOP yet. Can someone please help me to see where I've gone wrong.

Effectively this code (and I've censored the code to remove some sensitive information) should produce a table with the headings pertaining to companies that
are effected by a certain problem. So there is a list of problems, then either the word Yes or No should appear under the company/department according to
whether or not the company is effected by that problem.

The database and everything connects, the table generates, but for some reason, and I haven't figured it out yet, the following code gets executed even when it's not supposed to. In other words, I get a whole heap of redundant cells (td's) in each row (tr). I'm thinking it could be because multiple departments are effected by an issue.

Thanks in advance.

Code: Select all

					else {
						echo "\t\t\t<td>NO</td>\n";
					}

Code: Select all

<?php
    include("dbc.php");

			//actions for existing problem
		echo "	<h2>Table of Contents</h2>		";
		
			$result = mysql_query("SELECT * FROM problem where problem_number LIKE '%PBI%' ORDER BY problem_approveddate desc");

			echo "<table border='1'>
			<tr>
			<th>#</th>
			<th>Short Description</th>
			<th>SomeDepartment</th>
			<th>AnotherDepartment</th>
			<th>OtherDepartment</th>
			<th>DifferentDepartment</th>
			<th>SomeoneElse</th>
			<th>AnotherCustomer</th>
			<th>SomeClient</th>
			<th>Someotherclient</th>
			<th>Someuserbase</th>
			<th>somebuilding</th>
			</tr>\n\n";

			while($row = mysql_fetch_array($result)) {
				$problem_id = $row['problem_id'];
				echo "\t\t\t<tr>\n";
				echo "\t\t\t<td>" . $row['problem_number'] . "</td>\n";
				echo "\t\t\t<td>" . $row['problem_short'] . "</td>\n";
			
				$departmentresult = mysql_query("SELECT department.*, relate_problem_department.* FROM problemmanagement.relate_problem_department INNER JOIN problemmanagement.department ON relate_problem_department.department_id = department.department_id WHERE relate_problem_department.problem_id = \"$row[problem_id]\"");
				while($departmentsimpacted = mysql_fetch_array($departmentresult)) {
					if (($departmentsimpacted[department_name]) == "SomeDepartment") {
						echo "\t\t\t<td><b>YES</b></td>\n";
						}
					else {
						echo "\t\t\t<td>NO</td>\n";
					}
					if (($departmentsimpacted[department_name]) == "AnotherDepartment") {
						echo "\t\t\t<td><b>YES</b></td>\n";
						}
					else {
						echo "\t\t\t<td>NO</td>\n";
					}
					if (($departmentsimpacted[department_name]) == "OtherDepartment") {
						echo "\t\t\t<td><b>YES</b></td>\n";
						}
					else {
						echo "\t\t\t<td>NO</td>\n";
					}
					if (($departmentsimpacted[department_name]) == "DifferentDepartment") {
						echo "\t\t\t<td><b>YES</b></td>\n";
						}
					else {
						echo "\t\t\t<td>NO</td>\n";
					}
					if (($departmentsimpacted[department_name]) == "SomeoneElse") {
						echo "\t\t\t<td><b>YES</b></td>\n";
						}
					else {
						echo "\t\t\t<td>NO</td>\n";
					}
					if (($departmentsimpacted[department_name]) == "AnotherCustomer") {
						echo "\t\t\t<td><b>YES</b></td>\n";
						}
					else {
						echo "\t\t\t<td>NO</td>\n";
					}
					if (($departmentsimpacted[department_name]) == "SomeClient") {
						echo "\t\t\t<td><b>YES</b></td>\n";
						}
					else {
						echo "\t\t\t<td>NO</td>\n";
					}
					if (($departmentsimpacted[department_name]) == "Someotherclient") {
						echo "\t\t\t<td><b>YES</b></td>\n";
						}
					else {
						echo "\t\t\t<td>NO</td>\n";
					}
					if (($departmentsimpacted[department_name]) == "Someuserbase") {
						echo "\t\t\t<td><b>YES</b></td>\n";
						}
					else {
						echo "\t\t\t<td>NO</td>\n";
					}
					if (($departmentsimpacted[department_name]) == "somebuilding") {
						echo "\t\t\t<td><b>YES</b></td>\n";
						}
					else {
						echo "\t\t\t<td>NO</td>\n";
					}

					
					echo "\t\t\t</tr>\n\n";
				}
			}
			
			echo "</table>";
			?>
Example HTML Output of script:

Code: Select all

<h2>Table of Contents</h2>		
	<table border='1'> 
			<tr> 
			<th>#</th> 
			<th>Short Description</th> 
			<th>SomeDepartment</th> 
			<th>AnotherDepartment</th> 
			<th>OtherDepartment</th> 
			<th>DifferentDepartment</th> 
			<th>SomeoneElse</th> 
			<th>AnotherCustomer</th> 
			<th>SomeClient</th> 
			<th>Someotherclient</th> 
			<th>Someuserbase</th> 
			<th>somebuilding</th> 
			</tr> 
 
			<tr> 
			<td>PBI123</td> 
			<td>Some Problem</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td><b>YES</b></td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			</tr> 
 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td><b>YES</b></td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			</tr> 
 
			<tr> 
			<td>PBI124</td> 
			<td>Some Other Problem</td> 
			<td><b>YES</b></td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			</tr> 
 
			<td>NO</td> 
			<td><b>YES</b></td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			</tr> 
 
			<td>NO</td> 
			<td>NO</td> 
			<td><b>YES</b></td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			</tr> 
 
			<tr> 
			<td>PBI125</td> 
			<td>Some New Problem</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td><b>YES</b></td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			</tr> 
 
			<tr> 
			<td>PBI126</td> 
			<td>Some Different Problem</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td><b>YES</b></td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			<td>NO</td> 
			</tr>
		</table>
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Creating a Matrix table

Post by JakeJ »

Each time one of your IF statements evaluates to false, you're doing this: echo "\t\t\t<td>NO</td>\n";

I think what you need to do is use a switch and case statements.

Code: Select all

$i = 10;

switch ($i) {

case 1:
  //do something
break;

case 2:
  //do something else
break;
default:
echo "\t\t\t<td>NO</td>\n";
}
Note that if none of the cases match, the default is your oft repeated else statement. It's like a bunch of different If statements with one else at the end that covers the previous If's.
Post Reply