EShop for interviews


/ Published in: PHP
Save to your folder(s)

EShop for interviews


Copy this code and paste it in your HTML
  1. function cde($db) {
  2. if ($db->errno!=0)
  3. throw new Exception ('db error: '.$db->error);
  4. }
  5.  
  6.  
  7. $db = new mysqli ('','','','');
  8. throw new Exception ('connect error: '.mysqli_connect_error());
  9.  
  10.  
  11. check_db();
  12.  
  13. if (isset($_GET['checkout'])) {
  14. checkout();
  15. exit();
  16. }
  17.  
  18. if (isset($_GET['add_to_basket'])) {
  19. add_to_basket($_GET['add_to_basket']);
  20. }
  21.  
  22. if (isset($_GET['delete_from_basket'])) {
  23. delete_from_basket($_GET['delete_from_basket']);
  24. show_basket();
  25. exit();
  26. }
  27.  
  28. if (isset($_GET['show_basket'])) {
  29. show_basket();
  30. exit();
  31. }
  32.  
  33. show_page();
  34.  
  35.  
  36. //---------------------------------
  37. function check_db() {
  38. global $db;
  39.  
  40. $rez = $db->query('show tables like \'zshop\'');
  41. cde($db);
  42.  
  43. if ($rez->num_rows==0) {
  44. $db->query('create table zshop( id int(11) not null auto_increment,'.
  45. 'product varchar(500), rating int(11),'.
  46. 'primary key (`id`))');
  47.  
  48. cde($db);
  49.  
  50. $db->query ('insert into zshop(product, rating)
  51. select link, rating from bookm_tmp a left outer join zstat b
  52. on a.id=b.pic_id');
  53.  
  54. cde($db);
  55. }
  56.  
  57. }
  58. //---------------------------------
  59. function add_to_basket($id) {
  60.  
  61. if (!isset($_SESSION['basket']))
  62. $_SESSION['basket'] = array();
  63.  
  64. $_SESSION['basket'][] = $id;
  65.  
  66. }
  67. //---------------------------------
  68. function show_page() {
  69.  
  70.  
  71. global $db;
  72. $perpage = 10;
  73. $page = isset($_GET['page']) ? $_GET['page'] : 1;
  74. $start = ($page-1)*$perpage;
  75.  
  76. $rez = $db->query('select count(*) from zshop');
  77. cde($db);
  78. $pages = $rez->fetch_row();
  79. $pages = ceil($pages[0]/$perpage);
  80.  
  81. echo '<br/><br/>';
  82. for ($i=0; $i<$pages; $i++) {
  83. echo str_repeat('&nbsp;',3);
  84. if ($i+1==$page) {
  85. echo ($i+1);
  86. } else {
  87. echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.($i+1).'">'.($i+1).'</a>';
  88. }
  89. if (($i+1)%20==0)
  90. echo '<br/>';
  91. }
  92. echo '<br/><br/>';
  93.  
  94. $nav = ob_get_clean();
  95.  
  96. echo (isset($_SESSION['basket']) && (count($_SESSION['basket'])>0)) ?
  97. '<a href="'.$_SERVER['PHP_SELF'].'?show_basket">'.
  98. count($_SESSION['basket']). ' items in the basket</a><br/>' : 'basket is empty<br/>';
  99.  
  100. echo $nav;
  101.  
  102. $stmt = $db->prepare('select id, product, rating from zshop order by id limit ?,?');
  103. cde($db);
  104. $stmt->bind_param('dd',$start,$perpage);
  105.  
  106. $stmt->execute();
  107. $stmt->bind_result($id, $product, $rating);
  108.  
  109. echo '<table>';
  110. $i = 0;
  111. while ($stmt->fetch()) {
  112. if ($i%3==0)
  113. echo '<tr>';
  114.  
  115. $inf = $_SERVER['REQUEST_URI'];
  116. preg_match ('/(?<=http).*?(https?.*)/', $product, $match);
  117. $link = 'http://freedomplace.ru/diff/php_dbg/loader.php?thumb='.$match[1];
  118.  
  119. echo '<td><a href="'.$inf.((strpos($inf,'?')!==false)?'&':'?').'add_to_basket='.
  120. $id.'"><img src="'.$link.'" /></a><h2>Rating: '.$rating.'</h2></td>';
  121.  
  122. if (($i+1)%3==0)
  123. echo '</tr>';
  124. $i++;
  125. }
  126.  
  127. }
  128. //---------------------------------
  129. function show_basket() {
  130.  
  131. $catlink = (strpos($_SERVER['HTTP_REFERER'], $_SERVER['PHP_SELF'])!==false)
  132. && (strpos(strstr($_SERVER['HTTP_REFERER'],'?'),'checkout')===false)?
  133. $_SERVER['HTTP_REFERER']: $_SERVER['PHP_SELF'];
  134.  
  135. $inf = parse_url($catlink);
  136.  
  137. $catlink = $inf['scheme'].'://'.$inf['host'].$inf['path'];
  138.  
  139. if (isset($inf['query'])) {
  140.  
  141. parse_str($inf['query'], $zq);
  142. $f = create_function('$a','return $a==\'page\';');
  143.  
  144. $catlink .= '?'.http_build_query($zq);
  145.  
  146. }
  147.  
  148.  
  149.  
  150. $catlink = '<a href="'.$catlink.'">back to catalog</a>';
  151.  
  152. if ((empty($_SESSION['basket'])) || (count($_SESSION['basket'])==0)) {
  153. echo 'basket empty<br/>';
  154. echo $catlink.'<br/>';
  155. return;
  156. }
  157.  
  158. echo $catlink.'<br/>';
  159.  
  160. global $db;
  161. $rez = $db->query('select id,product,rating from zshop where id in ('.
  162. implode(',', $_SESSION['basket']).')');
  163.  
  164. cde($db);
  165.  
  166. while ($row = $rez->fetch_assoc()) {
  167.  
  168.  
  169. preg_match ('/(?<=http).*?(https?.*)/', $row['product'], $match);
  170. $link = 'http://freedomplace.ru/diff/php_dbg/loader.php?thumb='.$match[1];
  171.  
  172. echo '<div><img src="'.$link.'" /><h2>Rating: '.$row['rating'].'</h2>';
  173. echo '<a href="'.$_SERVER['PHP_SELF'].'?delete_from_basket='.$row['id'].
  174. '">delete from basket</a></div>';
  175. }
  176.  
  177. $rez->free();
  178. echo '<h2><a href="'.$_SERVER['PHP_SELF'].'?checkout" />checkout</a></h2>';
  179.  
  180. }
  181. //---------------------------------
  182. function delete_from_basket($id) {
  183.  
  184. $key = array_search ( $id, $_SESSION['basket']);
  185. if ($key!==false)
  186. unset($_SESSION['basket'][$key]);
  187. }
  188. //---------------------------------
  189. function checkout() {
  190.  
  191. global $db;
  192. $rez = $db->query ('select id, product, rating from zshop where
  193. id in ('.implode(',',$_SESSION['basket']).')');
  194.  
  195. cde($db);
  196.  
  197. if (!file_exists('./checks') || !is_dir('./checks'))
  198. mkdir('checks');
  199.  
  200. $fname = './checks/check_'.date('d_m_Y_H_i_s_').rand(1,10000).'.txt';
  201. $cfile = fopen($fname, 'w');
  202. fwrite ($cfile, "items list\n\r");
  203. fwrite ($cfile, str_repeat("\n\r",3));
  204.  
  205. while ($row = $rez->fetch_object()) {
  206. fwrite($cfile, $row->id.' '.$row->product.' '.$row->rating."\n\r");
  207. }
  208.  
  209. fclose($cfile);
  210.  
  211. header('Content-type: text/plain; charset=windows-1251');
  212. include($fname);
  213. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.