简体   繁体   中英

How to make two $db->query from the same table with two different ORDER by column that each other stand independenty in one php page?

I want to make two blocktables in one page from the same DB table. The first blocktable I want to ORDER it by "posted_date" column, and the second I want to ORDER it by "numviews" column. I want the both blocktables not influence each other, altough they're on the same page. Is it possible?

I have made like this,

first block

$popresult = $db->query('
    SELECT 
        t.id, 
        t.poster, f.id AS forum_id, 
        f.forum_name, 
        t.subject, 
        t.mainimg, 
        posted_date, 
        t.num_views 
    FROM '.$db->prefix.'forums 
    ORDER BY 'posted_date' DESC 
    LIMIT '.$numtopik.'', true) 
or error('Tidak dapat mengambil topik populer', __FILE__, __LINE__, $db->error()); 

second block

$popresult = $db->query('
    SELECT 
        t.id, 
        t.poster, 
        f.id AS forum_id, 
        f.forum_name, 
        t.subject, 
        t.mainimg, 
        posted_date, 
        t.num_views 
    FROM '.$db->prefix.'forums 
    ORDER BY t.num_views DESC 
    LIMIT '.$numtopik.'', true) 
or error('Tidak dapat mengambil topik populer', __FILE__, __LINE__, $db->error());

but it still got error to fetch.

Can anybody help me please?

In your first query you don't order by the date, but use the value of $date in the LIMIT part of your query.

So your last part in your first query, should be more like:

[...] ORDER BY t.`yourDateColumnHere` DESC LIMIT '.$yourLimitVariableHere [...]

Hope that helps! If not, feel free to add a comment!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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