jquery select change event

i have select element on my page

<select id="Sort By" class="form-control" style="width:250px;">
                        <option>Select One</option>
                        <option value="1">Star Rating (1-5)</option>
                        <option value="2">Star Rating (5-1)</option>
                        </select>

Open in new window


i need to search in data twice if change to val 1

$sqlpackages="SELECT * FROM packages ORDER BY Package_Price ASC ";

if change to val 2

$sqlpackages="SELECT * FROM packages ORDER BY Package_Price DSC";
AHMED SAMYownerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Hey Ahmed,

Take a look at this to see if it makes sense.

//HTML
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Chris Stanyon</title>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            $('#sortBy').change(function() {
                $.get('data.php', { sort: $(this).val() }, function(data) {
                    $('#response').html(data);
                });
            });
        }); 
        </script>
    </head>

    <body>

        <select id="sortBy" class="form-control" style="width:250px;">
            <option>Select One</option>
            <option value="1">Star Rating (1-5)</option>
            <option value="2">Star Rating (5-1)</option>
        </select>

        <div id="response"></div>

    </body>
</html>

Open in new window

And the data.php file for searching the DB:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
 
$hostname = 'localhost';
$username = 'yourUser';
$password = 'yourPass';
$database = 'yourDb';

$dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

switch ($_GET['sort']):
    case "1":
        $sql = "SELECT * FROM packages ORDER BY Package_Price ASC";
        break;
    case "2":
        $sql = "SELECT * FROM packages ORDER BY Package_Price DESC";
        break;
    default:
        $sql = "SELECT * FROM packages ORDER BY Package_Price";
endswitch;

var_dump($dbh->query($sql)->fetchAll());

Open in new window

You'll probably need to adapt it to suit your needs, but hopefully it should get you started. If you're unsure about anything, just ask :)
AHMED SAMYownerAuthor Commented:
it is good but it bring all and not arranging them like that
array(13) { [0]=> object(stdClass)#3 (36) { ["package_id"]=> string(3) "512" ["package_name"]=> string(19) "gfdsgsdffdgsdfgsdfg" ["Detailed_Itinerary"]=> string(15) "gdfgfdgfdsgfdgs" ["Inclusion"]=> string(17) "gsdfgdfgdfgsdfgbr" ["Exclusion"]=> string(14) "gsdfgdfgsdfgbr" ["Important_Note"]=> string(20) "gdfsgdsfgdfgsdfgdsbr" ["Trip_Duration"]=> string(4) "1DAY" ["Package_Price"]=> string(8) "66666666" ["Car_Used"]=> string(14) "trtrtrtrtrtrtr" ["type1"]=> string(6) "Hotels" ["HName1"]=> string(3) "fre" ["period1"]=> string(1) "4" ["Adults1"]=> string(0) "" ["meal1"]=> string(9) "Breakfast" ["type2"]=> string(6) "Hotels" ["HName2"]=> string(6) "hilton" ["period2"]=> string(1) "4" ["Adults2"]=> string(0) "" ["meal2"]=> string(16) "Breakfast/Dinner" ["type3"]=> string(6) "select" ["HName3"]=> string(3) "fre" ["period3"]=> string(0) "" ["Adults3"]=> string(0) "" ["meal3"]=> string(0) "" ["type4"]=> string(6) "select" ["HName4"]=> string(3) "fre" ["period4"]=> string(0) "" ["Adults4"]=> string(0) "" ["meal4"]=> string(0) "" ["type5"]=> string(6) "select" ["HName5"]=> string(3) "fre" ["period5"]=> string(0) "" ["Adults5"]=> string(0) "" ["meal5"]=> string(0) "" ["valid_date"]=> string(10) "12/01/2017" ["publisher_name"]=> string(3) "aly" } [1]=> object(stdClass)#4 (36) { ["package_id"]=> string(3) "503" ["package_name"]=> string(7) "trtrtrt" ["Detailed_Itinerary"]=> string(8) "trtrtrtr" ["Inclusion"]=> string(6) "trtrtr" ["Exclusion"]=> string(10) "trtrtrtrbr" ["Important_Note"]=> string(9) "trtrtrtbr" ["Trip_Duration"]=> string(17) "gfdgfdgfgfdgfdgfg" ["Package_Price"]=> string(7) "6666666" ["Car_Used"]=> string(12) "rererererere" ["type1"]=> string(6) "Hotels" ["HName1"]=> string(6) "hilton" ["period1"]=> string(5) "54545" ["Adults1"]=> string(1) "1" ["meal1"]=> string(16) "Breakfast/Dinner" ["type2"]=> string(6) "select" ["HName2"]=> string(3) "fre" ["period2"]=> string(0) "" ["Adults2"]=> string(0) "" ["meal2"]=> string(0) "" ["type3"]=> string(6) "select" ["HName3"]=> string(3) "fre" ["period3"]=> string(0) "" ["Adults3"]=> string(0) "" ["meal3"]=> string(0) "" ["type4"]=> string(6) "select" ["HName4"]=> string(3) "fre" ["period4"]=> string(0) "" ["Adults4"]=> string(0) "" ["meal4"]=> string(0) "" ["type5"]=> string(6) "select" ["HName5"]=> string(3) "fre" ["period5"]=> string(0) "" ["Adults5"]=> string(0) "" ["meal5"]=> string(0) "" ["valid_date"]=> string(10) "12/05/2017" ["publisher_name"]=> string(3) "aly" } [2]=> object(stdClass)#5 (36) { ["package_id"]=> string(3) "501" ["package_name"]=> string(10) "gfdgfdgfdg" ["Detailed_Itinerary"]=> string(13) "gfdgfdgfdgdfg" ["Inclusion"]=> string(9) "gfdgfdgdf" ["Exclusion"]=> string(9) "gdfgdfgdf" ["Important_Note"]=> string(9) "gdfgfdgfd" ["Trip_Duration"]=> string(11) "2days1night" ["Package_Price"]=> string(6) "434324" ["Car_Used"]=> string(3) "BMW" ["type1"]=> string(6) "Hotels" ["HName1"]=> string(6) "hilton" ["period1"]=> string(5) "43432" ["Adults1"]=> string(1) "1" ["meal1"]=> string(9) "Breakfast" ["type2"]=> string(6) "select" ["HName2"]=> string(6) "select" ["period2"]=> string(0) "" ["Adults2"]=> string(0) "" ["meal2"]=> string(0) "" ["type3"]=> string(6) "select" ["HName3"]=> string(6) "select" ["period3"]=> string(0) "" ["Adults3"]=> string(0) "" ["meal3"]=> string(0) "" ["type4"]=> string(6) "select" ["HName4"]=> string(6) "select" ["period4"]=> string(0) "" ["Adults4"]=> string(0) "" ["meal4"]=> string(0) "" ["type5"]=> string(6) "select" ["HName5"]=> string(0) "" ["period5"]=> string(0) "" ["Adults5"]=> string(0) "" ["meal5"]=> string(0) "" ["valid_date"]=> string(10) "12/19/2017" ["publisher_name"]=> string(3) "aly" } [3]=> object(stdClass)#6 (36) { ["package_id"]=> string(3) "508" ["package_name"]=> string(29) "ededededededeedededededededed" ["Detailed_Itinerary"]=> string(27) "deddddddddddddddddddddddddd" ["Inclusion"]=> string(30) "ddddddddddddddddddddddddddddbr" ["Exclusion"]=> string(33) "dddddddddddddddddddddddddddddddbr" ["Important_Note"]=> string(30) "ddddddddddddddddddddddddddddbr" ["Trip_Duration"]=> string(58) "trtrtrtrtrtrttrtrtrtrttrtttttttttttttttttttttttttttttttttt" ["Package_Price"]=> string(5) "44444" ["Car_Used"]=> string(12) "rererererere" ["type1"]=> string(6) "Hotels" ["HName1"]=> string(3) "fre" ["period1"]=> string(5) "44444" ["Adults1"]=> string(1) "2" ["meal1"]=> string(16) "Breakfast/Dinner" ["type2"]=> string(6) "select" ["HName2"]=> string(3) "fre" ["period2"]=> string(0) "" ["Adults2"]=> string(0) "" ["meal2"]=> string(0) "" ["type3"]=> string(6) "select" ["HName3"]=> string(3) "fre" ["period3"]=> string(0) "" ["Adults3"]=> string(0) "" ["meal3"]=> string(0) "" ["type4"]=> string(6) "select" ["HName4"]=> string(3) "fre" ["period4"]=> string(0) "" ["Adults4"]=> string(0) "" ["meal4"]=> string(0) "" ["type5"]=> string(6) "select" ["HName5"]=> string(3) "fre" ["period5"]=> string(0) "" ["Adults5"]=> string(0) "" ["meal5"]=> string(0) "" ["valid_date"]=> string(10) "12/26/2017" ["publisher_name"]=> string(3) "aly" } [4]=> object(stdClass)#7 (36) { ["package_id"]=> string(3) "502" ["package_name"]=> string(15) "yyyyyyyyyyyyyyy" ["Detailed_Itinerary"]=> string(15) "yyyyyyyyyyyyyyy" ["Inclusion"]=> string(10) "yyyyyyyyyy" ["Exclusion"]=> string(13) "yyyyyyyyyyybr" ["Important_Note"]=> string(7) "yyyyybr" ["Trip_Duration"]=> string(58) "trtrtrtrtrtrttrtrtrtrttrtttttttttttttttttttttttttttttttttt" ["Package_Price"]=> string(5) "43434" ["Car_Used"]=> string(12) "rererererere" ["type1"]=> string(5) "Boats" ["HName1"]=> string(6) "hilton" ["period1"]=> string(4) "4545" ["Adults1"]=> string(1) "1" ["meal1"]=> string(16) "Breakfast/Dinner" ["type2"]=> string(6) "select" ["HName2"]=> string(3) "fre" ["period2"]=> string(0) "" ["Adults2"]=> string(0) "" ["meal2"]=> string(0) "" ["type3"]=> string(6) "select" ["HName3"]=> string(3) "fre" ["period3"]=> string(0) "" ["Adults3"]=> string(0) "" ["meal3"]=> string(0) "" ["type4"]=> string(6) "select" ["HName4"]=> string(3) "fre" ["period4"]=> string(0) "" ["Adults4"]=> string(0) "" ["meal4"]=> string(0) "" ["type5"]=> string(6) "select" ["HName5"]=> string(3) "fre" ["period5"]=> string(0) "" ["Adults5"]=> string(0) "" ["meal5"]=> string(0) "" ["valid_date"]=> string(10) "12/27/2017" ["publisher_name"]=> string(3) "aly" } [5]=> object(stdClass)#8 (36) { ["package_id"]=> string(3) "506" ["package_name"]=> string(5) "qqqqq" ["Detailed_Itinerary"]=> string(5) "qqqqq" ["Inclusion"]=> string(20) "qqqqqqqqqqqqqqqqqqbr" ["Exclusion"]=> string(17) "qqqqqqqqqqqqqqqbr" ["Important_Note"]=> string(21) "qqqqqqqqqqqqqqqqqqqbr" ["Trip_Duration"]=> string(58) "trtrtrtrtrtrttrtrtrtrttrtttttttttttttttttttttttttttttttttt" ["Package_Price"]=> string(5) "22000" ["Car_Used"]=> string(12) "rererererere" ["type1"]=> string(6) "select" ["HName1"]=> string(3) "fre" ["period1"]=> string(0) "" ["Adults1"]=> string(0) "" ["meal1"]=> string(0) "" ["type2"]=> string(6) "select" ["HName2"]=> string(3) "fre" ["period2"]=> string(0) "" ["Adults2"]=> string(0) "" ["meal2"]=> string(0) "" ["type3"]=> string(6) "select" ["HName3"]=> string(3) "fre" ["period3"]=> string(0) "" ["Adults3"]=> string(0) "" ["meal3"]=> string(0) "" ["type4"]=> string(6) "select" ["HName4"]=> string(3) "fre" ["period4"]=> string(0) "" ["Adults4"]=> string(0) "" ["meal4"]=> string(0) "" ["type5"]=> string(6) "select" ["HName5"]=> string(3) "fre" ["period5"]=> string(0) "" ["Adults5"]=> string(0) "" ["meal5"]=> string(0) "" ["valid_date"]=> string(0) "" ["publisher_name"]=> string(3) "aly" } [6]=> object(stdClass)#9 (36) { ["package_id"]=> string(3) "507" ["package_name"]=> string(13) "tyrutyrfufnyu" ["Detailed_Itinerary"]=> string(11) "yuyuyervuer" ["Inclusion"]=> string(0) "" ["Exclusion"]=> string(6) "bfgrbr" ["Important_Note"]=> string(7) "cnurebr" ["Trip_Duration"]=> string(17) "gfdgfdgfgfdgfdgfg" ["Package_Price"]=> string(5) "16000" ["Car_Used"]=> string(12) "rererererere" ["type1"]=> string(6) "select" ["HName1"]=> string(3) "fre" ["period1"]=> string(0) "" ["Adults1"]=> string(0) "" ["meal1"]=> string(0) "" ["type2"]=> string(6) "select" ["HName2"]=> string(3) "fre" ["period2"]=> string(0) "" ["Adults2"]=> string(0) "" ["meal2"]=> string(0) "" ["type3"]=> string(6) "select" ["HName3"]=> string(3) "fre" ["period3"]=> string(0) "" ["Adults3"]=> string(0) "" ["meal3"]=> string(0) "" ["type4"]=> string(6) "select" ["HName4"]=> string(3) "fre" ["period4"]=> string(0) "" ["Adults4"]=> string(0) "" ["meal4"]=> string(0) "" ["type5"]=> string(6) "select" ["HName5"]=> string(3) "fre" ["period5"]=> string(0) "" ["Adults5"]=> string(0) "" ["meal5"]=> string(0) "" ["valid_date"]=> string(0) "" ["publisher_name"]=> string(3) "aly" } [7]=> object(stdClass)#10 (36) { ["package_id"]=> string(3) "511" ["package_name"]=> string(5) "cairo" ["Detailed_Itinerary"]=> string(35) "homefhghghgh hgh h hg gh gh gh gh g" ["Inclusion"]=> string(23) "gfgfdgdgfdgsdgdfgsdfgbr" ["Exclusion"]=> string(15) "gdfgfsfdgsdfgbr" ["Important_Note"]=> string(19) "gsdfgsdfgsdfgsdfgbr" ["Trip_Duration"]=> string(12) "3days2nights" ["Package_Price"]=> string(5) "14000" ["Car_Used"]=> string(14) "trtrtrtrtrtrtr" ["type1"]=> string(6) "Hotels" ["HName1"]=> string(6) "hilton" ["period1"]=> string(1) "3" ["Adults1"]=> string(0) "" ["meal1"]=> string(22) "Breakfast/Lunch/Dinner" ["type2"]=> string(6) "select" ["HName2"]=> string(3) "fre" ["period2"]=> string(0) "" ["Adults2"]=> string(0) "" ["meal2"]=> string(0) "" ["type3"]=> string(6) "select" ["HName3"]=> string(3) "fre" ["period3"]=> string(0) "" ["Adults3"]=> string(0) "" ["meal3"]=> string(0) "" ["type4"]=> string(6) "select" ["HName4"]=> string(3) "fre" ["period4"]=> string(0) "" ["Adults4"]=> string(0) "" ["meal4"]=> string(0) "" ["type5"]=> string(6) "select" ["HName5"]=> string(3) "fre" ["period5"]=> string(0) "" ["Adults5"]=> string(0) "" ["meal5"]=> string(0) "" ["valid_date"]=> string(10) "12/01/2017" ["publisher_name"]=> string(3) "aly" } [8]=> object(stdClass)#11 (36) { ["package_id"]=> string(3) "513" ["package_name"]=> string(4) "giza" ["Detailed_Itinerary"]=> string(15) "giza home home" ["Inclusion"]=> string(9) "home home" ["Exclusion"]=> string(9) "home home" ["Important_Note"]=> string(9) "home home" ["Trip_Duration"]=> string(15) "3 days 2 nights" ["Package_Price"]=> string(4) "4500" ["Car_Used"]=> string(14) "trtrtrtrtrtrtr" ["type1"]=> string(6) "Hotels" ["HName1

Open in new window

AHMED SAMYownerAuthor Commented:
and also need to hide another select
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Chris StanyonWebDevCommented:
Hey Ahmed,

Yeah - like I said, it will need to be adapted to suit your own needs.

You haven't said how you want the data to be shown when it's returned from the DB, so we may need more information. Depending on how you want to show the data, you can either handle it in the PHP script, or you can handle it in the jQuery script.

Here's a quick demo on how to loop through your DB results in PHP:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
 
$hostname = 'localhost';
$username = 'yourUser';
$password = 'yourPass';
$database = 'yourDb';

$dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

switch ($_GET['sort']):
    case "1":
        $sql = "SELECT * FROM packages ORDER BY Package_Price ASC";
        break;
    case "2":
        $sql = "SELECT * FROM packages ORDER BY Package_Price DESC";
        break;
    default:
        $sql = "SELECT * FROM packages ORDER BY Package_Price";
endswitch;

$result = $dbh->query($sql);

// Loop through the results
while ($row = $result->fetch()): ?>

<div>
    <h1><?php echo $row->package_name ?></h1>
    <p><?php echo $row->Important_Note ?></p>
</div>

<?php endwhile; ?>

Open in new window

No idea what you mean by "hide another select"
AHMED SAMYownerAuthor Commented:
this is data.php

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
 
	$localhost="*******";
	$db_users="******";
	$db_password="******";
	$db_name="*******";

$dbh = new PDO("mysql:host=$localhost;dbname=$db_name", $db_users, $db_password);
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);


switch ($_GET['sort']):
    case "1":
        $sql = "SELECT * FROM packages ORDER BY Package_Price ASC";
        break;
    case "2":
        $sql = "SELECT * FROM packages ORDER BY Package_Price DESC";
        break;
    default:
        $sql = "SELECT * FROM packages ORDER BY Package_Price";
endswitch;

$result = $dbh->query($sql);

// Loop through the results
while ($row = $result->fetch()): ?>

<div class="row" id="response">
        <div class="col-lg-6 portfolio-item">
          <div class="card h-100">
            <a href="ViewDetails.php?id=<?php echo $row->package_id ?>">
            	<?php
					$pac_name=$row->package_id;
					$sqlimg="SELECT content FROM packages_image WHERE packageID='$pac_name' LIMIT 1";
					$queryimg=mysql_query($sqlimg);
					while($resultimg=mysql_fetch_array($queryimg)){
						echo '<a href="#"><img src="data:image/png;base64,'.base64_encode( $resultimg['content'] ).'" width="100%" height="350"/></a>';
					}
				?>
            </a>
            <div class="card-body">
              <h4 class="card-title">
                <a href="ViewDetails.php?id=<?php echo $row['package_id']; ?>"><?php echo $row['package_name']; ?></a>
              </h4>
              <h3 class="breadcrumb" style="color:#C00; text-align:center"><?php echo $row['Package_Price']; ?> euro.&nbsp;</h3>
              <p class="card-text"><?php echo substr($row['Detailed_Itinerary'],0,150);?></p>
              <a href="ViewDetails.php?id=<?php echo $row['package_id']; ?>" class="btn btn-primary">More Details</a>
            </div>
          </div>
        </div>
        
      </div>

<?php endwhile; ?>

Open in new window


and this is new1.php
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Chris Stanyon</title>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            $('#sortBy').change(function() {
                $.get('data.php', { sort: $(this).val() }, function(data) {
                    $('#response').html(data);
                });
            });
        }); 
        </script>
    </head>

    <body>

        <select id="sortBy" class="form-control" style="width:250px;">
            <option>Select One</option>
            <option value="1">Star Rating (1-5)</option>
            <option value="2">Star Rating (5-1)</option>
        </select>

     <div class="row" id="response">
        <div class="col-lg-6 portfolio-item">
          <div class="card h-100">
            <a href="ViewDetails.php?id=<?php echo $row->package_id ?>">
            	<?php
					$pac_name=$row->package_id;
					$sqlimg="SELECT content FROM packages_image WHERE packageID='$pac_name' LIMIT 1";
					$queryimg=mysqli_query($connect,$sqlimg);
					while($resultimg=mysqli_fetch_array($queryimg)){
						echo '<a href="#"><img src="data:image/png;base64,'.base64_encode( $resultimg['content'] ).'" width="100%" height="350"/></a>';
					}
				?>
            </a>
            <div class="card-body">
              <h4 class="card-title">
                <a href="ViewDetails.php?id=<?php echo $row['package_id']; ?>"><?php echo $row['package_name']; ?></a>
              </h4>
              <h3 class="breadcrumb" style="color:#C00; text-align:center"><?php echo $row['Package_Price']; ?> euro.&nbsp;</h3>
              <p class="card-text"><?php echo substr($row['Detailed_Itinerary'],0,150);?></p>
              <a href="ViewDetails.php?id=<?php echo $row['package_id']; ?>" class="btn btn-primary">More Details</a>
            </div>
          </div>
        </div>
        
      </div>   

    </body>
</html>

Open in new window


and this is the result in new1.php

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\AppServ\www\egitto\new1.php on line 31

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\AppServ\www\egitto\new1.php on line 32

and when i use option "1" this is the result



Notice: Undefined variable: connect in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\AppServ\www\egitto\data.php on line 33

Notice: Undefined variable: connect in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\AppServ\www\egitto\data.php on line 33

Notice: Undefined variable: connect in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\AppServ\www\egitto\data.php on line 33

Notice: Undefined variable: connect in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\AppServ\www\egitto\data.php on line 33

Notice: Undefined variable: connect in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\AppServ\www\egitto\data.php on line 33

Notice: Undefined variable: connect in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\AppServ\www\egitto\data.php on line 33

Notice: Undefined variable: connect in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\AppServ\www\egitto\data.php on line 33

Notice: Undefined variable: connect in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\AppServ\www\egitto\data.php on line 33

Notice: Undefined variable: connect in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\AppServ\www\egitto\data.php on line 33

Notice: Undefined variable: connect in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\AppServ\www\egitto\data.php on line 33

Notice: Undefined variable: connect in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\AppServ\www\egitto\data.php on line 33

Notice: Undefined variable: connect in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\AppServ\www\egitto\data.php on line 33

Notice: Undefined variable: connect in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\AppServ\www\egitto\data.php on line 32

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\AppServ\www\egitto\data.php on line 33
AHMED SAMYownerAuthor Commented:
if i understand the code i can do or help you
Chris StanyonWebDevCommented:
Hi Ahmed,

There are a few issues with your code, so I'll try and explain. Firstly, all the Database stuff should be done in the data.php file and not in the new1.php file, so you should remove all of that. The idea is that all the HTML is generated in data.php and then sent back to new1.php and inserted into the #response DIV.

The code I posted is using PDO for the database access but you are trying to mysqli - that won't work - you need to stick with PDO.

Because you are executing your image query inside a while loop, it makes more sense to use what's called a 'prepared' query. This is a lot quicker than the way you're doing it. Alternatively, you could use a JOIN to execute a single query instead of running 2 separate ones.

Take a look at these 2 files, and if you have any questions, just ask:

// HTML
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Chris Stanyon // EE - 29073129</title>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            $('#sortBy').change(function() {
                $.get('data.php', { sort: $(this).val() }, function(data) {
                    $('#response').html(data);
                });
            });
        }); 
        </script>
    </head>
    <body>

        <select id="sortBy" class="form-control" style="width:250px;">
            <option>Select One</option>
            <option value="1">Star Rating (1-5)</option>
            <option value="2">Star Rating (5-1)</option>
        </select>

        <div id="response"></div>

    </body>
</html>

Open in new window

//PHP
<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

$localhost   = "*******";
$db_users    = "******";
$db_password = "******";
$db_name     = "*******";

$dbh = new PDO("mysql:host=$localhost;dbname=$db_name", $db_users, $db_password);
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

switch ($_GET['sort']):

    case "1":
        $sql = "SELECT * FROM packages ORDER BY Package_Price ASC";
        break;

    case "2":
        $sql = "SELECT * FROM packages ORDER BY Package_Price DESC";
        break;

    default:
        $sql = "SELECT * FROM packages ORDER BY Package_Price";

endswitch;

// Prepare the IMG query to be used later on
$imgQuery = $dbh->prepare("SELECT content FROM packages_image WHERE packageID = ? LIMIT 1");

// Execute the Packages query
$packages = $dbh->query($sql);

// Loop through the Package results
while ($package = $packages->fetch()): ?>

    <?php
    // Get the image data from the prepared query
    $imgQuery->execute(array($package->package_id));
    $imgData = $imgQuery->fetchColumn();
    ?>

    <div class="row">
        <div class="col-lg-6 portfolio-item">
            <div class="card h-100">
                <a href="ViewDetails.php?id=<?php echo $package->package_id ?>">
                    <img src="data:image/png;base64,<?php base64_encode($imgData) ?>" width="100%" height="350"/>
                </a>
                <div class="card-body">
                    <h4 class="card-title">
                        <a href="ViewDetails.php?id=<?php echo $package->package_id ?>"><?php echo $package->package_name ?></a>
                    </h4>
                    <h3 class="breadcrumb" style="color:#C00; text-align:center"><?php echo $package->package_price ?> euro.</h3>
                    <p class="card-text"><?php echo substr($package->detailed_itinerary, 0, 150) ?></p>
                    <a href="ViewDetails.php?id=<?php echo $package->package_id ?>" class="btn btn-primary">More Details</a>
                </div>
            </div>
        </div>
    </div>

<?php endwhile; ?>

Open in new window

AHMED SAMYownerAuthor Commented:
Notice: Undefined property: stdClass::$package_price in /home/content/74/8021074/html/egitto2017/data.php on line 53

and the image not appears as well
Untitled.png
Chris StanyonWebDevCommented:
Hey Ahmed,

PHP Is case-sensitive, so instead of

$package->package_price
$package->detailed_itinerary

you need:

$package->Package_Price
$package->Detailed_Itinerary

Notice the uppercase letters :)
AHMED SAMYownerAuthor Commented:
thanks ..
every thing is done but i have two thing to do

first image problem
second i need when load page to do default don' make it clear
AHMED SAMYownerAuthor Commented:
thank you
it is working now i need to make default 1-5 when loading page and when he select make query

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
 
	$localhost="*****";
	$db_users="*****";
	$db_password="******";
	$db_name="*******";

$dbh = new PDO("mysql:host=$localhost;dbname=$db_name", $db_users, $db_password);
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

switch ($_GET['sort']):

    case "1":
        $sql = "SELECT * FROM packages ORDER BY Package_Price ASC";
        break;

    case "2":
        $sql = "SELECT * FROM packages ORDER BY Package_Price DESC";
        break;

    default:
        $sql = "SELECT * FROM packages ORDER BY Package_Price";

endswitch;

// Prepare the IMG query to be used later on
$imgQuery = $dbh->prepare("SELECT content FROM packages_image WHERE packageID = ? LIMIT 1");

// Execute the Packages query
$packages = $dbh->query($sql);

// Loop through the Package results
while ($package = $packages->fetch()): ?>

    <?php
    // Get the image data from the prepared query
    $imgQuery->execute(array($package->package_id));
    $imgData = $imgQuery->fetchColumn();
    ?>

    <div class="row">
        <div class="col-lg-6 portfolio-item">
            <div class="card h-100">
                <a href="ViewDetails.php?id=<?php echo $package->package_id ?>">
                  <?php  echo '<a href="#"><img src="data:image/jpeg;base64,'.base64_encode($imgData).'" width="140px" height="140px"/></a>'; ?>
                </a>
                <div class="card-body">
                    <h4 class="card-title">
                        <a href="ViewDetails.php?id=<?php echo $package->package_id ?>"><?php echo $package->package_name ?></a>
                    </h4>
                  <h3 class="breadcrumb" style="color:#C00; text-align:center"><?php echo $package->Package_Price ?> euro.</h3>
                    <p class="card-text"><?php echo substr($package->Detailed_Itinerary, 0, 150) ?></p>
                    <a href="ViewDetails.php?id=<?php echo $package->package_id ?>" class="btn btn-primary">More Details</a>
                </div>
            </div>
        </div>
    </div>

<?php endwhile; ?>

Open in new window


and page preview is :

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Chris Stanyon // EE - 29073129</title>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            $('#sortBy').change(function() {
                $.get('data.php', { sort: $(this).val() }, function(data) {
                    $('#response').html(data);
                });
            });
        }); 
        </script>
    </head>
    <body>

        <select id="sortBy" class="form-control" style="width:250px;">
            <option value="2">Star Rating (1-5)</option>
            <option value="1">Star Rating (5-1)</option>
        </select>

        <div id="response"></div>

    </body>
</html>

Open in new window

Chris StanyonWebDevCommented:
For the image problem, it looks like I missed out the echo. Should be:

<img src="data:image/png;base64,<?php echo base64_encode($imgData) ?>" width="100%" height="350"/>

And to show data when your page first loads, the simplest way is just to trigger the change event on the dropdown:

$(document).ready(function() {
    $('#sortBy').change(function() {
        $.get('data.php', { sort: $(this).val() }, function(data) {
            $('#response').html(data);
        });
    });

    // Trigger the change event when the page loads
    $('#sortBy').trigger('change');
});

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AHMED SAMYownerAuthor Commented:
good work
AHMED SAMYownerAuthor Commented:
suppose i need to make search with button to filter this data i will  bring this data

$Duration=$_GET['Duration'];
			$rooms=$_GET['rooms'];
			$Adults=$_GET['Adults'];
			$children=$_GET['children'];
			$agechildren=$_GET['agechildren'];
			$carused=$_GET['carused'];
			$mealplan=$_GET['mealplan'];
			$category=$_GET['category'];
			$date=filter_var($_GET['datepicker'],FILTER_SANITIZE_STRING,FILTER_FLAG_STRIP_HIGH);
			$tab=filter_var($_GET['tab'],FILTER_SANITIZE_STRING,FILTER_FLAG_STRIP_HIGH);

Open in new window


how i can do
Chris StanyonWebDevCommented:
Hey Ahmed,

I don't know what you mean, but it's probably worth asking a new question, as this seems like a different issue to your original question.
AHMED SAMYownerAuthor Commented:
Hey Expert Comment

i put it here to continue the last

i have search div on top of screen has this items and this code , how it should be to make search button result not conflict with the last code

if($_POST['Modify']){
			
			$Travel=$_POST['Travel'];
			$Duration=$_POST['Duration'];
			$tab=$_POST['tab'];
			$Categories=$_POST['Categories'];
			$rooms=$_POST['rooms'];
			$Adults=$_POST['Adults'];
			$children=$_POST['children'];
			$agechildren=$_POST['agechildren'];
			$carused=$_POST['carused'];
			
			$sqlsearch="SELECT * FROM packages WHERE Trip_Duration LIKE '%$Travel%' OR Trip_Duration LIKE '%$Duration%' OR Detailed_Itinerary LIKE '%$tab%' OR HName1 LIKE '%$Categories%' OR HName2 LIKE '%$Categories%' OR Detailed_Itinerary LIKE '%$rooms%' OR Detailed_Itinerary LIKE '%$Adults%' OR Detailed_Itinerary LIKE '%$children%' OR Car_Used LIKE '%$carused%'";
			
			
			$querysearch=mysqli_query($connect,$sqlsearch);
			while($resultsearch=mysqli_fetch_array($querysearch)){
				
				$searchid=$resultsearch['package_id'];
				$sqlimg="SELECT content FROM packages_image WHERE packageID='$searchid' LIMIT 1";
				$imgQuery=mysqli_query($connect,$sqlimg);
				$imgData=mysqli_fetch_array($imgQuery);

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.