duankan8739 2014-04-08 14:19
浏览 31

用while循环或其他方法消除多余的代码

I have a select drop down list of items on my website and I want the total of each category in the list to have a number beside it that shows the total number of items in that category. I want my directory list of items to update when the user selects a category from my drop down list.

I am having trouble coding it. I have a table with the SiteTypes and SiteTypeID's called sitetypes. And another table called sites with all the urls in the database which is what I want to display to my users. There are different categories of urls, my select menu should display each category of urls when selected. I am having trouble figuring out how to display

$sql = "SELECT COUNT(u.url) AS total, u.SiteTypeID, p.SiteType FROM sites AS u LEFT JOIN sitetypes AS p USING (SiteTypeID) WHERE SiteTypeID=3";
$sq2 = mysqli_query($dbc, $sql);

$sq3 = "SELECT COUNT(u.url) AS total, u.SiteTypeID, p.SiteType FROM sites AS u LEFT JOIN sitetypes AS p USING (SiteTypeID) WHERE SiteTypeID=4";
$sq4 = mysqli_query($dbc, $sq3);

$sq5 = "SELECT COUNT(u.url) as total, u.SiteTypeID, p.SiteType FROM sites AS u LEFT JOIN sitetypes AS p USING (SiteTypeID) WHERE SiteTypeID=5";
$sq6 = mysqli_query($dbc, $sq5);


echo $row['url'] $row['SiteType']; 
echo $row2['url'] $row2['SiteType'];
echo $row3['url'] $row3['SiteType'];

I want to display the total number of urls in the database per category. Any ideas would be appreciated. I tried using while loops but I can't get it to work.

  • 写回答

1条回答 默认 最新

  • dongtangxi1584 2014-04-08 16:00
    关注

    I am a bit confused about exactly what you are trying to get (could you post some sample data and sample output?). However why not merge the 3 queries together?

    <?php
    
    $sql = "SELECT COUNT(u.url) AS total, u.SiteTypeID, p.SiteType 
            FROM sites AS u 
            LEFT JOIN sitetypes AS p USING (SiteTypeID) 
            WHERE SiteTypeID IN (3, 4, 5)
            GROUP BY u.SiteTypeID, p.SiteType";
    
    if ($sql2 = mysqli_query($dbc, $sql))
    {
        while ($row = mysqli_fetch_assoc($sql2)) 
        {
            echo $row["total"].." - ".$row["SiteTypeID"].." - ".$row["SiteType"]."<br >";
        }
    }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测