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 12724
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";
-
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>'
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();
-
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;
-
}
-
}
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();
-
-
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>';
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
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...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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...
| |