Jump to content

Need An Opinion


glenelkins

Recommended Posts

Hi

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:

[code]
while ($co = mysql_fetch_array($result)) {
  $content .= "
            <tr>
                <td>
                    Header
                </td>
              <td>
                    Data
              </td>
      ....................and so on...agh![/code]
Link to comment
Share on other sites

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

[code]
<?php
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');
?>
[/code]
Link to comment
Share on other sites

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:

[code]<?php
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("ID","id"),
        array("Login","login"),
        array("First Name","firstname"),
        array("Last Name","lastname")
        );

buildAdminTable ($fields, $sql);

?>[/code]

You could use the same sort of logic to build your forms.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.