Jump to content

Archived

This topic is now archived and is closed to further replies.

glenelkins

Need An Opinion

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.