2017-02-24 8 views
-5

は、私はこのコードを持って言う:PHPクエリの結果をスタイルすることはできますか?

$sql="SELECT * FROM $tbl_name ORDER BY id"; 

は、私はまた、クエリ(?「配列)の結果をスタイルする方法に関してこのシーケンスの命令を含むことができます。 「最初の結果を太字にし、残りの部分を通常のフォントにする」と言うことができますか?

+0

は、これを行いますクエリーからのHTML出力を生成するPHPコードに記述します。 – Barmar

+3

結果をHTMLテーブルに出力する場合は、CSSで実行できます。 'tr:first-child {font-weight:bold; } ' – Barmar

+1

しかし、尋ねられたように質問に答えるには、いいえsqlクエリはスタイルとは関係ありませんが、これはPHPで簡単に結果を構築することができます。 – happymacarts

答えて

0

私はあなたがあなたのデシベルに接続されていますが(未テスト)このような何かを行うことができるかわからない

PHP

// I typicaly load this part in a different file to protect my passwords 
$hostname = "127.0.0.1"; 
$database = "db_name"; 
$username = "root"; 
$password = "Password"; 
$connStr = 'mysql:host=' . $hostname . ';dbname=' . $database; 
$conn = new PDO ($connStr, $username, $password); 
////////////////////// 

$tbl_name = 'the_table'; 
$sql="SELECT col1, col2,col3 FROM $tbl_name ORDER BY id"; 
$stmt = $conn->prepare ($sql); 
$stmt->execute(); 
$results = $stmt->fetchAll(); 

if($results){ // only execute this if there are results ?> 
    <ul> 
    <?php 
    $count = 0; 
    foreach($results as $row){ //loop over all the results?> 
     <li class="<?php // if this is the first row output the first-row class, 
         // otherwise output other-row class 
      echo $count==0 ? 'first-row' : 'other-row'; ?>"> 
      <?php echo $row['col1']; ?></li> 
<?php $count++; // increment my count var 
    } // endforeach?> 
    </ul> 

<?php 
} //end if?> 

CSS

.first-row { 
    color:red; 
    font-weight:700; 
} 
.other-row { 
    color:black; 
    font-weight:normal; 
} 
関連する問題