ÔõôʹÓÃthinkphp¾ÙÐÐÊý¾ÝÇóºÍ²¢ÅÅÐÐ
°ì·¨1£ºÅþÁ¬Êý¾Ý¿â
»á¼ûThinkPHP¹ÙÍø²¢ÏÂÔØ×îа汾£¬È»ºó½âѹ²¢½«Æä°²ÅÅÔÚЧÀÍÆ÷ÇéÐÎÖС£ÔÚThinkPHPµÄĿ¼ÖУ¬ÐèÒª½¨ÉèÒ»¸öÃûΪTestµÄÏîÄ¿£¬È»ºóÔÚ¸ÃÏîÄ¿Öн¨ÉèÒ»¸ö¿ØÖÆÆ÷ºÍÄ£×ÓÀ´»ñÈ¡Êý¾Ý¡£
// application/index/controller/Index.php namespace app\index\controller; use think\Controller; use app\index\model\Data; class Index extends Controller { public function index() { $list = Data::all(); // ´ËËæ´¦ÀíÊý¾ÝÇóºÍÅÅÃû $this->assign('list', $list); return $this->fetch(); } }
µÇ¼ºó¸´ÖÆ
// application/index/model/Data.php namespace app\index\model; use think\Model; class Data extends Model {}
µÇ¼ºó¸´ÖÆ
ThinkPHPÌṩÁ˺ܺõÄÊý¾Ý»á¼û²ã£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÉÏÃæµÄ´úÂëÇáËɵؽ«Êý¾Ý´ÓÊý¾Ý¿âÖÐÌáÈ¡³öÀ´¡£
°ì·¨2£ºÇóºÍ
ÎÒÃÇ¿ÉÒÔ½«×ÅʵÏÖΪÈçÏÂËùʾ£º
$sum = 0; foreach ($list as $item) { $sum += $item['field']; }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇÐèÒª½«fieldÌæ»»³ÉÎÒÃÇÒªÇóºÍµÄ×Ö¶ÎÃû¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
°ì·¨3£ºÅÅÐÐ
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª¶ÔÊý¾ÝµÄÇóºÍЧ¹û¾ÙÐÐÅÅÐС£ÎÒÃÇÊ×ÏȽ«Êý¾ÝÅÅÐò£¬È»ºóƾ֤ËùÐèµÄ˳ÐòÊä³öÓÐÐò±í¸ñ¡£
function cmp($a, $b) { if ($a['sum'] == $b['sum']) { return 0; } return $a['sum'] > $b['sum'] ? -1 : 1; } usort($list, "cmp");
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÐèÒª½«sumÌæ»»³ÉÎÒÃÇËùÐèµÄÇóºÍ×ֶεÄÃû³Æ¡£
°ì·¨4£ºÊä³ö
ÏÖÔÚ£¬ÎÒÃÇÒѾÍê³ÉÁËÊý¾ÝµÄÇóºÍºÍÅÅÐУ¬ÎÒÃÇÖ»ÐèÒª½«Êý¾Ýƾ֤˳ÐòäÖȾµ½HTMLÖм´¿É¡£
<table> <thead> <tr> <th>Ãû³Æ</th> <th>ÇóºÍЧ¹û</th> <th>ÅÅÃû</th> </tr> </thead> <tbody> {volist name="list" id="vo"} <tr> <td>{$vo.name}</td> <td>{$vo.sum}</td> <td>{$i++}</td> </tr> {/volist} </tbody> </table>
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÐèÒª½«nameÌ滻ΪÎÒÃÇ´æ´¢Ãû³ÆµÄÁУ¬sumÌ滻ΪÎÒÃÇÇóºÍµÄÁС£
ÒÔÉϾÍÊÇÔõôʹÓÃthinkphp¾ÙÐÐÊý¾ÝÇóºÍ²¢ÅÅÐеÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡