2017-02-24 40 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; 
} 
相關問題