473,569 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Grid view php

19 New Member
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 12928
zorgi
431 Recognized Expert Contributor
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 Recognized Expert Top Contributor
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 Recognized Expert Contributor
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 Recognized Expert Top Contributor
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 Recognized Expert Contributor
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 Recognized Expert Top Contributor
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 Recognized Expert Contributor
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
2224
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 guess some of you also use this tool. http://dret.net/lectures/xml/a/4/wwww.xml I'm quite new to XML at all, so I wondered about the following. Maybe someone can tell me whether this is correct or...
0
2063
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 in a GRID VIEW control. but i want to remove or flush the XML file content on page load. I tried parentnode.removeall () but no use.. (1) how to clean/delete the XML file nodes on page load for...
1
2568
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 have the recordset returned from the query and i am loading the data, but the problem is... this recordset is going to hold about 25,000+ records and the grid view and form "goes to sleep" and i can...
1
422
by: sonali_aurangabadkar | last post by:
i want to edit whole grid on singel button click
2
4076
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 cool, but my list box is multi select. My questions is, how can I bind a Grid View Control to a multi select list box (or any multi values controls) Thanks
2
3844
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 of increasing the size of grid view. I have written the following code. <div id="Layer1" style="width:350px; height:140px; z-index:1;overflow-y: scroll"> But the problem was scroll bar will...
1
2605
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 provided by the grid view control. When i click the paging link (2,3,4.. ) it produces the result properly, But my problem is when i tried to press the Refresh button in the explorer, it throws...
2
7583
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 tried the following to methods in Row_command event of gridview but it gives Null reference exception error while clicking the button in button column of grid view : int index =...
1
2790
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 one column in my grid view. I placed the textbox and button on top of grid view. Please send the process or code to do this... I am new to programming field. Your help will be greatly...
6
2057
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 view when we set this property to true & simply connect it to the sql data source we use a update command property in sql data source by doing all this we create a such type of data grid in...
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.