Search code examples
phpmysqlauto-generate

PHP - How can we display generated numbers for related rows if using class function


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>       
        ';
    }

Solution

  • 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.