Trick Question; Showing 'nr of results' of MySql search

I have a classifieds website, and users can search ads. The results are displayed in three tabs on top of the page. These three are "**All**", "**Private**", and "**Company**". Each tab has a nr attached to it, which represents the nr of ads found in that tab. So for example: All Private Company 5 3 2 `All` is just a total of private+company! I am using MySql as a database. I am trying to figure out a way to find out these "numbers of ads found" for each tab. I have one way of doing this, which is like this, but gives me a headache because it is so messy: $query = "SELECT SQL_CACHE * FROM classified WHERE classified.classified_id=$id"; if ($adtypes=="Private"){ $query_priv_comp = "SELECT SQL_CACHE * FROM classified WHERE priv_comp='Company'"; } else { $query_priv_comp = "SELECT SQL_CACHE * FROM classified WHERE priv_comp='Private'"; } switch ($adtypes){ case "Private": $query.= " AND classified.priv_comp='Private'"; break; case "Company": $query.= " AND classified.priv_comp='Company'"; break; } $qry_result = mysql_query($query); // main query $result_priv_comp = mysql_query($query_priv_comp); // second query $num_priv_comp = mysql_num_rows($result_priv_comp); if ($adtypes=="All"){ $num_total = mysql_num_rows($qry_result); } else if ($adtypes!="All"){ $num_total=mysql_num_rows($qry_result) + mysql_num_rows($result_priv_comp); } if ($adtypes=="Private"){ $num_private = $num_total - $num_priv_comp; $num_company = $num_priv_comp; } else { $num_company = $num_total - $num_priv_comp; $num_private = $num_priv_comp; } Do you know of any other way which this can be done? Thanks **BTW**: I need the rows too, in order to display information to the user of the ads found!
Please clarify, do you just need the counts, or do you need the individual rows too?

以上就是Trick Question; Showing 'nr of results' of MySql search的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » HTML5 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏