Need help in table design in displaying data from database

Hi,

I created query to display the reject acronym and reject name.

here is my code:



            $sql_reject = "select r.reject_acro, r.reject_id, r.reject_name from process_list AS p LEFT JOIN reject_list AS r ON
p.reject_id = r.reject_process_id
 where p.process_name LIKE '" . ($_POST["id"]) . "%'";

           $res_reject = mysql_query($sql_reject);
           echo "<table>";
           while($row_reject = mysql_fetch_assoc($res_reject)){
               $acro        = $row_reject['reject_acro'];
               $reject_name = $row_reject['reject_name'];

            echo "<tr>";
            echo "<td>$acro</td>";
            echo "<td>$reject_name</td>";
            echo "</tr>";

            }
           echo "</table>";

and my data in my database is this:

reject_acro|reject_name
AA---------Ant
BB---------Bee
CC---------Cat
DD---------Dog
EE---------Elephant
FF---------Frog
GG--------Goat
HH--------Horse
KK---------Kangaroo

now in this code the output is the same above

but i want it to display like this: I want to display in for columns

AA|Ant|BB|Bee|CC|CatDD|Dog|
EE|Elephant|FF|Frog|GG|Goat|HH|Horse
KK|Kangaroo

Thank you

When you get your associative array back from your mysql query use a foreach loop.

foreach ($row_reject AS $key => $value) {
	echo $key . " - " . $value . "<br>";
}

Thank you… I’ll try it later

I don’t think Banana Man’s comment is relevant. If I’m reading correctly your code is working but you want the data shown in 8 columns (you say 4 but your example shows 8 - in any case the principle is the same). So try something like this:


// add values to an array
$data = array();
while($row_reject = mysql_fetch_assoc($res_reject)){
 $data[] = $row_reject['reject_acro'];
 $data[] = $reject_name = $row_reject['reject_name'];
}

// set number of columns
$cols = 8;

// output table
echo "<table>\
";
$x = 0;
foreach ($data AS $v) {
 if ($x % $cols == 0) { // if $x is 0 or a multiple of $cols, start a new row
  echo '<tr>';
 }
 echo '<td>' . $v . '</td>';
 $x++;
 if ($x % $cols == 0) { // if $x is 0 or a multiple of $cols, end row
  echo "</tr>\
";
 }
}

// complete row if there aren't 8 items
if ($x % $cols > 0) {
 $rem = $cols - ($x % $cols); // number of columns needed to complete row
 for ($i = 0; $i < $rem; $i++) {
  echo '<td>&nbsp;</td>';
 }
 echo "</tr>\
";
}
echo "</table>\
";

Hi I tried this and it works.

Now I want to add table header.

Where can I add code for table header.

Thank you

As you are making a query directly from post, might I suggest making an approved list of Process Names and check POST against this array before making your query. Then adding the heading is much like was done for columns.

<?php
include "connection.php"; 
$display = "";

//Make array of valid process names
$valid_process_names = array();
$sql = "SELECT DISTINCT(process_name) FROM process_list";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
	$valid_process_names[] = $row['process_name'];
}
if(isset($_POST['id'])){ 
	//Double check that POST is a valid name
	if(!in_array($_POST['id'],$valid_process_names)){
		$display .= "Not a valid process name";
	}else{
		$sql_reject = "select r.reject_acro, r.reject_id, r.reject_name from process_list AS p LEFT JOIN reject_list AS r ON 
		p.reject_id = r.reject_process_id
		 where p.process_name LIKE '" . ($_POST['id']) . "%'";
		$res_reject = mysql_query($sql_reject);
		
		// add values to an array
		$data = array();
		while($row_reject = mysql_fetch_assoc($res_reject)){
		 $data[] = $row_reject['reject_acro'];
		 $data[] = $reject_name = $row_reject['reject_name'];
		}
		// set number of columns
		$cols = 8;
		
		// output table
		$display .= "<table>\
";
		
		
		$xh = 0;
		
			foreach ($data AS $v) {
				if($xh<$cols){
					$heading = ($xh % 2 == 0 ? "Reject Acro" : "Reject Name");
					if ($xh % $cols == 0) {
						$display .= '<tr>';
					}
					$display .= '<th>' . $heading . '</th>';
					$xh++;
					if ($xh % $cols == 0) { 
						$display .= "</tr>\
";
					}
				}
			}
		
		$x = 0;
		foreach ($data AS $v) {
			if ($x % $cols == 0) { // if $x is 0 or a multiple of $cols, start a new row
				$display .= '<tr>';
			}
			$display .= '<td>' . $v . '</td>';
			$x++;
			if ($x % $cols == 0) { // if $x is 0 or a multiple of $cols, end row
				$display .= "</tr>\
";
			}
		}
		
		// complete row if there aren't 8 items
		if ($x % $cols > 0) {
			$rem = $cols - ($x % $cols); // number of columns needed to complete row
			for ($i = 0; $i < $rem; $i++) {
				$display .= '<td>&nbsp;</td>';
			}
			$display .= "</tr>\
";
		}
		$display .= "</table>\
";
	}
} 
?>
<html>
<body>
<?php 
if(isset($display)){ echo $display;} 
?> 
</body>
</html>