The attached PHP code display all users of a mysql table, I want to make a modification and sort displayed names automatically by "item_amt". This is not a column name but it's a calculated value using Count function.
There are two tables involved here: Ad (for ads details, including userid for each ad) & User (for user details).
Replace
$A_ITEMS = $db->two_col_assoc('SELECT userid,COUNT(*) FROM '.TBL_AD.' WHERE userid IN('.implode(',',$A_UID).') GROUP BY userid');
by
$A_ITEMS = $db->two_col_assoc('SELECT userid,COUNT(*) FROM '.TBL_AD.' WHERE userid IN('.implode(',',$A_UID).') GROUP BY userid irder by 2');
Regards,
alain34
alain34
majdolyan,
sorry
'irder' should be 'order'
$A_ITEMS = $db->two_col_assoc('SELECT userid,COUNT(*) FROM '.TBL_AD.' WHERE userid IN('.implode(',',$A_UID).') GROUP BY userid order by 2');
alain34
majdolyan
ASKER
Thanks Alain but nothing has been changed in the display. Names are still not sorted by amount of ads.
I gave it more thought and I found your solution logically to be incorrect. The SQL statement you are ordering is inside the 2nd while loop which is responsible to count the amount of ads per user.
What I want is to order the users in the outer loop based on the number of their ads. But the problem in that SQL statement it's currently only for the users, I may need to join it with ad table to order users correctly from there.
But I don't know how!
Any help is appreciated.
bleach77
Check the #### mark
$ql = 'SELECT COUNT(*) FROM '.TBL_USER.' WHERE 1'; #### Delete this line$ql = 'SELECT COUNT(*) FROM '.TBL_USER.' u LEFT JOIN (SELECT userid,COUNT(*) as amount FROM '.TBL_AD.' GROUP BY userid) a ON a.userid = u.id WHERE 1'; #### Use this line instead $sel_search_by = array( 'username' => '{Username}', );$add_url = '';if($_GET['s_res']) { if($clean['search_by'] && $sel_search_by[$clean['search_by']] && $clean['q']) { $ql.=' AND '.$clean['search_by']." LIKE '%".addcslashes($db->quote($clean['q']), '%_')."%'"; } if($clean['sort_by']) { $ql.=' ORDER BY "'.$clean['sort_by'].'" '.$clean['a_d']; } else { #### add this else $ql.=' ORDER BY a.amount'; } $add_url='&s_res=1'; if($clean['search_by'] && $clean['q']) $add_url.='&search_by='.$clean['search_by'].'&q='.urlencode($clean['q']); if($clean['sort_by']) $add_url.='&sort_by='.$clean['sort_by'].'&a_d='.$clean['a_d'];}else { #### add this else $ql.=' ORDER BY a.amount'; }
$ql = str_replace('COUNT(*)','id,username,UNIX_TIMESTAMP(regdate) AS regdate',$ql); #### Please include amount in $ql = str_replace('COUNT(*)','id,username,UNIX_TIMESTAMP(regdate), a.amount AS regdate',$ql); #### Use this
$ql = str_replace('COUNT(*)','id,username,UNIX_TIMESTAMP(regdate) AS regdate',$ql); #### Please include amount in $ql = str_replace('COUNT(*)','u.id,u.username,UNIX_TIMESTAMP(u.regdate) AS regdate' a.amount,$ql); #### Use this
Please Ignore the other post. Sorry for any inconveniences.
$ql = str_replace('COUNT(*)','id,username,UNIX_TIMESTAMP(regdate) AS regdate',$ql); #### Please include amount in $ql = str_replace('COUNT(*)','u.id,u.username,UNIX_TIMESTAMP(u.regdate) AS regdate, a.amount',$ql); #### Use this
Hi, I have added the last change and I'm still getting the following error:
mySQL error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as amount FROM ec3_ad GROUP BY userid) a ON a.userid = u.id WHERE 1 ORDER BY a.a' at line 1
mySQL error code: 1064
Date: Friday 20th 2009f March 2009 02:04:22 PM
majdolyan
ASKER
I still have the error code, any help is appreciated.
Replace
$A_ITEMS = $db->two_col_assoc('SELECT
by
$A_ITEMS = $db->two_col_assoc('SELECT
Regards,
alain34