简体   繁体   中英

converting php code to html

I have been having hard time trying to convert my php code to html for 5 hours and at this point, I'm really burned out :X. Here's my Php code

 <?php
  $con=mysqli_connect("localhost","dbuser","pw","dbname");
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

 $result = mysqli_query($con,"SELECT * FROM tablea");

 echo "<table border='1'  >
 <tr>
 <th>id</th>
 <th>Subject_Code</th>
 <th>date</th>
 <th>name</th>
 <th>description</th>
 <th>Other_Details</th>
 </tr>";

 while($row = mysqli_fetch_array($result))
   {
   echo "<tr>";
   echo "<td>" . $row['id'] . "</td>";
   echo "<td>" . $row['Subject_Code'] . "</td>";
   echo "<td>" . $row['date'] . "</td>";
   echo "<td>" . $row['name'] . "</td>";
   echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
   echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
   echo "</tr>";
   }
 echo "</table>";

 mysqli_close($con);
 ?> 

and here's what i have done so far for HTML

<!doctype html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>database connections</title>
    </head>
    <body>
      <?php
      $username = "dbuser";
      $password = "pw";
      $host = "localhost";
      $database= "dbname";

      $connector = mysql_connect($localhost,$dbuser,$pw,dbname)
          or die("Unable to connect");
        echo "Connections are made successfully::";
      $selected = mysql_select_db("test_db", $connector)
        or die("Unable to connect");

      //execute the SQL query and return records
      $result = mysql_query("SELECT * FROM tablea ");
      ?>
      <table border="2">
      <thead>
        <tr>
          <th>id</th>
          <th>Subject_Code</th>
          <th>date</th>
          <th>name</th>
          <th>description</th>
          <td>Other_Details</td>
        </tr>
      </thead>
      <tbody>

        <?php
    while ($row = mysql_fetch_array($result)) {
        ?>
        <tr>
            <td><?php echo $row['id']; ?></td> 
            <td><?php echo $row['Subject_Code']; ?></td> 
            <td><?php echo $row['date']; ?></td> 
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['description']; ?></td>
            <td><?php echo $row['Other_Details']; ?></td>
        </tr>

     <?php mysql_close($connector); ?>
    </body>
    </html>

little Help here please, Thanks !!

EDIT: seems like some people are not understanding my question. My php is working fine so i want to convert the php code into html. Basically, i want my table from database to show up using HTML table.

you not close while;

so change code to :

    <!doctype html>
        <html lang="en">
        <head>
          <meta charset="UTF-8">
          <title>database connections</title>
        </head>
        <body>
          <?php

/////////////////////////////////// change \\\
          $username = "dbuser";
          $password = "pw";
          $host = "localhost";
          $database= "dbname";

          $connector = mysql_connect($localhost,$username,$password)
              or die("Unable to connect");
            echo "Connections are made successfully::";
          $selected = mysql_select_db($database, $connector)
            or die("Unable to connect");

    /////////////////////////////////// end change \\\

          //execute the SQL query and return records
          $result = mysql_query("SELECT * FROM tablea ");
          ?>
          <table border="2">
          <thead>
            <tr>
              <th>id</th>
              <th>Subject_Code</th>
              <th>date</th>
              <th>name</th>
              <th>description</th>
              <td>Other_Details</td>
            </tr>
          </thead>
          <tbody>

            <?php
        while ($row = mysql_fetch_array($result)) :
            ?>
            <tr>
                <td><?php echo $row['id']; ?></td> 
                <td><?php echo $row['Subject_Code']; ?></td> 
                <td><?php echo $row['date']; ?></td> 
                <td><?php echo $row['name']; ?></td>
                <td><?php echo $row['description']; ?></td>
                <td><?php echo $row['Other_Details']; ?></td>
            </tr>
         <?php endwhile;?>
         <?php mysql_close($connector); ?>
        </body>
        </html>

Actually your problem is as you said like html version there is no variable like $localhost but your passing the parameter to connect mysql etc. below error code shows the variable mismatch of your code.

Error code :

<?php
  $username = "dbuser";
  $password = "pw";
  $host = "localhost";
  $database= "dbname";

  $connector = mysql_connect($localhost,$dbuser,$pw,dbname);
                             ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^

  //here there is no variable like what you passing to connect mysql that's problem here 
  ?>

solution :

         <!doctype html>
        <html lang="en">
            <head>
          <meta charset="UTF-8">
          <title>database connections</title>
        </head>
        <body>

    <?php
      $con=mysqli_connect("localhost","dbuser","pw","dbname");
     // Check connection
     if(mysqli_connect_errno())
       {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
       }

     $result = mysqli_query($con,"SELECT * FROM tablea");

     echo "<table border='1'  >
     <tr>
     <th>id</th>
     <th>Subject_Code</th>
     <th>date</th>
     <th>name</th>
     <th>description</th>
     <th>Other_Details</th>
     </tr>";

      while($row = mysqli_fetch_array($result))
       {
            echo "<tr>";
           echo "<td>" . $row['id'] . "</td>";
           echo "<td>" . $row['Subject_Code'] . "</td>";
           echo "<td>" . $row['date'] . "</td>";
           echo "<td>" . $row['name'] . "</td>";
           echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
           echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
           echo "</tr>";
       }
     echo "</table>";

     mysqli_close($con);

     ?>

       </body>
        </html>

Here's another version that makes use of PDO - by far the superior of php connectors in terms of maintainability and versatility. I have the same code working under MySQL, SQLite and SQLServer - the only thing that changes is the connection string.

You'll note I pull all of the results at once. I also make use of the fact that we get back an array. Each element of that array is a row. Each row is an array of cells. This greatly reduces the code needed to output a result-set. We just need two forEach loops and that's it.

<?php
    $host = 'localhost';
    $userName = 'dbuser';
    $password = 'pw';
    $nameOfDb = 'dbname';
    $nameOfTable = 'tablea';

    $pdo = new PDO('mysql:host='.$host, $userName, $password);
    $pdo->query("use " . $nameOfDb);

    // desired results requested explicitly, so when we get an array for the row's data, the elements will be in the same order
    // - not required if the order of the columns in the database matches the desired display order. (we could just use 'select *' then)
    //$query = $pdo->prepare('select * from '.$nameOfTable);
    $query = $pdo->prepare('select id, Subject_Code, date, name, description, Other_Details from '. $nameOfTable);

    $query->execute();
    $query->setFetchMode(PDO::FETCH_ASSOC);
    $rows = $query->fetchAll();
?><!doctype html>
<html>
<head>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>id</th><th>Subject_Code</th><th>date</th><th>name</th><th>description</th><th>Other_Details</th>
            </tr>
        </thead>
        <tbody><?php
                forEach($rows as $curRow)
                {
                    echo '<tr>';

                        // use this one if you want to display the columns in the same order they are returned in the query results
                        // AND you'd like to display all columns in the result
                        forEach($curRow as $curCell)
                            echo '<td>'.$curCell.'</td>';

                        // otherwise, you can request the desired elements by name
                        //
                        //  echo '<td>' . $curCell['id'] . '</td>'
                        //  echo '<td>' . $curCell['Subject_Code'] . '</td>'

                    echo '</tr>';
                }
            ?></tbody>
    </table>
</body>

Download HTTrack Website Copier and install and run the soft. Run your script and paste your URL in web copier software. You will get HTML output in your desire folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM