PageRenderTime 63ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/jqGrid/server.php

https://bitbucket.org/dynamikus/research-behavior-and-succes
PHP | 504 lines | 431 code | 16 blank | 57 comment | 85 complexity | b20106dcc0e2a5bb02e0f47ecbc05c9b MD5 | raw file
Possible License(s): GPL-2.0, MIT
  1. <?php
  2. include("dbconfig.php");
  3. // coment the above lines if php 5
  4. //include("JSON.php");
  5. //$json = new Services_JSON();
  6. // end comment
  7. $examp = $_REQUEST["q"]; //query number
  8. $page = $_REQUEST['page']; // get the requested page
  9. $limit = $_REQUEST['rows']; // get how many rows we want to have into the grid
  10. $sidx = $_REQUEST['sidx']; // get index row - i.e. user click to sort
  11. $sord = $_REQUEST['sord']; // get the direction
  12. if(!$sidx) $sidx =1;
  13. // search options
  14. // IMPORTANT NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  15. // this type of constructing is not recommendet
  16. // it is only for demonstration
  17. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  18. $wh = "";
  19. $searchOn = Strip($_REQUEST['_search']);
  20. if($searchOn=='true') {
  21. $fld = Strip($_REQUEST['searchField']);
  22. if( $fld=='id' || $fld =='invdate' || $fld=='name' || $fld=='amount' || $fld=='tax' || $fld=='total' || $fld=='note' ) {
  23. $fldata = Strip($_REQUEST['searchString']);
  24. $foper = Strip($_REQUEST['searchOper']);
  25. // costruct where
  26. $wh .= " AND ".$fld;
  27. switch ($foper) {
  28. case "bw":
  29. $fldata .= "%";
  30. $wh .= " LIKE '".$fldata."'";
  31. break;
  32. case "eq":
  33. if(is_numeric($fldata)) {
  34. $wh .= " = ".$fldata;
  35. } else {
  36. $wh .= " = '".$fldata."'";
  37. }
  38. break;
  39. case "ne":
  40. if(is_numeric($fldata)) {
  41. $wh .= " <> ".$fldata;
  42. } else {
  43. $wh .= " <> '".$fldata."'";
  44. }
  45. break;
  46. case "lt":
  47. if(is_numeric($fldata)) {
  48. $wh .= " < ".$fldata;
  49. } else {
  50. $wh .= " < '".$fldata."'";
  51. }
  52. break;
  53. case "le":
  54. if(is_numeric($fldata)) {
  55. $wh .= " <= ".$fldata;
  56. } else {
  57. $wh .= " <= '".$fldata."'";
  58. }
  59. break;
  60. case "gt":
  61. if(is_numeric($fldata)) {
  62. $wh .= " > ".$fldata;
  63. } else {
  64. $wh .= " > '".$fldata."'";
  65. }
  66. break;
  67. case "ge":
  68. if(is_numeric($fldata)) {
  69. $wh .= " >= ".$fldata;
  70. } else {
  71. $wh .= " >= '".$fldata."'";
  72. }
  73. break;
  74. case "ew":
  75. $wh .= " LIKE '%".$fldata."'";
  76. break;
  77. case "ew":
  78. $wh .= " LIKE '%".$fldata."%'";
  79. break;
  80. default :
  81. $wh = "";
  82. }
  83. }
  84. }
  85. //echo $fld." : ".$wh;
  86. // connect to the database
  87. $db = mysql_connect($dbhost, $dbuser, $dbpassword)
  88. or die("Connection Error: " . mysql_error());
  89. mysql_select_db($database) or die("Error conecting to db.");
  90. switch ($examp) {
  91. case 1:
  92. $result = mysql_query("SELECT COUNT(*) AS count, SUM(amount) AS amount, SUM(tax) AS tax, SUM(total) AS total FROM invheader a, clients b WHERE a.client_id=b.client_id ".$wh);
  93. $row = mysql_fetch_array($result,MYSQL_ASSOC);
  94. $count = $row['count'];
  95. if( $count >0 ) {
  96. $total_pages = ceil($count/$limit);
  97. } else {
  98. $total_pages = 0;
  99. }
  100. if ($page > $total_pages) $page=$total_pages;
  101. $start = $limit*$page - $limit; // do not put $limit*($page - 1)
  102. if ($start<0) $start = 0;
  103. $SQL = "SELECT a.id, a.invdate, b.name, a.amount,a.tax,a.total,a.note FROM invheader a, clients b WHERE a.client_id=b.client_id".$wh." ORDER BY ".$sidx." ". $sord." LIMIT ".$start." , ".$limit;
  104. $result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
  105. if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
  106. header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
  107. header("Content-type: text/xml;charset=utf-8");
  108. }
  109. $et = ">";
  110. echo "<?xml version='1.0' encoding='utf-8'?$et\n";
  111. echo "<rows>";
  112. echo "<page>".$page."</page>";
  113. echo "<total>".$total_pages."</total>";
  114. echo "<records>".$count."</records>";
  115. echo "<userdata name='tamount'>".$row['amount']."</userdata>";
  116. echo "<userdata name='ttax'>".$row['tax']."</userdata>";
  117. echo "<userdata name='ttotal'>".$row['total']."</userdata>";
  118. // be sure to put text data in CDATA
  119. while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
  120. echo "<row id='". $row[id]."'>";
  121. echo "<cell>". $row[id]."</cell>";
  122. echo "<cell>". $row[invdate]."</cell>";
  123. echo "<cell><![CDATA[". $row[name]."]]></cell>";
  124. echo "<cell>". $row[amount]."</cell>";
  125. echo "<cell>". $row[tax]."</cell>";
  126. echo "<cell>". $row[total]."</cell>";
  127. echo "<cell><![CDATA[". $row[note]."]]></cell>";
  128. echo "</row>";
  129. }
  130. echo "</rows>";
  131. break;
  132. case 2:
  133. $result = mysql_query("SELECT COUNT(*) AS count FROM invheader a, clients b WHERE a.client_id=b.client_id".$wh);
  134. $row = mysql_fetch_array($result,MYSQL_ASSOC);
  135. $count = $row['count'];
  136. if( $count >0 ) {
  137. $total_pages = ceil($count/$limit);
  138. } else {
  139. $total_pages = 0;
  140. }
  141. if ($page > $total_pages) $page=$total_pages;
  142. $start = $limit*$page - $limit; // do not put $limit*($page - 1)
  143. if ($start<0) $start = 0;
  144. $SQL = "SELECT a.id, a.invdate, b.name, a.amount,a.tax,a.total,a.note FROM invheader a, clients b WHERE a.client_id=b.client_id".$wh." ORDER BY ".$sidx." ".$sord. " LIMIT ".$start." , ".$limit;
  145. $result = mysql_query( $SQL ) or die("Could not execute query.".mysql_error());
  146. $responce->page = $page;
  147. $responce->total = $total_pages;
  148. $responce->records = $count;
  149. $i=0; $amttot=0; $taxtot=0; $total=0;
  150. while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
  151. $amttot += $row[amount];
  152. $taxtot += $row[tax];
  153. $total += $row[total];
  154. $responce->rows[$i]['id']=$row[id];
  155. $responce->rows[$i]['cell']=array($row[id],$row[invdate],$row[name],$row[amount],$row[tax],$row[total],$row[note]);
  156. $i++;
  157. }
  158. $responce->userdata['amount'] = $amttot;
  159. $responce->userdata['tax'] = $taxtot;
  160. $responce->userdata['total'] = $total;
  161. $responce->userdata['name'] = 'Totals:';
  162. //echo $json->encode($responce); // coment if php 5
  163. header('Content-type: application/json');
  164. echo json_encode($responce);
  165. break;
  166. case 3:
  167. $result = mysql_query("SELECT COUNT(*) AS count FROM invheader a, clients b WHERE a.client_id=b.client_id".$wh);
  168. $row = mysql_fetch_array($result,MYSQL_ASSOC);
  169. $count = $row['count'];
  170. if( $count >0 ) {
  171. $total_pages = ceil($count/$limit);
  172. } else {
  173. $total_pages = 0;
  174. }
  175. if ($page > $total_pages) $page=$total_pages;
  176. $start = $limit*$page - $limit; // do not put $limit*($page - 1)
  177. if ($start<0) $start = 0;
  178. $SQL = "SELECT a.id, a.invdate, b.name, a.amount,a.tax,a.total,a.note FROM invheader a, clients b WHERE a.client_id=b.client_id".$wh." ORDER BY ".$sidx." ".$sord. " LIMIT ".$start." , ".$limit;
  179. $result = mysql_query( $SQL ) or die("Couldn’t execute query.".mysql_error());
  180. $responce->page = $page;
  181. $responce->total = $total_pages;
  182. $responce->records = $count;
  183. $i=0;
  184. while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
  185. $responce->rows[$i]['id']=$row[id];
  186. $responce->rows[$i]['cell']=array("",$row[id],$row[invdate],$row[name],$row[amount],$row[tax],$row[total],$row[note]);
  187. $i++;
  188. }
  189. //echo $json->encode($responce); // coment if php 5
  190. header('Content-type: application/json');
  191. echo json_encode($responce);
  192. break;
  193. case 4:
  194. $result = mysql_query("SELECT COUNT(*) AS count FROM invheader a, clients b WHERE a.client_id=b.client_id".$wh);
  195. $row = mysql_fetch_array($result,MYSQL_ASSOC);
  196. $count = $row['count'];
  197. if( $count >0 ) {
  198. $total_pages = ceil($count/$limit);
  199. } else {
  200. $total_pages = 0;
  201. }
  202. if ($page > $total_pages) $page=$total_pages;
  203. $start = $limit*$page - $limit; // do not put $limit*($page - 1)
  204. if ($start<0) $start = 0;
  205. $SQL = "SELECT a.id, a.invdate, b.name, a.amount,a.tax,a.total,a.note FROM invheader a, clients b WHERE a.client_id=b.client_id".$wh." ORDER BY ".$sidx." ".$sord. " LIMIT ".$start." , ".$limit;
  206. $result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());
  207. $responce->page = $page;
  208. $responce->total = $total_pages;
  209. $responce->records = $count;
  210. $i=0;
  211. while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
  212. $responce->rows[$i]=$row;
  213. $i++;
  214. }
  215. //echo $json->encode($responce); // coment if php 5
  216. header('Content-type: application/json');
  217. echo json_encode($responce);
  218. break;
  219. case 5:
  220. $result = mysql_query("SELECT COUNT(*) AS count FROM invheader a, clients b WHERE a.client_id=b.client_id".$wh);
  221. $row = mysql_fetch_array($result,MYSQL_ASSOC);
  222. $count = $row['count'];
  223. if( $count >0 ) {
  224. $total_pages = ceil($count/$limit);
  225. } else {
  226. $total_pages = 0;
  227. }
  228. if ($page > $total_pages) $page=$total_pages;
  229. $start = $limit*$page - $limit; // do not put $limit*($page - 1)
  230. if ($start<0) $start = 0;
  231. $SQL = "SELECT a.id, a.invdate, b.name, a.amount,a.tax,a.total,a.note FROM invheader a, clients b WHERE a.client_id=b.client_id".$wh." ORDER BY ".$sidx." ".$sord. " LIMIT ".$start." , ".$limit;
  232. $result = mysql_query( $SQL ) or die("Couldn’t execute query.".mysql_error());
  233. $responce->page = $page;
  234. $responce->total = $total_pages;
  235. $responce->records = $count;
  236. $i=0;
  237. while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
  238. $responce->rows[$i]=$responce->rows[$i]['cell']=array($row[id],$row[invdate],$row[name],$row[amount],$row[tax],$row[total],$row[note]);
  239. $i++;
  240. }
  241. //echo $json->encode($responce); // coment if php 5
  242. header('Content-type: application/json');
  243. echo json_encode($responce);
  244. break;
  245. case 'tree':
  246. $node = (integer)$_REQUEST["nodeid"];
  247. // detect if here we post the data from allready loaded tree
  248. // we can make here other checks
  249. if( $node >0) {
  250. $n_lft = (integer)$_REQUEST["n_left"];
  251. $n_rgt = (integer)$_REQUEST["n_right"];
  252. $n_lvl = (integer)$_REQUEST["n_level"];
  253. $n_lvl = $n_lvl+1;
  254. $SQL = "SELECT account_id, name, acc_num, debit, credit, balance, level, lft, rgt FROM accounts WHERE lft > ".$n_lft." AND rgt < ".$n_rgt." AND level = ".$n_lvl." ORDER BY lft";
  255. } else {
  256. // initial grid
  257. $SQL = "SELECT account_id, name, acc_num, debit, credit, balance, level, lft, rgt FROM accounts WHERE level=0 ORDER BY lft";
  258. }
  259. $result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
  260. if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
  261. header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
  262. header("Content-type: text/xml;charset=utf-8");
  263. }
  264. $et = ">";
  265. echo "<?xml version='1.0' encoding='utf-8'?$et\n";
  266. echo "<rows>";
  267. echo "<page>1</page>";
  268. echo "<total>1</total>";
  269. echo "<records>1</records>";
  270. // be sure to put text data in CDATA
  271. while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
  272. echo "<row>";
  273. echo "<cell>". $row[account_id]."</cell>";
  274. echo "<cell>". $row[name]."</cell>";
  275. echo "<cell>". $row[acc_num]."</cell>";
  276. echo "<cell>". $row[debit]."</cell>";
  277. echo "<cell>". $row[credit]."</cell>";
  278. echo "<cell>". $row[balance]."</cell>";
  279. echo "<cell>". $row[level]."</cell>";
  280. echo "<cell>". $row[lft]."</cell>";
  281. echo "<cell>". $row[rgt]."</cell>";
  282. if($row[rgt] == $row[lft]+1) $leaf = 'true';else $leaf='false';
  283. echo "<cell>".$leaf."</cell>";
  284. echo "<cell>false</cell>";
  285. echo "</row>";
  286. }
  287. echo "</rows>";
  288. break;
  289. case 'tree2':
  290. $node = (integer)$_REQUEST["nodeid"];
  291. // detect if here we post the data from allready loaded tree
  292. // we can make here other checks
  293. if( $node >0) {
  294. $n_lft = (integer)$_REQUEST["n_left"];
  295. $n_rgt = (integer)$_REQUEST["n_right"];
  296. $n_lvl = (integer)$_REQUEST["n_level"];
  297. $n_lvl = $n_lvl+1;
  298. $SQL = "SELECT account_id, name, acc_num, debit, credit, balance, level, lft, rgt FROM accounts WHERE lft > ".$n_lft." AND rgt < ".$n_rgt." AND level = ".$n_lvl." ORDER BY lft";
  299. } else {
  300. // initial grid
  301. $SQL = "SELECT account_id, name, acc_num, debit, credit, balance, level, lft, rgt FROM accounts WHERE 0=0 ORDER BY lft";
  302. }
  303. $result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
  304. if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
  305. header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
  306. header("Content-type: text/xml;charset=utf-8");
  307. }
  308. $et = ">";
  309. echo "<?xml version='1.0' encoding='utf-8'?$et\n";
  310. echo "<rows>";
  311. echo "<page>1</page>";
  312. echo "<total>1</total>";
  313. echo "<records>1</records>";
  314. // be sure to put text data in CDATA
  315. while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
  316. echo "<row>";
  317. echo "<cell>". $row[account_id]."</cell>";
  318. echo "<cell>". $row[name]."</cell>";
  319. echo "<cell>". $row[acc_num]."</cell>";
  320. echo "<cell>". $row[debit]."</cell>";
  321. echo "<cell>". $row[credit]."</cell>";
  322. echo "<cell>". $row[balance]."</cell>";
  323. echo "<cell>". rand(0,1)."</cell>";
  324. echo "<cell>". $row[level]."</cell>";
  325. echo "<cell>". $row[lft]."</cell>";
  326. echo "<cell>". $row[rgt]."</cell>";
  327. if($row[rgt] == $row[lft]+1) $leaf = 'true';else $leaf='false';
  328. echo "<cell>".$leaf."</cell>";
  329. echo "<cell>true</cell>";
  330. echo "</row>";
  331. }
  332. echo "</rows>";
  333. break;
  334. case 'tree3' :
  335. $SQLL = "SELECT t1.account_id FROM accounts t1 LEFT JOIN accounts t2 ON t1.account_id = t2.parent_id WHERE t2.account_id IS NULL";
  336. $resultl = mysql_query( $SQLL ) or die("Couldn t execute query.".mysql_error());
  337. $leafnodes = array();
  338. while($rw = mysql_fetch_array($resultl,MYSQL_ASSOC)) {
  339. $leafnodes[$rw[account_id]] = $rw[account_id];
  340. }
  341. $node = (integer)$_REQUEST["nodeid"];
  342. $n_lvl = (integer)$_REQUEST["n_level"];
  343. if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
  344. header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
  345. header("Content-type: text/xml;charset=utf-8");
  346. }
  347. $et = ">";
  348. echo "<?xml version='1.0' encoding='utf-8'?$et\n";
  349. echo "<rows>";
  350. echo "<page>1</page>";
  351. echo "<total>1</total>";
  352. echo "<records>1</records>";
  353. if($node >0) {
  354. $wh = 'parent_id='.$node;
  355. // we ouput the next level
  356. $n_lvl = $n_lvl+1;
  357. } else {
  358. $wh = 'ISNULL(parent_id)';
  359. //$level = 0;
  360. }
  361. $SQL = "SELECT account_id, name, acc_num, debit, credit, balance, parent_id FROM accounts WHERE ".$wh;
  362. $result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
  363. while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
  364. echo "<row>";
  365. echo "<cell>". $row[account_id]."</cell>";
  366. echo "<cell>". $row[name]."</cell>";
  367. echo "<cell>". $row[acc_num]."</cell>";
  368. echo "<cell>". $row[debit]."</cell>";
  369. echo "<cell>". $row[credit]."</cell>";
  370. echo "<cell>". $row[balance]."</cell>";
  371. echo "<cell>". $n_lvl."</cell>";
  372. if(!$row[parent_id]) $valp = 'NULL'; else $valp = $row[parent_id];
  373. echo "<cell><![CDATA[".$valp."]]></cell>";
  374. if($row[account_id] == $leafnodes[$row[account_id]]) $leaf='true'; else $leaf = 'false';
  375. echo "<cell>".$leaf."</cell>";
  376. echo "<cell>false</cell>";
  377. echo "</row>";
  378. }
  379. echo "</rows>";
  380. break;
  381. case 'tree31' :
  382. $SQLL = "SELECT t1.account_id FROM accounts t1 LEFT JOIN accounts t2 ON t1.account_id = t2.parent_id WHERE t2.account_id IS NULL";
  383. $resultl = mysql_query( $SQLL ) or die("Couldn t execute query.".mysql_error());
  384. $leafnodes = array();
  385. while($rw = mysql_fetch_array($resultl,MYSQL_ASSOC)) {
  386. $leafnodes[$rw[account_id]] = $rw[account_id];
  387. }
  388. $node = (integer)$_REQUEST["nodeid"];
  389. $n_lvl = (integer)$_REQUEST["n_level"];
  390. if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
  391. header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
  392. header("Content-type: text/xml;charset=utf-8");
  393. }
  394. /*
  395. $et = ">";
  396. echo "<?xml version='1.0' encoding='utf-8'?$et\n";
  397. echo "<rows>";
  398. echo "<page>1</page>";
  399. echo "<total>1</total>";
  400. echo "<records>1</records>";
  401. */
  402. if($node >0) {
  403. $wh = 'parent_id='.$node;
  404. // we ouput the next level
  405. $n_lvl = $n_lvl+1;
  406. } else {
  407. $wh = 'ISNULL(parent_id)';
  408. //$level = 0;
  409. }
  410. $SQL = "SELECT account_id, name, acc_num, debit, credit, balance, parent_id FROM accounts WHERE ".$wh;
  411. $result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
  412. $response->page = 1;
  413. $response->total = 1;
  414. $response->records = $count;
  415. $i=0;
  416. while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
  417. if($row[account_id] == $leafnodes[$row[account_id]]) $leaf='true'; else $leaf = 'false';
  418. if(!$row[parent_id]) $valp = NULL; else $valp = $row[parent_id];
  419. // if($row[rgt] == $row[lft]+1) $leaf = 'true';else $leaf='false';
  420. $response->rows[$i]['cell']=array($row[account_id],
  421. $row[name],
  422. $row[acc_num],
  423. $row[debit],
  424. $row[credit],
  425. $row[balance],
  426. $n_lvl,
  427. $valp,
  428. $leaf,
  429. 'false'
  430. );
  431. $i++;
  432. }
  433. header('Content-type: application/json');
  434. echo json_encode($response);
  435. /*
  436. while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
  437. echo "<row>";
  438. echo "<cell>". $row[account_id]."</cell>";
  439. echo "<cell>". $row[name]."</cell>";
  440. echo "<cell>". $row[acc_num]."</cell>";
  441. echo "<cell>". $row[debit]."</cell>";
  442. echo "<cell>". $row[credit]."</cell>";
  443. echo "<cell>". $row[balance]."</cell>";
  444. echo "<cell>". $n_lvl."</cell>";
  445. if(!$row[parent_id]) $valp = 'NULL'; else $valp = $row[parent_id];
  446. echo "<cell><![CDATA[".$valp."]]></cell>";
  447. if($row[account_id] == $leafnodes[$row[account_id]]) $leaf='true'; else $leaf = 'false';
  448. echo "<cell>".$leaf."</cell>";
  449. echo "<cell>false</cell>";
  450. echo "</row>";
  451. }
  452. echo "</rows>";
  453. */
  454. break;
  455. }
  456. mysql_close($db);
  457. function Strip($value)
  458. {
  459. if(get_magic_quotes_gpc() != 0)
  460. {
  461. if(is_array($value))
  462. if ( array_is_associative($value) )
  463. {
  464. foreach( $value as $k=>$v)
  465. $tmp_val[$k] = stripslashes($v);
  466. $value = $tmp_val;
  467. }
  468. else
  469. for($j = 0; $j < sizeof($value); $j++)
  470. $value[$j] = stripslashes($value[$j]);
  471. else
  472. $value = stripslashes($value);
  473. }
  474. return $value;
  475. }
  476. function array_is_associative ($array)
  477. {
  478. if ( is_array($array) && ! empty($array) )
  479. {
  480. for ( $iterator = count($array) - 1; $iterator; $iterator-- )
  481. {
  482. if ( ! array_key_exists($iterator, $array) ) { return true; }
  483. }
  484. return ! array_key_exists(0, $array);
  485. }
  486. return false;
  487. }
  488. ?>