简体   繁体   English

使用php和mysql进行数据过滤

[英]data filtering using php and mysql

This couple of days I've working on this project. 这几天我正在研究这个项目。 i didnt know whats the error of this code and what method should i use to make my code better, my friend told me to apply array but im not familiar on that method i try to research and research in couple of days but it not working. 我不知道这个代码的错误以及我应该使用什么方法来使我的代码更好,我的朋友告诉我应用数组但我不熟悉该方法我试着在几天内研究和研究但它不起作用。 What i need is to make a program that will count level 1,2,3,4 and 5 and display for example how many data(row) got level 5 in jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov and dec, in this sample code i only show filtering level 5 only because i dont want may reader of this to be confuse. 我需要的是制作一个计算1,2,3,4和5级的程序,并显示例如在jan,feb,mar,apr,may,jun,jul,aug中有多少数据(行)得到5级,sep,oct,nov和dec,在这个示例代码中我只显示过滤级别5,因为我不想要读者可能会混淆。 thank you guys :-( 感谢大伙们 :-(

This is My sample table 这是我的样本表

Table: sample
+-------------+------------------+-----------+
|    date     |     level        |   id      |
+-------------+------------------+-----------+
| 2012-03-01  |      4           |     1     |
| 2012-02-02  |      1           |     2     |
| 2012-04-03  |      4           |     3     |
| 2012-05-14  |      1           |     4     |
| 2010-01-05  |      5           |     5     |
| 2009-01-05  |      2           |     6     |
| 2008-01-05  |      3           |     7     |
| 2012-02-02  |      1           |     8     |
| 2012-06-03  |      4           |     9     |
| 2012-07-14  |      1           |     10    |
| 2010-01-05  |      5           |     11    |
| 2009-01-05  |      2           |     12    |
| 2012-08-05  |      3           |     13    |
+-------------+------------------+-----------+

This query is selecting all data with a year of 2012 and order by date 此查询选择2012年的所有数据并按日期排序

$query = mysql_query("select * from table where year(date)='2012' order by date");

This while loop generate all the record that $query prefer. 这个while循环生成$ query更喜欢的所有记录。 In the loop there was the HOPING condition that filter the records with all 5 LEVEL... when the data(produce by $query) is level 5 the $level5 will increment by 1, then that data will under go to another if statement to determined what month the data(produce by $query) belong, let say the data is 2012-05-14, so the data will go in the if statement if(Date_format($ii['date'],'%m'))==05) and increase the $level5_may by 1 and after that... 在循环中有HOPING条件,用所有5 LEVEL过滤记录...当数据(由$ query产生)为5级时,$ level5将增加1,然后该数据将转到另一个if语句到确定数据(由$ query生成)属于哪个月,假设数据是2012-05-14,所以数据将进入if语句if(Date_format($ ii ['date'],'%m') )== 05)并将$ level5_may增加1,然后......

while($ii=mysql_fetch_array($query))
{
if($ii['level']==5)
{
 $level5++;
  if(Date_format($ii['date'],'%m'))==1)
  {
  $level5_jan++;
  }elseif(Date_format($ii['date'],'%m'))==02)
  {
  $level5_feb++;
  }elseif(Date_format($ii['date'],'%m'))==03)
  {
  $level5_mar++;
  }elseif(Date_format($ii['date'],'%m'))==04)
  {
  $level5_apr++;
  }elseif(Date_format($ii['date'],'%m'))==05)
  {
  $level5_may++;
  }elseif(Date_format($ii['date'],'%m'))==06)
  {
  $level5_jun++;
  }elseif(Date_format($ii['date'],'%m'))==07)
  {
  $level5_jul++;
  }elseif(Date_format($ii['date'],'%m'))==08)
  {
  $level5_aug++;
  }elseif(Date_format($ii['date'],'%m'))==09)
  {
  $level5_sep++;
  }elseif(Date_format($ii['date'],'%m'))==10)
  {
  $level5_oct++;
  }elseif(Date_format($ii['date'],'%m'))==11)
  {
  $level5_nov++;
  }elseif(Date_format($ii['date'],'%m'))==12)
  {
  $level5_dec++;
  }
}
}

this part of the code will now collect the result of the if statement 这部分代码现在将收集if语句的结果

echo"
number of 5 stars: $level5  <br>
jan:$level5_jan     <br>
feb:$level5_feb     <br>
mar:$level5_mar     <br>
apr:$level5_apr     <br>
may:$level5_may     <br>
jun:$level5_jun     <br>
jul:$level5_jul     <br>
aug:$level5_aug     <br>
sep:$level5_sep     <br>
oct:$level5_oct     <br>
nov:$level5_nov     <br>
dec:$level5_dec     <br>
";

SQL: SQL:

"SELECT level, id, date AS UNIX_TIMESTAMP(date) FROM table WHERE year(date) = '2012' ORDER BY date"

PHP: PHP:

while($row = mysql_fetch_assoc($query))
{
    $levels = array(); // This is where the data for the monthly counting goes in
    $currentLevel = $row['level']; // The current level in the loop
    $currentDate = date('n', $row['date']); // The current month without leading zeroes

    if ($currentLevel === 5) // Check if the current level equals 5
    {
        $levels[$currentLevel][$currentDate] += 1;
    }
}

Should do what you want but it is not tested. 应该做你想要的但是没有经过测试。

NOTE: It is not recommendet to use multiple query of course (bad performance) 注意:当然不建议使用多个查询(性能不佳)

EDIT 编辑

SELECT field1, field2, field3, date AS UNIX_TIMESTAMP(date) FROM table WHERE year(date) = '2012' ORDER BY date)

or try this (not sure if this works properly): 或试试这个(不确定这是否正常):

SELECT *, date AS UNIX_TIMESTAMP(date) FROM ...

UNIX_TIMESTAP UNIX_TIMESTAP

If your field is typeof timestamp or datetime UNIX_TIMESTAMP(date) will return the date in seconds since November 1971 (see: http://en.wikipedia.org/wiki/Unix_time ) and here (see: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp ) 如果您的字段是时间戳或日期时间类型,则UNIX_TIMESTAMP(日期)将返回自1971年11月以来的日期(参见: http//en.wikipedia.org/wiki/Unix_time )和此处(请参阅: http://dev.mysql .com / doc / refman / 5.5 / en / date-and-time-functions.html #function_unix-timestamp

With unix timestamp it is very easy to handle dates - and you can easily calculate with them and format to whatever you like. 使用unix时间戳可以非常轻松地处理日期 - 您可以轻松地使用它们进行计算并将其格式化为您喜欢的任何内容。

 select *,count(1) as total from table where year(date)='2012' 
group by level order by date
$date = 'date';
$level = 'level';
$year = '2012';

$result1 = mysql_query("SELECT *, DATE_FORMAT( $date, '%m' ) AS monthz FROM sample WHERE year($date) = '$year' ORDER BY $date");
while($row=mysql_fetch_array($result1))
{
$count++;


    if($row['smi_level']=='5star')
    {
        if($row['monthz']==1)
        {
        $jan++;
        }elseif($row['monthz']==2)
        {
        $feb++;
        }elseif($row['monthz']==3)
        {
        $mar++;
        }elseif($row['monthz']==4)
        {
        $apr++;
        }elseif($row['monthz']==5)
        {
        $may++;
        }elseif($row['monthz']==6)
        {
        $jun++;
        }elseif($row['monthz']==7)
        {
        $jul++;
        }elseif($row['monthz']==8)
        {
        $aug++;
        }elseif($row['monthz']==9)
        {
        $sep++;
        }elseif($row['monthz']==10)
        {
        $oct++;
        }elseif($row['monthz']==11)
        {
        $nov++;
        }elseif($row['monthz']==12)
        {
        $dec++;
        }
    }   
}
echo" $jan $feb $mar $apr $may $jun $jul $aug $sep $oct $nov $dec";

This is my complete solution about my problem(but im not happy about my solution because the code was bulky or bulk, and I've not use array so i was disappoint o my self) thank to F. Müller for giving me idea about arrays and the use of AS in *, DATE_FORMAT( $date, '%m' ) AS monthz and to Arun Killu Thanks for suggestion. 这是我对我的问题的完整解决方案(但是我对我的解决方案不满意,因为代码很庞大或者很大,而且我没有使用数组,所以我对自己很失望)感谢F.Müller让我了解数组并利用*, DATE_FORMAT( $date, '%m' ) AS monthz阿伦Killu感谢建议。 I know my solution code is not quite good, hahaha Im open for any suggestion thanks 我知道我的解决方案代码不是很好,哈哈哈我愿意接受任何建议,谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM