php mysql自定义排序,如何使用php和mysql按顺序按id排序

I'm trying to connect MySQL database and getting out datas

I need it to get only (Data) in order by ID(daily)

and separate both datas and calling them by two different variables

the database looks something like this:

------------------------------

| ID | Data | Data2

------------------------------

| 1 | tea | hot

| 2 | milk | hot

| 3 | pepsi | cold

and output will be only one line (one data)

I don't make it as above :) it's just for clarifying....and what's best collation and type for huge data in UTF-8 ?

Edit:

Can you make it return 5 rows instead of 1? by ID? for e.g. for first day (1-2-3-4-5)ID's and for second day (6-7-8-9-10) and so on?

解决方案

quotes

----------------------------------

| id | data | data2

----------------------------------

| 1 | first quote | translated quote

| 2 | second... | bla bla

And then you select it like:

$firstday="2011-06-06";

$getquote = mysql_query("SELECT * FROM quotes WHERE id=(DATEDIFF(CURDATE()+1, '$firstday'))");

$quote = mysql_fetch_object($getquote);

echo $quote->data . $quote->data2;

EDIT!!: I have eliminated datediff, so the ID returned from date difference is DIRECTLY in WHERE.

What this does is calculate difference between first day and current date. So each day that datediff will be 1 bigger.

DATEDIFF(CURDATE()+1, '$firstday') as datediff can be interpreted as

datediff = differenceBetween(Currentday +1 and firstDay)

Yesterday was 2011-07-06, therefore datediff = 2011-07-07 (there is +1!) - 2011-07-06 which is 1

today, it's 2011-07-08 - 2011-07-06 which is 2

tomorrow 2011-07-09 - 2011-07-06 which is 3

day after tomorrow 2011-07-10 - 2011-07-06 which is 4

in one month it will be 2011-08-08 - 2011-07-06 which is 33

so, datediff is each day bigger by 1

quotes

-------------------------

|id| data

-------------------------

|1| quote day 1 (because date difference from start == 1)

|2| quote 2 day 2 (datediff == 2)

|3| quote 3 day 3 (datediff == 3)

|4| quote 4 day 4

.....

Or shortly: Each day will be a different quote, starting with ID 1 forward.

I can't explain more then this..

EDIT #2: 5 quotes a day

$offset = date_diff(new DateTime('now'), new DateTime('2011-08-29'))->format('%d');

$getquote = "SELECT * FROM quotes LIMIT {$offset},5";

second edit thanks to ajreal (SQL LIMIT syntax error)

EDIT #3: 5 quotes a day, changeable by variable..

option 1:

$choose=0; //statically defined, only first of that day will pop out

option 2:

$choose = mysql_real_escape_string($_GET["qid"]); //which one will be defined in url.. (watch out, people can figure it out and browse through all quotes

option 3:

$choose = rand(0,4); //will choose it randomly from those 5 daily quotes

So pick one of those options you like, and add it before this:

$offset = 5*date_diff(new DateTime('now'), new DateTime('2011-08-29'))->format('%d') + $choose;

$getquote = mysql_query("SELECT * FROM quotes WHERE id = '$offset'");

$quote = mysql_fetch_object($getquote);

echo $quote->data . $quote->data2;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值