在Php中创建带循环的表

时间:2016-09-29 03:21:51

标签: php html5 loops

我想要的是显示每个Category id项目的总点击次数,仅按每天最高总点击次数排列的20个项目。

现在我正在使用硬代码,结果必须看起来像这样

i am using hard code to t

因此,如果item idtotal clicks已填满

  

20列(对于项目ID和总点击次数)+ 2为tittle因此意味着

     

22columns

必须转到下一行。

因为我指的是我的数据库,所以我使用循环来创建表,当我这样做时,我得到了这个结果....

enter image description here

  

结果将一直显示到左侧结束。这非常难以阅读以用于报告目的。我希望结果看起来像是我上传的第一个数字。

以下是我现在正在做的事情:

  include "Con.php";
        //get the value from Get Method 
        $CatidValue = $_GET['CatIds'];
        //(The date format would looks like yyyy-mm-dd)
        $DateFrom = $_GET['DateFrom'];  
        $DateTo = $_GET['DateTo'];
        //select the CatID
        $SqlGet= "Select CatId from try where CatId = $CatidValue";
        $_SqlGet = mysqli_query($connection,$SqlGet);
        $TakeResultGet = mysqli_fetch_array($_SqlGet);
        $CatIdTittle = $TakeResultGet ['CatId'];

        echo"
            For Category Id : $CatIdTittle
            <br>

        ";

        //For Loop purpose and break the value
        $explodeValueFrom = explode("-",$DateFrom);
        $explodeValueTo = explode("-",$DateTo);
        $DateValueFrom = $explodeValueFrom[0].$explodeValueFrom[1].$explodeValueFrom[2];
        $DateValueTo = $explodeValueTo[0].$explodeValueTo[1].$explodeValueTo[2];    

        //Loop through the date
        for($Loop=$DateValueFrom; $Loop <= $DateValueTo; $Loop++){
            $YearLoop= substr($Loop, 0,4);
            $MonthLoop =substr($Loop, 4,2);
            $DayLoop = substr($Loop, 6,2);

            $DateTittleValue = $YearLoop."-".$MonthLoop."-".$DayLoop;
            $trValue = "<th class='tg-amwm' colspan='2'>$DateTittleValue</th>";


                echo"
                <table class='tg'>
                    <tr>
                        $trValue
                    </tr>
                ";
                echo"
                <table class='tg'>
                    <tr>
                        <td class='tg-yw4l'>Items Id</td>
                        <td class='tg-yw4l'>Total Clicks</td>
                    </tr>
                ";



            //to get the item id and total clicks
            $SqlSelect = "select `Item Id`,`Total Clicks`,Day from try where CatId = $CatidValue and Day = '$DateTittleValue' ORDER BY `try`.`Total Clicks`  DESC limit 20";
            $_SqlSelect = mysqli_query($connection,$SqlSelect);
            foreach ($_SqlSelect as $ResultSelect) {
                $Day = $ResultSelect['Day'];
                $ItemId = $ResultSelect['Item Id'];
                $TotalClicks = $ResultSelect['Total Clicks'];

                echo"       
                    <tr>
                        <td class='tg-yw4l'>$ItemId</td>
                        <td class='tg-yw4l'>$TotalClicks</td>
                    </tr>               
                ";
            }
        }           
    ?>

3 个答案:

答案 0 :(得分:3)

您需要在每个报告的表格中动态添加 td (数据应按日期分组)。

答案 1 :(得分:1)

您只需要使用float:left css属性浮动每个表,并根据您想要在同一行上的表数量使用一些百分比。在这种情况下,33%的人会这样做。看一下这个代码:http://codepen.io/anon/pen/EgXqPk

魔术发生在:

.tg {
  width: 33%;
  float:left;
}

答案 2 :(得分:1)

you can try i hope this perfect for you to draw a table using PHP Loop.
<?php  
echo "<table border =\"1\" style='border-collapse: collapse'>"; 
for ($row=1; $row <= 10; $row++) { 
echo "<tr> \n";
for ($col=1; $col <= 10; $col++) { 
$p = $col * $row;
echo "<td>$p</td> \n";
 }
echo "</tr>";
}
echo "</table>";
?>