PHP XML paginator with foreach - php

I have code, which take information from xml file(in xml have example 5 blocks "groupRecord", can be more ). And i want show per page 4 blocks. I already wrote this part.
$xmlDoc = new DOMDocument();
$xmlDoc->load('GetLoyalty5001.xml');
$totaldata = $xmlDoc->getElementsByTagName("groupRecord")->length;
$Pages = intval($_GET['page']);
if(!isset($Pages) || $Pages==0)
{
$Pages=1;
}
$DataPerPage=4;
$numPages = ceil($totaldata/$DataPerPage);
$shopingdata = $xmlDoc->getElementsByTagName("groupRecord");
foreach($shopingdata as $key=>$datashoping)
{
if(??)
{
?>
<tr>
<td width="156">STORE ADDRESS</td>
<td width="222">STORE ADDRESS</td>
<td width="266">STORE ADDRESS</td>
<td width="161">STORE ADDRESS</td>
<td width="156">STORE ADDRESS</td>
</tr>
<?php
}
}
?>
What must I put in place of question mark in condition?

Well, you have everything you need already, just use $numPages which yields the number of pages you have.
for ($a = 1; $a <= $numPages; $a++) {
echo '' . $a . '';
}

You were close, but the idea of how to calculate the number of items in the list seemed to go wrong. All you needed to do was use the number of elements that you fetched using $xmlDoc->getElementsByTagName("groupRecord"). I've reduced the code and this should do what your after...
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);
$shopingdata = $xmlDoc->getElementsByTagName("groupRecord");
$perPage = 4;
$currentPage = intval($_GET['page']);
$numPages = ceil($shopingdata->length / $perPage);
if(!$currentPage || $currentPage > $numPages)
{
$currentPage = 0;
}
$start = $currentPage * $perPage;
$end = ($currentPage * $perPage) + $perPage;
foreach($shopingdata as $key=>$datashoping)
{
if($key >= $start && $key < $end)
{
?>
<tr>
<td width="156">STORE ADDRESS</td>
<td width="222">STORE ADDRESS</td>
<td width="266">STORE ADDRESS</td>
<td width="161">STORE ADDRESS</td>
<td width="156">STORE ADDRESS</td>
</tr>
<?php
}
}
for($a=1;$a<=$numPages;$a++)
{
echo ''.$a.'';
}

Related

How to change style of embdeded HTML in a PHP file?

So i am currently on 14 day internship and my job is to redesign a webpages, however these webapges are made so they display links to a files in a folder and once you click on these files you download them.
My question is -> there a way of how I can change the style of this page with an external CSS file ? My problem is I don't know PHP at all and don't have time to learn it, so I don't know what these files exactly do, but I know that at the botom there is HTML code being passed on the site, I tried changing the CSS file in the HTML , but it didn't change anything and this is how the page was displayed.
This is the link to the image:
https://imgur.com/skjQEk6
And this is the code that was located in the same folder as the .xls files
I am sorry for long code, I don't know how to shorten it.
<?php
//
// --- Make your customizations below. All customizations set here will apply ONLY to this indexed directory --- \\
//
//
// $HomeDir - Absolute path (not url) to TotalIndex script (usually something like: /home/user/public_html/TotalIndex/
// on Linux and something like: c:/inetpub/wwwroot/TotalIndex/ on Windows systems).
// Be sure to include the ending slash in the home directory
//
$HomeDir = "/dcweb/totalindex/";
// Include config file - DO NOT REMOVE
include $HomeDir."config.php";
// To turn off any readme file descriptions (explained in the config.php file) just uncomment the next line (This will prevent a folder description from appearing at the top of the page)...
// $Allow_Readme = 0;
// To exclude a certain file name from the index listing, just add another line of code with the name of the file that
// you want to exclude from the list.
//
// EXAMPLE - If you want to exclude a file named "hidden.txt", then add the following line below:
// $Exclude_File = "hidden.txt";
//
// You can add as many of these as you wish to exclude
// - This is case sensitive -
//
$Exclude_File[] = "exclude_me.txt";
// To exclude a certain folder name from the index listing, just add another line of code with the name of the folder that
// you want to exclude from the list.
//
// EXAMPLE - If you want to exclude a folder named "hidden_folder", then add the following line below:
// $Exclude_File = "hidden_folder";
//
// You can add as many of these as you wish to exclude
// - This is case sensitive -
//
$Exclude_Folder[] = "hidden_folder";
// To exclude a files with certain extensions from the index listing, just add another line of code with the file extension that
// you want to exclude from the list.
//
// EXAMPLE - If you want to exclude file with the extension "txt", then add the following line below:
// $Exclude_Extension = "txt";
//
// You can add as many of these as you wish to exclude
// - This is case sensitive -
//
$Exclude_Extension[] = "hide_me";
//
// ***----- Do not change below this line -----*** \\
//
// include functions
include $HomeDir."functions.php";
//Path to themes folder with ending slash
$ThemeURL = $HomeURL."themes/";
// Path to icon folder with ending slash
$iconfolder = $HomeURL."icons/";
$_GLOBAL['image'] = "";
$fdir=$_GET["fdir"];
$NumSort=$_GET["NumSort"];
$SortBy=$_GET["SortBy"];
// Open folder directory
if(!isset($fdir))
{
$fdir = "./";
}
$fdir = str_replace("../", "", $fdir);
// check to see if still inside directory boundry
$check = substr($fdir, 0, 2);
if($check != "./") {
$fdir = "./";
}
// setup file properties class
class File_Properties
{
var $file_name;
// just the file name
var $file_ext;
// file extension
var $file_size;
// size of file
var $file_date;
// date modified
var $file_icon;
// icon for file type
var $file_type;
// short description for file type
// constructor method - build object
function Build($file)
{
$this->setFname($file);
$this->setFext($file);
$this->setFsize($file);
$this->setFdate($file);
$this->setFicon_type();
}
// Set file name
function setFname($file)
{
$this->file_name = basename($file);
}
// set file extension
function setFext($file)
{
$this->file_ext = array_pop(explode('.', $file));
}
// set file size
function setFsize($file)
{
$kbs = filesize($file);
$units = array(' B', ' KB', ' MB', ' GB', ' TB');
for ($i = 0; $kbs > 1024; $i++) { $kbs /= 1024; }
$this->file_size = ((int)($kbs)).$units[$i];
}
// set date modified
function setFdate($file)
{
date_default_timezone_set('Europe/Prague');
$modified = filemtime($file);
$this->file_date = date("d-M-Y H:i", $modified);
}
// set file type
function setFicon_type()
{
list($this->file_type, $this->file_icon) = split("\?", GetExt($this->file_ext), 2);
}
// setup all get/return methods for class vars
function getFname()
{
return $this->file_name;
}
function getFext()
{
return $this->file_ext;
}
function getFsize()
{
return $this->file_size;
}
function getFdate()
{
return $this->file_date;
}
function getFicon()
{
return $this->file_icon;
}
function getFtype()
{
return $this->file_type;
}
}
// setup folder properties class
class Folder_Properties
{
var $dir_name; // just the directory name
var $dir_date; // date modified
var $dir_icon = "folder.gif"; // icon for directory
var $dir_type = "File Folder"; // short description for file type
// constructor method - build object
function Build($dir)
{
$this->setFname($dir);
$this->setFdate($dir);
}
// Set file name
function setFname($dir)
{
$this->dir_name = basename($dir);
}
// set date modified
function setFdate($dir)
{
$modified = filemtime($dir);
$this->dir_date = date("d-M-Y H:i", $modified);
}
// setup all get/return methods for class vars
function getFname()
{
return $this->dir_name;
}
function getFdate()
{
return $this->dir_date;
}
function getFicon()
{
return $this->dir_icon;
}
function getFtype()
{
return $this->dir_type;
}
}
// initialize file and folder arrays
$file_array = array();
$dir_array = array();
$Fname_array = array();
$Dname_array = array();
// open directory
$dir = opendir($fdir);
// Read files into array
while(false !== ($file = readdir($dir)))
{
if($file != "." && $file != "..")
{
$type = filetype($fdir.$file);
$info = pathinfo($file);
if($type != "dir")
{
if(isset($info["extension"]))
{
$file_extension = $info["extension"];
}
}
if($type == "dir" && !in_array($file, $Exclude_Folder))
{
// setup folder object
$This_Dir = new Folder_Properties;
$This_Dir->Build($fdir.$file);
$dir_array[] = $This_Dir;
}
elseif($type == "file" && !in_array($file, $Exclude_File) && !in_array($file_extension, $Exclude_Extension))
{
// setup file object
$This_File = new File_Properties;
$This_File->Build($fdir.$file);
$file_array[] = $This_File;
}
}
}
closedir($dir);
// Set default sort by method
if(!isset($SortBy) || $SortBy != 0 && $SortBy != 1) {
$SortBy = 0;
}
// Number of the column to sort by (0-3) set default to 0
if(!isset($NumSort) || $NumSort != 0 && $NumSort != 1 && $NumSort != 2 && $NumSort != 3) {
$NumSort = 0;
}
// determin object sorting methods
switch($NumSort)
{
case 0;
$Fsort_method = "file_name";
$Dsort_method = "dir_name";
break;
case 1;
$Fsort_method = "file_size";
$Dsort_method = "dir_name";
break;
case 2;
$Fsort_method = "file_type";
$Dsort_method = "dir_name";
break;
case 3;
$Fsort_method = "file_date";
$Dsort_method = "dir_date";
break;
default:
$Fsort_method = "file_name";
$Dsort_method = "dir_name";
}
// object sorting functions
function ASC_sort_file_objects($a, $b)
{
global $Fsort_method;
$obj1 = strtolower($a->$Fsort_method);
$obj2 = strtolower($b->$Fsort_method);
if ($obj1 == $obj2) return 0;
return ($obj1 < $obj2) ? -1 : 1;
}
function ASC_sort_dir_objects($a, $b)
{
global $Dsort_method;
$obj1 = strtolower($a->$Dsort_method);
$obj2 = strtolower($b->$Dsort_method);
if ($obj1 == $obj2) return 0;
return ($obj1 < $obj2) ? -1 : 1;
}
function DESC_sort_file_objects($a, $b)
{
global $Fsort_method;
$obj1 = strtolower($a->$Fsort_method);
$obj2 = strtolower($b->$Fsort_method);
if ($obj1 == $obj2) return 0;
return ($obj1 > $obj2) ? -1 : 1;
}
function DESC_sort_dir_objects($a, $b)
{
global $Dsort_method;
$obj1 = strtolower($a->$Dsort_method);
$obj2 = strtolower($b->$Dsort_method);
if ($obj1 == $obj2) return 0;
return ($obj1 > $obj2) ? -1 : 1;
}
// sort ascending
if($SortBy == 0) {
// sort arrays (ASCENDING)
usort($file_array, 'ASC_sort_file_objects');
usort($dir_array, 'ASC_sort_dir_objects');
$arrow = "▲";
$SortBy = 1;
}
// sort descending
else {
// sort arrays (DESCENDING)
usort($file_array, 'DESC_sort_file_objects');
usort($dir_array, 'DESC_sort_dir_objects');
$arrow = "▼";
$SortBy = 0;
}
echo "<html>
<head>
<link rel=\"stylesheet\" type=\"text/css\" href=\"".$ThemeURL.$ThemeFolder."style.css\">
</head>
<body>
<table width=\"100%\" height=\"5%\" cellspacing=\"0\" cellpadding=\"5\">
<tr>
<td class=\"Horni\">
<div align=\"center\"><img src=\"/stranky/dc.GIF\" alt=\"dc\" width=\"330\" height=\"50\" border=\"0\"></div>
</td>
</TR>
</TABLE>
<table class='tbl01'>
";
echo "
<tr>
<td class='td01' valign='top'>
<div align='left'>
<table width='100%'>
<tr>
<td class='td02'> </td>
<td class='td03' width='43%' align='left'> <a href='index.php?NumSort=0&SortBy=$SortBy&fdir=$fdir' class='link01'>Name $arrow</a></td>
<td class='td04'> </td>
<td class='td02'> </td>
<td class='td03' width='13%' align='right'><a href='index.php?NumSort=1&SortBy=$SortBy&fdir=$fdir' class='link01'>Size</a></td>
<td class='td04'> </td>
<td class='td02'> </td>
<td class='td03' width='14%' align='left'><a href='index.php?NumSort=2&SortBy=$SortBy&fdir=$fdir' class='link01'>Type</a></td>
<td class='td04'> </td>
<td class='td02'> </td>
<td class='td03' width='18%' align='right'><a href='index.php?NumSort=3&SortBy=$SortBy&fdir=$fdir' class='link01'>Date Modified</a></td>
<td class='td04'> </td>
<td width='20%'> </td>
</tr>
";
// directory is not the base dir
if($fdir != "./") {
// Make every other row a color
$othernum = 1;
// Get folder one level up
$UpPath = dirname($fdir)."/";
echo "
<tr>
<td class='td05'> </td>
<td class='td06' width='43%' align='left'><a href='index.php?fdir=$UpPath' class='link01'><img src='".$iconfolder."levelup.gif' border='0'> Up One Level</a></td>
<td class='td07'> </td>
<td class='td05'> </td>
<td class='td06' width='13%'> </td>
<td class='td07'> </td>
<td class='td05'> </td>
<td class='td06' width='14%'> </td>
<td class='td07'> </td>
<td class='td05'> </td>
<td class='td06' width='18%'> </td>
<td class='td07'> </td>
<td width='20%'> </td>
</tr>";
}
else {
$othernum = 0;
}
// alternate row counter
$count = 0;
// Output folder information
for($y = 0; $y < count($dir_array); $y++)
{
// alternate row colors
if($count % 2 != $othernum) {
$special = "bgcolor='$RowColor'";
}
else {
$special = "";
}
$count++;
echo "
<tr>
<td class='td05' $special> </td>
<td class='td06' $special width='43%' align='left'><a href=\"index.php?SortBy=".$SortBy."&fdir=".$fdir.$dir_array[$y]->getFname()."/\" class=\"link01\"><img src=\"".$iconfolder.$dir_array[$y]->getFicon()."\" border=\"0\"> ".$dir_array[$y]->getFname()."</td>
<td class='td07' $special> </td>
<td class='td05' $special> </td>
<td class='td06' $special width='13%' align='right'> </td>
<td class='td07' $special> </td>
<td class='td05' $special> </td>
<td class='td06' $special width='14%' align='left'>".$dir_array[$y]->getFtype()."</td>
<td class='td07' $special> </td>
<td class='td05' $special> </td>
<td class='td06' $special width='18%' align='right'>".$dir_array[$y]->getFdate()."</td>
<td class='td07' $special> </td>
<td width='20%' $special> </td>
</tr>
";
}
// output file info
for($y = 0; $y < count($file_array); $y++)
//while (list($key, $val) = each($Fname_array))
{
// alternate row colors
if($count % 2 != 0) {
$special = "bgcolor='$RowColor'";
}
else {
$special = "";
}
$count++;
echo "
<tr>
<td class='td05' $special> </td>
<td class='td06' $special width='43%' align='left'><a href=\"".$fdir.$file_array[$y]->getFname()."\" class=\"link01\" target=\"_blank\"><img src=\"".$iconfolder.$file_array[$y]->getFicon()."\" border=\"0\"> ".$file_array[$y]->getFname()."</td>
<td class='td07' $special> </td>
<td class='td05' $special> </td>
<td class='td06' $special width='13%' align='right'>".$file_array[$y]->getFsize()."</td>
<td class='td07' $special> </td>
<td class='td05' $special> </td>
<td class='td06' $special width='14%' align='left'>".$file_array[$y]->getFtype()."</td>
<td class='td07' $special> </td>
<td class='td05' $special> </td>
<td class='td06' $special width='18%' align='right'>".$file_array[$y]->getFdate()."</td>
<td class='td07' $special> </td>
<td width='20%' $special> </td>
</tr>
";
}
echo "
</table>
</div>
</td>
</tr>
<tr>
<td width='100%'> </td>
</tr>
<tr>
<td class=\"td01\" valin=\"bottom\" align=\"center\">
<p class=\"txt02\"> Systém datového centra vyvíjí a spravuje IS.</p> </td>
</tr>
<tr>
<td class=\"td01\" valin=\"bottom\" align=\"center\"> <p class=\"txt02\"> Kontakt: Ing. Jaromír Mikulka, jaromir.mikulka#unex.cz, tel. 2526</p> </td>
</tr>
<tr>
<td class=\"td01\" valin=\"bottom\" align=\"center\"><p class=\"txt02\">Powered By: <b>TotalIndex</b></p></td>
</tr>
</table>
</body>
</html>
";
?>```
[1]: https://i.stack.imgur.com/BXm4n.png
You use double quote(") inside double quote
You must have to use single quote(') inside double quote(")
So your code should be...
....................................
....................................
echo "<html>
<head>
<link rel='stylesheet' type='text/css' href='../your-directory/style.css'>
</head>
....................................
....................................
You can also include external css file in php file
<style>
<?php include 'CSS/main.css'; ?>
</style>

PHPExcel Dynamic column range is not working

I'm a novice to PHP. Here, I am getting an empty table while applying the below code for setting the columns name dynamic.
Here, $highestColumm ideally it should be int value of number of columns.
<table>
<?php $objPHPExcel->setActiveSheetIndexByName('Excel Sheet');
$highestColumm = $objPHPExcel->setActiveSheetIndex(0)->getHighestColumn();
$highestRow = $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$highestColumm++;
for ($row = 1; $row < $highestRow + 1; $row++) {
$dataset = array();
for ($column = 'A'; $column != $highestColumm; $column++) {
$dataset[] = $objPHPExcel->setActiveSheetIndex(0)->getCell($column . $row)->getValue();
}
$datasets[] = $dataset;
}
?>
<tbody>
#for($i=1;$i<=$highestColumm;$i++)
#php
#$dataC = $objPHPExcel->getActiveSheet(0)->getCell('A'.#$i)->getCalculatedValue();
#endphp
<tr>
<td #php #$name = $objPHPExcel->getActiveSheet(0)->getCell('A'.#$i)->getCalculatedValue(); #endphp {{#$name}} </td>
<td #if(#$i==1) #endif>#php #$name = $objPHPExcel->getActiveSheet(0)->getCell('B'.#$i)->getCalculatedValue(); #endphp {{#$name}} </td>
<td #if(#$i==1) #endif>#php #$name = $objPHPExcel->getActiveSheet(0)->getCell('C'.#$i)->getCalculatedValue(); #endphp {{#$name}} </td>
</tr>
#endfor
</tbody>
</table>
Can i use HTML inside PHP will that in this case?
Thanks in Advance

print data with auto increment in paging

i know it's a silly question, but it is creating a problem for me....
I want to print data from mysql with auto increment, but when i use the below code, it auto increments the value in the same page but i want it to be continued to the next pages also, here is my code
<?php
$perpage = 10;
$start = (isset($_GET['id'])) ? $_GET['id'] : 0;
$TotalRec = mysql_result(mysql_query("SELECT COUNT(*) FROM register where
r_bid='".$_SESSION["id"]."'"), 0);
$select = "SELECT * FROM register where r_bid='".$_SESSION["id"]."' LIMIT
$start,$perpage";
$result = mysql_query($select) or die(mysql_error());
$res = mysql_query("select * from regi_balic where b_id='".$_SESSION["id"]."'");
$row1=mysql_fetch_array($res);
$i=1;
while($row = mysql_fetch_array($result))
{
echo '<tr>
<td align="center" width=5%><font size=3>'.$i.'</font></td>
<td width=12%><font size=3>'.$row1['name'].'</font></td>
<td align="center" width=5%><font size=3><a href="edit_detail.phpid='.$row["r_id"].'
&cand_id='.$_SESSION["id"].'&email='.$row["email"].'">'.$row['name'].'</a></font></td>
<td align="center" width=5%><font size=3>'.$row['reference'].'</font></td>
<td align="right" style="padding-right:8px" width=12%>
<fontsize=3>'.$row['age'].'</font></td>
<td align="right" style="padding-right:8px" width=12%>
<fontsize=3>'.$row['occupation'].'</font></td>
<td width=12%><font size=3>'.$row['mob_no'].'</font></td>
<td width=2%><a href="process_del_client.php?id='.$row['r_id'].'"
onClick="returnConfirmSubmit(\'Are You sure ?\')"><img src = "images/delete.png">
</a></td>
</tr>';
}
$i++;
echo '</table>';
if($start == 0)
{
echo "<br>Previous Page ";
}
else
{
echo '<br><a href='."./view.php?id=" . ($start - $perpage) . '>'.
"PreviousPage".'</a> ';
}
if($start + $perpage >= $TotalRec)
{
echo " Next Page<br>";
}
else
{
echo ' <a href='."./view.php?id=" . ($start + $perpage) . '>'."Next Page".'
</a><br>';
}
?>
update value in a session variable and use that onto another page for updation .
Ex..
$_SESSION['value']=0
$_SESSION['value']=$_SESSION['value']++;
try changing
$i=1;
to
$i=1+$start;

Removing the last element in a do-while

How can I remove the last element in a do-while generated table?
In my case the last tr/td where div.dividing_line is stored.
The code:
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>
<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>
';
++$i;
} while ($i < $ArrayLength+1);
For example: If I have an array with 6 items, normally the do-while will do the job, so finally there will be 6 tr's with data and 6 tr's with the dividing_line.
What I need is 6 tr's of data and 5 tr's of dividing_line. Is that possible?
Try this-
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>';
if($i != $ArrayLength) {
echo '<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>
';
}
++$i;
} while ($i < $ArrayLength+1);
Use an extra if Statement to check whether you are at the last element:
if (%i < $ArrayLength) { echo '<tr>...dividing_line</tr>'; }
$ArrayLength = 6;
$i = 1;
do {
echo '
<tr>
<td valign="middle">Data_Position</td>
<td valign="middle">Data_Item</td>
<td valign="middle">Data_Pieces</td>
<td valign="middle">Data_Price</td>
</tr>';
if($i < $ArrayLength)
{
echo '
<tr>
<td colspan="4"><div class="dividing_line"></div></td>
</tr>';
}
++$i;
} while ($i < $ArrayLength+1);
I think your approach just needs an additional step.
It could be something like this:
$data = null;
foreach ($rows as $row) {
$data[] = "<tr><td valign=\"middle\">Data_Position</td><td valign=\"middle\">Data_Item</td><td valign=\"middle\">Data_Pieces</td><td valign=\"middle\">Data_Price</td></tr>";
}
print implode("<tr><td colspan=\"4\"><div class=\"dividing_line\"></div></td></tr>", $data);
This way, you could accomplish what you want without any more logic. Of course, it can be changed or re-design, but I think this way will provide you with a simple yet elegan solution to your problem.
Hope it helps :P

PHP 2-dimensional array -> how to convert to HTML table syntax?

I need to read a bi-dimensional array and convert the values into an HTML table. For example:
$grid = array( array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0));
$grid[0][0]=4;$grid[0][1]=4;$grid[0][2]=4;$grid[0][3]=4;
$grid[1][0]=3;$grid[1][1]=3;$grid[1][2]=5;$grid[1][3]=5;
$grid[2][0]=3;$grid[2][1]=3;$grid[2][2]=5;$grid[2][3]=5;
$grid[3][0]=0;$grid[3][1]=0;$grid[3][2]=5;$grid[3][3]=5;
Reading this array, I want to dynamically write the <TABLE> syntax where the cells with the same number in the grid are merged horizontally or vertically:
That is, I should be able to create the following syntax from the $grid above:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td colspan="4">4</td>
</tr><tr>
<td colspan="2">3</td>
<td colspan="2" rowspan="2">5</td>
</tr><tr>
<td> </td>
<td> </td>
</tr></table>
I have been able to process the colspan correctly, but am having trouble with the rowspan issue. Any ideas? basically I want to be able to create custom homepages with custom content from the array "format" established by the $grid.
Any help greatly appreciated!
Code:
<?php
$grid = array(
array(0,0,0,0),
array(0,0,0,0),
array(0,0,0,0),
array(0,0,0,0),
);
$grid[0][0]=4;$grid[0][1]=4;$grid[0][2]=4;$grid[0][3]=4;
$grid[1][0]=3;$grid[1][1]=3;$grid[1][2]=5;$grid[1][3]=5;
$grid[2][0]=3;$grid[2][1]=3;$grid[2][2]=5;$grid[2][3]=5;
$grid[3][0]=0;$grid[3][1]=0;$grid[3][2]=5;$grid[3][3]=5;
$w = count($grid[0]);
$h = count($grid);
for ($y = 0; $y < $h; ++$y) {
echo "<tr>\n";
for ($x = 0; $x < $w; ++$x) {
$value = $grid[$y][$x];
if ($value === null) {
continue;
}
$colspan = 1;
while ($x + $colspan < $w && $grid[$y][$x + $colspan] === $value) {
$grid[$y][$x + $colspan++] = null;
}
$rowspan = 1;
$rowMatches = true;
while ($rowMatches && $y + $rowspan < $h) {
for ($i = 0; $i < $colspan; ++$i) {
if ($grid[$y + $rowspan][$x + $i] !== $value) {
$rowMatches = false;
break;
}
}
if ($rowMatches) {
for ($i = 0; $i < $colspan; ++$i) {
$grid[$y + $rowspan][$x + $i] = null;
}
++$rowspan;
}
}
$rowspan = $rowspan > 1 ? " rowspan=\"$rowspan\"" : "";
$colspan = $colspan > 1 ? " colspan=\"$colspan\"" : "";
echo " <td$rowspan$colspan>$value</td>\n";
}
echo "</tr>\n";
}
?>
Output:
<tr>
<td colspan="4">4</td>
</tr>
<tr>
<td rowspan="2" colspan="2">3</td>
<td rowspan="3" colspan="2">5</td>
</tr>
<tr>
</tr>
<tr>
<td colspan="2">0</td>
</tr>
<?php
$grid = array( array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0),array(0,0,0,0));
foreach($grid AS $rowkey => $row)
{
echo "<tr>\n";
foreach($grid[$rowkey] AS $columnKey => $field)
{
echo "<td>{$field}</td>\n";
}
echo "</tr>\n";
}
?>

Categories