tags:

views:

108

answers:

2

Hello!

I have following script:

$cbid = 0;
 $arrayid = 0;
 while ($FINDNEWSresult = mysql_fetch_array($FINDNEWSquery)) {
  echo "<tr>";
  // Prvi stolpec - označevanje (checkbox)
  echo "<td class=\"middlerow checkbox\"><input id=\"" . $FINDNEWSresult['NEWSid'] . "\" type=\"checkbox\" /></td>";
  echo "<td class=\"middlerow\"><a class=\"ahover\" href=\"?mod=news&command=edit&edit=true&id=" . $FINDNEWSresult['NEWSid'] ."\">" . substr($FINDNEWSresult['NEWStitle'], 0, $MOD_NEWStextlimit)  . "</a></td>";
  echo "<td class=\"middlerow\"><a class=\"ahover\" href=\"?mod=news&command=edit&edit=true&id=" . $FINDNEWSresult['NEWSid'] ."\">" . substr($FINDNEWSresult['NEWStext'], 0, $MOD_NEWStextlimit) . "</a></td>";
  echo "<td class=\"middlerow\">" . $FINDNEWSresult['NEWSuser'] . "</td>";
  echo "<td class=\"middlerow\">" . $FINDNEWSresult['NEWSdate'] . "</td>";
  echo "</tr>";
  $NEWSarray = array ( $arrayid => array ( "Id" => $FINDNEWSresult['NEWSid'],
             "Title" => $FINDNEWSresult['NEWStitle'],
             "Text" => $FINDNEWSresult['NEWStext'],
             "User" => $FINDNEWSresult['NEWSuser'],
             "Date" => $FINDNEWSresult['NEWSdate']
            )
       );
  // Povecaj $cb zarad checkbox id
  $cbid++;
  $arrayid++;
 }

How to modify script, that array will be filled with each news? Currently, array stores only last news.

Thanks for help!

+1  A: 

Like this:

$cbid = 0;
        $arrayid = 0;
     $NEWSarray = array(); // Not necessary but good for clarity
        while ($FINDNEWSresult = mysql_fetch_array($FINDNEWSquery)) {
                echo "<tr>";
                // Prvi stolpec - oznacevanje (checkbox)
                echo "<td class=\"middlerow checkbox\"><input id=\"" . $FINDNEWSresult['NEWSid'] . "\" type=\"checkbox\" /></td>";
                echo "<td class=\"middlerow\"><a class=\"ahover\" href=\"?mod=news&command=edit&edit=true&id=" . $FINDNEWSresult['NEWSid'] ."\">" . substr($FINDNEWSresult['NEWStitle'], 0, $MOD_NEWStextlimit)  . "</a></td>";
                echo "<td class=\"middlerow\"><a class=\"ahover\" href=\"?mod=news&command=edit&edit=true&id=" . $FINDNEWSresult['NEWSid'] ."\">" . substr($FINDNEWSresult['NEWStext'], 0, $MOD_NEWStextlimit) . "</a></td>";
                echo "<td class=\"middlerow\">" . $FINDNEWSresult['NEWSuser'] . "</td>";
                echo "<td class=\"middlerow\">" . $FINDNEWSresult['NEWSdate'] . "</td>";
                echo "</tr>";
                $NEWSarray[] = array ( $arrayid => array ( "Id" => $FINDNEWSresult['NEWSid'],
                                                                                                 "Title" => $FINDNEWSresult['NEWStitle'],
                                                                                                 "Text" => $FINDNEWSresult['NEWStext'],
                                                                                                 "User" => $FINDNEWSresult['NEWSuser'],
                                                                                                 "Date" => $FINDNEWSresult['NEWSdate']
                                                                                                )
                                                        );
                // Povecaj $cb zarad checkbox id
                $cbid++;
                $arrayid++;
        }

Alternatively to the [] you can use PHPs array_push as well.

Daff
+1  A: 

You need to append to the array, not to create it all over again.

$NEWSarray[] = <new_element> ;

or

array_push($NEWSarray, <new_element>);
Milen A. Radev