|
 |
|
จะ sum ค่ารวม ที่แสดงผลออกมา อย่างไรคะ คือค่าออกมาเป็น 1 0 0 ต้องแสดงผลรวมเป็น 1 คะ |
|
 |
|
|
 |
 |
|
ตามนี้ครับ
Code (PHP)
<?php
$string = "100";
$arr_string = str_split($string);
echo array_sum($arr_string);
?>
|
 |
 |
 |
 |
Date :
2013-11-05 09:50:23 |
By :
mangkunzo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตามที่ k'mangkunzo ให้มาถ้าลองแทนค่าตรงๆมันได้คะ
แต่พอแทนค่า มันก็ออกมาเหมือนเดิม มันผิดเพราะการวน while($arr4=mssql_fetch_array($rs4)) รึป่าวคะ
จะต้องแก้ไขอย่างไรดีคะ รบกวนด้วยคะ
Code (PHP)
<?
$rs2=mssql_query("select * from area");
while($arr2=mssql_fetch_array($rs2)){
?>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="18%"><? echo $arr2[aname]; ?></td>
<td width="82%"><?
$rs4=mssql_query("select * from leasing");
while($arr4=mssql_fetch_array($rs4)){
$rs3=mssql_query("select COUNT(*) as cc from $arr4[lid] where $arr4[lid].aid = '$arr2[aid]'");
$arr3=mssql_fetch_array($rs3);
$sum = $arr3[cc];
//echo $sum;
$string = "$sum";
$arr_string = str_split($string);
echo array_sum($arr_string);
}
?>
</td>
</tr>
</table>
<?
}
?>
|
ประวัติการแก้ไข 2013-11-05 10:35:05
 |
 |
 |
 |
Date :
2013-11-05 09:55:59 |
By :
Baitong_ch |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ปรับ code ครับ
จากเดิม
Code (PHP)
$arr3=mssql_fetch_array($rs3);
$sum = $arr3[cc];
//echo $sum;
$string = "$sum";
$arr_string = str_split($string);
echo array_sum($arr_string);
เปลี่ยนเป็นใช้แบบนี้ก็พอ
Code (PHP)
$arr3=mssql_fetch_array($rs3); //$arr3 เป็น array อยู่แล้วใช่ไม๊ครับ
echo array_sum($arr3); // ถ้า $arr3 มี key ให้ระบุ key เข้าไปด้วยน่ะครับ ตัวอย่างเช่น echo array_sum($arr3[values]);
|
ประวัติการแก้ไข 2013-11-05 11:37:12
 |
 |
 |
 |
Date :
2013-11-05 11:34:22 |
By :
mangkunzo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
<?
$rs4=mssql_query("select * from leasing");
while($arr4=mssql_fetch_array($rs4)){
$rs3=mssql_query("select COUNT(*) as cc from $arr4[lid] where $arr4[lid].aid = '$arr2[aid]'");
$arr3=mssql_fetch_array($rs3);
$sum = $arr3[cc];
echo $sum;
$sum_all+=$sum; //เก็บค่า
}
//เมื่อจบ Loop หาร 100 เพราะ 100 =1
$sum_all= $sum_all/100;
echo $sum_all;
?>
edit : ปรับปรุงโค้ดผิดพลาด พิมพ์ตกไปหนึ่งตัว
|
ประวัติการแก้ไข 2013-11-05 12:47:00 2013-11-05 12:50:46
 |
 |
 |
 |
Date :
2013-11-05 12:46:33 |
By :
meannerss |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
1 0 0 <<< แบบนี้เหรอครับ แสดงมีค่าว่างแทรกระหว่างนี้ไช่ไหมก็ตัดค่าว่างออกก่อน... เอ้าไปลองใหม่ดูครับ
<?
$rs4=mssql_query("select * from leasing");
while($arr4=mssql_fetch_array($rs4)){
$rs3=mssql_query("select COUNT(*) as cc from $arr4[lid] where $arr4[lid].aid = '$arr2[aid]'");
$arr3=mssql_fetch_array($rs3);
$sum = ereg_replace('[[:space:]]+','', trim($arr3[cc]));
echo $sum;
$sum_all+=$sum; //เก็บค่า
}
//เมื่อจบ Loop หาร 100 เพราะ 100 =1
$sum_all= $sum_all/100;
echo $sum_all;
?>
|
 |
 |
 |
 |
Date :
2013-11-05 13:27:32 |
By :
meannerss |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 7 เขียนโดย : meannerss เมื่อวันที่ 2013-11-05 13:27:32
ได้เพราะเอาของพี่ๆ 2 คนมารวมกันคะ
Code (PHP)
<?
$rs2=mssql_query("select * from area");
while($arr2=mssql_fetch_array($rs2)){
?>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="18%"><? echo $arr2[aname]; ?></td>
<td width="82%"><?
$rs4=mssql_query("select * from leasing");
while($arr4=mssql_fetch_array($rs4)){
$rs3=mssql_query("select COUNT(*) as cc from $arr4[lid] where $arr4[lid].aid = '$arr2[aid]'");
$arr3=mssql_fetch_array($rs3); //$arr3 เป็น array อยู่แล้วใช่ไม๊ครับ
$sum_all+=$arr3[cc]; //เก็บค่า
//echo array_sum($arr3[cc]); // ถ้า $arr3 มี key ให้ระบุ key เข้าไปด้วยน่ะครับ ตัวอย่างเช่น echo array_sum($arr3[values]);
}echo $sum_all;
$sum_all = 0;
?>
</td>
</tr>
</table>
<?
}
?>
ขอบคุณมากๆคะ :)
|
 |
 |
 |
 |
Date :
2013-11-05 15:31:25 |
By :
Baitong_ch |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
กรรม มีแบบนี้ได้ด้วย เป็นไปได้อย่างไร 5 5 5 5   
|
 |
 |
 |
 |
Date :
2013-11-05 15:41:26 |
By :
meannerss |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|