-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathurl_fns.php
More file actions
112 lines (94 loc) · 3.19 KB
/
url_fns.php
File metadata and controls
112 lines (94 loc) · 3.19 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
require_once('db_fns.php');
function get_user_urls($username)
{
//extract from the database all the URLs this user has stored
$conn = db_connect();
$result = $conn->query( "select bm_URL
from bookmark
where username = '$username'");
if (!$result)
return false;
//create an array of the URLs
$url_array = array();
for ($count = 1; $row = $result->fetch_row(); ++$count)
{
$url_array[$count] = $row[0];
}
return $url_array;
}
function add_bm($new_url)
{
// Add new bookmark to the database
echo "Attempting to add ".htmlspecialchars($new_url).'<br />';
$valid_user = $_SESSION['valid_user'];
$conn = db_connect();
// check not a repeat bookmark
$result = $conn->query("select * from bookmark
where username='$valid_user'
and bm_URL='$new_url'");
if ($result && ($result->num_rows>0))
throw new Exception('Bookmark already exists.');
// insert the new bookmark
if (!$conn->query( "insert into bookmark values
('$valid_user', '$new_url')"))
throw new Exception('Bookmark could not be inserted.');
return true;
}
function delete_bm($user, $url)
{
// delete one URL from the database
$conn = db_connect();
// delete the bookmark
if (!$conn->query( "delete from bookmark
where username='$user' and bm_url='$url'"))
throw new Exception('Bookmark could not be deleted');
return true;
}
/*function recommend_urls($valid_user, $popularity = 1)*/
function recommend_urls()
{
// We will provide semi intelligent recomendations to people
// If they have an URL in common with other users, they may like
// other URLs that these people like
$conn = db_connect();
// find other matching users
// with an url the same as you
// as a simple way of excluding people's private pages, and
// increasing the chance of recommending appealing URLs, we
// specify a minimum popularity level
// if $popularity = 1, then more than one person must have
// an URL before we will recomend it
$query="
select x.bm_URL from (
select count(a.bm_URL) counth, c.bm_url
from bookmark a, user b,bookmark c where a.username=b.username and a.username=c.username
group by c.bm_url
ORDER by counth desc limit 4) x";
/*$query = "select max(count(bm_URL))
from bookmark
where username in
(select distinct(b2.username)
from bookmark b1, bookmark b2
where b1.username='$valid_user'
and b1.username != b2.username
and b1.bm_URL = b2.bm_URL)
and bm_URL not in
(select bm_URL
from bookmark
where username='$valid_user')
group by bm_url
having count(bm_url)>$popularity";*/
if (!($result = $conn->query($query)))
throw new Exception('Could not find any bookmarks to recommend.');
if ($result->num_rows==0)
throw new Exception('Could not find any bookmarks to recommend.');
$urls = array();
// build an array of the relevant urls
for ($count=0; $row = $result->fetch_object(); $count++)
{
$urls[$count] = $row->bm_URL;
}
return $urls;
}
?>