0

I'm looking for a solution whereby it loops through each record in my database query and gives it it's own row in a html table with fixed table headings. Below is the current php I use to query the data and get results. Each of the heading names for my table are: PropertyType, Description, RentPrice and Location. However for some reason when I try to run my code nothing is shown on the screen

<!DOCTYPE html>
<html lang="en">
<body>

<p style ="text-align: center" id="php_style">
<?php
    error_reporting(E_ERROR | E_PARSE);
    session_start();
    $counter_name = "counter.txt";
    // Check if a text file exists. If not create one and initialize it to zero.
    if (!file_exists($counter_name)) {
      $f = fopen($counter_name, "w");
      fwrite($f,"0");
      fclose($f);
    }
    // Read the current value of our counter file
    $f = fopen($counter_name,"r");
    $counterVal = fread($f, filesize($counter_name));
    fclose($f);
    // Has visitor been counted in this session?
    // If not, increase counter value by one
    if(!isset($_SESSION['hasVisited'])){
      $_SESSION['hasVisited']="yes";
      $counterVal++;
      $f = fopen($counter_name, "w");
      fwrite($f, $counterVal);
      fclose($f); 
    }
    echo  nl2br ("You are visitor number ".$counterVal. " to this site \n Today's date is ". date("d/m/Y"));





    $con=mysqli_connect("localhost:8889","root","root","booksmart_properties");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    else{

        echo "we connected";
    }

    // Perform queries 
$result=mysqli_query("SELECT * FROM ListedProperties"); ?> </p>



    <table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
  <thead>
    <tr>
      <th>PropertyType</th>
      <th>Description</th>
      <th>RentPrice</th>
        <th>Location</th>
    </tr>
  </thead>
  <tbody>
    <?php
      while( $row = mysqli_fetch_assoc( $result ) ){
        echo
        "<tr>
          <td>{$row\['PropertyType'\]}</td>
          <td>{$row\['Description'\]}</td>
          <td>{$row\['RentPrice'\]}</td>
          <td>{$row\['Location'\]}</td>
        </tr>\n";
      }
    ?>
  </tbody>
</table>
 <?php mysql_close($connector); ?>


    //$result=mysqli_fetch_assoc($sql);

    //foreach($result as $key => $val) {
    //print $val; 
    //}

    //echo $result['*'];


    //mysqli_close($con);

//?>

I know it may be trivial but I'm new to php and would appreciate all the help I could get.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
KONADO
  • 189
  • 1
  • 13
  • ` //$result=mysqli_fetch_assoc($sql); ... //?>` no idea why you have that like it, that should be throwing you a parse error, as would `$row\['PropertyType'\]` etc. – Funk Forty Niner Oct 08 '16 at 15:14

1 Answers1

2

Here's a simple solution:

while ( $row = mysqli_fetch_assoc( $result ) ){?>
    <tr>
      <td><?=$row['PropertyType']?></td>
      <td><?=$row['Description']?></td>
      <td><?=$row['RentPrice']?></td>
      <td><?=$row['Location']?></td>
    </tr>
<?php
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64