dtt5830659 2013-06-06 00:03
浏览 27
已采纳

mysql_fetch_array - 如果推荐==是先显示它

Building a page that outputs each result from a search based on location/distance/w.e however I want the first result to be a "recommended" page if it falls within the search parameters. Each entry in the database has a row "recommended" and either yes or no.

If its =="yes" then it changes the background to yellow however if could be the 3rd, 6th and 17th result in the list. I want it so it shows 1 recommended at the top of the search results and then the rest and normal.

Search results so far:

    while($row = mysql_fetch_array( $result )) {
    if ($row['recommended'] == "Yes") { 

        echo '<div id="results_box_site" style="background-color: #ffffdf;">';
}

        else {
                echo '<div id="results_box_site">';
        }

    echo '<a href="site.php?id=' . $row['site_ID'] . '"><img class="results_site_image" src="' . $row['site_logo'] . '" /></a>';
    echo '<h1><a href="site.php?id=' . $row['site_ID'] . '">' . $row['site_name'];
if ($row['recommended'] == "Yes") { 

        echo ' - Recommended site';
}
    echo '</a></h1>';
    echo '<h2>Next game date: ' . $row['next_game'] . '</h2>

        <img src="images/rating/5star.png" /> (rating 5/5 - 2 reviews)

        <p id="results_box_content">';
    echo $row['short_desc'];
    echo '<br/><br/><a href="site.php?id=' . $row['site_ID'] . '"> >> More information <<</a>
        </p>

        <h3 id="results_box_content">Facilities</h3>

        <div id="details">
            <ul>';
            echo '<li>' . $row['lunch'] . '</li>';
            echo '<li>' . $row['shop'] . '</li>';
            echo '<li>' . $row['toilets'] . '</li>';
            echo '<li>Minimum age: ' . $row['min_age'] . '</li>';
            echo '<li>Game Type: ' . $row['game_type'] . '</li>';
            echo '<li>Site Type: ' . $row['site_type'] . '</li>';
            echo '<li>' . $row['price'] . '</li>';
            echo '<li>' . $row['packages'] . '</li>';
            echo '</ul>
        </div>
    </div>';
} 

Messy code I know but any help would be appreaciated

  • 写回答

1条回答 默认 最新

  • dongyun8075 2013-06-06 00:05
    关注

    You want to do this in your query. Add this to the end of your query:

    ORDER BY `recommended` DESC
    

    It will return the recommended ones first and then the non-recommended one.s

    This assumes the valid values for that column is "Yes" and "No"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错