ok, i did the same thing with this function. I took the input froma form from the user and then searche through the db to find all the matches. This funcitons looks a little c omplex put it dynamically displays all the colum names and number of colums. so no matter what you do with the db it will automatically be refleced in the table that is output.
it also display the colum names as links so that the user can click on the links and sort my that colum asc or dsc. my results come up on multiple pages and so there is a part for jumping to other pages.
take your time and look through this and then rethink how you are coding your script. you don't want to have to write all of it again, ir you change your db. for example if you add antoher colum you are going to have to go back into your code and hardcode it. this dynamically pulls the mysql_num_cols out of the table nad display the correct number.
function display_record($table, $query, $title, $num_of_rows, $num_of_pages, $searchField, $searchString, $order_col, $order_dir, $start_record, $page_num, $category, $group_num)
{
$conn = db_connect();
if(!$conn)
return "Could not connect to databse at this time - please try again later.";
$result = mysql_query($query);
if (!$result)
{
echo "<font face=\"Tahoma\" size=\"3\">Please Enter Your Search Criteria Above...";
}
else if ($result)
{
$numFields = mysql_num_fields($result);
$numRows = mysql_num_rows($result);
$myrow = mysql_fetch_array($result);
echo "<table border=\"0\" width=\"50%\" cellspacing=\"3\" valign=\"top\">";
echo "<tr><td colspan=\"11\" height=\"2\" class=formFieldName align=\"left\" valign=\"top\"><font><b>$title</b></font></td></tr><tr>";
echo "<td width=\"40\" align=\"center\" bgcolor=\"f3f3f3\"><font face=\"Tahoma\" size=\"2\">Actions</font></td>";
if (strcmp($category, "search")== 0 || strcmp($category, "userdel") == 0)
{
for($i = 0; $i < $numFields; $i++)
{
if (strcmp($order_dir, "DESC") == 0)
{
$arrow = "images/downArrow.gif";
}
else
{
$arrow = "images/upArrow.gif";
}
if (mysql_field_name($result, $i) == $order_col)
{
printf ("<td align=\"center\" width=\"120\" bgcolor=\"#f3f3f3\" class=formFieldName valign=\"top\"><font face=\"Tahoma\" size=\"2\"><a href=\"search.php?page_num=%s&searchFeild=%s&searchString=%s&order_col=%s&order_dir=%s\">%s</a> <img src=\"%s\" valign=\"top\" border=\"0\"></font></td>", $page_num, $searchField, $searchString, mysql_field_name($result, $i), $order_dir, mysql_field_name($result, $i), $arrow);
}
else
{
printf ("<td align=\"center\" width=\"120\" bgcolor=\"#f3f3f3\" class=formFieldName valign=\"top\"><font face=\"Tahoma\" size=\"2\"><a href=\"search.php?page_num=%s&searchFeild=%s&searchString=%s&order_col=%s&order_dir=%s\">%s</a> </font></td>", $page_num, $searchField, $searchString, mysql_field_name($result, $i), $order_dir, mysql_field_name($result, $i));
}
}
}
else if (strcmp($category, "add") == 0)
{
for($i = 0; $i < $numFields; $i++)
{
printf ("<td align=\"center\" width=\"120\" bgcolor=\"#f3f3f3\" class=formFieldName valign=\"top\"><font face=\"Tahoma\" size=\"2\">%s</td>", mysql_field_name($result, $i));
}
}
else if (strcmp($category, "del") == 0)
{
for($i = 0; $i < $numFields; $i++)
{
if ($i == 0)
{
$org_serial = $myrow[0];
}
printf ("<td align=\"center\" width=\"120\" bgcolor=\"#f3f3f3\" class=formFieldName valign=\"top\"><font face=\"Tahoma\" size=\"2\">%s</td>", mysql_field_name($result, $i));
}
}
echo "<tr><td></td></tr>";
if (strcmp($category, "userdel") == 0)
{
for($i = 0; $i < $numRows; $i++)
{
printf ("<tr><td align=\"center\" bgcolor=\"#f3f3f3\" > <font face=\"Tahoma\" size=\"2\"><a href=\"add.php?serial_num=%s&edit=1\"><img src=\"images\edit.gif\" alt=\"Edit Record\" border=\"0\"></a> <a href=\"add.php?serial_num=%s&delete=1\"><img src=\"images\delete.gif\" alt=\"Delete Record\" border=\"0\"></a> <font></td>", $myrow[0], $myrow[0]);
for($j = 0; $j < $numFields; $j++)
{
printf ("<td align=\"center\" width=\"120\" bgcolor=\"#f3f3f3\" class=display_record valign=\"top\"><font face=\"Tahoma\" size=\"2\">%s</font></td>\n\n", $myrow[$j]);
}
$myrow = mysql_fetch_array($result);
echo "</tr>";
}
}
else
{
for($i = 0; $i < $numRows; $i++)
{
printf ("<tr><td align=\"center\" bgcolor=\"#f3f3f3\" > <font face=\"Tahoma\" size=\"2\"><a href=\"add.php?serial_num=%s&edit=1\"><img src=\"images\edit.gif\" alt=\"Edit Record\" border=\"0\"></a> <a href=\"add.php?serial_num=%s&delete=1\"><img src=\"images\delete.gif\" alt=\"Delete Record\" border=\"0\"></a> <font></td>", $myrow[0], $myrow[0]);
for($j = 0; $j < $numFields; $j++)
{
if ($j == 2)
{
$myrow[$j] = date("m/d/y", $myrow[$j]);
if (strcmp($myrow[$j], "12/31/69") == 0)
{
$myrow[$j] = "Not Available";
}
}
if (strcmp($myrow[$j], "") == 0)
{
$myrow[$j] = "--";
}
printf ("<td align=\"center\" width=\"120\" bgcolor=\"#f3f3f3\" class=display_record valign=\"top\"><font face=\"Tahoma\" size=\"2\">%s</font></td>\n\n", $myrow[$j]);
}
$myrow = mysql_fetch_array($result);
echo "</tr>";
}
}
echo "<tr>\n<td colspan=\"4\" height=\"10\" valign=\"top\"></td>\n</tr>\n\n";
echo "<tr>\n<td width=\"2\"></td><td colspan=\"2\" valign=\"top\"><font></font></td><td colspan=\"2\">\n<p align=\"right\"></p>\n</td>\n</tr>\n\n</table>\n\n";
echo "<div align=\"left\">";
if (strcmp($category, "search") == 0)
{
if ($num_of_pages < 10)
{
if ($num_of_rows < 20)
{
printf ("<b><font face=\"Tahoma\" color=\"#98A39A\" size=\"2\">Displaying Results %s-%s out of %s, comprised of %s Pages", $start_record, $num_of_rows, $num_of_rows, $num_of_pages);
}
}
else
{
printf ("<b><font face=\"Tahoma\" color=\"#98A39A\" size=\"2\">Displaying Results %s-%s out of %s, comprised of %s Pages", $start_record, $start_record + 19, $num_of_rows, $num_of_pages);
}
}
else if (strcmp($category, "del") == 0)
{
printf ("<b> <font class=warning>WARNING<br> This record will be deleted! <br><br> <a class=body_link href=\"add.php?delete=2&org_serial=$org_serial\">Click here to delete.</a>");
}
}
if (strcmp($category, "search") == 0 || strcmp($category, ""))
{
display_pages($num_of_rows, $searchField, $searchString, $num_of_pages, $group_num, $order_dir, $order_col);
echo "</div>";
}
mysql_close($conn);
}