wenpi51

我的路,我的道,斩道,破而后立!

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
代码
  1 /*表结构*/
  2 /*
  3     表一
  4     table_大类(id,name)
  5     表二
  6     table_小类(id,name,cat_id)
  7     此表中cat_id是表table_大类的id
  8 */
  9 
 10 /*效果*/
 11 /*
 12 大类1                    大类2
 13     小类19个顺序列出        小类19个顺序列出
 14 大类3                    大类4
 15     小类19个顺序列出        小类19个顺序列出
 16 大类5                    大类6
 17     小类19个顺序列出        小类19个顺序列出
 18 .
 19 .
 20 .
 21 .
 22 */
 23 
 24 /*
 25 方法GetList是嵌套循环
 26 方法GetList2是单循环
 27 
 28 二者结果相同
 29 方法GetList2优于方法GetList,
 30 原因是单循环,只读一次数据库,
 31 而GetList读数据的次数是 table_大类的记录+1 [解释:读一次表table_大类,此表中每一条记录要对应表table_小类中的19条记录,故表table_大类有多少条记录,小类表就要读多少次]
 32 */
 33 
 34 /// <summary>
 35 /// 大类
 36 /// </summary>        
 37 static public string GetList()
 38 {
 39     StringBuilder sb = new StringBuilder();
 40     DataTable dt = Control.SQLSelect("select id,[name] from table_大类");
 41     for (int i = 0; i < dt.Rows.Count; i++)
 42     {
 43         string dt_id = dt.Rows[i]["id"].ToString();
 44         string dt_name = dt.Rows[i]["name"].ToString();
 45         if (i % 2 == 0)
 46         {
 47             string strcss = (i % 4 == 0? "" : " class=\"class1a\"";
 48             sb.Append("<div" + strcss + ">");
 49         }
 50         if (i < 12)
 51         {
 52             sb.Append("<dl class=\"class1\">");
 53         }
 54         else
 55         {
 56             sb.Append("<dl class=\"class2\" style=\"display:none;\">");
 57         }
 58         sb.AppendFormat("<dt class=\"fo14 lan alan\"><a href=\"{0}\">{1}</a></dt>", Url.GetCat(int.Parse(dt_id)), dt_name);                
 59         sb.Append("<dd class=\"lana alana\">");
 60 
 61         #region =========================小类=========================
 62         DataTable dtSub = Control.SQLSelect("select id,[name] from table_小类 where cat_id="+dt_id);
 63         int subCount = Math.Min(19, dtSub.Rows.Count);
 64         for (int j = 0; j < subCount; j++)
 65         {
 66             string dtsub_id = dtSub.Rows[j]["id"].ToString();
 67             string dtsub_name = dtSub.Rows[j]["name"].ToString();
 68             string strColor = "";
 69             if (j == 0)
 70             {
 71                 strColor = " style=\"color:#f60\"";
 72             }
 73             sb.AppendFormat("<a href=\"{0}\"{1}>{2}</a>&nbsp;|&nbsp;", Url.GetSubcat(int.Parse(dtsub_id)), strColor, dtsub_name);
 74         }
 75         dtSub.Dispose();
 76         #endregion
 77 
 78         sb.AppendFormat("<a href=\"{0}\">更多&gt;&gt;</a>", Url.GetCat(int.Parse(dt_id)));
 79         sb.Append("</dd></dl>");
 80         if (i % 2 != 0 || i == dt.Rows.Count - 1)
 81         {
 82             sb.AppendLine("</div>");
 83         }
 84     }
 85     dt.Dispose();
 86     return sb.ToString();
 87 }
 88 
 89 /// <summary>
 90 /// 大类
 91 /// </summary>
 92 /// <returns></returns>
 93 static public string GetList2()
 94 {
 95     int pre_catid = 0;  //上一个catid
 96     int order_catid = 0;  //当前catid顺序
 97     int numTop19Subcat = 0;   //小类只显示前19个
 98 
 99     StringBuilder sb = new StringBuilder();
100     sb.Append("<div>");
101     DataTable dt = Control.SQLSelect("select aa.id,aa.name,bb.id as [catid],bb.name as [catname] from table_小类 as aa join table_大类 bb on aa.cat_id=bb.id");
102     for (int i = 0; i < dt.Rows.Count; i++)
103     {
104         string dt_id = dt.Rows[i]["id"].ToString();
105         string dt_name = dt.Rows[i]["name"].ToString();
106         int dt_catid = int.Parse(dt.Rows[i]["catid"].ToString());
107         string dt_catname = dt.Rows[i]["catname"].ToString();
108         if (pre_catid != dt_catid)
109         {
110             if (pre_catid != 0)
111             {
112                 sb.AppendFormat("<a href=\"{0}\">更多&gt;&gt;</a>", Url.GetCat(pre_catid));
113                 sb.Append("</dd></dl>");
114                 if (order_catid % 2 == 0)
115                 {
116                     string strcss = ((order_catid % 4== 0? "" : " class=\"class1a\"";
117                     sb.AppendLine("</div>");
118                     sb.Append("<div" + strcss + ">");
119                 }
120             }
121             order_catid++;
122             if (order_catid < 12)
123             {
124                 sb.Append("<dl class=\"class1\">");
125             }
126             else
127             {
128                 sb.Append("<dl class=\"class2\" style=\"display:none;\">");
129             }
130             sb.AppendFormat("<dt class=\"fo14 lan alan\"><a href=\"{0}\">{1}</a></dt>", Url.GetCat(dt_catid), dt_catname);                    
131             sb.Append("<dd class=\"lana alana\">");
132         }
133         #region =========================小类=========================
134         string strColor = "";
135         if (pre_catid != dt_catid)
136         {
137             pre_catid = dt_catid;
138             strColor = " style=\"color:#f60\"";
139             numTop19Subcat = 0;
140         }
141         numTop19Subcat++;
142         if (numTop19Subcat <= 19)
143         {
144             sb.AppendFormat("<a href=\"{0}\"{1}>{2}</a>&nbsp;|&nbsp;", Url.GetSubcat(int.Parse(dt_id)), strColor, dt_name);
145         }
146         #endregion
147     }
148     dt.Dispose();
149     sb.AppendFormat("<a href=\"{0}\">更多&gt;&gt;</a>", Url.GetCat(pre_catid));
150     sb.Append("</dd></dl>");
151     sb.AppendLine("</div>");
152 
153     return sb.ToString();
154 }

 

posted on 2010-10-30 14:23  wenpi51  阅读(227)  评论(0)    收藏  举报