Grid view php – PHP

  php

Q(Question):

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….

A(Answer):

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";

A(Answer):

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

A(Answer):

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();

A(Answer):

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;
}
}

A(Answer):

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();

A(Answer):

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>';

A(Answer):

Well, now he/she has choice :))

LEAVE A COMMENT