How to put information from sql database into html table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chocomon
    New Member
    • Feb 2012
    • 10

    How to put information from sql database into html table

    Hi, i want to code a .php file that will take information from an sql database based on a search query and then put the retrieved data into an html table that i've created within another file

    essentially, i'm trying to 'add students' into a club table...

    my current php code posts my collected data
    Code:
    <?php
    
    mysql_connect ("localhost", "testuser","")  or die (mysql_error());
    mysql_select_db ("studentinformation");
    
    $term = $_POST['term'];
    
    $sql = mysql_query("select * from testtable where StudentNumber like '%$term%'");
    while ($row = mysql_fetch_array($sql)){    
    	echo 'Student Number: '.$row['StudentNumber'];
    	echo '<br/> First Name: '.$row['FirstName'];
    	echo '<br/> Last Name: '.$row['LastName'];
    	echo '<br/> Grade: '.$row['Grade'];
    	echo '<br/><br/>';    
    	}
    
    ?>
    based on what my html page passes to it
    Code:
    <html>
    <head>
    <title>Search the Database</table>
    </head>
    <body>
    
    <form action="search.php" method="post"
    Search: <input type="text" name="term" /><br />
    <input type="submit" name="submit" value="Submit" />
    </body>
    </html>
    and this is a snippet from the file containing the table i want this info to load into:
    Code:
    <div align="center">
    	<table border="1" width="96%">
    		<tr>
    			<td width="33" bgcolor="#FFFFFF">
    			<p align="center"><input type="checkbox" name="C5" value="ON"></td>
    			<td width="185" bgcolor="#FFFFFF" align="center"><b>
    			<font face="Arial" size="4" color="#008000">Student Number</font></b></td>
    			<td width="214" bgcolor="#FFFFFF" align="center"><b>
    			<font face="Arial" size="4" color="#008000">Student First Name</font></b></td>
    			<td width="251" bgcolor="#FFFFFF" align="center"><b>
    			<font face="Arial" size="4" color="#008000">Student Surname</font></b></td>
    			<td bgcolor="#FFFFFF" align="center"><b>
    			<font face="Arial" size="4" color="#008000">Grade</font></b></td>
    			<td bgcolor="#FFFFFF" align="center"><b>
    			<font face="Arial" size="4" color="#008000">Number of Points</font></b></td>
    			<td bgcolor="#FFFFFF" align="center"><b>
    			<font face="Arial" size="4" color="#008000">Date Added</font></b></td>
    		</tr>
    		<tr>
    			<td width="33" align="center">
    			<input type="checkbox" name="C1" value="ON"></td>
    			<td width="185">&nbsp;</td>
    			<td width="214">&nbsp;</td>
    			<td width="251">&nbsp;</td>
    			<td>&nbsp;</td>
    			<td>&nbsp;</td>
    			<td>&nbsp;</td>
    		</tr>
    		<tr>
    			<td width="33" align="center">
    			<input type="checkbox" name="C2" value="ON"></td>
    			<td width="185">&nbsp;</td>
    			<td width="214">&nbsp;</td>
    			<td width="251">&nbsp;</td>
    			<td>&nbsp;</td>
    			<td>&nbsp;</td>
    			<td>&nbsp;</td>
    		</tr>
    		<tr>
    			<td width="33" align="center">
    			<input type="checkbox" name="C3" value="ON"></td>
    			<td width="185">&nbsp;</td>
    			<td width="214">&nbsp;</td>
    			<td width="251">&nbsp;</td>
    			<td>&nbsp;</td>
    			<td>&nbsp;</td>
    			<td>&nbsp;</td>
    		</tr>
    		<tr>
    			<td width="33" align="center">
    			<input type="checkbox" name="C4" value="ON"></td>
    			<td width="185">&nbsp;</td>
    			<td width="214">&nbsp;</td>
    			<td width="251">&nbsp;</td>
    			<td>&nbsp;</td>
    			<td>&nbsp;</td>
    			<td>&nbsp;</td>
    		</tr>
    	</table>
    </div>
    So....is it possible to change the $POST function into something that loads the info into the html table and if yes, how so?
    Thanks
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    that loads the info into the html table
    verbatim that’s not possible as PHP can only load the HTML code into itself.

    however (on a slightly broader sense) you can load the HTML into PHP and do various kinds of string replacement. e.g.
    Code:
    $table = <<<TBL
    <table>
      <thead>
        <tr>
          <th>table heading</th>
        </tr>
      </thead>
      <tbody>
    {row}
      </tbody>
    </table>
    TBL;
    
    $tblrow = <<<ROW
        <tr>
          <td>%s</td>
        </tr>
    ROW;
    
    // DB using PDO (http://php.net/pdo)
    // ... DB code here ...
    
    $tr = "";
    foreach ($result as $row)
    {
      $tr .= sprintf($tblrow, $row["field"]) . PHP_EOL;
    }
    
    echo str_replace('{row}', $tblrow, $table);

    Comment

    • chocomon
      New Member
      • Feb 2012
      • 10

      #3
      thank you for the reply!
      but i'm not sure what you're doing in your coding example, could you summarize?
      sorry...i'm new to php coding

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I define 2 templates (one for the table, one for the table row) then I fill the row template with values from the DB and then I fill the filled-in row template into the table template.

        Comment

        Working...