glenelkins Posted October 24, 2006 Share Posted October 24, 2006 HiSay 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 fashionNow 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] Quote Link to comment Share on other sites More sharing options...
Caesar Posted October 24, 2006 Share Posted October 24, 2006 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 24, 2006 Share Posted October 24, 2006 Do you mean you need something like this "table2Table" function[code]<?phpinclude 'db.php'; //connnection stufffunction 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 functionecho table2Table('mytablename');?>[/code] Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 24, 2006 Share Posted October 24, 2006 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]<?phpfunction 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.