简体   繁体   English

当querystring为空时,如何计算特定类别?

[英]How to I make a count on specific category when querystring is empty?

I have a menu on my masterpage / defaultpage where I'm listing x categories. 我的主页/默认页面上有一个菜单,其中列出了x个类别。 I would like to make a count of how many products there are in each category. 我想计算每个类别中有多少种产品。

EX: 例如:

Bananas(20) 香蕉(20)

Apples(8) 苹果(8)

Strawberries(5) 草莓(5)

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_it?"];
        var count = __account.GetSpecificCategory(id);

        sb.AppendFormat(String.Format(@"<li{0}><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 GetAllProductCategories()
    {
        const string request =
        @"
            SELECT * FROM products_category
            WHERE cat_active = 1
            ORDER BY cat_name ASC
        ";
        using (var query = new MySqlCommand(request))
        {
            return __dbConnect.GetData(query);
        }
    }

Obiously i need the specific categoryid, but how to I request that without having querystrings running since it is on the default page. 显然,我需要特定的categoryid,但是如何在不运行查询字符串的情况下请求它,因为它位于默认页面上。 Am I missing something obious? 我想念一些令人讨厌的东西吗?

Thanks alot. 非常感谢。

You should loop through your categories and get the ID from there. 您应该遍历类别并从中获取ID。 Not from the querystring since that is related to you page (as you wrote yourself as well). 不是来自querystring,因为那与您的页面相关(因为您自己也写过)。

Given your example, I would expect that __account.GetAllProductCategories() would return already the ID's you need 给定您的示例,我希望__account.GetAllProductCategories()可以返回您所需的ID。

In that case you would use something like 在这种情况下,您将使用类似

var catid = listSubMenu.id;

But id depends on the type of what your __account returns. 但是id取决于__account返回的类型。

If I'm correct in my guess at your result schema from GetAllProductCategories()... 如果我对GetAllProductCategories()的结果模式是正确的,则...

["cat_id"]["cat_name"] [“ cat_id”] [“ cat_name”]

[1][Apples] [1] [苹果]

[2][Bananas] [2] [香蕉]

[3][Oranges] [3] [橘子]

var cat_id = r["cat_id"]

or possibly 或可能

var cat_id = Int32.Parse(r["cat_id"])

I would also change: 我也会更改:

sb.AppendFormat(String.Format(@"<li{0}><a href='/account/products.aspx?categoryid={0}'>{1} ({2})</a></li>", r["cat_id"], r["cat_name"], count.Rows.Count));

To: 至:

sb.AppendFormat(String.Format(@"<li><a href='/account/products.aspx?categoryid={0}'>{1} ({2})</a></li>", cat_id, r["cat_name"], count.Rows.Count));

(There are two changes, (1) <li{0}> to <li> {proper html syntax} and (2) r["cat_id"] to cat_id {you already have it in a variable and string.Format doesn't mind recasting to a string for you}) (有两个更改,(1)将<li {0}>更改为<li> {适当的html语法},将(2)r [“ cat_id”]更改为cat_id {您已经在变量和string.Format中添加了它。介意为您重铸为字符串})

Beyond that I would suggest looking into an ORM like LinqToSql so you could work directly with objects... 除此之外,我建议您像LinqToSql一样研究ORM,以便您可以直接使用对象...

First render the category links in the master page: 首先在母版页中呈现类别链接:

** When you call GetAllProductCategories, each row of the result will have at least two columns (cat_id and cat_name). **调用GetAllProductCategories时,结果的每一行将至少有两列(cat_id和cat_name)。

When you get each row by index (var r = listSubMenu.Rows[i]) the row it returns will have the cat_id and cat_name for that record, I added (var name = r["cat_name"]) for illustration. 当您通过索引获取每一行时(var r = listSubMenu.Rows [i]),它返回的行将具有该记录的cat_id和cat_name,我添加了(var name = r [“ cat_name”])进行说明。

If you debug this and step through you should see each iteration through the for loop gives the id variable the next category's id which is then used in the line (var count = __account.GetSpecificCategory(id);) 如果您对此进行调试并逐步进行,则应该看到for循环中的每个迭代都为id变量提供了下一个类别的ID,然后在该行中使用它(var count = __account.GetSpecificCategory(id);)

var listSubMenu = __account.GetAllProductCategories();
var sb = new StringBuilder();
for (int i = 0; i < listSubMenu.Rows.Count; i++)
{
    var r = listSubMenu.Rows[i];

    var id = Int32.Parse(r["cat_id"]);
    var name = r["cat_name"];

    var count = __account.GetSpecificCategory(id);

    sb.AppendFormat(String.Format(@"<li{0}><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();

Then into another textbox or area of the actual page "products.aspx" 然后进入另一个文本框或实际页面的区域“ products.aspx”

var sbProducts = new StringBuilder();
var selectedCat = Request.QueryString["categoryid"];

if(!string.IsNullOrWhitespace(selectedCat))
{
    var selectedCatId = Int32.Parse(selectedCat);
    var products = __account.GetSpecificCategory(selectedCatId);

    for(int j = 0; j < products.Rows.Count; j++)
    {
         // ... do product listing stuff here
         // sbProducts.Append(...);
    }
}
else
{
    sbProducts.AppendLine("Invalid Category Id Selected!");
}
active_selected_products.Text = sbProducts.ToString();

** Note: when you call Request.QueryString["value"] it will either: **注意:当您调用Request.QueryString [“ value”]时,它将:

  1. Return null indicating that there isn't a querystring parameter with a matching name 返回null,表明不存在名称匹配的querystring参数

    or 要么

  2. Return the string representing the content between value= and the end of the url or the next & found. 返回表示内容的字符串,该内容介于value =和url的末尾之间或next与found之间。

** this isn't fully production quality code, there are additional checks you should be doing on the query string value, switch to tryparse for example, check number of products returned and show "No products found for that category" ... etc ** **这不是完整的生产质量代码,您需要对查询字符串值进行其他检查,例如切换到tryparse,检查返回的产品数量并显示“未找到该类别的产品” ...等等**

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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