English 中文(简体)
我的我的小组
原标题:Separate my Mysql Group Results
  • 时间:2012-04-11 01:26:00
  •  标签:
  • php
  • mysql

我有我的我的奇克勒数据库的成果清单,基本上列出我网站浏览的网页,并按日期分类。 现在,结果是微薄的,一切都分得。 页: 1

29/03/2012 09:18 页 次

29/03/2012 09:24 页 次 姓名

现在,我想要做的是,我是否能够改变格式,以便把结果列入这样的清单。

29/03/2012

姓名

姓名

30/03/2012

姓名

姓名

其中一个问题是,实地的日期是日期,不过,这只能使日期偏离变数,并使用这一日期,但我并不肯定如何列出上述结果。 以下是我目前的法典。

$query = "SELECT date, page_name FROM page_views GROUP BY date"; 
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo $row[ date ]. " - $". $row[ page_name ];
echo "<br />";
问题回答

1. 您可使用从购买力平价开始的日期来安排时间:

while($row = mysql_fetch_array($result)) { 
    echo date("d/m/Y", strtotime($row[ date ]));
    echo "<br />";
    echo $row[ page_name ];
    echo "<br />"; 
}

这将导致:

29/03/2012

姓名

30/03/2012

姓名

Edit:

这是我现在唯一的解决办法:

//First you select only the DISTINCT DATEs without TIME
$query1 =  SELECT DISTINCT DATE_FORMAT(date, %d/%m/%Y") as date FROM page_views ;
$result1 = mysql_query($query1) or die(mysql_error()); 

//Create a while-loop to store DATE data in array
while($row1 = mysql_fetch_array($result1)) {
    $array[][ date ] = $row1[ date ]

    //SELECT the page_names where the date is LIKE the date in array
    $query2 =  SELECT page_name FROM page_views WHERE date LIKE "% .$row1[ date ]. %" ;
    $result2 = mysql_query($query2) or die(mysql_error());


    //Create a second while-loop to store the pagenames data in array who fits to the date
    while($row2 = mysql_fetch_array($result2) {
        $array[][ pagenames ][] = $row2[ page_name ];
    }
}

print_r($array);
//This will output this:
$array = Array(
    [0] => Array(
        [date] => 29/03/2012
        [pagenames] => Array(
            [0] => pagename1
            [1] => pagename2
        )
    )
    [1] => Array(
        [date] => 30/03/2012
        [pagenames] => Array(
            [0] => pagename1
            [1] => pagename2
        )
    )
)

//use foreach to display the data
foreach($array as $entry) {
    echo $entry[ date ];
    echo  <br /> ;
    foreach($entry[ pagenames ] as $pagename) {
        echo $pagename;
        echo  <br /> ;
    }
}




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签