0

I have my company project which they require PHP4 development.Please check the complete code. It should display list of mobile brands and price and should filter according to multiple checkbox. As i have to work in PHP4 i dont know what mistake i am doing.Its not displaying the information from database.

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;
          }

          #phones {
            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: 300px;
          }

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

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

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

          #filter {
            float:left;
          }
        </style>
      </head>
      <body> 
        <h1>Phones database</h1>

        <table id="phones">
          <thead>
            <tr>
              <th>ID</th>
              <th>Brand</th>
              <th>Model</th>
              <th>Price</th>
            </tr>
          </thead>
          <tbody>
          </tbody>
        </table>

        <div id="filter">
          <h2>Filter options</h2>
          <div>
            <input type="checkbox" id="Samsung" checked>
            <label for="Samsung">Samsung</label>
          </div>
          <div>
            <input type="checkbox" id="iPhone" checked>
            <label for="iPhone">iPhone</label>
          </div>
          <div>
            <input type="checkbox" id="HTC" checked>
            <label for="HTC">HTC</label>
          </div>
          <div>
            <input type="checkbox" id="LG" checked>
            <label for="LG">LG</label>
          </div>
          <div>
            <input type="checkbox" id="Nokia" checked>
            <label for="Nokia">Nokia</label>
          </div>
        </div>

        <script src="http://code.jquery.com/jquery-latest.js"></script> 
        <script>
          function makeTable(data){
            console.log(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 getPhoneFilterOptions(){
            var opts = [];
            $checkboxes.each(function(){
              if(this.checked){
                opts.push(this.id);
              }
            });

            return opts;
          }

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

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

          $checkboxes.trigger("change");
        </script> 
      </body> 
    </html>

submit.php

<?php 
require 'Database.php';
#### TEMP SET NAMES FÜR UTF8 ###################################################
require_once('Json.php');

  $opts = $_POST['filterOpts'];
  foreach ($opts as &$opt) {
      $opt = sprintf("'%s'", mysql_real_escape_string($opt));
  }
  $query = sprintf(
      "SELECT mobile_phone.id, name, model, price
       FROM mobile_phone
       INNER JOIN brand ON brand_id = brand.id
       WHERE name IN (%s)",
      join(',', $opts)
  );

  $result = mysql_query($query);
  $data   = array();
  while ($row = mysql_fetch_assoc($result)) {
      $data[] = $row;
  }

  echo json_encode($data);
?>

Json enter link description here

1 Answer 1

0

json_encode OR json_decode functions are not included in PHP4, please use PHP4 json manipulation library.

PS: you have included Json.php, the library have not json_* functions, the Json.php file have Services_JSON class, please use this way

$jsonObj = new Services_JSON();
echo $jsonObj->encode($data);
Sign up to request clarification or add additional context in comments.

11 Comments

Hi, you mean i should not include Json.php file and use above line of code in submit.php file ? Thanks.
Hi,I tried removing Json.php file and including the above line of code u gave me but still not getting data..
@user3659737 Json.php file have JSON manipulation methods e.g encode, decode, please create object of Services_JSON class and call method by object., i have mentioned sample code in bottom
@user3659737 what you got ajax response, can you please share with me?
Hi,I am getting checkboxes filter options and just a table without any data from the database.Only the problem is i am not getting data from database.Otherwise i think my checkbox will filter the data once i get the data from database.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.