<?php

/**
 * 处理数据的函数
 * ============================================================================
 * Power By Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: Utility.class.php 4 2011-11-02 11:02:08Z zhangwentao $
 * http://www.t-mac.org；
 */
class Utility
{
    /**
     * 下拉列表<option>
     * @param <type> $a      数组
     * @param <type> $v      当前
     * @param <type> $type   类别 $type='db'从数据库中用getAll查出来的
     * @return string 
     */
    static public function Option($a=array(), $v=null, $type = null)
    {
        $option = null;
        if ($type == null) {
            foreach ($a AS $key => $value) {
                $selected = $v == $key ? 'selected' : null;
                $option .= "<option value='{$key}' {$selected}>" . strip_tags($value) . "</option>";
            }
        } else {
            $type_ary = explode(',', $type);
            foreach ($a AS $key => $value) {
                if (is_array($value)) {
                    $key = strval($value['' . $type_ary{'0'} . '']);
                    $value = strval($value['' . $type_ary{'1'} . '']);
                }
                $selected = $v == $key ? 'selected' : null;
                $option .= "<option value='{$key}' {$selected}>" . strip_tags($value) . "</option>";
            }
        }

        return $option;
    }

    /**
     * 单选Radio生成
     * @param <array> $a    数组
     * @param <string> $n   name
     * @param <string> $v   当前值
     * @param <string> $action onclick="X"
     * @param <type> $type  类别 $type='db'从数据库中用getAll查出来的
     * @return <type> 
     */
    static public function RadioButton($a=array(), $n, $v = -1, $action = null, $type = null)
    {
        $cbox = null;
        if ($type == null) {
            foreach ($a AS $key => $value) {
                $checked = $v == $key ? 'checked' : null;
                $cbox .= "<input type='radio' name='{$n}' value='{$key}' {$checked} {$action} />{$value}&nbsp;";
            }
        } else {
            $type_ary = explode(',', $type);
            foreach ($a AS $key => $value) {
                if (is_array($value)) {
                    $key = strval($value['' . $type_ary{'0'} . '']);
                    $value = strval($value['' . $type_ary{'1'} . '']);
                }
                $checked = $v == $key ? 'checked' : null;
                $cbox .= "<input type='radio' name='{$n}' value='{$key}' {$checked} {$action} />{$value}&nbsp;";
            }
        }
        return $cbox;
    }

}