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....
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: -
$rez = array(array("a", "b", "c"), array("e", "f", "h"), .............etc)
-
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: -
echo "<table>\n";
-
foreach($rez as $row){
-
echo "<tr>\n";
-
foreach($row as $td){
-
echo "<td>\n";
-
echo $td."\n";
-
echo "</td>\n";
-
}
-
echo "</tr>\n";
-
}
-
echo "</table>\n";
-
kovik 1,044
Recognized Expert Top Contributor
Or you could just use 1 regular for loop. - $result = mysql_query("select * from `table`");
-
$maxColumns = 3;
-
-
echo '<table><tr>';
-
for ($currentColumn = 0; $data = mysql_fetch_object($result); $currentColumn++) {
-
if ($currentColumn = $maxColumns) {
-
echo '</tr><tr>';
-
$currentColumn = 0;
-
}
-
-
echo '<td>' . $data->columnName . '</td>';
-
}
-
echo '</tr></table>'
zorgi 431
Recognized Expert Contributor
Or u coud do it like this: -
class Table{
-
private $table_rows = array();
-
private $width = "100%";
-
-
public function setWidth($width){
-
$this -> width = $width;
-
}
-
/*use this function to add just one row to the table (good for looping)*/
-
public function addRow($row){
-
$this -> table_rows[] = $row;
-
}
-
-
/*use this function if you have array of table rows*/
-
public function setTable($table_rows){
-
$this -> table_rows = $table_rows;
-
}
-
-
public function printT(){
-
echo "<table width='".$this->width."'>\n";
-
foreach($this -> table_rows as $row){
-
echo "<tr>\n";
-
foreach($row as $td){
-
echo "<td>\n";
-
echo $td."\n";
-
echo "</td>\n";
-
}
-
echo "<tr>\n";
-
}
-
echo "</table>\n";
-
}
-
-
}
-
Now you can expand this class easily and use it over and over again. -
$rez = array(array("a", "b", "c"), array("e", "f", "h"), .............etc);
-
-
$table = new Table();
-
$table->setTable($rez);
-
$table->setWidth("50%"); //change table width if you want to
-
$table->printT();
-
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. - class Table {
-
private $data = array();
-
private $columns;
-
-
public function __construct($columns = 5, array $data = null) {
-
$this->columns = (int)$columns;
-
-
if ($data) {
-
foreach ($data as $item) {
-
$this->addItem($item);
-
}
-
}
-
}
-
-
public function addItem($item) {
-
$currentRow = sizeof($this->data);
-
-
if (!isset($this->data[$currentRow])) {
-
$this->data[$currentRow] = array();
-
$currentColumn = 0;
-
} else {
-
$currentColumn = sizeof($this->data[$currentRow]);
-
-
if ($currentColumn >= $this->columns) {
-
$this->data[++$currentRow] = array();
-
$currentColumn = 0;
-
}
-
}
-
-
$this->data[$currentRow][$currentColumn] = (string)$item;
-
}
-
}
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: -
$tb = new Table();
-
$link = mysqli_connect("localhost", "", "", "");
-
-
if (mysqli_connect_errno()) {
-
printf("Connect failed: %s\n", mysqli_connect_error());
-
exit();
-
}
-
-
$query = "SELECT * FROM some_table";
-
-
if ($result = mysqli_query($link, $query)) {
-
-
while ($row = mysqli_fetch_row($result)) {
-
$tb->addRow($row);
-
/*
-
or you can pick your table fields
-
$tb->addRow(array($row[0], $row[1]));
-
*/
-
}
-
-
/* free result set */
-
mysqli_free_result($result);
-
}
-
-
/* close connection */
-
mysqli_close($link);
-
/*and echo your table out*/
-
$tb->printT();
-
-
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: - ul.grid li {
-
float: left;
-
width: 64px;
-
height: 64px;
-
margin: 8px;
-
border: 1px solid #000000;
-
}
HTML: - <ul class="grid">
-
<li><a href="#">Grid Item #1</a></li>
-
<li><a href="#">Grid Item #2</a></li>
-
<li><a href="#">Grid Item #3</a></li>
-
<li><a href="#">Grid Item #4</a></li>
-
<li><a href="#">Grid Item #5</a></li>
-
</ul>
PHP: - echo '<ul class="grid">';
-
-
while ($data = mysql_fetch_object($result)) {
-
echo '<li><a href="' . $data->link . '">' . $data->title . '</a></li>';
-
}
-
-
echo '</ul>';
zorgi 431
Recognized Expert Contributor
Well, now he/she has choice :))
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
by: sonali_aurangabadkar |
last post by:
i want to edit whole grid on singel button click
|
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
| |
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...
|
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...
|
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 =...
|
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...
|
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...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |