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> </td>';
}
echo "</tr>\
";
}
echo "</table>\
";
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> </td>';
}
$display .= "</tr>\
";
}
$display .= "</table>\
";
}
}
?>
<html>
<body>
<?php
if(isset($display)){ echo $display;}
?>
</body>
</html>