472,353 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 12724
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

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

Similar topics

2
by: Jan Mueller | last post by:
Hi all, I don't know whether this is the right group, let me know :-). I tried to open this XML file (with internal DTD) with XMLSpy 2005. I...
0
by: Mahesh Kumar | last post by:
I have created a XML file as datasource which i 'm binding to a grid view control. During runtime I'm capturing items in XML file and displaying...
1
by: Ed | last post by:
hi everyone, maybe someone can give me a hand here. I am working with vb 2005 and i have a gridview that i am loading data from a recordset. I...
1
by: sonali_aurangabadkar | last post by:
i want to edit whole grid on singel button click
2
by: probashi | last post by:
Hi, Using the SqlDataSource/SelectParameters/ControlParameter one can easily bind a Grid View with a list box (or any other controls), pretty...
2
by: Umeshnath | last post by:
Hi, I have placed a grid view inside Atlas panel. On click of a button event, data is populated in the grid view, I want to add scroll bar instead...
1
by: =?Utf-8?B?TG95b2xhIHN0YWxpbg==?= | last post by:
Hi, I am using ASP.Net 2.0 version and Windows'XP OS. I am useing the Grid view control to display the user details along with the paging concept...
2
by: rameshgohil | last post by:
I am using grid view and a button column in it using <itemTemplate> but I am not able to rerive cell value of a selected row from grid view. I have...
1
by: 111111222222 | last post by:
Hi All, As of now I am populating grid view using code behind. I want to place a textbox and a button to search the grid view using TIN which is...
6
by: AnagJohari | last post by:
actually i want to edit the row of an grid view by code behind not using any sql data source. u know that that show edit button property in a grid...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.