2016-10-08 14 views
0

私はデータベースクエリの各レコードをループし、固定テーブル見出しのあるHTMLテーブルに独自の行を付けるというソリューションを探しています。以下は、私がデータをクエリして結果を得るために使用する現在のPHPです。私のテーブルの見出し名はそれぞれPropertyType、Description、RentPrice、Locationです。しかし、私は自分のコードを実行しようとすると、何も画面入力する方法SQLクエリの結果をPHPのhtmlテーブルに入力

<!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); 

//?> 

に示されていない何らかの理由で、私はそれは些細なことかもしれ知っているが、私は、PHPに新たなんだと、すべての感謝します私が得ることができるのを助ける。 ?ここで

+0

' // $ result = mysqli_fetch_assoc($ sql);なぜなら、$ row \ ['PropertyType' \] 'のように、解析エラーを投げているはずです。 –

答えて

2

はシンプルなソリューションです:

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 
} 
+0

'php' PHPコードのすべての開口部の周りに? – KONADO

+0

あなたはすでにあなたの提供されたコードでそれらを書いています。 –

+0

''は '<?php echo $ x?>'の省略形ですが、PHP上で必ずしも有効になっているわけではありません。http://stackoverflow.com/questions/1386620/php-echo-vs-php-shortタグ#1386627 – aland

関連する問題