Add "download to excel" button on search engine - php

I want to add a "download to Excel" button on a website.
I have built a search engine using PHP and mySQL.
When I search a keyword a table appears, I want this table to be
I have tried solution on this website but the data wouldn't go in the right cell!
Thanks for your help!!
<?php
$dbhost = 'xxx';
$username = 'xxx';
$password = 'xxx';
mysql_connect("$dbhost" , "$username", "$password" );
mysql_select_db("Data") or die("could not find the database!");
$output = '';
if(isset($_GET['search']))
{
$searchkey = $_GET['search'];
$query = mysql_query("SELECT * FROM Linkedin WHERE email LIKE '%$searchkey%' ") or die("Could not search");
$count = mysql_num_rows($query);
if ($count == 0)
{
$output = 'There was no search results !' ;
}
else
{
echo '<table class="table table-striped table-bordered table-hover">';
echo "<tr><th>Email</th><th>Hashkey</th><th>Breached account</th></tr>";
while ($row = mysql_fetch_array($query))
{
$email = preg_replace('/(' . $searchkey . ')/i', '<mark>\1</mark>', $row["email"]);
$hashkey = $row['hashkey'];
echo "<tr><td>";
echo $email;
echo "</td><td>";
echo $hashkey;
echo "</td><td>";
echo "Linkedin";
echo "</td></tr>";
$output = '</table>';
}
echo '<div>'."$count results were found for '$searchkey'.".'</div>'.'</br>';
}
echo $output;
}
?>

Use jQuery datatables for the purpose. It is easy and elegant.
https://datatables.net/extensions/buttons/examples/initialisation/export

Related

Add pagination to my search engine

How can I add pagination to a search engine results page?
I have build a search engine but there are hundreds of thousands of results for every search so I want to add pages to it.
The results of the search engine are outputted in a table.
I have started to learn php and sql recently...
How can I add those pages?
I have tried this so far but with no success:
<?php
$con = mysqli_connect(xxxx);
mysqli_select_db($con, 'Data') or die("could not find the database!");
$output = '';
$results_per_page = 1000;
//positioning
if(isset($_GET['search']))
{
$starttime = microtime(true); //TIME
$searchkey = $_GET['search'];
$query = mysqli_query($con, "SELECT * FROM table1 WHERE email LIKE '%$searchkey%'") or die("Could not search") ;
$count = mysqli_num_rows($query);
// count number of pages for the search
$number_of_pages = ceil($count/$results_per_page);
// determine which page number visitor is currently on
if (!isset($_GET['page']))
{
$page = 1;
}
else
{
$page = $_GET['page'];
}
// LIMIT
$this_page_first_result = ($page-1)*$results_per_page;
if ($count == 0)
{
echo "</br>";
$output = 'There are no search results !' ;
}
else
{
echo '<table class="myTable">';
echo "<tr><th>aaa</th><th>bbb</th></tr>";
$query = mysqli_query($con, "SELECT * FROM table1 WHERE email LIKE '%$searchkey%' LIMIT " . $this_page_first_result . ',' . $results_per_page" ") or die("Could not search") ;
while ($row = mysqli_fetch_array($query))
{
$email = preg_replace('/(' . $searchkey . ')/i', '<mark>\1</mark>', $row["aaa"]);
$password = $row['bbb'];
echo "<tr><td>";
echo $aaa;
echo "</td><td>";
echo $bbb;
echo "</td></tr>";
$output = '</table>';
}
//echo "</table>";
$endtime = microtime(true);
$duration = $endtime - $starttime;
echo "</br>";
if ($count == 1)
{
echo "<div class=resinfo>";
echo '<div>'."1 result was found for '$searchkey' in $duration seconds.".'</div>'.'</br>';
echo "</div>";
}
else
{
echo "<div class=resinfo>";
echo '<div>'."$count results were found for '$searchkey' in $duration seconds.".'</div>'.'</br>';
echo "</div>";
}
}
echo $output;
}
//LINKS to other pages
for ($page = 1; $page<=$number_of_pages;$page++){
echo '' . $page . '';
}
?>
What have I done wrong, what can I improve to make it work?
Thanks a lot for your help!
It is not a good idea to build a pagination from scratch, instead use a lib like this: https://github.com/KnpLabs/knp-components/blob/master/doc/pager/intro.md

PHP Highlight Input Search Keyword

I want to highlight every $searchkey results.
I tried other solutions but they don't work with a search engine.
Results appear in a table.
I want every appearance of the keyword to be highlighted.
Thanks for your help!
<?php
$dbhost = 'xxx';
$username = 'xxx';
$password = 'xxx';
mysql_connect("$dbhost" , "$username", "$password" );
mysql_select_db("xxx") or die("could not find the database!");
$output = '';
if(isset($_GET['search']))
{
$searchkey = $_GET['search'];
$query = mysql_query("SELECT * FROM xxx WHERE email LIKE '%$searchkey%' ") or die("Could not search");
$count = mysql_num_rows($query);
if ($count == 0)
{
$output = 'There was no search results !' ;
}
else
{
echo '<table class="table table-striped table-bordered table-hover">';
echo "<tr><th>email</th><th>hashkey</th></tr>";
while ($row = mysql_fetch_array($query))
{
$email = $row['email'];
$hashkey = $row['hashkey'];
echo "<tr><td>";
echo $email;
echo "</td><td>";
echo $hashkey;
echo "</td></tr>";
}
echo '<div>'."$count results were found for '$searchkey'.".'</div>'.'</br>';
}
echo "</table>";
$searchkey= $words;
}
?>
You can use Regular Expression in PHP to do this. In this code I use <strong> tag, you can replace it as you want.
while ($row = mysql_fetch_array($query))
{
$email = preg_replace('/(' . $searchkey . ')/s', '<strong>\1</strong>', $row["email"]);
$hashkey = $row['hashkey'];
echo "<tr><td>";
echo $email;
echo "</td><td>";
echo $hashkey;
echo "</td></tr>";
}

link to specific user profile php

I'm creating a search bar feature on my website where the user can search for users using a name.The search result may come up with multiple users with similar names (ex. if I search "Jenna", my database may have multiple users with the name "Jenna" so multiple results will show).I want the user to be able to click on one of the profiles and see that specific "Jenna's" user profile. Kind of like Twitter, where I can search for accounts and view different profiles. Right now I have code that returns the search and also makes the search result a clickable link. However, when I try to save the user id, it only saves the latest user id.
home.php (where the search bar for users is0
<form method="GET" action="search.php" id="searchform">
Search for users:
<input type="text" name="search_user" placeholder="Enter username">
<input type="submit" name="submit" value="Search">
</form>
search.php (prints out the users with the name that the user is searching for)
session_start();
$user = '';
$password = '';
$db = 'userAccounts';
$host = 'localhost';
$port = 3306;
$link = mysqli_connect($host, $user, $password, $db);
mysqli_query($link,"GRANT ALL ON comment_schema TO 'oviya'#'localhost'");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$search_user = $_GET['search_user'];
$sql = "SELECT * FROM users WHERE username LIKE '%$search_user%'";
$result = mysqli_query($link, $sql);
if(mysqli_num_rows($result)>0){
while ($row = mysqli_fetch_assoc($result)) {
$a = '<a';
$b = ' href="';
$c = 'user_profiles.php';
$d = '">';
$e = $row['username'];
$f = '</a';
$g = '>';
$_SESSION['user'] = $row['user_id'];
$userID = $_SESSION['user'];
echo $a.$b.$c.$d.$e.$f.$g;
header("Location: user_profiles.php");
}
}
user_profiles.php (supposed to be where a specific user's profile is shown, based on the link the user clicks with the specific userID)
session_start();
$userID=$_SESSION['user'];
$link = mysqli_connect('localhost', 'x', '', 'userAccounts');
$query="SELECT * FROM dataTable WHERE user_id='$userID'";
$results = mysqli_query($link,$query);
while ($row = mysqli_fetch_assoc($results)) {
echo '<div class="output" >';
$entry_id = $row["entry_id"];
$output= $row["activity"];
echo "Activity: ";
echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."<br>"."<br>";
$output= $row["duration"];
echo "Duration: ";
echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')." hrs"."<br>"."<br>";
$output= $row["date_"];
echo "Date: ";
echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."<br>"."<br>";
echo '</div>';
}
I get where my mistake is, the while loop in search.php will only save the latest userID so the link will always take me to the user profile with that useriD. I'm just not sure how to implement it so that when the user views the list of profiles, the link they click will take them to a specific profile based on the user id.
You need to do changes in search and user.php files :
Search.php :
<?php
session_start();
$user = '';
$password = '';
$db = 'userAccounts';
$host = 'localhost';
$port = 3306;
$link = mysqli_connect($host, $user, $password, $db);
mysqli_query($link, "GRANT ALL ON comment_schema TO 'oviya'#'localhost'");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$search_user = $_GET['search_user'];
$sql = "SELECT * FROM users WHERE username LIKE '%$search_user%'";
$result = mysqli_query($link, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$id = $row['user_id'];
?>
<a href="user_profiles.php?id=<?php echo $id; ?>" >
<?php echo $row['username']; ?>
</a>
<?php
$_SESSION['user'] = $row['user_id'];
$userID = $_SESSION['user'];
header("Location: user_profiles.php");
}
}
User_profile.php:
$userid = $_GET['id'];
$link = mysqli_connect('localhost', 'x', '', 'userAccounts');
$query = "SELECT * FROM dataTable WHERE user_id='$userid'";
$results = mysqli_query($link, $query);
while ($row = mysqli_fetch_assoc($results)) {
echo '<div class="output" >';
$entry_id = $row["entry_id"];
$output = $row["activity"];
echo "Activity: ";
echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8') . "<br>" . "<br>";
$output = $row["duration"];
echo "Duration: ";
echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8') . " hrs" . "<br>" . "<br>";
$output = $row["date_"];
echo "Date: ";
echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8') . "<br>" . "<br>";
echo '</div>';
}
Very first thing, you are saving multiple user ids to a string.
Another thing, you are saving it in while loop.
Therefore, latest value updates old value.
In your case, it will always save the last value. That is prime issue.
You can take array of user ids and save them in it.
$userIds = array();
while ($row = mysqli_fetch_assoc($result)) {
$a = '<a';
$b = ' href="';
$c = 'user_profiles.php';
$d = '">';
$e = $row['username'];
$f = '</a';
$g = '>';
$userIds[] = $row['user_id'];
$userID = $_SESSION['user'];
echo $a.$b.$c.$d.$e.$f.$g;
header("Location: user_profiles.php");
}
$_SESSION['user'] = $userIds;
And in your user_profiles.php, loop over the array or use MySQL IN() condition to get all user profiles.
Also, why did you take too many variables for html link. You can do it in single variable using concatenation like following:
$userIds = array();
while ($row = mysqli_fetch_assoc($result)) {
$a = '<a'
. ' href="';
. 'user_profiles.php';
. '">';
. $row['username'];
. '</a';
. '>';
$userIds[] = $row['user_id'];
$userID = $_SESSION['user'];
echo $a;
header("Location: user_profiles.php");
}
$_SESSION['user'] = $userIds;
Another mistake is that you are echo ing HTML link and doing redirection.
That will cause headers already sent... error.
This will display list of users with searched string
if(mysqli_num_rows($result)>0){
while ($row = mysqli_fetch_assoc($result)) {
$link="<a href='user_profiles.php?user_id=".$row['user_id']."'>".$row['username']."</a>";
}
}
After clicking on link it will redirect to user_profiles.php (no need to header. header is used for automatic redirection)
In user_profiles.php
session_start();
$userID=$_GET['user_id'];
$link = mysqli_connect('localhost', 'x', '', 'userAccounts');
$query="SELECT * FROM dataTable WHERE user_id='$userID'";
$results = mysqli_query($link,$query);
while ($row = mysqli_fetch_assoc($results)) {
echo '<div class="output" >';
$entry_id = $row["entry_id"];
$output= $row["activity"];
echo "Activity: ";
echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."<br>"."<br>";
$output= $row["duration"];
echo "Duration: ";
echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')." hrs"."<br>"."<br>";
$output= $row["date_"];
echo "Date: ";
echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."<br>"."<br>";
echo '</div>';
}

learning how to read data from MYSQL

I'm trying to learn php and mysql, I was trying to read data from my database where i was following something online and encounter an error(s) here is my code
<?php
include 'includes/conn.php';
$query =sprintf("Select * from customer");
$result = mysql_query($query);
if (!$result)
{
$message ='from you see this then it's not connecting!'.mysql_error() ."\n";
$message .= 'everything' .$query;
die($message);
}
while ($customer = mysql_fetch_assoc($result))
{
echo $row['cust_id'];
echo $row['fname'];
echo $row['lname'];
echo $row['gender'];
echo $row['dob'];
}
mysql_free_result($result);
?>
//this is where i was trying to make a connection with the database
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db ='telmar_php';
$con = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
?>
Replace
$message ='from you see this then it's not connecting!'.mysql_error() ."\n";
here your string is single quotation marks in it's is causing the problem
with
$message ="from you see this then it's not connecting!".mysql_error() ."\n";
...and to display your results change this:
while ($customer = mysql_fetch_assoc($result))
to this:
while ($row = mysql_fetch_assoc($result))
Please review the below example for reading data my mysql using php.
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}

Displaying a $result as a result for a Search

I have recently completed my search engine but now I have a new challenge.
This following code I am using it to read out values from a callflow table in my DB and displaying them in a table letting u know wether the call was answered yes or no.
if(isset($res))
{
//creating table
echo '<table style="width:1500px; cell-padding:4px; cell-spacing:0; margin:auto;">';
echo'<th>Time</th><th>Answered Y/N</th></th><th>Naam</th><th>Caller ID</th>';
while($result = mysql_fetch_assoc($res))
{
echo '<tr>';
echo '<td>'.$result['statusCalling'].'</td>';
if ($result['statusAnswered'] =="NULL"||$result['statusAnswered'] =="Null" || $result['statusAnswered'] =="null" || $result['statusAnswered'] =="")
{
echo "<td>Not Answered!</td>";
}
else
{
echo "<td>Answered!</td>";
}
echo '<td>'.$result['calleridname'].'</td>'.'<td>'.$result['calleridnum'].'</td>' ;
echo '</tr>';
}
echo '</table>';
}
I need now to display these results in a search engine result!
I tried this but I doesnt work! No idea how else to go about this! Please help!
$output = '';
//collect
if(isset($_POST['asd'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query('SELECT * FROM callflow WHERE statusCalling LIKE "%'.$searchq.'%" OR calleridname LIKE "%'.$searchq.'%" OR calleridnum LIKE "%'.$searchq.'%" OR $results LIKE "%'.$searchq'%"');
$count = mysql_num_rows($query);
if($count == 0) {
$output = 'There was no search results!';
}else{
while($row = mysql_fetch_array($query)) {
$statusCalling = $row['statusCalling'];
$calleridname = $row['calleridname'];
$calleridnum = $row['calleridnum'];
$results = $row['statusAnswered'];
$id = $row['ID'];
$output .= '<div>'.$statusCalling.' '.$calleridname.' '.$calleridnum.' '.$results.'</div>';
}
}
}
I know that mysql is deprecated, I am learning to program still and i figure if I don't know mysql I cant learn pdo because I don't understand what is what. Please help!
I've worked out the answer and I am posting it here so others can see a way of solving this when they researching for something similar.
<?php
mysql_connect("localhost","root","") or die("Could not connect");
mysql_select_db("voizxl_wachtrij") or die("Could not find Database");
$output = '';
//collect
if(isset($_POST['asd'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query('SELECT * FROM callflow WHERE statusCalling LIKE "%'.$searchq.'%" OR calleridname LIKE "%'.$searchq.'%" OR calleridnum LIKE "%'.$searchq.'%"');
$count = mysql_num_rows($query);
if($count == 0) {
$output = 'There was no search results!';
}else{
while($row = mysql_fetch_array($query)) {
$statusCalling = $row['statusCalling'];
$calleridname = $row['calleridname'];
$calleridnum = $row['calleridnum'];
$id = $row['ID'];
$output[] = $row;
}
}
}
?>
<?php foreach($output as $o){;
if($o['statusAnswered']){
echo $o['statusCalling'].' Answered: '.$o['calleridname'].' '.$o['statusAnswered'].' '.$o['calleridnum'].'<br />';
}else{
echo $o['statusCalling'].' Not Answered: '.$o['calleridname'].' '.$o['calleridnum'].'<br/>';
}
}?>
<br/><br/><br/>
<?php
Cheers

Categories