18.02.2025
Konstancja, Krystian, Sylwana, Szymon, Albert, Fryda, Zuzanna
Programowanie PHP - Wyświetlanie Danych Z Tablicy Wielowymiarowej
Jest to klasa pozwalająca na wyświetlanie danych zapisanych w postaci tablicy wielowymiarowej. Dane mogą być wyświetlone w dowolnej kolejności indeksów ($arindex ), a przy dużej ilości danych można zastosować paginację z dowolną ilością wierszy na stronie ($perpage ).
WSPARCIE / FORUM
CODE : php
/**
* displays data from a multidimensional array
* copyright : (c) 2012 Andrzej Krokos
* version PHP : 5
* author : shreker983@gmail.com
* licence : GPL
*/
class disp_array
{
// set data
function set_data( $data , $arindex , $perpage , $site )
{
$this -> data = $data ;
$this -> arindex = $arindex ;
$this -> perpage = $perpage ;
$this -> all = count ( $this -> data ) ;
$this -> col = count ( $this -> arindex ) ;
if ( isset ( $_GET [ 'start' ] ) )
$this -> start = $_GET [ 'start' ] ;
else
$this -> start = 0;
$this -> site = $site ;
}
// display data from array
function display_array( )
{
$data = array_slice ( $this -> data , $this -> start , $this -> perpage ) ;
echo '<table>' ;
echo '<tr align="center">' ;
for ( $i = 0; $i < $this -> col ; $i ++ )
{
echo '<td><b>' . $this -> arindex [ $i ] . '</b></td>' ;
}
echo '</tr>' ;
foreach ( $data as $k => $v )
{
echo '<tr>' ;
for ( $i = 0; $i < $this -> col ; $i ++ )
{
echo '<td>' . $data [ $k ] [ $this -> arindex [ $i ] ] . '</td>' ;
}
echo '</tr>' ;
}
echo '</table>' ;
}
// generate & display pagination
function display_pagin( $pshow )
{
echo '<p><div style="font-family: arial; font-size: 12px; font-weight: normal;">' ;
if ( $pshow == 1)
{
$sites = ceil ( $this -> all / $this -> perpage ) ;
if ( $sites > 1)
{
echo '(all : ' . $sites . ' sites) ' ;
}
else
{
echo '(all : ' . $sites . ' site) ' ;
}
}
if ( $this -> start > 0)
{
echo '<a href="' . $this -> site . '?start=' . ( $this -> start - $this -> perpage ) . '" style="text-decoration: none;"><< prev</a>' ;
}
if ( $this -> start > 0 && $this -> all > $this -> perpage && $this -> start < $this -> all - $this -> perpage )
{
echo ' | ' ;
}
if ( $this -> all > $this -> perpage && $this -> start < $this -> all - $this -> perpage )
{
echo '<a href="' . $this -> site . '?start=' . ( $this -> start + $this -> perpage ) . '" style="text-decoration: none;">next >></a>' ;
}
echo '</div></p>' ;
}
}
// example
1
=> array ( 'name' => 'Adam' , 'adm' => '0' , 'age' => '16' , 'info' => '24568902' ) ,
2
=> array ( 'name' => 'Tomasz' , 'adm' => '1' , 'age' => '21' , 'info' => '99628763' ) ,
3
=> array ( 'name' => 'Jan' , 'adm' => '0' , 'age' => '19' , 'info' => '28375472' ) ,
4
=> array ( 'name' => 'Krzysztof' , 'adm' => '0' , 'age' => '15' , 'info' => '82632563' ) ,
5
=> array ( 'name' => 'Wojciech' , 'adm' => '0' , 'age' => '16' , 'info' => '22753372' ) ,
6
=> array ( 'name' => 'Darek' , 'adm' => '0' , 'age' => '26' , 'info' => '12562317' ) ,
7
=> array ( 'name' => 'Janusz' , 'adm' => '0' , 'age' => '22' , 'info' => '34565317' ) ,
) ;
$ar = new disp_array( ) ;
$ar -> set_data ( $data , array ( 'name' , 'adm' , 'age' , 'info' ) , 2
, 'print_array.php' ) ; // array data, name index (array), per page, path site
$ar -> display_array ( ) ; // display array
$ar -> display_pagin ( 1) ; // display pagination & option 1 or 0 - show count sites
przykład :
CODE : php
1
=> array ( 'name' => 'Adam' , 'adm' => '0' , 'age' => '16' , 'info' => '24568902' ) ,
2
=> array ( 'name' => 'Tomasz' , 'adm' => '1' , 'age' => '21' , 'info' => '99628763' ) ,
3
=> array ( 'name' => 'Jan' , 'adm' => '0' , 'age' => '19' , 'info' => '28375472' ) ,
4
=> array ( 'name' => 'Krzysztof' , 'adm' => '0' , 'age' => '15' , 'info' => '82632563' ) ,
5
=> array ( 'name' => 'Wojciech' , 'adm' => '0' , 'age' => '16' , 'info' => '22753372' ) ,
6
=> array ( 'name' => 'Darek' , 'adm' => '0' , 'age' => '26' , 'info' => '12562317' ) ,
7
=> array ( 'name' => 'Janusz' , 'adm' => '0' , 'age' => '22' , 'info' => '34565317' ) ,
) ;
$ar = new disp_array( ) ;
$ar -> set_data ( $data , array ( 'name' , 'adm' , 'age' , 'info' ) , 3
, 'index.php?site=sites/prog_class_php01' ) ;
$ar -> display_array ( ) ;
$ar -> display_pagin ( 1) ;
online : 2 użytkowników, dziś odwiedziło : 251 osób
Zgodnie z nowelizacją ustawy o Prawie Telekomunikacyjnym informujemy, że strona krokos.net.pl w swoim działaniu korzysta z zapisywanych informacji w postaci ciasteczek (ang. cookies).
powered by scms © 2004 - 2025
design by sid