如何在不使用查询字符串值的情况下对特定类别进行计数
我的主页上有一个左侧菜单,其中列出了我的类别。我想统计一下每个类别中有多少产品。
示例:
- 香蕉(20)
- 苹果(8)
- 草莓(5)
- 芒果(9)
我的问题是,我没有查询字符串值,因为它位于母版页上,并且没有值正在运行。 到目前为止,我有这个:
var listSubMenu = __account.GetAllProductCategories();
var sb = new StringBuilder();
for (int i = 0; i < listSubMenu.Rows.Count; i++)
{
var r = listSubMenu.Rows[i];
var catid = Request.QueryString["thespecific_category_id_but_how_do_i_get_the_value?"];
var count = __account.GetSpecificCategory(id);
sb.AppendFormat(String.Format(@"<li><a href='/account/products.aspx?categoryid={0}'>{1} ({2})</a></li>", r["cat_id"], r["cat_name"], count.Rows.Count));
}
active_sub_products.Text = sb.ToString();
My DataTable:
public DataTable GetSpecificCategory(int categoryid)
{
const string request =
@"
SELECT * FROM products_category
WHERE
cat_id=?categoryid
AND
cat_active = 1
LIMIT 1
";
using (var query = new MySqlCommand(request))
{
query.Parameters.AddWithValue("?categoryid", categoryid);
return __dbConnect.GetData(query);
}
}
显然我需要特定的categoryid来进行计数,但是如何在不运行查询字符串的情况下获取id,因为它位于母版页上。我错过了一些明显的事情吗?
多谢。
I have a leftsidemenu on my masterpage where I'm listing my categories. I would like to make a count of how many products there are in each category.
EXAMPLE:
- Bananas(20)
- Apples(8)
- Strawberries(5)
- Mangoes(9)
My problem is, that I dont have a querystring value since it is at the masterpage, and no values is running.
So far, I have this:
var listSubMenu = __account.GetAllProductCategories();
var sb = new StringBuilder();
for (int i = 0; i < listSubMenu.Rows.Count; i++)
{
var r = listSubMenu.Rows[i];
var catid = Request.QueryString["thespecific_category_id_but_how_do_i_get_the_value?"];
var count = __account.GetSpecificCategory(id);
sb.AppendFormat(String.Format(@"<li><a href='/account/products.aspx?categoryid={0}'>{1} ({2})</a></li>", r["cat_id"], r["cat_name"], count.Rows.Count));
}
active_sub_products.Text = sb.ToString();
My DataTable:
public DataTable GetSpecificCategory(int categoryid)
{
const string request =
@"
SELECT * FROM products_category
WHERE
cat_id=?categoryid
AND
cat_active = 1
LIMIT 1
";
using (var query = new MySqlCommand(request))
{
query.Parameters.AddWithValue("?categoryid", categoryid);
return __dbConnect.GetData(query);
}
}
Obiously i need the specific categoryid to make a count, but how to I get the id without having querystrings running since it is on the masterpage. Am I missing something obious?
Thanks alot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试直接修改 SQL 查询并使用 count 函数,您将获得所需的结果。检查此处 http://www.techonthenet.com/sql/count.php
Try to modify directly your SQL query and by using the count function, you'll get what you need. Check here http://www.techonthenet.com/sql/count.php