| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 
 | <?php
    class Datagrid
    {
      // Holds the order by information
        private static $orderby;
        private static $orderdir;
 
       //Properties. I really can't be bothered right now to document each one,
       // so they're all lumped together here
        public $allowSorting;
        public $showHeaders;
        public $headerHTML;
        public $cellpadding;
        public $cellspacing;
        public $numresults;
        public $startnum;
        public $perPage;
        public $colnum;
        public $noSpecialChars;
        public $colnames;
        public $rowcallback;
        public $headers;
 
        private $noSort;
        private $initialcols;
        private $connection;
        private $resultset;
        private $hiddenColumns;
 
       // Creates a datagrid from an array. Similar to a MySQL datasrc
      //@param array $data The data (array)
 
        public static function CreateFromArray($array)
        {
           // Order by
 
            if (isset($_GET['orderDir']) AND !empty($_GET['orderBy'])) {
 
                // Store it so the direction indicators appear
                Datagrid::$orderby['column']    = $_GET['orderBy'];
                Datagrid::$orderby['direction'] = $_GET['orderDir'];
 
                // FIXME - implement sorting
                uasort($array, array('Datagrid', '_sortArray'));
           }
 
            $grid =  new Datagrid($array);
 
            return $grid;
        }
 
        /**
        * Creates Datagrid object for you and returns it
        *
        *                               $connection The connection to the database. This can also be an array
        *                               containing host/user/pass/dbas parameters to connect to the
        *                               database. This can also be used to create a datagrid from
        *                               an array data source by supplying an array instead of the
        *                               database connection, eg: $grid = Datagrid::Create($myArray);
        * @param string     $sql        The SQL query with or without the ORDER BY clause
        */
        public static function Create($connection, $sql = null)
        {
            /**
            * Creates an array based datagrid if the first arg is am array
            */
            if (is_array($connection) AND is_null($sql)) {
                return Datagrid::CreateFromArray($connection);
            }
 
            // Connect if need be
            if (is_array($connection)) {
                $host = $connection['hostname'];
                $user = $connection['username'];
                $pass = $connection['password'];
                $dbas = $connection['database'];
 
 
                $connection = mysql_connect($host, $user, $pass) OR die('<span style="color: red">Failed to connect: ' . mysql_error() . '</span>');
 
                mysql_select_db($dbas);
            }
 
            /**
            * Order by
            */
            if (isset($_GET['orderDir']) AND !empty($_GET['orderBy'])) {
 
                // Store it so the direction indicators appear
                Datagrid::$orderby['column']    = $_GET['orderBy'];
                Datagrid::$orderby['direction'] = $_GET['orderDir'];
 
                $orderby = 'ORDER BY ' . $_GET['orderBy'] . ' ' . ($_GET['orderDir'] ? 'ASC' : 'DESC');
                $sql = preg_replace('/ORDER\s+BY.*(ASC|DESC)/is', $orderby, $sql);
            }
 
            /**
            * Perform the query to get the result set
            */
            $resultset = mysql_query($sql, $connection);
 
            $grid =  new Datagrid($connection, $resultset);
 
            // If the query doesn't have an ORDER BY, then disable ordering
            if (strpos($sql, 'ORDER BY') === false) {
                $grid->allowSorting = false;
            }
            return $grid;
        }
 
        /**
        * The constructor
        * 
        * @param mixed    $connection This can be either a MySQL connection resource or an array
        * @param resource $resultset Only used for MySQL based datagrids - the MySQL result.
        */
        public function __construct($connection, $resultset = null)
        {
            $this->noSort         = array();
            $this->allowSorting   = true;
            $this->showHeaders    = true;
            $this->headerHTML     = '';
            $this->cellpadding    = 0;
            $this->cellspacing    = 0;
            $this->connection     = $connection;
            $this->resultset      = $resultset;
            $this->numresults     = is_resource($connection) ? mysql_num_rows($resultset) : count($connection);
            $this->startnum       = @(int)$_GET['start'];
            $this->perPage        = 20;
            $this->hiddenColumns  = array();
            $this->colnum         = is_resource($connection) ? mysql_num_fields($this->resultset) : count($connection[0]);
            $this->noSpecialChars = array();
 
            // Don't allow startnum to be lower than zero
            if ($this->startnum < 0) {
                $this->startnum = 0;
            }
 
            // Don't allow startnum to be greater than the number of rows in the result set,
            // well, one less to allow for zero indexing
            if ($this->startnum >= $this->numresults) {
                $this->startnum = 0;
            }
 
            // Check the MySQL connection is valid
            if (!is_resource($connection) AND !is_array($connection)) {
                die('<p /><span style="color: red">Error - the MySQL connection you have passed to the datagrid constructor is not valid</span>');
            }
 
            // Check the MySQL result set is valid
            if (is_resource($connection) AND (!$resultset OR !is_resource($resultset))) {
                die('<p /><span style="color: red">Error - the MySQL result set you have passed to the datagrid constructor is not valid</span>');
            }
        }
 
        /**
        * Sets the displayed header names for the columns
        *
        * @param array $cols The column names
        */
        public function SetDisplayNames($cols)
        {
            $this->colnames = $cols;
        }
        /**
        * Hides a particular column, or multiple columns
        *
        * @param ... strings One or more column names
        */
        public function HideColumn()
        {
            $this->hiddenColumns = array_unique(func_get_args());
        }
 
        /**
        * Sets the column names (not using the display names) that
        * don't get htmlspecialchars() applied to them
        *
        * @param string ... One or more column names
        */
        public function NoSpecialChars()
        {
            $this->noSpecialChars = func_get_args();
        }
 
        /**
        * This method allows you to specify one or more columns that cannot be sorted by
        * 
        * @param string ... The column name (s). You can specify one or more.
        */
        public function NoSort()
        {
            $args = func_get_args();
 
            foreach ($args as $v) {
                $this->noSort[] = $v;
            }
 
            // Should do this before running the query, but hey ho.
            if (in_array(Datagrid::$orderby['column'], $this->noSort)) {
                die('<span style="color: red">You are not allowed to sort by that column</span>');
            }
        }
 
        /**
        * Returns the number of pages in the datagrid
        *
        * @return int The number of pages in the datagrid
        */
        public function GetPageCount()
        {
            $count = is_resource($this->connection) ? mysql_num_rows($this->resultset) : count($this->connection);
 
            return  ceil($count / $this->perPage);
        }
 
        /**
        * Returns the number of rows in the result set.
        *
        * @return int The number of rows
        */
        public function GetRowCount()
        {
            return $this->numresults;
        }
 
        /**
        * I can't see the need for this, but you may. Simply returns the MySQL result set.
        *
        * @return resource The MySQL result set
        */
        public function GetResultset()
        {
            if (is_array($this->connection)) {
                die('<span style="color: red">Cannot get the result set - data source is an array</span>');
            }
 
            return $this->resultset;
        }
 
 
        /**
        * Returns the MySQL connection
        *
        * @return resource The MySQL resouce
        */
        public function GetConnection()
        {
            if (is_array($this->connection)) {
                die('<span style="color: red">Cannot get the connection - data source is an array</span>');
            }
 
            return $this->connection;
        }
 
        /**
        * Sets the header HTML./ This is NOT related to the table
        * column headers. This is here purely for decorative purposes.
        *
        * @param string $html The HTML to set
        */
        public function SetHeaderHTML($html)
        {
            $this->headerHTML = $html;
        }
 
        /**
        * Sets the MySQL connection
        *
        * @param resource $connection The MySQL connection resouce
        */
        public function SetConnection($connection)
        {
            if (is_array($this->connection)) {
                die('<span style="color: red">Cannot set the connection - data source is an array</span>');
            }
 
            $this->connection = $connection;
        }
 
        /**
        * This function sets the amount of rows to display
        * per page
        *
        * @param int $perPage How many rows to show per page
        */
        public function SetPerPage($perPage)
        {
            $this->perPage = $perPage;
        }
 
        /**
        * For whatever reason you can use this to set the MySQL
        * result set
        *
        * @param resource $result The MySQL result set. If you do use this method, it
        *                          should come before the call to Display
        */
        public function SetResultset($resultset)
        {
            if (is_array($this->connection)) {
                die('<span style="color: red">Cannot set the result set - data source is an array</span>');
            }
 
            $this->resultset  = $resultset;
            $this->numresults = mysql_num_rows($this->resultset);
            $this->colnum     = mysql_num_fields($this->resultset) - count($this->hiddenColumns);
        }
 
        /**
        * Adds a rowcallback function which gets called just before each row is going
        * to be displayed
        *
        * @param string &$row The function name that is the callback function.
        */
        public function AddCallback($callback)
        {
            $this->rowcallback = $callback;
        }
 
        /**
        * Shows the datagrid.
        */
        function Display()
        {
            /**
            * Seek to the correct place in the result set
            */
            if (is_array($this->connection)) {
                $this->orig_array = $this->connection;
                $this->connection = array_slice($this->connection, $this->startnum, $this->perPage);
            } else {
                if (mysql_num_rows($this->resultset)) {
                    mysql_data_seek($this->resultset, $this->startnum);
                }
            }
 
            /**
            * Initialise the row number
            */
            $rownum = 0;
 
            /**
            * Get the headers from the first row, then seek back to zero
            */
            $row = is_array($this->connection) ? $this->connection[0] : mysql_fetch_array($this->resultset, MYSQL_ASSOC);
            $this->headers     = !empty($row) ? array_keys($row) : array();
            $this->initialcols = count($row);
            $this->colnum = (is_array($this->connection) ? count($row) : mysql_num_fields($this->resultset)) - count($this->hiddenColumns);
            is_array($this->connection) || mysql_num_rows($this->resultset) == 0 ? null : mysql_data_seek($this->resultset, $this->startnum);
            $rowcount = 0;
 
            ?>
<script language="javascript" type="text/javascript">
<!--
    /**
    * The row mouseover function
    */
    function MouseOver(rownum)
    {
        var tags = document.getElementsByTagName('td')
 
        for (var i=0; i<tags.length; i++) {
            if(tags[i].className.indexOf('row_' + rownum + ' ') != -1) {
                tags[i].className = tags[i].className += ' mouseover';
            };
        }
    }
 
    /**
    * the row mouseout function
    */
    function MouseOut(rownum)
    {
        var tags = document.getElementsByTagName('td')
 
        for (var i=0; i<tags.length; i++) {
            if(tags[i].className.indexOf('row_' + rownum) != -1) {
                tags[i].className = tags[i].className.replace(/ mouseover/, '');
            };
        }
    }
// -->
</script>
<table border="0" cellspacing="<?=$this->cellspacing?>" cellpadding="<?=$this->cellpadding?>" class="datagrid">
    <thead>
        <?if($this->headerHTML):?>
            <tr>
                <th id="header" colspan="<?=$this->colnum?>">
                    <?=$this->headerHTML?>
                </th>
            </tr>
        <?endif?>
 
        <?if($this->showHeaders):?>
                <tr>
                    <?foreach($this->headers as $k => $h):?>
                        <?if(in_array($h, $this->hiddenColumns)) continue?>
 
                        <th class="col_<?=$k?>" title="<?=($printable = !empty($this->colnames[$h]) ? $this->colnames[$h] : $h)?>">
 
                            <?if($this->allowSorting AND !in_array($h, $this->noSort)):?>
                                <a href="<?=$this->getQueryString()?>&orderBy=<?=$h?>&orderDir=<?=(!empty($_GET['orderDir']) && $_GET['orderBy'] == $h ? 0 : 1)?>">
                                    <?=$printable?>
                                </a>
 
                            <?else:?>
                                <?=$printable?>
                            <?endif?>
 
                            <?if($this->allowSorting):?>
                                <!-- The order indicator -->
 
                                <?if($h == Datagrid::$orderby['column']):?>
                                    <span style="font-family: WebDings">
                                        <?=(!empty(Datagrid::$orderby['direction']) && trim(Datagrid::$orderby['direction']) == 1 ? 5 : 6)?>
                                    </span>
                                <?endif?>
                            <?endif?>
                        </th>
                    <?endforeach?>
                </tr>
        <?endif?>
    </thead>
 
    <tbody>
        <?while($row = (is_array($this->connection) ? current($this->connection) : mysql_fetch_array($this->resultset, MYSQL_ASSOC))):?>
 
            <?$colnum = 0; @$rowcount++?>
 
            <?if($this->rowcallback):?>
                <?call_user_func($this->rowcallback, &$row)?>
            <?endif?>
 
            <tr onmouseover="MouseOver(<?=intval($rownum)?>)" onmouseout="MouseOut(<?=intval($rownum)?>)">
                <?foreach($row as $k => $v):?>
 
                    <?if(in_array($k, $this->hiddenColumns)) continue?>
 
                    <td class="row_<?=intval($rownum)?> col_<?=(!empty($colnum) ? $colnum : 0)?> <?if($rownum % 2 == 1):?>altrow<?endif?>  <?if($colnum % 2 == 1):?>altcol<?endif?>">
                        <?=(in_array($k, $this->noSpecialChars) ? $v : htmlspecialchars($v))?>
                    </td>
 
                    <?$colnum++?>
                <?endforeach?>
            </tr>
 
            <?if($rownum++ == ($this->perPage - 1) ) break?>
            <?if(is_array($this->connection)): next($this->connection); endif?>
        <?endwhile?>
    </tbody>
 
    <tfoot>
        <tr>
            <td colspan="<?=$this->colnum?>" class="paging">
                <?if(@$this->startnum > 0):?>
                    <span style="float: left">
                        <a href="<?=$this->getQueryString(intval($this->startnum) - $this->perPage)?>">
                            « Prev
                        </a>
                    </span>
                <?endif?>
 
 
                <?if($this->numresults > (@$this->startnum + $this->perPage)):?>
                    <span style="float: right">
                        <a href="<?=$this->getQueryString(intval($this->startnum) + $this->perPage)?>">
                            Next »
                        </a>
                    </span>
                <?endif?>
            </td>
        </tr>
 
        <tr>
            <td align="center" colspan="<?=$this->colnum?>" class="paging_results">
                <?=($this->numresults > 0 ? intval($this->startnum) + 1 : 0)?>-<?=(intval($this->startnum) + $rowcount)?> of <?=intval($this->numresults)?> results
            </td>
        </tr>
    </tfoot>
</table>
            <?php
        }
 
        /**
        * A private method used to build the query string
        *
        * @param  int    The starting number
        * @return string The query string
        */
        private function getQueryString($startnum = null)
        {
            if ($startnum === null) {
                $startnum = !empty($_GET['start']) ? $_GET['start'] : 0;
            }
 
            $_GET['start'] = $startnum;
 
            $qs = '?';
            foreach ($_GET as $k => $v) {
                $qs .= urlencode($k) . '=' . urlencode($v)  . '&';
            }
 
            // If the query string is just a question mark, lose it
            if ($qs == '?') {
                $qs = '';
            }
 
            return preg_replace('/&$/', '', $qs);
        }
 
        /**
        * Sort an array based datagrid
        */
        private function _sortArray($a, $b)
        {
            if (empty(Datagrid::$orderby)) {
                Datagrid::$orderby  = key($a);
                Datagrid::$orderdir = 1; // Ascending
            }
 
            // Ascending
            if (Datagrid::$orderby['direction']) {
                if ($a[Datagrid::$orderby['column']] > $b[Datagrid::$orderby['column']]) {
                    return 1;
                } elseif ($a[Datagrid::$orderby['column']] < $b[Datagrid::$orderby['column']]) {
                    return -1;
                } else {
                    return 0;
                }
 
            // Descending
            } else {
 
                if ($a[Datagrid::$orderby['column']] > $b[Datagrid::$orderby['column']]) {
                    return -1;
                } elseif ($a[Datagrid::$orderby['column']] < $b[Datagrid::$orderby['column']]) {
                    return 1;
                } else {
                    return 0;
                }
            }
        }
    }
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>pour afficher les contacts</title>
</head>
<body background="bg.jpg">
<?php
if(isset($_POST['liste'])){
$sql_serveur="localhost";
$sql_user="xxx";
$sql_passwd="xxx";
$dbase="gest_contacts";
$connection=mysql_connect($sql_serveur,$sql_user,$sql_passwd);
mysql_select_db($dbase,$connection);
$resultset = mysql_query("SELECT * FROM contacts where etat ='A' odrer by nom_contact",$connection); 
$grid = DataGrid::Create($dbase, $resultset);
 
    $grid->Display();
 
}
else{
  echo "la variable n'exist pas"; 
}
?>
</body>
</html> | 
Partager