Retrieve data dynamically from Mysql into php page

I need to display data from mysql database dynamically into the html format below. Since I am new to php, I want to know how can I do this using php. The data should be in the template as in the html code.

HTML code:

<div class = 'square-box' style = "width:977px; height:125px;">
  <div class = 'square-content' style = "padding:5px; background-color: #FAFAFA;"><div>
    <span style = "font-size: 10px;">
      <table>
        <tr>

          <td><label style = "font-size: 14px;">Company Name: &nbsp;
            </label></td>
          <td><input class = "form-control" style = "background:#FAFAFA; border-style:none; box-shadow: inset 0px 0px 0px 0px;" type = "text" name = "company_name"</td>

          <td><label style = "font-size: 14px;">&nbsp;Date of Audit: &nbsp;
            </label></td>
          <td><input class = "form-control" style = "background:#FAFAFA; border-style:none; box-shadow: inset 0px 0px 0px 0px;" type = "date" name = "audit_date"></td>
        <tr>

        <tr>
          <td><label style = "font-size: 14px;">Time: &nbsp;
            </label></td>
          <td><input class = "form-control" style = "background:#FAFAFA; border-style:none; box-shadow: inset 0px 0px 0px 0px;" type = "text" name = "audit_time"></td>

          <td><label style = "font-size: 14px;">&nbsp;
            Status: &nbsp;
            </label></td>
          <td><input class = "form-control" style = "background:#FAFAFA; border-style:none; box-shadow: inset 0px 0px 0px 0px;" type = "text" name = "audit_status"></td>
        <tr>

      </table>
      <table>
        <tr>
          <td><a href = "#" style = "font-size: 14px;">View Risks</a> <br>
            <a href = "#" style = "font-size: 14px;">View Report</a></td>
        </tr>
      </table>
    </span>

    </div>
 </div>

PHP code :

<?php 
    $username = "root"; 
    $password = ""; 
    $hostname = "localhost";
    $dbhandle = mysql_connect($hostname, $username, $password) or die("cannot connect to database");
    $selected = mysql_select_db("audit", $dbhandle); 
    $strQuery = "SELECT * FROM audit_process"; 

    $retval = mysql_query($strQuery, $dbhandle); 

    if (!$retval) { 
        die('Could not get data: ' . mysql_error()); 
    }
?>

Answers 1

  • So you will need a while loop, and you would do something like this :

    <table>
    <?php 
        while($r = mysql_fetch_array($retval)): ?> // end php and show html
    <tr>
        <td><label style = "font-size: 14px;">Company Name: &nbsp;</label></td>
        <td><input class = "form-control" style = "background:#FAFAFA; border-style:none; box-shadow: inset 0px 0px 0px 0px;" type = "text" name = "company_name"</td>
    </tr>
     //add other rows
        <?php endwhile;?>
    </table>
    

    and then just echo $r['columnname'] to position you want inside of while, example:

    <td><label style = "font-size: 14px;"><?php echo $r['columnname']; ?></label></td>
    

Related Articles