Thursday, 27 March 2014

Ajax multi checkbox filter search PHP Mysql

Ajax multi checkbox filter search
Lekhulal
lekhulal@gmail.com
---------------------------------------------------------------------------

1) Let’s create a database called "db_name"

-----------------------------------------------

2) create a table

CREATE TABLE IF NOT EXISTS `workers` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(255) DEFAULT NULL,
 `age` int(11) DEFAULT NULL,
 `address` varchar(255) DEFAULT NULL,
 `hasCar` tinyint(1) DEFAULT NULL,
 `speaksForeignLanguage` tinyint(1) DEFAULT NULL,
 `canWorkNights` tinyint(1) DEFAULT NULL,
 `isStudent` tinyint(1) DEFAULT NULL,
 PRIMARY KEY (`id`)
 );
 
-----------------------------------------------
 
3) Insert some data
 
INSERT INTO `workers` (`id`, `name`, `age`, `address`, `hasCar`, 
`speaksForeignLanguage`, `canWorkNights`, `isStudent`) VALUES
(1, 'Jim', 39, '12 High Street, London', 1, 1, 1, 1),
(2, 'Fred', 29, '13 High Street, London', 1, 1, 1, 0),
(3, 'Bill', 19, '14 High Street, London', 1, 1, 0, 0),
(4, 'Tom', 39, '15 High Street, London', 1, 0, 0, 0),
(5, 'Cathy', 29, '16 High Street, London', 1, 0, 0, 1),
(6, 'Petra', 19, '17 High Street, London', 1, 0, 1, 0),
(7, 'Heide', 39, '18 High Street, London', 1, 1, 0, 0),
(8, 'William', 29, '19 High Street, London', 1, 1, 0, 1),
(9, 'Ted', 19, '20 High Street, London', 0, 0, 0, 1),
(10, 'Mike', 19, '21 High Street, London', 1, 0, 0, 1),
(11, 'Jo', 19, '22 High Street, London', 0, 1, 0, 1);
 
-----------------------------------------------
 
4) Create a page called index.php
 
<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>AJAX filter demo</title>
    <style>
      body {
        padding: 10px;
      }

      h1 {
          margin: 0 0 0.5em 0;
          color: #343434;
          font-weight: normal;
          font-family: 'Ultra', sans-serif;   
          font-size: 36px;
          line-height: 42px;
          text-transform: uppercase;
          text-shadow: 0 2px white, 0 3px #777;
      }
 
      h2 {
          margin: 1em 0 0.3em 0;
          color: #343434;
          font-weight: normal;
          font-size: 30px;
          line-height: 40px;
          font-family: 'Orienta', sans-serif;
      }

      #employees {
        font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
        font-size: 12px;
        background: #fff;
        margin: 15px 25px 0 0;
        border-collapse: collapse;
        text-align: center;
        float: left;
        width: 700px;
      }

      #employees th {
        font-size: 14px;
        font-weight: normal;
        color: #039;
        padding: 10px 8px;
        border-bottom: 2px solid #6678b1;
      }

      #employees td {
        border-bottom: 1px solid #ccc;
        color: #669;
        padding: 8px 10px;
      }

      #employees tbody tr:hover td {
        color: #009;
      }

      #filter {
        float:left;
      }
    </style>
  </head>
  <body> 
    <h1>Ajax filter search using PHP Mysql</h1>

    <table id="employees">
      <thead>
        <tr>
          <th>ID</th>
          <th>Name</th>
          <th>Age</th>
          <th>Address</th>
          <th>Car</th>
          <th>Language</th>
          <th>Nights</th>
          <th>Student</th>
        </tr>
      </thead>
      <tbody>
      </tbody>
    </table>

    <div id="filter">
      <h2>Filter options</h2>
      <div>
        <input type="checkbox" id="car" name="hasCar">
        <label for="car">Has own car</label>
      </div>
      <div>
        <input type="checkbox" id="language" name="speaksForeignLanguage">
        <label for="language">Can speak foreign language</label>
      </div>
      <div>
        <input type="checkbox" id="nights" name="canWorkNights">
        <label for="nights">Can work nights</label>
      </div>
      <div>
        <input type="checkbox" id="student" name="isStudent">
        <label for="student">Is a student</label>
      </div>
    </div>

    <script src="http://code.jquery.com/jquery-latest.js"></script> 
    <script>
      function makeTable(data){
       var tbl_body = "";
          $.each(data, function() {
            var tbl_row = "";
            $.each(this, function(k , v) {
              tbl_row += "<td>"+v+"</td>";
            })
            tbl_body += "<tr>"+tbl_row+"</tr>";                 
          })

        return tbl_body;
      }

      function getEmployeeFilterOptions(){
        var opts = [];
        $checkboxes.each(function(){
          if(this.checked){
            opts.push(this.name);
          }
        });

        return opts;
      }

      function updateEmployees(opts){
  //alert(opts);
        $.ajax({
          type: "POST",
          url: "submit.php",
          dataType : 'json',
          cache: false,
          data: {filterOpts: opts},
          success: function(records){
  alert("records"+records);
            $('#employees tbody').html(makeTable(records));
          }
        });
      }

      var $checkboxes = $("input:checkbox");
      $checkboxes.on("change", function(){
        var opts = getEmployeeFilterOptions();
        updateEmployees(opts);
      });

      updateEmployees();
    </script> 
  </body> 
</html> 

-----------------------------------------------

4)create a action page called submit.php :
  set database configuration(database username and password) 
 

<?php
  $pdo = new PDO('mysql:host=localhost;dbname=db_name', 'root', 'password'); 
  $select = 'SELECT *'; 
  $from = ' FROM workers'; 
  $where = ' WHERE TRUE'; 
   $opts = isset($_POST['filterOpts'])? $_POST['filterOpts'] : array('');
  
  if (in_array("hasCar", $opts)){
    $where .= " AND hasCar = 1";
  }

  if (in_array("speaksForeignLanguage", $opts)){
    $where .= " AND speaksForeignLanguage = 1";
  }

  if (in_array("canWorkNights", $opts)){
    $where .= " AND canWorkNights = 1";
  }

  if (in_array("isStudent", $opts)){
    $where .= " AND isStudent = 1";
  }

  $sql = $select . $from . $where; 
  $statement = $pdo->prepare($sql);
  $statement->execute();
  $results=$statement->fetchAll(PDO::FETCH_ASSOC);
  $json=json_encode($results);
  echo($json);
?>

-----------------------------------------------

Wednesday, 26 March 2014

Full text Search



Full-Text Searches PHP - Mysql

Lekhulal 

lekhulal@gmail.com

---------------------------------------------------------------------------------------------

--
--
-Create a table called articles
-That table type must support FULLTEXT indexes.
-Set of one or more columns included in a FULLTEXT index.
--
--

CREATE TABLE articles
(
title VARCHAR(200),
body TEXT,
FULLTEXT (title,body)
);


--
--
Insert values into table called articles
--
--

INSERT INTO articles (title,body)
VALUES
('MySQL Tutorial','DBMS stands for DataBase ...'),
('How To Use MySQL Well','After you went through a ...'),
('Optimizing MySQL','In this tutorial we will show ...'),
('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
('MySQL vs. YourSQL','In the following database comparison ...'),
('MySQL Security','When configured properly, MySQL ...');


--
--
-Write query for full text search.
-The search string is given as the argument to AGAINST().
-For each row in the table, MATCH() returns a relevance value.
-The search is performed in case-insensitive.
--
--

SELECT * FROM articles
WHERE MATCH (title,body) AGAINST ('database');


--
--
-The number of unique words in that row.
--
--

SELECT
COUNT(IF(MATCH (title,body) AGAINST ('database'), 1, NULL))
AS count
FROM articles;


--------------------------------------- Thank You -------------------------------------

Friday, 7 March 2014

 

HOW TO VIEW AN UPLOADED  FILE IN PHP ?

                   
                                                                                                  Rahul R 
                                                                                                                        rahul9629@gmail.com
------------------------------------------------------------------------------------------------------------------------------------




We can use google document viewer to view an uploded file.


Step 1:Go to google document viewer  https://docs.google.com/viewer

Step 2:Enter your filename(http://yoursite.com/uploads/resumename.doc)  in to the google document viewer textbox.Then click generate link

Step 3: Copy the needed link(iframe or href) from google document viewer.

Step 4:Then paste in to your page.  
            
----------------------------------------------------------------------------------------------------------------------------------
1.index.php
----------------------------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <h1>VIEW FILE</h1>


        <h3>Direct redirection to the google document viewer</h3>
       

       <!-- It will redirect to the google document viewer -->
       <a href="https://docs.google.com/viewer?url=http://yoursitename.com/uploadfoldername/filename.doc">View File</a>

       <br/><br/>
       <h3>Integrating Gogle viewer to Our Project</h3>
       

       <!-- Redirect to our own next page there we will view the document -->
       <a href="resume_view.php?doc=filename.doc">File view in our page</a>
    </body>
</html>




----------------------------------------------------------------------------------------------------------------------------------
2.resume_view.php (Integrating Google document viewer to our page)
----------------------------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Document Viewer/Downloads</title>

    </head>
    <body style="margin:auto;">
        <div id="container">
            <div class="header1">
                <h1>DOCUMENT VIEWER AND DOWNLOADS</h1>
            </div>


            <?php
                    // Getting the file name
                    if(isset($_REQUEST['doc']))
                    {
                       $filename    =   $_REQUEST['doc'];
                    }

            ?>
            <div class="content">

             <!--Generate the iframe from google document viewer and copy iframe to our page-->
             <iframe src="http://docs.google.com/viewer?url=http%3A%2F%2Fyoursitename.com%2Ffoldername%2F<?php echo  $filename ?>&embedded=true" width="75%" height="1000" style="border: none;"></iframe>
            </div>
       </div>
    </body>
</html>

 

HOW TO DOWNLOAD FILE IN PHP?

                   
                                                                                                  Rahul R 
                                                                                                                        rahul9629@gmail.com
------------------------------------------------------------------------------------------------------------------------------------

HTML SECTION WITH LINK FOR DOWNLOAD FILE

<html>
    <head>
        <title>Download File</title>
    </head>
    <body>
        <!--Link For Downloading Resume -->
        <a href="download.php?doc=coding stds in brief.doc">Download</a> 
    </body>
</html>

 

PHP SECTION FOR DOWNLOAD

<?php


if(isset($_REQUEST['doc']))
   
     // change the path to fit your websites document structure 
     $path = "uploads/";
   
    //Getting filename and seeting the path like uploads/coding stds in brief.doc
    $fullPath = $path.$_GET['doc'];  


    if ($fd = fopen ($fullPath, "r"))
    {
        $fsize = filesize($fullPath);
        $path_parts = pathinfo($fullPath);
        $ext = strtolower($path_parts["extension"]);
        switch ($ext)
        {
            case "pdf":
            header("Content-type: application/pdf"); // add here more headers for diff. extensions
            header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
            break;
            default;
            header("Content-type: application/octet-stream");
            header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
        }
        header("Content-length: $fsize");
        header("Cache-control: private"); //use this to open files directly
        while(!feof($fd))
        {
        $buffer = fread($fd, 2048);
        echo $buffer;
        }
    }
    fclose ($fd);
    exit;
}
?>