Jump to content


Need An Opinion

  • Please log in to reply
3 replies to this topic

#1 glenelkins

  • Members
  • PipPipPip
  • Advanced Member
  • 760 posts
  • LocationBridlington - East Riding of Yorkshire - United Kingdom

Posted 24 October 2006 - 07:27 PM


Say you have a large project with lots of tables that need to display a) Information in a table with headers, the headers match fields in the table b) The same information needs to also be displayed in a form editing fashion

Now you have shit loads of tables to do like this. What are peoples opinions on how to go about this? I dont really want to have to type out code for each row and column in the table with the headers.

example of what i dont want to do:

while ($co = mysql_fetch_array($result)) {
   $content .= "
       ....................and so on...agh!

http://thewebsolutionprovider.com - A Solution For Everything Web!

#2 Caesar

  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 24 October 2006 - 08:15 PM

OOP my friend. Create an HTML class and just make the call to the function with the correct format, and define the values for the variables accordingly....if I understood correctly.
PHP Ninja

#3 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 24 October 2006 - 08:34 PM

Do you mean you need something like this "table2Table" function

include 'db.php';    //connnection stuff

function table2Table($tname) {
    $result = mysql_query("SELECT * FROM `$tname` ");

    $str = "<TABLE border='1' cellpadding='4'>\n";

    // column headings
          $str .=  "<tr>\n";
          while ($fld = mysql_fetch_field ($result)) {
                   $str .= "<th>{$fld->name}</th>\n";
          $str .=  "</tr>\n";
      #    echo "</tr>\n";

    // list data
          while ($row = mysql_fetch_row($result)) {
          $str .=  "<tr>\n";
          foreach ($row as $field) {
                   $str .=  "<td>$field</td>\n";
          $str .=  "</tr>\n";

    $str .=  "</TABLE>\n";
    return $str;

// call function
echo table2Table('mytablename');

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 Psycho

  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 24 October 2006 - 08:43 PM

Here's a bit of code I am in the middle of that might help you. Basically I wanted to use the same format for all of my display tables int he administration pages. So, I created a function to do it for me. All I have to do is pass the function the MySQL query and the fields I want displayed. The fields are passed in an array with the Label and database field:

function buildAdminTable ($fieldArray, $sql) {

    $results = runSql($sql);

    if (mysql_num_rows($results) == 0)	{
        echo "No results returned";
    } else {

        echo "<table border=1 class=\"admin\" align=\"center\">\n";
        //Show the table headers
        echo "  <tr>\n";
        foreach($fieldArray as $header){ echo "    <th>".$header[0]."</th>"; }
        echo "<th></th><th></th>"; //Empty cells for edit/delete columns
        echo "  </tr>\n";

        //Show the data
        while ($row = mysql_fetch_array($results)) {
            echo "  <tr>\n";
            foreach($fieldArray as $field){ echo "    <td>".$row[$field[1]]."</td>\n"; }
            echo "    <td><a href=\"admin.php?page=".$_GET["page"]."&mode=edit&id=$row[0]\">Edit</a></td>\n";
            echo "    <td><a href=\"admin.php?page=".$_GET["page"]."&mode=delete&id=$row[0]\">Delete</a></td>\n";
            echo "  </tr>\n";
        echo "</table>\n";
    echo "<br><center style=\"margin-top:12px;\"><a href=\"admin.php?page=".$_GET["page"]."&mode=new\">Add</a></center>\n";

$sql = "SELECT * FROM users ORDER BY login";
$fields = array(
        array("First Name","firstname"),
        array("Last Name","lastname")

buildAdminTable ($fields, $sql);


You could use the same sort of logic to build your forms.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users