469,270 Members | 1,151 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Grid view php

19
Is it possible to create grid view in php?Or any thing equivalent to that?What i actually want is to query the database(mySQL) and print the result in a table form on the web page.
Any help would be appreciated....
Feb 1 '10 #1
7 12477
zorgi
431 Expert 256MB
The way I do this is that I sort my database results into array of arrays. Something like this:

Expand|Select|Wrap|Line Numbers
  1. $rez = array(array("a", "b", "c"), array("e", "f", "h"),  .............etc)
  2.  
Now each row in the table is represented with one array in $rez. All you have to do now is to loop through all the element of $rez like this:

Expand|Select|Wrap|Line Numbers
  1. echo "<table>\n";
  2. foreach($rez as $row){
  3.      echo "<tr>\n";
  4.      foreach($row as $td){
  5.           echo "<td>\n";
  6.           echo $td."\n";
  7.           echo "</td>\n";
  8.      }
  9.      echo "</tr>\n";
  10. }
  11. echo "</table>\n";
  12.  
Feb 1 '10 #2
kovik
1,044 Expert 1GB
Or you could just use 1 regular for loop.

Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("select * from `table`");
  2. $maxColumns = 3;
  3.  
  4. echo '<table><tr>';
  5. for ($currentColumn = 0; $data = mysql_fetch_object($result); $currentColumn++) {
  6.   if ($currentColumn = $maxColumns) {
  7.     echo '</tr><tr>';
  8.     $currentColumn = 0;
  9.   }
  10.  
  11.   echo '<td>' . $data->columnName . '</td>';
  12. }
  13. echo '</tr></table>'
Feb 1 '10 #3
zorgi
431 Expert 256MB
Or u coud do it like this:

Expand|Select|Wrap|Line Numbers
  1. class Table{
  2.     private $table_rows = array();
  3.     private $width = "100%";
  4.  
  5.     public function setWidth($width){
  6.         $this -> width = $width;
  7.     }    
  8.     /*use this function to add just one row to the table (good for looping)*/
  9.     public function addRow($row){
  10.         $this -> table_rows[] = $row;
  11.     }    
  12.  
  13.     /*use this function if you have array of table rows*/
  14.     public function setTable($table_rows){
  15.         $this -> table_rows = $table_rows;
  16.     }
  17.  
  18.     public function printT(){
  19.         echo "<table width='".$this->width."'>\n";
  20.         foreach($this -> table_rows as $row){
  21.             echo "<tr>\n";
  22.             foreach($row as $td){
  23.                echo "<td>\n";
  24.                echo $td."\n";
  25.                echo "</td>\n";
  26.             }
  27.             echo "<tr>\n";
  28.         }
  29.         echo "</table>\n";
  30.     }
  31.  
  32. }
  33.  
Now you can expand this class easily and use it over and over again.

Expand|Select|Wrap|Line Numbers
  1. $rez = array(array("a", "b", "c"), array("e", "f", "h"),  .............etc);
  2.  
  3. $table = new Table();
  4. $table->setTable($rez);
  5. $table->setWidth("50%"); //change table width if you want to
  6. $table->printT();
  7.  
Feb 1 '10 #4
kovik
1,044 Expert 1GB
But then you still need to give the data in the format of a 2D array. I think the OP wants to do this with database results, which naturally come one at a time. Thus, you need support for a 1D array.

Expand|Select|Wrap|Line Numbers
  1. class Table {
  2.   private $data = array();
  3.   private $columns;
  4.  
  5.   public function __construct($columns = 5, array $data = null) {
  6.     $this->columns = (int)$columns;
  7.  
  8.     if ($data) {
  9.       foreach ($data as $item) {
  10.         $this->addItem($item);
  11.       }
  12.     }
  13.   }
  14.  
  15.   public function addItem($item) {
  16.     $currentRow = sizeof($this->data);
  17.  
  18.     if (!isset($this->data[$currentRow])) {
  19.       $this->data[$currentRow] = array();
  20.       $currentColumn = 0;
  21.     } else {
  22.       $currentColumn = sizeof($this->data[$currentRow]);
  23.  
  24.       if ($currentColumn >= $this->columns) {
  25.         $this->data[++$currentRow] = array();
  26.         $currentColumn = 0;
  27.       }
  28.     }
  29.  
  30.     $this->data[$currentRow][$currentColumn] = (string)$item;
  31.   }
  32. }
Feb 1 '10 #5
zorgi
431 Expert 256MB
But my example supports 1D and 2D arrays in ... :)))

1D support in Table::addRow($row) so basically you can do this:

Expand|Select|Wrap|Line Numbers
  1. $tb = new Table();
  2. $link = mysqli_connect("localhost", "", "", "");
  3.  
  4. if (mysqli_connect_errno()) {
  5.     printf("Connect failed: %s\n", mysqli_connect_error());
  6.     exit();
  7. }
  8.  
  9. $query = "SELECT * FROM some_table";
  10.  
  11. if ($result = mysqli_query($link, $query)) {
  12.  
  13.     while ($row = mysqli_fetch_row($result)) {
  14.        $tb->addRow($row);
  15.       /*
  16.       or you can pick your table fields
  17.       $tb->addRow(array($row[0], $row[1]));
  18.       */
  19.     }
  20.  
  21.     /* free result set */
  22.     mysqli_free_result($result);
  23. }
  24.  
  25. /* close connection */
  26. mysqli_close($link);
  27. /*and echo your table out*/
  28. $tb->printT();
  29.  
  30.  
Feb 1 '10 #6
kovik
1,044 Expert 1GB
You are assuming that the OP was asking for a way to output the columns of the database table as columns of the output table. When the OP stated that he wanted a "grid" view, I assumed that he meant he wanted each row of the database table as an entry in the grid.

Personally, I'd prefer using a list to approach the creation of a grid, but my post was simply to put yours into perspective.

Here's how to use a list:

CSS:
Expand|Select|Wrap|Line Numbers
  1. ul.grid li {
  2.   float: left;
  3.   width: 64px;
  4.   height: 64px;
  5.   margin: 8px;
  6.   border: 1px solid #000000;
  7. }
HTML:
Expand|Select|Wrap|Line Numbers
  1. <ul class="grid">
  2.   <li><a href="#">Grid Item #1</a></li>
  3.   <li><a href="#">Grid Item #2</a></li>
  4.   <li><a href="#">Grid Item #3</a></li>
  5.   <li><a href="#">Grid Item #4</a></li>
  6.   <li><a href="#">Grid Item #5</a></li>
  7. </ul>
PHP:
Expand|Select|Wrap|Line Numbers
  1. echo '<ul class="grid">';
  2.  
  3. while ($data = mysql_fetch_object($result)) {
  4.   echo '<li><a href="' . $data->link . '">' . $data->title . '</a></li>';
  5. }
  6.  
  7. echo '</ul>';
Feb 1 '10 #7
zorgi
431 Expert 256MB
Well, now he/she has choice :))
Feb 1 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Jan Mueller | last post: by
1 post views Thread by Ed | last post: by
1 post views Thread by sonali_aurangabadkar | last post: by
2 posts views Thread by probashi | last post: by
2 posts views Thread by Umeshnath | last post: by
1 post views Thread by =?Utf-8?B?TG95b2xhIHN0YWxpbg==?= | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.