dqdt45183 2012-11-02 01:18
浏览 13
已采纳

PHP - 如果使用类函数,我们如何显示相关行的生成数字

How can i generate a numbers to the table that has been fetch a data from database, here are my situation:

//Action File Calling a data

$loads = array();
$loadno = $_GET['eig'];
$result = mysql_query("SELECT * FROM task_load WHERE load_no='$loadno' ORDER BY inv ASC");

while($row = mysql_fetch_assoc($result))
{
    $loads[] = new VerifyLoads($row);
}
        foreach($loads as $c){
             echo $c->markup_print();
        }

// Class file receive request

    public function markup_print()
    {
    $d = &$this->data;

    return '
        <tr id="line-'.$d['id'].'">
            <td class="iprint">'.GENERATE_NUMBER_HERE.'</td>
            <td class="cprint">'.$d['customer_name'].'</td>
            <td class="cprint">'.$d['inv'].'</td>
            <td class="rprint">'.$d['value'].'</td>
            <td class="cprint">'.$d['ctn'].'</td>
            <td class="iprint"></td>
            <td class="iprint"></td>
            <td class="iprint"></td>
        </tr>       
        ';
    }
  • 写回答

1条回答 默认 最新

  • doukai2839 2012-11-02 02:54
    关注

    Though I am fairly unsure as to what kind of number you're looking to generate, based on your output I'm assuming you want a simple line-number for the current record that's being displayed. The difficulty, in this case, is because each line is a separate instance of the VerifyLoads class.

    To handle this you could take one of several ways. The quickest and easiest method will work because you display them in the order you created them. This method will involve two new variables in the VerifyLoads class; the first is a static variable to keep the total-line count and the second is a non-static variable to keep the current instance's count:

    class VerifyLoads {
        private $_lineTotal = 1;
        private $_lineNumber = -1;
    
        public function __construct() {
            $this->_lineNumber = self::$_lineTotal++;
        }
    
        public function markup_print() {
            $d = &$this->data;
    
            return '
                <tr id="line-'.$d['id'].'">
                <td class="iprint">'.$this->_lineNumber.'</td>
                <td class="cprint">'.$d['customer_name'].'</td>
                <td class="cprint">'.$d['inv'].'</td>
                <td class="rprint">'.$d['value'].'</td>
                <td class="cprint">'.$d['ctn'].'</td>
                <td class="iprint"></td>
                <td class="iprint"></td>
                <td class="iprint"></td>
                </tr>       
            ';
        }
    }
    

    An alternative method, if you didn't display the records in the same order you created them would be to also keep a static variable in the class but to have a local method that returned the current line:

    class VerifyLoads {
        private static $_lineNumber = 1;
    
        private function _currentLine() {
            return self::$_lineNumber++;
        }
    
        // .. code as normal ..
        // .. to access, just call $this->_currentLine()
    }
    

    If you need to use the second method and also use it to display multiple tables on the same page you could implement a resetLineNumber() method to set the static variable back to 1 as well.

    On the flip-side, I may have completely misunderstood your question. If this is the case, please elaborate as-to-what kind of number you need displayed and I'll revise.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行