Jump to content

Recommended Posts

I am trying to coonect my jquery price slider to my database in order to search for recipes depending on the price. I have written the code below but I am not sure were i am going wrong. Could anyone help?

 

<!--Javascript code for jquery price range slider-->

 

<script type="text/javascript">

$(function() {

    $( "#slider-range" ).slider({

      range: true,

      min: 0,

      max: 10,

      values: [ <?php echo $min?>,<?php echo $max?> ], // This line could be the issue?

      slide: function( event, ui ) {

        $( "#amount" ).val( "£" + ui.values[ 0 ] + " - £" + ui.values[ 1 ] );

      }

    });

    $( "#amount" ).val( "£" + $( "#slider-range" ).slider( "values", 0 ) +

      " - £" + $( "#slider-range" ).slider( "values", 1 ) );

  });

</script>

 

 

<!--php code to connect to jquery price slider-->

 

<?php

 

require_once './config.php';

include './header.php';

include('database.php');

 

 

if($_POST && isset($_POST['amount'])){

$values = $_POST['amount'];

 

$values = str_replace(array(' ', '£'), '', $_POST['amount']);

list($min, $max) = explode('-', $values);

$sql = "SELECT `recipe_name`, `recipe_price`,  `Image` 

        FROM `recipe` 

        WHERE `recipe_price` BETWEEN :min AND :max";

$stmt = $DB->prepare($sql);

$stmt->execute(array(':min' => $min, ':max' => $max));

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (count($rows) > 0) {

    foreach ($rows as $row) {

        // do loop stuff

    }

} else {

    $min = 0;

    $max = 10;

    $HTML = '';

}

}

 

?>

 

<!--HTML code for price slider -->

 

 <form action="" method="post" id="recipe">

<div style="margin-left:20px">

    <label for="amount">Price range:</label>

    <input type="text" id="amount" name="amount" style="border:0; color:#f6931f; font-weight:bold;" readonly>

    <br><br>

    <div id="slider-range" style="width:50%;"></div>

    <br><br>

    <input type="submit" value="Find" />

    <br><br>

    <?php echo $HTML?>

    

</div>

</form>

 

 

 <!-- connnect to Database - PDO connection-->

 

 <?php

 

error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);

ob_start();

 

define('DB_DRIVER', 'mysql');

define('DB_SERVER', 'localhost');

define('DB_SERVER_USERNAME', 'xxxxx');

define('DB_SERVER_PASSWORD', 'xxxx');

define('DB_DATABASE', 'xxxxx');

 

 

define('PROJECT_NAME', 'BudGet Recipes');

$dboptions = array(

    PDO::ATTR_PERSISTENT => FALSE,

    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,

    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,

    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',

);

 

try {

  $DB = new PDO(DB_DRIVER . ':host=' . DB_SERVER . ';dbname=' . DB_DATABASE, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, $dboptions);

} catch (Exception $ex) {

  echo $ex->getMessage();

  die;

}

?>
Edited by Hannah123456
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.