473,472 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to put information from sql database into html table

10 New Member
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
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. mysql_connect ("localhost", "testuser","")  or die (mysql_error());
  4. mysql_select_db ("studentinformation");
  5.  
  6. $term = $_POST['term'];
  7.  
  8. $sql = mysql_query("select * from testtable where StudentNumber like '%$term%'");
  9. while ($row = mysql_fetch_array($sql)){    
  10.     echo 'Student Number: '.$row['StudentNumber'];
  11.     echo '<br/> First Name: '.$row['FirstName'];
  12.     echo '<br/> Last Name: '.$row['LastName'];
  13.     echo '<br/> Grade: '.$row['Grade'];
  14.     echo '<br/><br/>';    
  15.     }
  16.  
  17. ?>
  18.  
based on what my html page passes to it
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Search the Database</table>
  4. </head>
  5. <body>
  6.  
  7. <form action="search.php" method="post"
  8. Search: <input type="text" name="term" /><br />
  9. <input type="submit" name="submit" value="Submit" />
  10. </body>
  11. </html>
  12.  
and this is a snippet from the file containing the table i want this info to load into:
Expand|Select|Wrap|Line Numbers
  1. <div align="center">
  2.     <table border="1" width="96%">
  3.         <tr>
  4.             <td width="33" bgcolor="#FFFFFF">
  5.             <p align="center"><input type="checkbox" name="C5" value="ON"></td>
  6.             <td width="185" bgcolor="#FFFFFF" align="center"><b>
  7.             <font face="Arial" size="4" color="#008000">Student Number</font></b></td>
  8.             <td width="214" bgcolor="#FFFFFF" align="center"><b>
  9.             <font face="Arial" size="4" color="#008000">Student First Name</font></b></td>
  10.             <td width="251" bgcolor="#FFFFFF" align="center"><b>
  11.             <font face="Arial" size="4" color="#008000">Student Surname</font></b></td>
  12.             <td bgcolor="#FFFFFF" align="center"><b>
  13.             <font face="Arial" size="4" color="#008000">Grade</font></b></td>
  14.             <td bgcolor="#FFFFFF" align="center"><b>
  15.             <font face="Arial" size="4" color="#008000">Number of Points</font></b></td>
  16.             <td bgcolor="#FFFFFF" align="center"><b>
  17.             <font face="Arial" size="4" color="#008000">Date Added</font></b></td>
  18.         </tr>
  19.         <tr>
  20.             <td width="33" align="center">
  21.             <input type="checkbox" name="C1" value="ON"></td>
  22.             <td width="185">&nbsp;</td>
  23.             <td width="214">&nbsp;</td>
  24.             <td width="251">&nbsp;</td>
  25.             <td>&nbsp;</td>
  26.             <td>&nbsp;</td>
  27.             <td>&nbsp;</td>
  28.         </tr>
  29.         <tr>
  30.             <td width="33" align="center">
  31.             <input type="checkbox" name="C2" value="ON"></td>
  32.             <td width="185">&nbsp;</td>
  33.             <td width="214">&nbsp;</td>
  34.             <td width="251">&nbsp;</td>
  35.             <td>&nbsp;</td>
  36.             <td>&nbsp;</td>
  37.             <td>&nbsp;</td>
  38.         </tr>
  39.         <tr>
  40.             <td width="33" align="center">
  41.             <input type="checkbox" name="C3" value="ON"></td>
  42.             <td width="185">&nbsp;</td>
  43.             <td width="214">&nbsp;</td>
  44.             <td width="251">&nbsp;</td>
  45.             <td>&nbsp;</td>
  46.             <td>&nbsp;</td>
  47.             <td>&nbsp;</td>
  48.         </tr>
  49.         <tr>
  50.             <td width="33" align="center">
  51.             <input type="checkbox" name="C4" value="ON"></td>
  52.             <td width="185">&nbsp;</td>
  53.             <td width="214">&nbsp;</td>
  54.             <td width="251">&nbsp;</td>
  55.             <td>&nbsp;</td>
  56.             <td>&nbsp;</td>
  57.             <td>&nbsp;</td>
  58.         </tr>
  59.     </table>
  60. </div>
  61.  
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
May 13 '12 #1

✓ answered by Dormilich

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.
Expand|Select|Wrap|Line Numbers
  1. $table = <<<TBL
  2. <table>
  3.   <thead>
  4.     <tr>
  5.       <th>table heading</th>
  6.     </tr>
  7.   </thead>
  8.   <tbody>
  9. {row}
  10.   </tbody>
  11. </table>
  12. TBL;
  13.  
  14. $tblrow = <<<ROW
  15.     <tr>
  16.       <td>%s</td>
  17.     </tr>
  18. ROW;
  19.  
  20. // DB using PDO (http://php.net/pdo)
  21. // ... DB code here ...
  22.  
  23. $tr = "";
  24. foreach ($result as $row)
  25. {
  26.   $tr .= sprintf($tblrow, $row["field"]) . PHP_EOL;
  27. }
  28.  
  29. echo str_replace('{row}', $tblrow, $table);

3 5762
Dormilich
8,658 Recognized Expert Moderator Expert
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.
Expand|Select|Wrap|Line Numbers
  1. $table = <<<TBL
  2. <table>
  3.   <thead>
  4.     <tr>
  5.       <th>table heading</th>
  6.     </tr>
  7.   </thead>
  8.   <tbody>
  9. {row}
  10.   </tbody>
  11. </table>
  12. TBL;
  13.  
  14. $tblrow = <<<ROW
  15.     <tr>
  16.       <td>%s</td>
  17.     </tr>
  18. ROW;
  19.  
  20. // DB using PDO (http://php.net/pdo)
  21. // ... DB code here ...
  22.  
  23. $tr = "";
  24. foreach ($result as $row)
  25. {
  26.   $tr .= sprintf($tblrow, $row["field"]) . PHP_EOL;
  27. }
  28.  
  29. echo str_replace('{row}', $tblrow, $table);
May 14 '12 #2
chocomon
10 New Member
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
May 14 '12 #3
Dormilich
8,658 Recognized Expert Moderator Expert
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.
May 15 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: adam | last post by:
Hi, I have an issue that i just cant seem to figure out hope some one can help. right i am getting people to input the own images directly into a blob within a mysql database. then on the next...
5
by: Michelle A. | last post by:
I have a four page form. Pages 1-3 stores there information in a session variables. Page four is reached (a final review page) and then the information will be written to the SQL server. When...
2
by: Michael Meckelein | last post by:
Environment: VS.NET 2003 Application: ASP.NET web application Program language: C# (Csharp) Situation: I get some information from a sql database. With this information I generate a html table...
2
by: buran | last post by:
Dear ASP.NET Programmers, I have a HTML table (running as server control with the control ID: tblInsertSP). The table has 16 rows with textboxes. Depending on the value of the ddlSPType, which...
2
by: Sehboo | last post by:
I have an ASP.NET page which has bunch of stuff. In the middle somewhere (in a table), I want to add rows of records from database. So when user clicks on this button, I want to go read the data...
2
by: afr0ninja | last post by:
Hello! I'm fairly new to access, but I'm starting to get the hang of it. This will be a bit lengthy but hopefully it'll have enough information for some kind soul to help me out. I'm currently...
4
by: McGowan | last post by:
Hi, I'm trying to display data from a mysql database in a HTML table but for some reason my code isn't working. At the moment I have got it to read and display the headers and the first row of the...
1
by: bb nicole | last post by:
Below is my interface for resume which need to post into database... I have did it all in one page and it already can send into database which the table name resume... But my letturer now want me...
7
by: Ulysse | last post by:
Hello, I'm trying to extract the data from HTML table. Here is the part of the HTML source : """ <tr> <td class="tdn" valign="top"> <input name="x44553130" value="y" type="checkbox"></td>...
2
by: susinthaa | last post by:
Hi All, Good Morning! My requirement is as below: I am having a HTML table and the text boxes are embedded as a row of the tabel. And I am having a button at the end of the page. When I...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.