Page 1 of 1

Stuck on adding multiple entrys, instead of just one!

Posted: Sat May 13, 2006 2:40 am
by AshrakTheWhite
reference code is here: viewtopic.php?t=48349


what the problem is is currently that whole thing just prints out the post entries that you type in, what i need it to do is maybe store every sent post in an array, untill i press another key say "finish" and only then does it send it into the printer to finally print out everything i entered, in the specific collumns and rows ofcourse.

im stuck at that point and i have no idea on how to do it :( also need it in a hurry, so any help would be appretiated.

Posted: Sat May 13, 2006 3:44 am
by timvw
I guess you've already described the solution yourself here... An abstract solution:

Code: Select all

<?php
class Foo {
  private $items;

  function __construct() {
    // initialize an empty array to store the items
    $this->items = array();
  }

  function addItem($item) {
    // append the item to the items array
    $this->items[] = $item;
  }

  function sendAndCleanUp() {
    foreach($this->items as $item) {
      // do something with $item
    }
    // clean up the items
    $this->items = array();
  }
}
?>

Posted: Sat May 13, 2006 9:05 am
by AshrakTheWhite
should this work?

Code: Select all

public $output;

    public function __construct()
    {
        $this->output = array();
    }

public function printItems() 
    {
        $sql = 'SELECT * FROM notos';
        $result = mysql_query($sql) or die (mysql_error());
        while ($info = mysql_fetch_array($result))
        {
            $summa = $this->getItemSum($info['kogus'], $info['hind']);
            $this->output = '
            <tr>
                <td width="400" height="20" valign="top">'. $info['nimetus'] .'</td>
                <td width="100" valign="top">'. $info['markused'] .'</td>
                <td width="100" valign="top">'. $info['kogus'] .'</td>
                <td width="100" valign="top">'. $info['uhik'].'</td>
                <td width="100" valign="top">'. $info['hind'] .'</td>
                <td width="98" valign="top">'. $summa .'</td>
            </tr>
            ';
            
        }        
    }


        echo '<pre>';
        print_r($this->output);
        echo '</pre>';
it should print out the HTML as many times as there are DB entries?

Posted: Sat May 13, 2006 9:20 am
by timvw
AshrakTheWhite wrote:should this work?
If you try it, you'll know..


Your code always assign the snipped code to this output.

Code: Select all

$this->output = ' .. snipped your code .. ';
You probably want to append it... ( documentation on array)

Code: Select all

$this->output[] = ' ... code ... ';
And now iterate with foreach over $this->output.

Posted: Sat May 13, 2006 9:52 am
by AshrakTheWhite
how do i iterate with foreach over the output and where, i tryed doing it in the midst of my HTML it threw me an error, if i do it in the midst of a function then how will it send out the chunks of data?!

Posted: Sat May 13, 2006 10:36 am
by timvw

Posted: Sat May 13, 2006 3:13 pm
by AshrakTheWhite
im sorry i cant get the hang of for each loops, at all :(