Php Folks,
The following code meets all my REQUIREMENTS mentioned on my original post with 2 exceptions:
* Search feature - EXACT MATCH & FUZZY SEARCH
* Web Responsive Design
This code of mine works fine. It PRACTICALLY displays all results (rows) from the mysql table.
<?php
//https://www.w3schools.com/whatis/tryit.asp?filename=trywhatis_bootstrap_table&stacked=h
//Required PHP Files.
include 'header_account.php'; //Required on all webpages of the Account.
?>
<?php
if (!$conn)
{
$error = mysqli_connect_error();
$errno = mysqli_connect_errno();
print "$errno: $error\n";
exit();
}
// Get the total number of records from our table "students".
$total_pages = $conn->query('SELECT * FROM browsing_histories')->num_rows;
// Check if the page number is specified and check if it's a number, if not return the default page number which is 1.
$page_number = isset($_GET['page_number']) && is_numeric($_GET['page_number']) ? $_GET['page_number'] : 1;
//Number of results to show on each page.
$num_results_on_page = 25;
if ($stmt_1 = $conn->prepare('SELECT * FROM browsing_histories ORDER BY id LIMIT ?,?')) {
// Calculate the page to get the results we need from our table.
$calc_page = ($page_number - 1) * $num_results_on_page;
$stmt_1->bind_param('ii', $calc_page, $num_results_on_page);
$stmt_1->execute();
//Get the results...
$result = $stmt_1->get_result(); //Do not make this into $result_1 as there is only 1 $result on this page.
if(!empty($_SERVER['HTTP_REFERER']))
{
$referral_page_http = $_SERVER['HTTP_REFERER']; //This fails to capture the Query String!
if(!empty($_SERVER['QUERY_STRING']))
{
$query_string = $_SERVER['QUERY_STRING'];
//$referral_page = "$referral_page_http?$query_string"; //Added the Query String.
$referral_page = "$referral_page_http"; //No Query String.
$referral_page_original = "$referral_page"; //Query String included in Url.
$referral_page_to_convert = "$referral_page_original"; //Query String included in Url.
$current_page_http = $_SERVER["PHP_SELF"]; //Fails to add query strings on dynamic pages' urls. Fails to go beyond the "?". Only displays my proxy's url. Not the proxied url. Eg. Only shows: Eg. http://www.proxy.com/proxify_url?
$current_page = "$current_page_http?$query_string"; //Added the Query String. Added the query string to add the proxied url onto my proxy's url. So now, the url includes both proxy's url and the proxied url. Eg. http://www.proxy.com/proxify_url?http://www.example.com
//$current_page = "$current_page_http"; //No Query String.
$current_page_original = "$current_page"; //Query String included in Url. Url includes proxy's url and proxied url.
$current_page_non_proxied = "$query_string"; //Url only includes proxied url's actual url. Excludes the proxy's url.
$current_page_to_convert = "$current_page_non_proxied"; //Convert only the proxied url's actual url. And not the proxy's url.
}
else
{
$referral_page = "$referral_page_http"; //Added NO Query String.
$referral_page_original = "$referral_page"; //Query String NOT included in Url.
$referral_page_to_convert = "$referral_page_original"; //Query String NOT included in Url.
$current_page_http = $_SERVER["PHP_SELF"]; //Fails to add query strings on dynamic pages' urls. Fails to go beyond the "?". Only displays my proxy's url. Not the proxied url. Eg. Only shows: Eg. http://www.proxy.com/proxify_url?
$current_page = "$current_page_http"; //Url only includes my proxy's url. Eg. http://www.proxy.com/proxify_url? and not http://www.proxy.com/proxify_url?http://www.example.com
$current_page_original = "$current_page"; //Query String NOT included in Url. Url includes proxy's url and NOT the proxied url.
$current_page_to_convert = "$current_page"; //Converting only the proxy's actual url. And not the proxied url as it NOT exists in the url.
}
}
else
{
if(!empty($_SERVER['QUERY_STRING']))
{
$query_string = $_SERVER['QUERY_STRING'];
$current_page_http = $_SERVER["PHP_SELF"]; //Fails to add query strings on dynamic pages' urls. Fails to go beyond the "?". Only displays my proxy's url. Not the proxied url. Eg. Only shows: Eg. http://www.proxy.com/proxify_url?
//$current_page = "$current_page_http?$query_string"; //Added the Query String. Added the query string to add the proxied url onto my proxy's url. So now, the url includes both proxy's url and the proxied url. Eg. http://www.proxy.com/proxify_url?http://www.example.com
$current_page = "$current_page_http"; //No Query String.
$current_page_original = "$current_page"; //Query String included in Url. Url includes proxy's url and proxied url.
$current_page_non_proxied = "$query_string"; //Url only includes proxied url's actual url. Excludes the proxy's url.
$current_page_to_convert = "$current_page_non_proxied"; //Convert only the proxied url's actual url. And not the proxy's url.
}
else
{
$current_page_http = $_SERVER["PHP_SELF"]; //Fails to add query strings on dynamic pages' urls. Fails to go beyond the "?". Only displays my proxy's url. Not the proxied url. Eg. Only shows: Eg. http://www.proxy.com/proxify_url?
$current_page = "$current_page_http"; //Url only includes my proxy's url. Eg. http://www.proxy.com/proxify_url? and not http://www.proxy.com/proxify_url?http://www.example.com
$current_page_original = "$current_page"; //Query String NOT included in Url. Url includes proxy's url and NOT the proxied url.
$current_page_to_convert = "$current_page"; //Converting only the proxy's actual url. And not the proxied url as it NOT exists in the url.
}
}
if(empty($visiting_pages_count))
{
$visiting_pages_count = "1";
}
else
{
$visiting_pages_count++;
}
if($visiting_pages_count == "1")
{
$current_page_converted = "$settings_user_first_quick_link"."$current_page_to_convert";
if(!empty($_SERVER['HTTP_REFERER']))
{
$referral_page_converted = "$settings_user_first_quick_link"."$referral_page_to_convert";
}
}
elseif($visiting_pages_count == "2")
{
$current_page_converted = "$settings_admin_second_quick_link"."$current_page_to_convert";
if(!empty($_SERVER['HTTP_REFERER']))
{
$referral_page_converted = "$settings_admin_second_quick_link"."$referral_page_to_convert";
}
}
elseif($visiting_pages_count == "3")
{
$current_page_converted = "$settings_user_third_quick_link"."$current_page_to_convert";
if(!empty($_SERVER['HTTP_REFERER']))
{
$referral_page_converted = "$settings_user_third_quick_link"."$referral_page_to_convert";
}
}
elseif($visiting_pages_count == "4")
{
$current_page_converted = "$settings_admin_fourth_quick_link"."$current_page_to_convert";
if(!empty($_SERVER['HTTP_REFERER']))
{
$referral_page_converted = "$settings_admin_fourth_quick_link"."$referral_page_to_convert";
}
}
elseif($visiting_pages_count == "5")
{
$current_page_converted = "$settings_user_fifth_quick_link"."$current_page_to_convert";
if(!empty($_SERVER['HTTP_REFERER']))
{
$referral_page_converted = "$settings_user_fifth_quick_link"."$referral_page_to_convert";
}
}
elseif($visiting_pages_count == "6")
{
$current_page_converted = "$settings_admin_first_quick_link"."$current_page_to_convert";
if(!empty($_SERVER['HTTP_REFERER']))
{
$referral_page_converted = "$settings_admin_first_quick_link"."$referral_page_to_convert";
}
}
elseif($visiting_pages_count == "7")
{
$current_page_converted = "$settings_user_second_quick_link"."$current_page_to_convert";
if(!empty($_SERVER['HTTP_REFERER']))
{
$referral_page_converted = "$settings_user_second_quick_link"."$referral_page_to_convert";
}
}
elseif($visiting_pages_count == "8")
{
$current_page_converted = "$settings_admin_third_quick_link"."$current_page_to_convert";
if(!empty($_SERVER['HTTP_REFERER']))
{
$referral_page_converted = "$settings_admin_third_quick_link"."$referral_page_to_convert";
}
}
elseif($visiting_pages_count == "9")
{
$current_page_converted = "$settings_user_fourth_quick_link"."$current_page_to_convert";
if(!empty($_SERVER['HTTP_REFERER']))
{
$referral_page_converted = "$settings_user_fourth_quick_link"."$referral_page_to_convert";
}
}
elseif($visiting_pages_count == "10")
{
$current_page_converted = "$settings_admin_fifth_quick_link"."$current_page_to_convert";
if(!empty($_SERVER['HTTP_REFERER']))
{
$referral_page_converted = "$settings_admin_fifth_quick_link"."$referral_page_to_convert";
}
}
elseif($visiting_pages_count = "1")
{
$current_page_converted = "$settings_user_first_quick_link"."$current_page_to_convert";
if(!empty($_SERVER['HTTP_REFERER']))
{
$referral_page_converted = "$settings_user_first_quick_link"."$referral_page_to_convert";
}
}
else
{
$visiting_pages_count = "1";
$current_page_converted = "$settings_user_first_quick_link"."$current_page_to_convert";
if(!empty($_SERVER['HTTP_REFERER']))
{
$referral_page_converted = "$settings_user_first_quick_link"."$referral_page_to_convert";
}
}
$followed_page_original = "$current_page_original";
$followed_page_converted = "$current_page_converted";
$follower_username = $user;
$follower_browser = $_SERVER['HTTP_USER_AGENT'];
//Insert the User's Click Logs into Mysql Database using Php's Sql Injection Prevention Method "Prepared Statements".
//All entries related to the "Follower" apart from "followee_username" and "followee_browsed_page_converted".
$stmt_2 = mysqli_prepare($conn,"INSERT INTO trailing_histories(query_type,followed_word,query_string,followed_page_original,followed_page_non_proxied,followed_page_converted,referral_page_original,referral_page_non_proxied,referral_page_converted,followee_username,username,gender,age_range,date_of_birth,skin_complexion,height,weight,sexual_orientation,religion,education,profession,marital_status,working_status,country_of_birth,home_town,home_neighbourhood,home_borough,home_council,home_city,home_county,home_district,home_region,home_state,home_country) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
mysqli_stmt_bind_param($stmt_2,'sssssssssssssiisssssssssssssssssss',$query_type,$followed_word,$query_string,$followed_page_original,$followed_page_non_proxied,$followed_page_converted,$referral_page_original,$referral_page_non_proxied,$referral_page_converted,$followee_username,$username,$gender,$age_range,$date_of_birth,$skin_complexion,$height,$weight,$sexual_orientation,$religion,$education,$profession,$marital_status,$working_status,$country_of_birth,$home_town,$home_neighbourhood,$home_borough,$home_council,$home_city,$home_county,$home_district,$home_region,$home_state,$home_country);
mysqli_stmt_execute($stmt_2);
//Check if User's Click Logs have been successfully submitted or not.
if (!$stmt_2)
{
die("<h3 style='text-align:center'>
ERROR 1: Sorry! Our system is currently experiencing a problem logging your trailing!<br>
SUGGESTION: Try visiting this page another time.</h3>");
}
else
{
mysqli_stmt_close($stmt_2);
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo "Browsing History in $server_time Time.";?></title>
<meta content="text/html; charset=ISO-8859-1" http-equiv=" content-type">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html {
font-family; Tahoma, Geneva, sans-serif;
padding: 20px;
background-color: #F8F9F9;
}
table {
border-collapse: collapse;
width: 500px;
}
td, th {
padding: 10px;
}
th {
background-color: #54585d;
color: #ffffff;
font-weight: bold;
font-size: 13px;
border: 1px solid #54585d;
}
td {
color: #636363;
border: 1px solid #dddfe1;
}
tr {
background-color: #f9fafb;
}
tr:nth-child(odd) {
background-color: #ffffff;
}
.pagination {
list-style-type: none;
padding: 10px 0;
display: inline-flex;
justify-content: space-between;
box-sizing: border-box;
}
.pagination li {
box-sizing: border-box;
padding-right: 10px;
}
.pagination li a {
box-sizing: border-box;
background-color: #e2e6e6;
padding: 8px;
text-decoration: none;
font-size: 12px;
font-weight: bold;
color: #616872;
border-radius: 4px;
}
.pagination li a:hover {
background-color: #d4dada;
}
.pagination .next a, .pagination .prev a {
text-transform: uppercase;
font-size: 12px;
}
.pagination .currentpage a {
background-color: #518acb;
color: #fff;
}
.pagination .currentpage a:hover {
background-color: #518acb;
}
</style>
</head>
<body>
<p align="center"><span style="font-weight:bold;"><?php echo "Search Result for:<br> \"All Users\""; ?></span></p>
<br>
<br>
<table>
<tr>
<th>Submission Number</th>
<th>Date & Time</th>
<th>Query Type</th>
<th>Followed Word</th>
<th>Query String</th>
<th>Browsed Page Original</th>
<th>Browsed Page Non-Proxied</th>
<th>Browsed Page Converted</th>
<th>Referral Page Original</th>
<th>Referral Page Non-Proxied</th>
<th>Referral Page Converted</th>
<th>Username</th>
<th>Gender</th>
<th>Age Range</th>
<th>Date Of Birth</th>
<th>Skin Complexion</th>
<th>Height</th>
<th>Weight</th>
<th>Sexual Orientation</th>
<th>Religion</th>
<th>Education</th>
<th>Profession</th>
<th>Marital Status</th>
<th>Working Status</th>
<th>Country Of Birth</th>
<th>Home Town</th>
<th>Home Neighbourhood</th>
<th>Home Borough</th>
<th>Home Council</th>
<th>Home City</th>
<th>Home County</th>
<th>Home District</th>
<th>Home Region</th>
<th>Home State</th>
<th>Home Country</th>
</tr>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['date_and_time']; ?></td>
<td><?php echo $row['query_type']; ?></td>
<td><?php echo $row['followed_word']; ?></td>
<td><?php echo $row['query_string']; ?></td>
<?php //Browsed Page Original contains both Proxy's Url and Proxied Url. ?>
<td><a href="follower_browser.php?followee_username=<?php echo $row['username']; ?>&page_type=browsed_page_original&browsed_page_original=<?php echo $row['browsed_page_original']; ?>"><?php echo $row['browsed_page_original']; ?></a></td>
<?php //Browsed Page Non-Proxied contains Direct Url. ?>
<td><a href="follower_browser.php?followee_username=<?php echo $row['username']; ?>&page_type=browsed_page_non_proxied&browsed_page_non_proxied=<?php echo $row['browsed_page_non_proxied']; ?>"><?php echo $row['browsed_page_non_proxied']; ?></a></td>
<?php //Browsed Page Converted contains only Proxied Url. ?>
<td><a href="follower_browser.php?followee_username=<?php echo $row['username']; ?>&page_type=browsed_page_converted&browsed_page_converted=<?php echo $row['browsed_page_converted']; ?>"><?php echo $row['browsed_page_converted']; ?></a></td>
<?php //Referral Page Original contains only Proxied Url. And so preceding the Proxy's Url onto the url from the followee_browser.php page. Check that page's source code. ?>
<td><a href="follower_browser.php?followee_username=<?php echo $row['username']; ?>&page_type=referral_page_original&referral_page_original=<?php echo $row['referral_page_original']; ?>"><?php echo $row['referral_page_original']; ?></a></td>
<?php //Browsed Page Non-Proxied contains Direct Url. ?>
<td><a href="follower_browser.php?followee_username=<?php echo $row['username']; ?>&page_type=referral_page_non_proxied&browsed_page_non_proxied=<?php echo $row['referral_page_non_proxied']; ?>"><?php echo $row['referral_page_non_proxied']; ?></a></td>
<?php //Referral Page contains only Proxied Url. ?>
<td><a href="follower_browser.php?followee_username=<?php echo $row['username']; ?>&page_type=referral_page_converted&referral_page_converted=<?php echo $row['referral_page_converted']; ?>"><?php echo $row['referral_page_converted']; ?></a></td>
<td><a href="profile.php?followee_username=<?php echo $row['username']; ?>"><?php echo $row['username']; ?></a></td>
<td><?php echo $row['gender']; ?></td>
<td><?php echo $row['age_range']; ?></td>
<td><?php echo $row['date_of_birth']; ?></td>
<td><?php echo $row['skin_complexion']; ?></td>
<td><?php echo $row['height']; ?></td>
<td><?php echo $row['weight']; ?></td>
<td><?php echo $row['sexual_orientation']; ?></td>
<td><?php echo $row['religion']; ?></td>
<td><?php echo $row['education']; ?></td>
<td><?php echo $row['profession']; ?></td>
<td><?php echo $row['marital_status']; ?></td>
<td><?php echo $row['working_status']; ?></td>
<td><?php echo $row['country_of_birth']; ?></td>
<td><?php echo $row['home_town']; ?></td>
<td><?php echo $row['home_neighbourhood']; ?></td>
<td><?php echo $row['home_borough']; ?></td>
<td><?php echo $row['home_council']; ?></td>
<td><?php echo $row['home_city']; ?></td>
<td><?php echo $row['home_county']; ?></td>
<td><?php echo $row['home_district']; ?></td>
<td><?php echo $row['home_region']; ?></td>
<td><?php echo $row['home_state']; ?></td>
<td><?php echo $row['home_country']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<?php if (ceil($total_pages / $num_results_on_page) > 0): ?>
<ul class="pagination">
<?php if ($page_number > 1): ?>
<li class="prev"><a href="<?php echo "{$_SERVER['PHP_SELF']}" ;?>?page_number=<?php echo $page-1 ?>">Prev</a></li>
<?php endif; ?>
<?php if ($page_number > 3): ?>
<li class="start"><a href="<?php echo "{$_SERVER['PHP_SELF']}" ;?>?page_number=1">1</a></li>
<li class="dots">...</li>
<?php endif; ?>
<?php if ($page_number-2 > 0): ?><li class="page"><a href="<?php echo "{$_SERVER['PHP_SELF']}" ;?>?page_number=<?php echo $page_number-2 ?>"><?php echo $page_number-2 ?></a></li><?php endif; ?>
<?php if ($page_number-1 > 0): ?><li class="page"><a href="<?php echo "{$_SERVER['PHP_SELF']}" ;?>?page_number=<?php echo $page_number-1 ?>"><?php echo $page_number-1 ?></a></li><?php endif; ?>
<li class="currentpage"><a href="<?php echo "{$_SERVER['PHP_SELF']}" ;?>?<?php echo $page_number; ?>"><?php echo $page_number ?></a></li>
<?php if ($page_number+1 < ceil($total_pages / $num_results_on_page)+1): ?><li class="page"><a href="<?php echo "{$_SERVER['PHP_SELF']}" ;?>?page_number=<?php echo $page_number+1 ?>"><?php echo $page_number+1 ?></a></li><?php endif; ?>
<?php if ($page_number+2 < ceil($total_pages / $num_results_on_page)+1): ?><li class="page"><a href="<?php echo "{$_SERVER['PHP_SELF']}" ;?>?page_number=<?php echo $page_number+2 ?>"><?php echo $page_number+2 ?></a></li><?php endif; ?>
<?php if ($page_number < ceil($total_pages / $num_results_on_page)-2): ?>
<li class="dots">...</li>
<li class="end"><a href="<?php echo "{$_SERVER['PHP_SELF']}" ;?>?page_number=<?php echo ceil($total_pages / $num_results_on_page) ?>"><?php echo ceil($total_pages / $num_results_on_page) ?></a></li>
<?php endif; ?>
<?php if ($page_number < ceil($total_pages / $num_results_on_page)): ?>
<li class="next"><a href="<?php echo "{$_SERVER['PHP_SELF']}" ;?>?page_number=<?php echo $page_number+1 ?>">Next</a></li>
<?php endif; ?>
</ul>
<?php endif; ?>
</body>
</html>
<?php
$stmt_1->close();
}
?>
<?php
include 'footer_account.php'; //Required on all webpages of the Account.
?>
Now, I need to add the search feature and so experimenting with the mysql queries with php.
Look at my current code below.
Look at my Mysql Queries. I am making mistakes there with the "?".
(I used the "?" as place-holders for the Prepared Statements).
I am told my error lies there in the mysql queries.
Q1. How to fix this ?
Q2. For our newbies' learning purposes. Can anyone be kind enough to show a mini pagination script ?
You may leave-out the following as I am likely to manage this with Bootsrap design tutorials:
* Page Design: Responsive Web Design using Html 5 & CSS (latest).
<?php
//https://www.w3schools.com/whatis/tryit.asp?filename=trywhatis_bootstrap_table&stacked=h
//Required PHP Files.
include 'header_account.php'; //Required on all webpages of the Account.
?>
<?php
if (!$conn)
{
$error = mysqli_connect_error();
$errno = mysqli_connect_errno();
print "$errno: $error\n";
exit();
}
$query_type = $_GET['query_type'];
//If $_GET['query_type']) is empty then show error as it must be set.
if(!isset($_GET['query_type']) && empty($_GET['query_type']))
{
echo "Invalid Query!";
}
/*If $_GET['query_type']) is full with value other than wild-card or "All"
or "all" then make specific query.
*/
elseif($query_type != 'all' OR $query_type != 'All' OR $query_type != '*')
{
${$query_type} = $_GET[$_GET['query_type']];
$followed_word = ${$query_type}; //Same as: $_GET[$_GET['query_type']];
$stmt_1->$total_pages = $conn->query("SELECT * FROM browsing_histories WHERE username = ?")->num_rows;
$stmt_1->bind_param('s', $query_type);
$stmt_1->execute();
// Check if the page number is specified and check if it's a number, if not return the default page number which is 1.
$page_number = isset($_GET['page_number']) && is_numeric($_GET['page_number']) ? $_GET['page_number'] : 1;
//Number of results to show on each page.
$num_results_on_page = 25;
if ($stmt_1 = $conn->prepare('SELECT * FROM browsing_histories WHERE username = ? ORDER BY id LIMIT ?,?')) {
// Calculate the page to get the results we need from our table.
$calc_page = ($page_number - 1) * $num_results_on_page;
//Added this line according to stackoverflow.com suggestion.
$stmt_2->bind_param('sii', $query_type, $num_results_on_page, $calc_page);
//Make the query.
$stmt_2->execute();
}
}
else //Make general query or query for all records.
{
//Grab "all records" from the table.
//Get the total number of records from the table: "browsing_histories".
$total_pages = $conn->query("SELECT * FROM browsing_histories")->num_rows;
//Make the query.
$stmt = $conn->prepare('SELECT * FROM browsing_histories ORDER BY id LIMIT ?,?');
$stmt->bind_param('ii', $num_results_on_page);
}
$stmt_1->execute();
?>
<?php
include 'footer_account.php'; //Required on all webpages of the Account.
?>
NOTE 1:
The header_account.php includes more files.
The $user is defined via SESSION from one of those included files. So don't worry that I have not defined the $user here. Error reporting is ON and I am not getting any error regarding the $user.
NOTE 2: I would appreciate it if someone modified my code according to my requirements mentioned on my original post and add it to this thread for all newbies to learn from.
NOTE 3: This forum is showing my codes funny. Hence, attached my code file. Attachment will show my code correctly.
1st attachment shows my 1st code that is working fine. I do not need your help on that file.
2nd attachment shows my 2nd code which is not working properly. I need your help on that file.
NOTE 4: The 2nd script is in OOP Style. I only know Procedural. Hence struggling. This is not really my code. I grabbed it from a tutorial and tried adding the search function but failing miserably.
Thanks