ฟังก์ชั่นmysql_num_rows()

เริ่มโดย aitim, 23 มีนาคม 2009, 17:14:19

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

aitim

PHP
ฟังก์ชั่นนับฐานข้อมูล mysql_num_rows()
ผมมีฟิลหนึ่งชื่อ id_listซึ่งเป็นค่าหมวดต่างๆของเว็บบอร์ด(แบ่งเป็นหมวดๆ)โดยค่าคือ 1,2,3...
ผมจะแสดงที่หน้าแรกว่าหมวดนี้มีกี่กระทู้โดยให้นับจากid_listว่าแต่ละค่ามีอย่างละกี่อันต้องเติมส่วนไหนครับ
[direct=http://www.aitim.co]
ไอติมจะครองโลก รีวิวหนัง ซีรีส์ อนิเมะ หนังสือ[/direct]

thenetxx

#1
ถ้า ต้องการแค่นับจำนวนให้ใช้

select count(*) as num from table where id_list = '1';
select count(*) as num from table where id_list = '2';
select count(*) as num from table where id_list = '3';


ถ้าต้องแสดงข้อมูลเป็น list ด้วย

select * from table;

while($arr = mysql_fetch_assoc($rs)){
   switch($arr['id_list']){
       case '1' :
           $data1[] = $arr;
           $num1 = count($data1);
       break;
       case '2' :
           $data2[] = $arr;
           $num2 = count($data2);
       break;
       case '1' :
           $data3[] = $arr;
           $num3 = count($data3);
       break;
   }
}


เสร็จแล้วจะเอา data array ไปทำอะไร ก็แล้วแต่ชอบครับ
:)
Develop site but can't develop life
ASIA

aitim

ต้องเพิ่มตรงไหนครับ ผมลองแล้วไม่แสดง
<html>
<head><title>.:เว็บบอร์ด:. อิสระทางความคิด ใช้ชีวิตเหนือจินตนาการ</title>
<? include('config.inc.php'); ?>
<script type="text/JavaScript">
function setsmile(what) {
document.emotion.messages.value = document.emotion.elements.messages.value+" "+what;
document.emotion.messages.focus();
}
</script>
<style type="text/css">
<!--
a.l1 { color: #146384;   text-decoration: none; }
a:hover.l1 { color: #4D98B5; text-decoration: underline; }
.style2 {
color: #999999;
font-size: 12px;
}
-->
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title></title>
<style type="text/css">
<!--
body {
background-image: url(pic/bgtile.png);
}
.style3 {
font-size: 16px;
font-weight: bold;
color: #FF0000;
}
-->
</style></head>
<body>
<div align="center">
  <table width="972" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td height="91" background="pic/frame_top.gif"><img src="pic/logo.png" width="247" height="91"></td>
    </tr>
    <tr>
      <td background="pic/frame_body.gif"></td>
    </tr>
   
    <tr>
      <td align="center" background="pic/frame_body.gif"><table width="900" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td align="right"><a href="newtopic.php" target="_blank"><img src="pic/newtopic.gif" width="147" height="44" border="0"></a></td>
        </tr>
      </table></td>
    </tr>
    <tr>
      <td align="center" background="pic/frame_body.gif"><table width="900" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td align="center"></td>
          <td align="center" class="style3">&nbsp;</td>
          <td align="center" class="style3">กระทู้</td>
          <td align="center" class="style3">ล่าสุด</td>
        </tr>
        <tr>
          <td width="60" rowspan="2"><img src="pic/icon/moon.gif" width="55" height="55"></td>
          <td><a href="http://www.pokpik.com/board/show.php?id_list=0" target="_blank" class="l1">ที่ทำการป๊อกปิ๊ก</a></td>
          <td align="center">&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td colspan="3" class="style2">ศูนย์รวบรวมข้อตกลงการใช้บอร์ด ประชาสัมพันธ์กิจกรรม รับร้องเรียนปัญหาการใช้งาน </td>
          </tr>
        <tr>
          <td rowspan="2"><img src="pic/icon/dara.gif" width="50" height="50"></td>
          <td><a href="http://www.pokpik.com/board/show.php?id_list=1" target="_blank" class="l1">สวนสาธารณะ</a></td>
          <td align="center">&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td class="style2">คุยเฟื่องเรื่องทั่วไป สัพเพเหระ แบ่งปันประสบการณ์ แสดงความคิดเห็นกับประเด็นที่กำลังร้อน </td>
          <td class="style2">&nbsp;</td>
          <td class="style2">&nbsp;</td>
        </tr>
        <tr>
          <td rowspan="2"><img src="pic/icon/photo.gif" width="55" height="55"></td>
          <td><a href="http://www.pokpik.com/board/show.php?id_list=2" target="_blank" class="l1">หอศิลป์</a></td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td class="style2">อวดภาพวาด ภาพถ่าย ภาพทิวทัศน์ ภาพสาวสวย-หนุ่มหล่อ การ์ตูนแสนน่ารัก ฯลฯ </td>
          <td class="style2">&nbsp;</td>
          <td class="style2">&nbsp;</td>
        </tr>
        <tr>
          <td rowspan="2"><img src="pic/icon/music.gif" width="55" height="55"></td>
          <td><a href="http://www.pokpik.com/board/show.php?id_list=3" target="_blank" class="l1">ตู้เพลงไทย</a></td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td class="style2">แบ่งปันเพลงไทย เนื้อร้อง MV ฯลฯ </td>
          <td class="style2">&nbsp;</td>
          <td class="style2">&nbsp;</td>
        </tr>
        <tr>
          <td rowspan="2"><img src="pic/icon/music2.gif" width="55" height="55"></td>
          <td><a href="http://www.pokpik.com/board/show.php?id_list=4" target="_blank" class="l1">ตู้เพลงเอเชีย/อินเตอร์</a></td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td class="style2">แบ่งปันเพลงเอเชีย/อินเตอร์ เนื้อร้อง MV ฯลฯ </td>
          <td class="style2">&nbsp;</td>
          <td class="style2">&nbsp;</td>
        </tr>
        <tr>
          <td rowspan="2"><img src="pic/icon/love.gif" width="50" height="50"></td>
          <td><a href="http://www.pokpik.com/board/show.php?id_list=5" target="_blank" class="l1">พิพิธภัณฑ์ป๊อกปิ๊ก</a></td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td class="style2">สาระพันเรื่องราวมีสาระ อุดมปัญญา </td>
          <td class="style2">&nbsp;</td>
          <td class="style2">&nbsp;</td>
        </tr>
        <tr>
          <td rowspan="2"><img src="pic/icon/pro.gif" width="50" height="50"></td>
          <td><a href="http://www.pokpik.com/board/show.php?id_list=6" target="_blank" class="l1">ชุมชนคนใจดี</a></td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td class="style2">แจกเกมส์ แจกโปรแกรม แจกต่างๆนานา สารพัดที่จะแจก </td>
          <td class="style2">&nbsp;</td>
          <td class="style2">&nbsp;</td>
        </tr>
      </table></td>
    </tr>
    <tr>
      <td background="pic/frame_body.gif"></td>
    </tr>
    <tr>
      <td height="155" background="pic/frame_body.gif"><div align="center"><img src="pic/frame_grass.png" width="952" height="155"></div></td>
    </tr>
    <tr>
      <td height="41" background="pic/frame_copyright.gif"><div align="center"><? include('foot.php'); ?></div></td>
    </tr>
  </table>
</div>
</body>
</html>
[direct=http://www.aitim.co]
ไอติมจะครองโลก รีวิวหนัง ซีรีส์ อนิเมะ หนังสือ[/direct]

kingmaster

เอ่อ นะครับ

เอาโค๊ดที่ว่าไปแปะมันก็ไม่แสดงหรอกครับ โค๊ดนั่นเป็นเพียงตัวอย่างและแนวทาง คุณต้องเอาไปประยุกติใช้เอง

ปล. การเขียนโค๊ดนับแบบนี้ไม่ค่อยถูกต้องเท่าไหร่ครับ เพราะมันจะกินทรัพยากรณ์ server มาก เนื่องจากต้องเรียกนับจำนวนทุกครั้ง น่าเก็บจำนวนกระทู้ที่นับแล้วไว้ในดาต้าเบสเพื่อเรียกใช้โดยไม่ต้องนับ

:)

thenetxx

อ้างถึงจาก: kingmaster ใน 23 มีนาคม 2009, 18:46:43
เอ่อ นะครับ

เอาโค๊ดที่ว่าไปแปะมันก็ไม่แสดงหรอกครับ โค๊ดนั่นเป็นเพียงตัวอย่างและแนวทาง คุณต้องเอาไปประยุกติใช้เอง

ปล. การเขียนโค๊ดนับแบบนี้ไม่ค่อยถูกต้องเท่าไหร่ครับ เพราะมันจะกินทรัพยากรณ์ server มาก เนื่องจากต้องเรียกนับจำนวนทุกครั้ง น่าเก็บจำนวนกระทู้ที่นับแล้วไว้ในดาต้าเบสเพื่อเรียกใช้โดยไม่ต้องนับ

:)

^___^ ปกติผมก็ไม่ได้เขียนแบบนี้ตรง ๆ หรอกครับ ส่วนมากข้อมูลที่ดึงมา จะดึงผ่าน dao แล้วผมค่อย serialize ลงไฟล์เป็น cache ไว้ครับ
Develop site but can't develop life
ASIA

aitim

อ้างถึงจาก: thenetxx ใน 23 มีนาคม 2009, 21:10:03
อ้างถึงจาก: kingmaster ใน 23 มีนาคม 2009, 18:46:43
เอ่อ นะครับ

เอาโค๊ดที่ว่าไปแปะมันก็ไม่แสดงหรอกครับ โค๊ดนั่นเป็นเพียงตัวอย่างและแนวทาง คุณต้องเอาไปประยุกติใช้เอง

ปล. การเขียนโค๊ดนับแบบนี้ไม่ค่อยถูกต้องเท่าไหร่ครับ เพราะมันจะกินทรัพยากรณ์ server มาก เนื่องจากต้องเรียกนับจำนวนทุกครั้ง น่าเก็บจำนวนกระทู้ที่นับแล้วไว้ในดาต้าเบสเพื่อเรียกใช้โดยไม่ต้องนับ

:)

^___^ ปกติผมก็ไม่ได้เขียนแบบนี้ตรง ๆ หรอกครับ ส่วนมากข้อมูลที่ดึงมา จะดึงผ่าน dao แล้วผมค่อย serialize ลงไฟล์เป็น cache ไว้ครับ


รบกวนขอโค้ดหน่อยครับ
[direct=http://www.aitim.co]
ไอติมจะครองโลก รีวิวหนัง ซีรีส์ อนิเมะ หนังสือ[/direct]

mixture

       case '1' :
           $data3[] = $arr;
           $num3 = count($data3);
       break;


ทำไมถึงเป็น case 1 อ่ะ
[direct=http://moomak.wordpress.com]จิ[/direct]

ioff

select count(*) from table where ฟิลด์ประเภท= 'ประเภท';


หรือไม่ก็ 

$result=mysql_query("select * from table where ฟิลด์ประเภท= 'ประเภท' ");
$total=mysql_num_rows($result);

ไม่รู้เข้าใจถูกรึป่าว ลองดูค่ะ


www.thaihoro.co.cc

DJJome

ถ้าเลี่ยง count(*) ได้ ให้เลี่ยง
คิดง่ายๆ จะเป็นยังไง ถ้ามีซักหมื่น, แสน, ล้านเรคคอร์ด

สร้างฟิลด์มาเก็บดีกว่า
นะ