source: inc/common.php@ 0791611

Last change on this file since 0791611 was 0791611, checked in by sstalevska <sara.stalevska@…>, 21 months ago

Push the entire project.

  • Property mode set to 100644
File size: 1.9 KB
Line 
1<?php
2session_start();
3
4/**
5 * Create a persistent connection to the database server.
6 */
7try {
8 //////////////////////////
9 // BEGIN: Local connection
10 // $conn = new PDO('pgsql:host=localhost;port=5433;dbname=weservice', 'postgres', 'postgres', array( // user: postgres
11 // $conn = new PDO('pgsql:host=localhost;port=5433;dbname=weservice', 'postgres', 'sara', array( // user: sara
12 // PDO::ATTR_PERSISTENT => true
13 // ));
14 // END: Local connection
15
16
17 ///////////////////////////
18 // BEGIN: Remote connection
19 // 1. Create an SSH tunnel in Git Bash:
20 // ssh -N -L 55432:127.0.0.1:5432 t_weservice@194.149.135.130
21 // password: 71b1f4ea
22 // 2. Connect:
23 $conn = new PDO('pgsql:host=localhost;port=55432;dbname=db_202122z_va_prj_weservice', 'db_202122z_va_prj_weservice_owner', '821bf9400a4e', array(
24 PDO::ATTR_PERSISTENT => true
25 ));
26 // 3. Set the SQL queries to first search in the schema "weservice", then "public".
27 $stm = $conn->prepare('SET search_path TO weservice,public;');
28 $stm->execute();
29 // END: Remote connection
30} catch (Exception $e) {
31 die("Unable to connect: " . $e->getMessage());
32}
33
34
35/**
36 * For a given float number, output the stars (5 SVG elements).
37 *
38 * @param float|null $rating
39 * @return string
40 */
41function outputStars($rating, $size = 24) {
42 $rating = $rating ?? 0;
43 $retVal = '<div title="Rating: ' . number_format($rating, 2, '.', '') . '">';
44 for ($j = 1; $j <= 5; $j++) {
45 /**
46 * Determine the star color (yellow or gray).
47 */
48 if (round($rating) >= $j) {
49 $color = '#ffd700';
50 } else {
51 $color = '#666';
52 }
53 // Output the icon.
54 $retVal .= '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="' . $size . 'px" height="' . $size . 'px" fill="' . $color . '"><path d="M0 0h24v24H0z" fill="none"/><path d="M0 0h24v24H0z" fill="none"/><path d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z"/></svg>';
55 }
56
57 return $retVal . '</div>';
58}
Note: See TracBrowser for help on using the repository browser.