-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.php
More file actions
87 lines (73 loc) · 2.34 KB
/
utilities.php
File metadata and controls
87 lines (73 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
// display_time_remaining:
// Helper function to help figure out what time to display
function display_time_remaining($interval) {
if ($interval->days == 0 && $interval->h == 0) {
// Less than one hour remaining: print mins + seconds:
$time_remaining = $interval->format('%im %Ss');
}
else if ($interval->days == 0) {
// Less than one day remaining: print hrs + mins:
$time_remaining = $interval->format('%hh %im');
}
else {
// At least one day remaining: print days + hrs:
$time_remaining = $interval->format('%ad %hh');
}
return $time_remaining;
}
// print_listing_li:
// This function prints an HTML <li> element containing an auction listing
function print_listing_li($item_id, $title, $desc, $price, $num_bids, $end_time)
{
// Truncate long descriptions
if (strlen($desc) > 250) {
$desc_shortened = substr($desc, 0, 250) . '...';
}
else {
$desc_shortened = $desc;
}
// Fix language of bid vs. bids
if ($num_bids == 1) {
$bid = ' bid';
}
else {
$bid = ' bids';
}
// Calculate time to auction end
$now = new DateTime();
if ($now > $end_time) {
$time_remaining = 'This auction has ended';
}
else {
// Get interval:
$time_to_end = date_diff($now, $end_time);
$time_remaining = display_time_remaining($time_to_end) . ' remaining';
}
// Print HTML
echo('
<li class="list-group-item d-flex justify-content-between">
<div class="p-2 mr-5"><h5><a href="listing.php?item_id=' . $item_id . '">' . $title . '</a></h5>' . $desc_shortened . '</div>
<div class="text-center text-nowrap"><span style="font-size: 1.5em">£' . number_format($price, 2) . '</span><br/>' . $num_bids . $bid . '<br/>' . $time_remaining . '</div>
</li>'
);
}
function process_str($str)
{
$str = substr($str, 0, -1);
//echo $str;
$str_arr = explode (",", $str);
$str_arr = array_unique($str_arr);
// echo $str_arr;
return $str_arr;
}
function page_calculation($result): array
{
$num_results = mysqli_num_rows($result);
$results_per_page = 8;
$max_page = ceil($num_results / $results_per_page);
$page = $_GET["page"] ?? 1;;
$start_from = ($page-1) * $results_per_page;
return [$max_page,$results_per_page,$start_from];
}
?>