Jump to content

Datagrid Class


munky334

Recommended Posts

Hi there,

 

I found a datagrid class script on the internet and it is exactly what I am looking for , the problem is that I can get the script to run. The localhost returns no errors or anything but just displays a page with a whole bunch of characters and things that don't make sense. I'm running PHP 5.2.6 on a wampserver configuration so I'm not too sure if this is perhaps causing a problem. I really need to get this datagrid to work as it will solve a lot of my issues with regards to displaying my data. It's vital that I get this script to work and I have been trying for days now , very frustrating! Please can someone have a look , I would really appreciate it.

 

Thanking you in advance.

 

test.php

 

<?

 

include_once("DBinit.php");

include_once("datagrid.class.php");

 

 

?>

 

 

 

<form name="frmAction" action="" method="POST">

 

<h2>Sections</h2>

 

 

<span class='normalTxt <?=$color?>'><?=$t_messg?></span>

 

<?

//datagrid for existing sections

 

$dbp = new flor_db_process();

$query = "select * from patientdemo";

$dbp->fetchQuery($query);

$gridData = $dbp->resultSet;

 

$dg = new DataGrid($gridData);

$dg->width = "300";

$dg->fontSize = .8;

$dg->autoColumns = false;

 

$dg->paging = true;

$dg->paging_byPageNos = true;

$dg->paging_NoOfRows = 3;

 

 

$dg->borderColor = "gray"; //or values like #C0C0C0

$dg->borderStyle = "dotted"; //or solid, or dashed, etc.,

$dg->borderWidth = 1; //integer

$dg->cellPadding = 2; //integer

$dg->headBackgroundColor = "#FFFFCC"; //or values like green

$dg->headForeColor = "green"; //or values like #FFFFFF

$dg->fontSize = .8; //integer

$dg->fontFamily = "courier new"; //or Arial, verdana, etc.,

 

$dg->colSet[0] = new Column();

$dg->colSet[0]->displayName = "Headline";

$dg->colSet[0]->fieldName = "dispName";

$dg->colSet[0]->width = "70%";

 

$dg->colSet[1] = new Column();

$dg->colSet[1]->displayName = "Code";

$dg->colSet[1]->fieldName = "positionCode";

$dg->colSet[1]->width = "30%";

 

 

$dg->showGrid();

 

?>

 

</form>

DBinit.php

 

<?

 

include_once("flor_db_process.class.php");

 

define ("DB_DOMAIN","localhost");

//define ("DB_PORT", 3306);

define ("DB_USER","root");

define ("DB_PASS","");

define ("DB_NAME","patient");

 

?>

DataGrid.class.php

 

<?

 

/* -----------------------------------------------

* Author: Harish Palaniappan, Floresense.com

* Ver: 1.1

* Last Updated: Jan 2008

* License: LGPL License

* You are free to use/modify/redistribute this script. Keep this comment block intact.

* ----------------------------------------------

*

* add your own comments here..

*

*/

 

 

class DataGrid{

 

//vars

private $gridHTML = "";

public $columnCount = 0;

public $rowCount = 0;

private $colInfo = null;

private $fontStyle = "";

 

private $curRowId = -1;

private $curColId = -1;

 

//properties

public $width = "100%";

public $autoColumns = true;

 

public $resultSet = null;

public $fontSize = 1;

public $fontFamily = "Verdana";

 

public $borderWidth = 1;

public $borderStyle = "solid";

public $borderColor = "black";

public $cellPadding = 2;

public $headBackgroundColor = "#5B59B7";

public $headForeColor = "#FFFFFF";

 

public $paging_indexAtTop = true;

public $paging_indexAtBottom = true;

 

public $pageByName = false;

public $pageByName_field = "";

private $curPgAlphaIndex = "";

private $pgAlphaIndexKeys = null;

 

public $paging = false;

public $paging_byPageNos = false;

public $paging_byNextPrev = false;

public $paging_NoOfRows = 30;

private $curPgIndex = 1;

private $totPages = 1;

 

public $colSet = array();

public $rowSet = array();

public $dataKey = null;

 

 

//events

public $onRowItem_Bound = null;

 

function DataGrid($resultSet=null)

{

$this->resultSet = $resultSet;

}

 

function showGrid()

{

$this->makeGrid();

 

echo $this->gridHTML;

}

 

function makeGrid()

{

 

//get params

$gridAction = $_POST['flor_grid_a'];

$gridVal = $_POST['flor_grid_aV'];

if ($gridAction)

{

$gridAction = strtoupper($gridAction);

switch($gridAction)

{

case "PGA":

$this->curPgAlphaIndex = strtoupper($gridVal);

break;

 

case "PG":

$this->curPgIndex = $gridVal;

break;

}

}

 

$this->gridHTML = "";

$this->fontStyle="font-size:".$this->fontSize."em;font-family:".$this->fontFamily.";";

 

$this->initDataInfo();

 

if ($this->paging_indexAtTop)

if ($this->paging || $this->pageByName)

$this->gridHTML .= $this->getPagingHTML();

 

$this->gridHTML .= $this->getTableHTML("OPEN");

 

$this->gridHTML .= $this->getDataHTML();

 

$this->gridHTML .= $this->getTableHTML("CLOSE");

 

if ($this->paging_indexAtBottom)

if ($this->paging || $this->pageByName)

$this->gridHTML .= $this->getPagingHTML();

 

if ($this->paging || $this->pageByName)

$this->gridHTML .= $this->getPaging_supportScripts();

}

 

private function getPagingHTML()

{

if ($this->pageByName)

{

$this->prepAlphabetIndex();

 

if ($this->curPgAlphaIndex=="" && count($this->pgAlphaIndexKeys)>0)

$this->curPgAlphaIndex = $this->pgAlphaIndexKeys[1];

 

return $this->getAlphabetIndex_HTML();

}

elseif($this->paging)

{

$this->prepPageIndex();

 

return $this->getPageIndex_HTML();

}

}

 

private function prepPageIndex()

{

$totRows = $this->resultSet->num_rows;

$this->totPages = ceil($totRows/$this->paging_NoOfRows);

}

 

private function getPageIndex_HTML()

{

if ($this->paging_byPageNos)

{

$html = "<div style='padding:2px 5px 2px 5px;background-color:".$this->headBackgroundColor.";".

"color:".$this->headForeColor.";width:".($this->width-10)."px;border:0px solid red;text-align:center;".

"margin:10px 0px 0px 0px;'> Pages - ";

 

for($idx = 1; $idx<=$this->totPages; $idx++)

{

$linkStr = "".$idx;

 

if ($this->curPgIndex == $idx)

$linkStr = "<span style='font-weight:bold;font-size:1.4em;'>$linkStr</span>";

else

$linkStr = "<a href=\"javascript:florDBgrid_action('pg',$idx);\" style='color:".$this->headForeColor.";'>".

$linkStr."</a>";

 

if ($idx == 1)

$html .= $linkStr."  ";

elseif($idx == $this->totPages)

$html .= " ".$linkStr." ";

else

$html .= " ".$linkStr."  ";

}

}

elseif ($this->paging_byNextPrev)

{

$html = "<div style='padding:2px 5px 2px 5px;background-color:".$this->headBackgroundColor.";".

"color:".$this->headForeColor.";width:".($this->width-10)."px;border:0px solid red;text-align:right;".

"margin:10px 0px 0px 0px;'>";

 

$prevPage = true; $nextPage = true;

 

if ($this->curPgIndex == 1)

$prevPage = false;

if ($this->curPgIndex == $this->totPages)

$nextPage = false;

 

if ($prevPage)

$html .= "<a href=\"javascript:florDBgrid_action('pg',".($this->curPgIndex-1).");\" style='color:".$this->headForeColor.";'>".

"< Prev</a>  ";

if($nextPage)

$html .= "<a href=\"javascript:florDBgrid_action('pg',".($this->curPgIndex+1).");\" style='color:".$this->headForeColor.";'>".

"Next ></a>";

}

 

$html .= "</div>";

 

return $html;

}

 

private function prepAlphabetIndex()

{

$db = new flor_db_process();

$db->resultSet = $this->resultSet;

 

$this->pgAlphaIndexKeys = array("");

 

while(1)

{

$row = $db->getRow2();

if (!$row)

break;

 

$tmpCellVal = $row[$this->pageByName_field];

$tmpChar = strtoupper(substr($tmpCellVal,0,1));

if (is_numeric($tmpChar) && !array_search("0-9",$this->pgAlphaIndexKeys))

array_push($this->pgAlphaIndexKeys, "0-9");

if (!array_search($tmpChar,$this->pgAlphaIndexKeys))

array_push($this->pgAlphaIndexKeys, $tmpChar);

}

 

}

 

private function getAlphabetIndex_HTML()

{

$html = "<div style='padding:2px 5px 2px 5px;background-color:".$this->headBackgroundColor.";".

"color:".$this->headForeColor.";width:".($this->width-10)."px;border:0px solid red;text-align:center;".

"margin:10px 0px 0px 0px;'>";

 

$alphabets = preg_split('//', "ABCDEFGHIJKLMNOPQRSTUVWXYZ", -1, PREG_SPLIT_NO_EMPTY);

$chars = array("0-9");

$chars = array_merge($chars,$alphabets);

 

foreach($chars as $char)

{

$linkStr = $char;

 

if ($this->curPgAlphaIndex == $char || (is_numeric($this->curPgAlphaIndex) && $char=="0-9"))

$linkStr = "<span style='font-weight:bold;font-size:1.4em;'>$linkStr</span>";

elseif (array_search($char,$this->pgAlphaIndexKeys))

$linkStr = "<a href=\"javascript:florDBgrid_action('pga','$char');\" style='color:".$this->headForeColor.";'>".

$linkStr."</a>";

 

if ($char == "0-9")

$html .= $linkStr."  ";

elseif($char =="Z")

$html .= " ".$linkStr." ";

else

$html .= " ".$linkStr."  ";

}

 

$html .= "</div>";

 

return $html;

}

 

private function getPaging_supportScripts()

{

$html = <<<END

<input type=hidden name=flor_grid_a id=flor_grid_a value=""/>

<input type=hidden name=flor_grid_aV id=flor_grid_aV value=""/>

 

<script language="javascript">

<!--//

function florDBgrid_action(action, value)

{

var F = document.forms['frmAction'];

 

F.flor_grid_a.value = action;

F.flor_grid_aV.value = value;

 

F.submit();

}

//-->

</script>

 

END;

 

return $html;

}

 

private function initDataInfo()

{

if ($this->autoColumns)

{

if ($this->resultSet)

{

$this->colInfo = $this->resultSet->fetch_fields();

$this->columnCount = count($this->colInfo);

 

for($i=0; $i<$this->columnCount; $i++)

{

$col = new Column();

$col->fieldName = $this->colInfo[$i]->name;

$this->colSet[$i] = $col;

}

}

}

else

{

$this->columnCount = count($this->colSet);

}

}

 

private function getDataHTML()

{

$tmpHTML = "";

 

$db = new flor_db_process();

$db->resultSet = $this->resultSet;

$db->reset();

 

$gridHeaderComplete = false;

if (!$gridHeaderComplete)

{

$tmpHTML = $this->getHeaderHTML();

 

$gridHeaderComplete = true;

}

 

if ($this->resultSet)

{

$this->rowCount = $this->resultSet->num_rows;

 

if ($this->rowCount==0)

$tmpHTML .= $this->getTableHTML("NODATA");

else

{

while($tmpRow = $db->getRow2())

{

$this->curRowId++;

 

if ($this->pageByName)

{

$tmpCellVal = $tmpRow[$this->pageByName_field];

if (!$tmpCellVal)

continue; //skip row

 

$tmpStr1 = strtoupper(substr($tmpCellVal,0,1));

 

if (is_numeric($tmpStr1) && $this->curPgAlphaIndex == "0-9")

{

//is numeric value

}

elseif ($tmpStr1 != strtoupper($this->curPgAlphaIndex))

continue; //skip row

 

}

elseif($this->paging)

{

if ($this->curPgIndex == 1)

$lowRowID = 0;

else

$lowRowID = (($this->curPgIndex-1) * $this->paging_NoOfRows)+1;

$hiRowID = $this->curPgIndex * $this->paging_NoOfRows;

 

if ($this->curRowId >= $lowRowID &&

$this->curRowId <= $hiRowID)

{

//allow row.. it belongs to page

}

else

continue; //skip row.

}

 

$tmpHTML .= $this->getDataRowHTML($tmpRow);

}

}

}

else

$tmpHTML .= $this->getTableHTML("NODATA");

 

 

//echo "*".$this->rowCount."*".$this->columnCount."*";

//print_r($this->colInfo);

 

return $tmpHTML;

}

 

private function getAddNewRowHTML()

{

///if ($this->resultSet)

 

return "";

}

 

private function getDataRowHTML($rowData)

{

$t1 = "\n<tr>";

 

$this->curColId = -1;

 

$cells = array();

foreach($this->colSet as $colItem)

{

$this->curColId++;

 

if ($colItem->fieldName!=null)

$cellContent = $rowData[$colItem->fieldName];

else

$cellContent = "";

 

//row item bound event

//call data_rowItem_bound event handler

$cell = new cell();

$cell->rowId = $this->curRowId;

$cell->colId = $this->curColId;

$cell->dataKey = $rowData[$this->dataKey];

$cell->HTMLContent = $cellContent;

 

array_push($cells, $cell);

}

 

if ($this->onRowItem_Bound != null)

{

$tmpFn = $this->onRowItem_Bound;

$cells = $tmpFn($cells); //passing a row.. for each row this entire fn is called.

}

 

$j=-1;

foreach($this->colSet as $colItem)

{

$j++;

 

$t1 .= "\n<td style=\"".$this->fontStyle."\">\n".

$cells[$j]->HTMLContent.

"\n</td>";

}

 

$t1 .= "\n</tr>";

 

return $t1;

}

 

private function getHeaderHTML()

{

if (!$this->colSet)

return "";

 

$t1 = "\n<tr>";

 

$headStyle = "background-color:".$this->headBackgroundColor.";color:".

$this->headForeColor.";";

 

foreach($this->colSet as $colItem)

{

if ($colItem->displayName != null)

$tmpText = $colItem->displayName;

else

$tmpText = $colItem->fieldName;

 

if ($colItem->width!=null)

$colW = "width='".$colItem->width."' ";

else

$colW = "";

 

$t1 .= "\n<td $colW style=\"font-weight:bold;".$this->fontStyle.$headStyle."\">\n".

$tmpText.

"\n</td>";

 

}

 

$t1 .= "\n</tr>";

 

return $t1;

}

 

private function getTableHTML($code)

{

switch(strtoupper($code))

{

case "OPEN":

$pad = $this->cellPadding;

$bw = $this->borderWidth;

$bc = $this->borderColor;

$bs = $this->borderStyle;

 

 

return "<table border=$bw width=".$this->width." cellpadding=$pad ".

"style=\"margin:10px 0px 10px 0px;border-collapse:collapse;border-style:$bs;\" borderColor='$bc'>";

 

break;

 

case "CLOSE":

return "\n</table>";

break;

 

case "NODATA":

if ($this->columnCount>1)

$colspan= "colspan='".$this->columnCount."' ";

else

$colspan=" ";

 

return "\n<tr>\n<td style='font-weight:bold;border:1px dotted gray;padding:4px;".

$this->fontStyle."' $colspan>No Data</td>\n</tr>";

 

break;

 

case "ALPHA_INDEX":

$this->prepAlphabetIndex();

$tmpHTML = $this->getAlphabetIndex_HTML();

 

if ($this->columnCount>1)

$colspan= "colspan='".$this->columnCount."' ";

else

$colspan=" ";

 

$tmpHTML = "\n<tr>\n<td style='font-weight:bold;border:1px dotted gray;padding:4px;".

$this->fontStyle."' $colspan>".$tmpHTML."</td>\n</tr>";

 

return $tmpHTML;

 

break;

 

}

 

}

}

 

class column{

public $displayName = null;

public $fieldName = null;

public $dataFormat = null;

public $width = null;

public $backgroundColor = null;

 

}

 

class cell{

public $rowId = null;

public $colId = null;

public $dataKey = null;

public $HTMLContent = null;

}

 

?>

 

/* -----------------------------------------------

* CLASS : flor_db_process

*

* Author: Harish Palaniappan, Floresense.com

* Ver: 1.2  (earlier DBProcess ver 1.0)

* Last Updated: Jan 2008

* License: LGPL License -> http://www.gnu.org/licenses/lgpl.html

* License in brief: You are free to use/modify/redistribute this script.

* Keep this comment block intact.

* ----------------------------------------------

*

* add your own comments here..

*

*/

 

 

class flor_db_process{

 

  var $query = "";

 

  var $isFetchMode = false;

  var $isUpdateMode = false;

  var $isInsertMode = false;

 

  var $resultSet = null;

  var $affRows=0;

  var $numRows=0;

  var $fieldCount = 0;

 

  var $errStr = "";

  var $hasErr = false;

 

  var $con = null;

  var $isDBConnected = false;

 

  function DBprocess2()

  {

  $this->openCon();

  }

 

  function openCon()

  {

    $this->closeCon();

 

    global $db;

 

  $dbname ="";

 

  $db_Domain;

  $dbname = $cmtDB;

 

 

//connecting with mysqli

//

$this->con = mysqli_init();

/* set connection options */

$this->con->options(MYSQLI_INIT_COMMAND, "SET AUTOCOMMIT=0");

$this->con->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);

/* connect to server */

error_reporting(0);

 

if (!defined("DB_DOMAIN") || !defined("DB_USER") || !defined("DB_PASS") || !defined("DB_NAME"))

exit("include common.php?.. db connect info missing for DBProcess2");

 

    $this->con->real_connect(DB_DOMAIN,DB_USER,DB_PASS,DB_NAME,DB_PORT);

 

error_reporting(E_ALL ^ E_NOTICE);

if (mysqli_connect_errno())

{

$this->hasErr = true;

$this->errStr = "Connect failed: \n" . mysqli_connect_error();

exit($this->errStr);

}

else

{

$this->hasErr=false;

$this->isDBConnected=true;

}

 

  }

 

  //function to close db connection

  function closeCon()

  {

//disconnecting with mysqli

//

if ($this->isDBConnected)

$this->con->close();

 

$this->con=null;

  }

 

  function processQuery()

  {

  global $errReportingType;

 

  if (!$this->query)

return null;

 

if (!$this->con)

$this->openCon();

 

error_reporting(0);

$tmpResult = $this->con->query($this->query);

 

error_reporting($errReportingType);

 

if ($this->con->error=="")

{

$this->hasErr = false;

  if ($this->isFetchMode)

  {

  $this->resultSet = $tmpResult;

if ($tmpResult)

{

  $this->numRows = $tmpResult->num_rows;

$this->fieldCount = $this->field_count;

}

  else

$this->numRows = 0;

  }

elseif($this->isUpdateMode)

{

  $this->affRows = $this->con->affected_rows;

}

elseif($this->isInsertMode)

{

  $this->affRows = $this->con->affected_rows;

  return $this->con->insert_id;

}

}

else

{

  $this->hasErr = true;

 

$this->errStr = $this->con->error;

 

if (APPMODE!="live")

$this->errStr = $this->query."___".$this->errStr;

 

if ($this->isInsertMode)

return null;

}

  }

 

  function fetchQuery($qry)

  {

  $this->query = $qry;

  $this->isFetchMode = true;

  $this->isUpdateMode = false;

  $this->isInsertMode =false;

  $this->processQuery();

 

  }

 

  function insertQuery($qry)

  {

  $this->query = $qry;

  $this->isFetchMode = false;

  $this->isUpdateMode = false;   

  $this->isInsertMode = true;

  $insert_id = $this->processQuery();

 

    return $insert_id;

  }

 

  function updateQuery($qry)

  {

  $this->query = $qry;

  $this->isFetchMode = false;

  $this->isUpdateMode = true;   

  $this->isInsertMode = false;

  $this->processQuery();

   

  }

 

  function getRow()

  {

$row = $this->resultSet->fetch_row();

 

  if (isset($row))

  return $row;

  else

  return null;

  }

 

  function getRow2()

  {

$row = $this->resultSet->fetch_assoc();

 

  if (isset($row))

  return $row;

  else

  return null;

  }

 

  function reset()

  {

  mysqli_data_seek($this->resultSet,0);

  }

   

}

?>

Link to comment
Share on other sites

The code was written using short open tags <? and <?= Which is sad really because code that is intended to be distributed must use full php tags <?php and <?php echo so that is will be parsed on all servers.

 

The fix is to go through the code and change any <? to <?php and any <?= to <?php echo

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.