RM - Source Code View

Source Code Viewer

This page lets you view source code from my server. The program uses a brute force code formatter to color code elements. NOTE: I wrote this program while trying to learn the vim text editor. This is not my usual coding style.

Use this select box to select a file.

Hide Lines Numbers

View: sqlLoop

sqlLoop() is a class used to loop through the results of a SQL statement.

Formatted Code

Below is the code all formatted with bright colors. The program links to files opened with include() and expands those opened with require(). Clicking on the require line should change visibility.

sqlLoop

001 <!DOCTYPE html> 002 <html lang="en"> 003 <head> 004 <meta charset="UTF-8" /> 005 <meta name="viewport" content="width=device-width, initial-scale=1" /> 006 <meta name="Author" content="Kevin Delaney" /> 007 <meta name="keywords" content="sqlLoop.php" /> 008 <meta name="description" content="Page demonstrates sqlLoop which was designed to let you loop through a SQL result." /> 009 <title>View Source Code - sqlLoop.php</title> 010 <link rel="canonical" href="https://yintercept.com/resources/sqlLoop.html" /> 011 <link rel="stylesheet" href="rm.css" type="text/css"> 012 </head> 013 <body> 014 <h1 id="pageTitle"><a href="https://yintercept.com/resources/">RM - Source Code View</a></h1> 015 <div class="wider"> 016 <h2>Getting Loopy with sqlLoop.php</h2> 017 <p>This program demonstrates using the sqlLoop class to loop through a SQL result set. NOTE: the <a href="https://yintercept.com/resources/view.php?script=10">source code viewer</a> shows both the code for this page and sqlLoop.php.</p> 018 <p>REQUIREMENT: The class uses the function dbMain() found on rmHead.php to create the PDOStatement.</p> 019 <?php 020 include('/var/www/php/rmHead.php'); // Constains dbConn();
require('/var/www/php/sqlLoop.php');
001 <?php 002 /** 003 * sqlLoop holds a PDOStatement Objects for use in a while loop 004 * it requires dbConn() foune in rmHead.php. 005 * 006 * @link https://yintercept.com/resources/view.php?script=1 007 * @see https://yintercept.com/resources/sqlLoop.html 008 * @package ResourceModel 009 * 010 * @author Kevin Delaney 011 * @copyright 2015 012 * @version 0.1 // pre-relesase version. 013 * @license https://yintercept.com/resources/license.html 014 */ 015 class sqlLoop { 016 const LOOP_MAX_ROWS = 1000; 017 // These variables hold the PDO statement and track the query. 018 // I made them public; So that I can see what is in them. 019 // You might want to make them private. 020 private $stmt; // holds the PDOStatement. 021 public $row = array(); // holds the current row. 022 public $rowCnt= 0; // holds the current row count. 023 public $colCnt=0; // holds the number of columns 024 public $hasData = false; // false if the data set is emgy 025 private $firstRow = array(); // will hold the first row of data. 026 private $ending = ''; // string printed by destruct. 027 private $topCalled = false; // Prevent people from callint top twice. 028 /** 029 * The constructor loads and executes a PDOStatment 030 * @param $sql is the SQL 031 * @param $arr holds the input parameters for the SQL. [] is empty. 032 * @param holds the PDO fetch style. Default is PDO::FETCH_NUM 033 */ 034 public function __construct($sql,$arr,$fetchStyle=PDO::FETCH_NUM) { 035 $this->stmt = dbConn($sql); 036 if (is_object($this->stmt)) { 037 if ($this->stmt->execute($arr)) { 038 $this->firstRow = $this->stmt->fetch(); 039 if (is_array($this->firstRow)) { 040 $this->hasData = true; 041 $this->rowCnt = 0; 042 $this->colCnt = $this->stmt->columnCount(); 043 } else { 044 $this->hasData = false; 045 } 046 } else { 047 $this->stmt = null; 048 msgError('SQL execute failed'); 049 msgComment($sql.'<br />Parameters = '.implode('|',$arr)); 050 } 051 } else { 052 msgNote('sqlLoop Error.'); 053 msgComment('sql = '.$sql); 054 } 055 } 056 057 function __destruct() { 058 echo $this->ending; 059 if (is_object($this->stmt)) unset($this->stmt); 060 } 061 /** 062 * Call ->top() before the while loop to see if the sql returned data. 063 * @return boolean 064 */ 065 public function top() { 066 $rv = ($this->topCalled)? false : $this->hasData; 067 // You should only call top once. I set hasData fales in case 068 // someone accidentally uses this function as condition for while loop. 069 $this->topCalled = true; 070 return $rv; 071 } 072 /** 073 * 074 * @return array Constains the SQL names for the columns 075 */ 076 public function columnNames() { 077 $rv = []; 078 for($i=0; $i<$this->colCnt; $i++) { 079 $rv[$i]=$this->stmt->getColumnMeta($i)['name']; 080 } 081 return $rv; 082 } 083 /** 084 * 085 * @param first gets returned on odd rows 086 * @param second is retured on even rows. 087 * @return alternates between first and second input. 088 */ 089 public function toggle($first, $second) { 090 return ($this->rowCnt %2 == 1)? $first : $second; 091 } 092 /** 093 * Sets a string to be printed by the destructor. 094 * 095 * @param string This string will be printed by __destruct. 096 */ 097 public function setDestruct($str) { 098 $this->ending = $str; 099 } 100 /** 101 * use ->chk() as the condition in the while loop. 102 * @return boolean indicating there is a row available for processing. 103 */ 104 public function chk() { 105 if ($this->rowCnt++ == 0) { 106 $this->row = $this->firstRow; 107 $rv = $this->hasData; 108 } else { 109 $this->row = $this->stmt->fetch(); 110 $rv = is_array($this->row); 111 } 112 return $rv; 113 } 114 } 115 ?>
// End Require
021 022 $sql = 'SELECT * FROM Test_Table LIMIT 10'; 023 $lp = new sqlLoop($sql,[],PDO::FETCH_NUM); 024 if ($lp->top()) { 025 // If there is data, the program will open a table. 026 echo '<table style="margin: 6px auto">'.PHP_EOL; 027 echo '<tr><th colspan="'.$lp->colCnt.'">Test Table</th>'.PHP_EOL; 028 echo '<tr>'; 029 $colNames = $lp->columnNames(); 030 for ($i=0; $i<$lp->colCnt; $i++) { 031 echo '<th>'.$colNames[$i].'</th>'; 032 } 033 echo '</tr>'.PHP_EOL; 034 $lp->setDestruct('</table>'); // Closed the table on unset(); 035 } 036 while ($lp->chk()) { 037 echo '<tr class="'.($lp->toggle('rowone','rowtwo')).'">'; 038 for ($i=0; $i<$lp->colCnt; $i++) { 039 echo '<td>'.$lp->row[$i].'</td>'; 040 } 041 echo '</tr>'.PHP_EOL; 042 } 043 unset($lp); 044 ?> 045 </div> 046 </body> 047 </html>

Use "view source" from your browser to grab the output. Feel free to link to this project and check out the Resource Model for information on PHP coding or my tumblr blog for picture of Arizona, Colorado or Utah.

File last modified at January 20 2020 07:55:31.. This page has been viewed 4722 Times.

Record of Revisions
RevbyDateDescription
0.1kd2015-12-29Created sqlLoop.php and the sqlLoop.html test page.

blog ~ Resource Model ~ links