-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathnellybaz10.php
265 lines (204 loc) · 6.83 KB
/
nellybaz10.php
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<!DOCTYPE html>
<html>
<head>
<title>Nelson's Profile</title>
</head>
<body style="text-align: center; font-family: cursive;">
<table align="center" width="100%">
<tr>
<td>
<div style="margin:30px 0 0 20%; border:1px solid gray; width: 60%; height: 500px; min-width: 300px; font-size: 14px; min-height: 300px" align="left" class="whole-content">
<img style="max-width: 200px; max-height: 200px; border-radius: 8px; margin:30px 0 0 30px;" src="profile.jpg">
<div style="padding-left: 30px">
<h1>Nelson Bassey</h1>
<p><b>Country:</b> Nigeria</p>
<p style="width: 250px"><b>Education:</b>Computer Science student at African Leaderahip University, Rwanda.</p>
<p><b>Interest:</b> Technology | Music</p>
<p><b>Skills:</b> Python | HTML/CSS | JavaScript</p>
</div>
</div>
</td>
<td align="right">
<div>
<div style="margin:30px 20% 0 0; border:1px solid gray; width: 50%; height: 500px; min-width: 300px; font-size: 14px; min-height: 300px" align="center" class="whole-content">
<h3 style="margin-left: 15px; color: navy">I'm Alice, Nelly's smart bot</h3>
<p>(Are you bored? chat with me)</p>
<hr>
<div id="bot-display" style="background-color:; height: 300px; width: 90%; overflow: scroll;">
<p>Ask me any question, I will give you the answer</p>
<!--<p>To train me: <br>
Tell me the question first by typing: <em><b>#your question</b></em><br>
Then the answer by typing: <em><b>@the answer</b></em><br>
Then see if I am smart as I said: Ask by typing <br><em><b>your question</b></em></p>
-->
</div>
<div style="width: 100%; position: relative; top: 30px;">
<table>
<tr>
<td style=" width: 80%">
<input id="input" style="width: 100%; height: 30px" type="text" name="input">
</td>
<td >
<button id="send" style="width: 100%; height: 35px; background-color: navy; color: white; border:none;">Send</button>
</td>
</tr>
</table>
</div>
</div>
</div>
</td>
</tr>
</table>
<?php
include ('../config.example.php');
//include('../db.php');
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
if(!$conn){
echo "couldn't connect";
}
function display_answer($question){
//list($keyvalue, $real_question) = explode('?', $question);
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
$question = mysqli_real_escape_string($con, $question);
$display_query = "SELECT answer FROM bot WHERE question = '$question'";
$result = mysqli_query($con, $display_query);
if(mysqli_num_rows($result) > 0){
$row = mysqli_fetch_array($result);
echo "<div class='this'>";
echo "<p>YOU: ".$question ;
echo "</p>";
echo "<p>Alice: ".$row['answer'];
echo "</p>";
echo "</div>";
}
else{
echo "<div class='this'>";
echo "<p>Sorry, I could't process that, probably my knowledge is not that wide. You can train me
using the correct format. <br>
<b>Corrrect Format:</b><br>
train: your question#your answer@password</p>";
echo "</div>";
}
}
function add_question($real_question, $real_answer){
//list($keyvalue, $real_question) = explode('#', $question);
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
$real_question = mysqli_real_escape_string($con, $real_question);
//check if already exist
$check_question = "SELECT * FROM bot WHERE question = '$real_question'";
$result = mysqli_query($con, $check_question);
if(mysqli_num_rows($result) > 0){
echo "<div class='this'>";
echo "<p>I already know the asnwer to this question, just ask me</p>";
echo "</div>";
}else{
$question_query = "INSERT INTO `bot`(`question`, `answer`) VALUES ('{$real_question}', '{$real_answer}')";
if(mysqli_query($con, $question_query)){
echo "<div class='this'>";
echo "<p>Thank you for training me</p>";
echo "<p>You can now ask that question</p>";
echo "</div>";
}
else{
echo "<div class='this'>";
echo "<p><b>Alice:</b> Sorry I couldn't process your question now. Ask again Later!</p?";
echo "</div>";
}
mysqli_close($con);
}
}
function add_answer($answer){
list($keyvalue, $real_answer) = explode('@', $answer);
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
$real_answer = mysqli_real_escape_string($con, $real_answer);
$get_last_id = "SELECT id from bot ORDER BY id DESC LIMIT 1";
$result = mysqli_query($con, $get_last_id);
$row = mysqli_fetch_array($result);
$last_id = $row['id'];
$answer_query = "UPDATE bot SET answer = '$real_answer' WHERE id = '$last_id'";
if(mysqli_query($con, $answer_query)){
echo "<div class='this'>";
echo "<p>Thank you for training me. <br>
Now you can ask me that question, and I will answer it.<br>
Type <b>The question</b></p>";
echo "</div>";
}
else{
echo "<div class='this'>";
echo "<p>Couldn't learn your answer, mayber it's wrong or I am just too tired. Try again later. Thanks</p>";
echo "</div>";
}
mysqli_close($con);
}
$question = $_POST['question'];
//echo $question;
$x = 0;
$count = 3;
$count_hash = 0;
list($train_word, $question1) = explode(':', $question);
list($real_question, $real_answer) = explode('#', $question1);
list($real_answer, $pass) = explode('@', $real_answer);
$pass = trim($pass);
$check_pass = 'password';
if($question[5] == ':' && $pass == $check_pass){
//echo "<div class='this'>";
//echo $question;
//echo "</div>";
$real_question = trim($real_question);
$real_answer = trim($real_answer);
add_question($real_question ,$real_answer);
}
else if($question == 'aboutbot'){
echo "<div class='this'>";
echo "<p>ABOUT BOT<br><br>
Name: Alice.<br>
Version: Alice 1.5.2</p>";
echo "</div>";
}
else{
display_answer($question);
}
//while( $x < $count){
// if ($question[$x] == '#') {
// add_question($question);
//break;
// }
// elseif($question[$x] == '@'){
// add_answer($question);
// break;
// }
// else{
// display_answer($question);
// break;
// }
// $x = $x + 1;
// }
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#send').click(function(){
//
var input = $('#input').val();
//alert(input);
$('#bot-display').load('profiles/nellybaz10.php .this', {
question: input
});
return false;
//$.ajax({
// url: '',
// type: 'post',
// data: {question: input},
// success: function(response){
//var data_to_display = "<p>" + response + "</p>";
//$('#bot-display').text(response);
// $('#bot-display').html('<p>answer displayed</p>'+response+'<p>seen</p>');
// }
//
//});
});
});
</script>
</body>
</html>