当查询字符串为空时,如何对特定类别进行计数?

发布于 2024-10-20 06:16:22 字数 1182 浏览 1 评论 0原文

我的主页/默认页面上有一个菜单,其中列出了 x 个类别。 我想计算每个类别中有多少产品。

例如:

香蕉(20)

苹果(8)

草莓(5)

到目前为止,我有这个:

 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);
        }
    }

显然我需要特定的类别ID,但如何在不运行查询字符串的情况下请求该类别ID,因为它位于默认页面上。 我错过了一些明显的事情吗?

多谢。

I have a menu on my masterpage / defaultpage where I'm listing x categories.
I would like to make a count of how many products there are in each category.

EX:

Bananas(20)

Apples(8)

Strawberries(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.
Am I missing something obious?

Thanks alot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝海 2024-10-27 06:16:22

您应该循环遍历您的类别并从那里获取 ID。不是来自查询字符串,因为它与您的页面相关(正如您自己编写的那样)。

鉴于您的示例,我希望 __account.GetAllProductCategories() 已经返回您需要的 ID

在这种情况下,您将使用类似的内容

var catid = listSubMenu.id;

但 id 取决于您 __account 返回的类型。

You should loop through your categories and get the ID from there. Not from the querystring since that is related to you page (as you wrote yourself as well).

Given your example, I would expect that __account.GetAllProductCategories() would return already the ID's you need

In that case you would use something like

var catid = listSubMenu.id;

But id depends on the type of what your __account returns.

云巢 2024-10-27 06:16:22

如果我对 GetAllProductCategories() 的结果模式的猜测是正确的...

["cat_id"]["cat_name"]

[1][Apples]

[2][Bananas]

[3][Oranges]

var cat_id = r["cat_id"]

或者可能

var cat_id = Int32.Parse(r["cat_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));

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

:(有两个更改,(1)

  • 更改为
  • {正确的 html 语法} 和 (2) r["cat_id"] 更改为 cat_id {您已经拥有它在变量和字符串中。Format 不介意为您重新转换为字符串})
  • 除此之外,我建议您研究像 LinqToSql 这样的 ORM,以便您可以直接使用对象...

    If I'm correct in my guess at your result schema from GetAllProductCategories()...

    ["cat_id"]["cat_name"]

    [1][Apples]

    [2][Bananas]

    [3][Oranges]

    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})

    Beyond that I would suggest looking into an ORM like LinqToSql so you could work directly with objects...

    笑,眼淚并存 2024-10-27 06:16:22

    首先在母版页中呈现类别链接:

    ** 当您调用 GetAllProductCategories 时,结果的每一行将至少有两列(cat_id 和 cat_name)。

    当您按索引 (var r = listSubMenu.Rows[i]) 获取每一行时,它返回的行将具有该记录的 cat_id 和 cat_name,我添加了 (var name = r["cat_name"]) 以进行说明。

    如果您对此进行调试并逐步执行,您应该会看到 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();
    

    然后进入另一个文本框或区域实际页面“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();
    

    ** 注意:当您调用 Request.QueryString["value"] 时,它会:

    1. 返回 null 表示不存在名称匹配的查询字符串参数

    2. 返回表示 value= 与 url 末尾或下一个 & 之间内容的字符串找到了。

    ** 这不是完全生产质量的代码,您应该对查询字符串值进行额外的检查,例如切换到 tryparse,检查返回的产品数量并显示“找不到该类别的产品”...等**

    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).

    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.

    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);)

    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"

    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:

    1. Return null indicating that there isn't a querystring parameter with a matching name

      or

    2. Return the string representing the content between value= and the end of the url or the 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 **

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文