Jump to content

imgrooot

Members
  • Posts

    367
  • Joined

  • Last visited

  • Days Won

    1

imgrooot last won the day on May 16 2018

imgrooot had the most liked content!

Recent Profile Visitors

2,928 profile views

imgrooot's Achievements

Advanced Member

Advanced Member (4/5)

1

Reputation

30

Community Answers

  1. Alright so I figured it out. Based on your code, here's my new code. And in the backend-search.php, I had to change $_POST to $_GET. Now it works great. Thanks so much. <script type='text/javascript'> $().ready(function() { $('#display-cities').hide(); $("#city-box").keyup(function() { var term = $(this).val(); $.get("snippets/backend-search.php", {"term":term}, function(resp) { $("#display-cities").html("").show(); $.each(resp, function(k,v) { var citydata = '<div class="output-results"><p class="output-p1">' + v.cityname + '</p><p class="output-p2">' + v.provincename + ', '+ v.countryname + '</p></div>'; var citydata2 = '' + v.cityname + ', ' + v.provincename + ', ' + v.countryname + ''; $("#display-cities").append(citydata); $( ".output-results" ).click(function() { $("#city-box-id").val(val.cityid); $("#city-box").val(citydata2); $("#display-cities").html(citydata).hide(); }); }) }, "JSON" ) }) }) </script>
  2. I think I partially understand this code. Shouldn't there be ajax code somewhere that retrieves the city values? Can you please update my code with this solution? I'll keep trying to modify it myself in the mean time.
  3. Come on man, you could've told me this a few days ago. But I suppose did learn little more about arrays and json while rigorously working this problem. So it's all good. Thanks.
  4. Alright so I have a solution. Or at least half a solution. I found a way to access json array inside ajax code. Here's my updated code. <script> $(document).ready(function() { $('#display-cities').hide(); $("#city-box").keyup(function() { var name = $('#city-box').val(); if (name == "") { $('#display-cities').hide(); } else { $.ajax({ type: "POST", url: "snippets/backend-search.php", dataType: "json", cache: false, data: { term: name }, success: function(data) { var parsed = JSON.parse(JSON.stringify(data)); $.each(parsed, function (key, val) { var citydata = '<div class="output-results"><p class="output-p1">' + val.cityname + '</p><p class="output-p2">' + val.provincename + ', '+ val.countryname + '</p></div>'; var citydata2 = '' + val.cityname + ', ' + val.provincename + ', ' + val.countryname + ''; $("#display-cities").html(citydata).show(); $( ".output-results" ).click(function() { $("#city-box-id").val(val.cityid); $("#city-box").val(citydata2); $("#display-cities").html(citydata).hide(); }); }); } }); } }); }); </script> It works great. Now the only thing left is to display multiple city rows in the div dropdown.
  5. So I took a few days break and now back tackling this problem. I believe I understand what you're saying. So here's my new php code that encodes array of rows. Now the issue is that it's showing "undefined" value when I do the console log. I don't get any errors though. So the issue is now definitely with the ajax code. Can you edit it so I can see what I did wrong? // BACKEND-SEARCH.PHP if(isset($_POST["term"])){ $param_term = $_POST["term"] .'%'; $get_city_select = $db->prepare("SELECT cities.city_id, cities.city_name, provinces.province_id, provinces.province_name, countries.country_id, countries.country_name FROM cities LEFT JOIN provinces ON cities.province_id = provinces.province_id LEFT JOIN countries ON provinces.country_id = countries.country_id WHERE city_name LIKE :param"); $get_city_select->bindParam(':param', $param_term); $get_city_select->execute(); $result_city_select = $get_city_select->fetchAll(PDO::FETCH_ASSOC); if(count($result_city_select) > 0) { $arr = array(); //create empty array foreach($result_city_select as $row) { $s_city_id = $row['city_id']; $s_city_name = $row['city_name']; $s_province_id = $row['province_id']; $s_province_name = $row['province_name']; $s_country_id = $row['country_id']; $s_country_name = $row['country_name']; $arr[] = array ( "cityid" => "{$s_city_id}", "cityname" => "{$s_city_name}", "provincename" => "{$s_province_name}", "countryname" => "{$s_country_name}" ); } echo json_encode($arr); } } // AJAX CODE <script> $(document).ready(function() { $('#display-cities').hide(); $("#city-box").keyup(function() { var name = $('#city-box').val(); if (name == "") { $('#display-cities').hide(); } else { $.ajax({ type: "POST", url: "snippets/backend-search.php", dataType: "json", cache: false, data: { term: name }, success: function(data) { console.log(data.cityname); var citydata = '<div class="output-results"><p class="output-p1">' + data.cityname + '</p><p class="output-p2">' + data.provincename + ', '+ data.countryname + '</p></div>'; var citydata2 = '' + data.cityname + ', ' + data.provincename + ', ' + data.countryname + ''; $("#display-cities").html(citydata).show(); $( ".output-results" ).click(function() { $("#city-box-id").val(data.cityid); $("#city-box").val(citydata2); $("#display-cities").html(citydata).hide(); }); } }); } }); }); </script> EDIT: Btw when I console.log(data), it returns the values like this "(58) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]". So the data is working. It seems like I need a proper way to access these values inside an array.
  6. Actually I found the issue. My query works fine. The issue was that only 1 user in my database matched all three parameters. Which is why it was only showing a single result. So it's all good now. Having said that if you would like to modify my query to show your example, that'd be great.
  7. Back with a new problem. I have 8 tables interconnected. Table#1 - Users user_id | name Table#2 - user_categories id | user_id | category_id Table#3 - user_cities id | user_id | city_id Table#4 - user_dates id | user_id | dates_available Table#5 - categories category_id | category_name Table#6 - cities city_id | city_name Table#7 - provinces province_id | province_name Table#8 - categories country_id | country_name Each user will have multiple categories, cities and available dates listed in these tables. I simply want to retrieve and list each user and their data on a page. Here's my query. $url_city = 1; $url_category = 2; $url_date = '2021-07-19'; $find_records = $db->prepare("SELECT user_categories.*, categories.*, user_cities.*, cities.*, provinces.*, countries.*, user_dates.*, users.* FROM users LEFT JOIN user_categories ON users.user_id = user_categories.user_id LEFT JOIN user_cities ON users.user_id = user_cities.user_id LEFT JOIN user_dates ON users.user_id = user_dates.user_id LEFT JOIN categories ON user_categories.category_id = user_categories.category_id LEFT JOIN cities ON user_cities.city_id = cities.city_id LEFT JOIN provinces ON cities.province_id = provinces.province_id LEFT JOIN countries ON provinces.country_id = countries.country_id WHERE user_cities.city_id = :city_id AND user_categories.category_id = :category_id AND user_dates.date_available = :date_available GROUP BY users.user_id"); $find_records->bindParam(':city_id', $url_city); $find_records->bindParam(':category_id', $url_category); $find_records->bindParam(':date_available', $url_date); $find_records->execute(); $result_records = $find_records->fetchAll(PDO::FETCH_ASSOC); if(count($result_records) > 0) { foreach($result_records as $row) { $user_id = $row['user_id']; $name = $row['name']; $country_id = $row['country_id']; $country_code = $row['country_code']; $country_name = $row['country_name']; $province_id = $row['province_id']; $province_code = $row['province_code']; $province_name = $row['province_name']; $city_id = $row['city_id']; $city_name = $row['city_name']; $category_id = $row['category_id']; $category_name = $row['category_name']; } } There are no errors but the above query would only return a single row with only 1 "user" despite having multiple users in the "users" table. If I remove the GROUP BY, then it'll return multiple rows of the same user instead of all the relevant users. So what do you think I am doing wrong with my query?
  8. I understand what you're saying but I am unable to locate the extra space. I tried the same LIKE query outside of the ajax search and it does return more than a single result. It's only when used with the ajax that something goes wrong. Here's my full code. Perhaps you can spot something I'm doing wrong. INDEX.PHP <form action="" method="POST"> <input type="text" id="city-box" class="type-input" name="sel-city" autocomplete="off" value="" placeholder="Type City Name" /> <input type="hidden" id="city-box-id" name="sel-city-hidden" value=""/> <div id="display-cities"></div> </form> JAVASCRIPT <script> $(document).ready(function() { $('#display-cities').hide(); $("#city-box").keyup(function() { var name = $('#city-box').val(); if (name == "") { $('#display-cities').hide(); } else { $.ajax({ type: "POST", url: "snippets/backend-search.php", dataType: 'json', cache: false, data: { term: name }, success: function(data) { var citydata = '<div class="output-results"><p class="output-p1">' + data.cityname + '</p><p class="output-p2">' + data.provincename + ', '+ data.countryname + '</p></div>'; $("#display-cities").html(citydata).show(); $( ".output-results" ).click(function() { var id = $(this).attr('id'); $("#city-box-id").val(data.cityid); $("#city-box").val(data.cityname); $("#display-cities").html(citydata).hide(); }); } }); } }); }); </script> BACKEND-SEARCH.PHP if(isset($_POST["term"])){ $param_term = $_POST["term"] .'%'; $get_city_select = $db->prepare("SELECT cities.city_id, cities.city_name, provinces.province_id, provinces.province_name, countries.country_id, countries.country_name FROM cities LEFT JOIN provinces ON cities.province_id = provinces.province_id LEFT JOIN countries ON cities.country_id = countries.country_id WHERE city_name LIKE :param"); $get_city_select->bindParam(':param', $param_term); $get_city_select->execute(); $result_city_select = $get_city_select->fetchAll(PDO::FETCH_ASSOC); if(count($result_city_select) > 0) { foreach($result_city_select as $row) { $s_city_id = $row['city_id']; $s_city_name = $row['city_name']; $s_province_id = $row['province_id']; $s_province_name = $row['province_name']; $s_country_id = $row['country_id']; $s_country_name = $row['country_name']; echo json_encode (array("cityid" => "{$s_city_id}", "cityname" => "{$s_city_name}", "provincename" => "{$s_province_name}", "countryname" => "{$s_country_name}")); } } }
  9. If my input text was "foot", then the $param_term would result in "foot%";
  10. I am using ajax live search to retrieve data from MySQL table. It works fine. However I noticed that it only returns a single result that matches the entire word instead of multiple results that match only the first couple letters. For e.g. DB Table ---------------- 1. Foot 2. Foot Path 3. Football ---------------- If I type "foo" in the ajax search field, it won't return any results. But if I type "foot", it'll return "Foot". It won't return the other two results that contain the letters "Foot". Here's my query $param_term = $_POST["term"] . '%'; $get_data = $db->prepare("SELECT name FROM table WHERE title LIKE :param"); $get_data->bindParam(':param', $param_term); $get_data->execute(); I was wondering how can I improve the above query so that it looks for all potential results based on partial matching of letters?
  11. I found the mistake. All I had to do was to add a "," after the beforeShowday to separate it from the onSelect function. You could've just told me this. Also I don't even have to create a variable outside the ajax as you previously told me. It works fine without it. Here's the updated code. <script> $(document).ready(function() { function formattedDate(d = new Date) { let month = String(d.getMonth() + 1); let day = String(d.getDate()); const year = String(d.getFullYear()); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; return `${year}-${month}-${day}`; } $.ajax({ type: "POST", url: 'snippets/adapter-fetch.php', data: {}, dataType: 'json', success: function(highlighted) { $('#datetimepicker1').datepicker({ dateFormat: "yy-mm-dd", multidate: true, beforeShowDay: function(date) { newDate = formattedDate(date); for(let x=0; x < highlighted.length; x++) { console.log(highlighted[x], newDate); if (highlighted[x] === newDate) { return [true, 'Highlighted', '']; // that 3rd index is the tooltip text } } return [true, '']; // default if not a highlight }, onSelect: function () { var getDate = $("#datetimepicker1").val(); $.ajax({ type: "POST", url: "snippets/adapter-set.php", data: { date: getDate }, success: function(data) { // alert(data); }, error: function() { alert("Error."); } }); } }); } }); }); </script>
  12. I'm not asking you to redo the whole code. The code I have works. It just needs to be reformatted so both work on the same page. And I understand about being only able to using a single datepicker instance. And like I said before, I have tried many different variations in the past 24 hours and none of them work. So either I keep going in circles or you can help me out and insert your code snippet in the right place into my code. It helps to see it visually. Here's the updated code with what you were trying to say. It doesn't show the Calendar anymore and I get this error in the console "Uncaught SyntaxError: Unexpected identifier". <script> $(document).ready(function() { function formattedDate(d = new Date) { let month = String(d.getMonth() + 1); let day = String(d.getDate()); const year = String(d.getFullYear()); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; return `${year}-${month}-${day}`; } //CODE#1 // THIS CODE FETCHES DATES FROM A MYSQL TABLE AND HIGHLIGHTS THEM ON A DATEPICKER CALENDAR var highlighted = null; $.ajax({ type: "POST", url: 'snippets/adapter-fetch.php', data: {}, dataType: 'json', success: function(data) { highlighted = data; $('#datetimepicker1').datepicker({ dateFormat: "yy-mm-dd", multidate: true, beforeShowDay: function(date) { newDate = formattedDate(date); for(let x=0; x < highlighted.length; x++) { console.log(highlighted[x], newDate); if (highlighted[x] === newDate) { return [true, 'Highlighted', '']; // that 3rd index is the tooltip text } } return [true, '']; // default if not a highlight } onSelect: function () { var getDate = $("#datetimepicker1").val(); $.ajax({ type: "POST", url: "snippets/adapter-set.php", data: { date: getDate }, success: function(data) { // alert(data); }, error: function() { alert("Error."); } }); } }); } }); }); </script>
  13. Been trying your method many different ways and it still doesn't work. Could you please add your edits to the entire code below instead of bits and pieces? Here's my code with your supposed update code. It doesn't return any errors, but it doesn't highlight the dates either. <script> $(document).ready(function() { function formattedDate(d = new Date) { let month = String(d.getMonth() + 1); let day = String(d.getDate()); const year = String(d.getFullYear()); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; return `${year}-${month}-${day}`; } //CODE#1 // THIS CODE FETCHES DATES FROM A MYSQL TABLE AND HIGHLIGHTS THEM ON A DATEPICKER CALENDAR var highlighted = null; $.ajax({ type: "POST", url: 'snippets/adapter-fetch.php', data: {}, dataType: 'json', success: function(data) { highlighted = data; $('#datetimepicker1').datepicker({ dateFormat: "yy-mm-dd", multidate: true, beforeShowDay: function(date) { newDate = formattedDate(date); for(let x=0; x < highlighted.length; x++) { console.log(highlighted[x], newDate); if (highlighted[x] === newDate) { return [true, 'Highlighted', '']; // that 3rd index is the tooltip text } } return [true, '']; // default if not a highlight } }); } }); // CODE#1 // THIS CODE INSERTS AND REMOVES THE SELECTED DATES FROM THE MYSQL TABLE. $('#datetimepicker1').datepicker({ dateFormat: "yy-mm-dd", multidate: true, onSelect: function () { var getDate = $("#datetimepicker1").val(); $.ajax({ type: "POST", url: "snippets/adapter-set.php", data: { date: getDate }, success: function(data) { // alert(data); }, error: function() { alert("Error."); } }); } }); }); </script>
  14. I sort of understand what you're trying to say. Can you show me an example of setting up a variable outside the ajax using my code? Seeing it would help a lot.
  15. I have finally solved this issue. It was a lot more complicated than I originally imagined. Here's code. <script> $(document).ready(function() { function formattedDate(d = new Date) { let month = String(d.getMonth() + 1); let day = String(d.getDate()); const year = String(d.getFullYear()); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; return `${year}-${month}-${day}`; } $.ajax({ type: "POST", url: 'snippets/adapter-fetch.php', data: {}, dataType: 'json', success: function(highlighted) { $('#datetimepicker1').datepicker({ dateFormat: "yy-mm-dd", multidate: true, beforeShowDay: function(date) { newDate = formattedDate(date); for(let x=0; x < highlighted.length; x++) { if (highlighted[x] === newDate) { return [true, 'Highlighted', '']; // that 3rd index is the tooltip text } } return [true, '']; // default if not a highlight } }); } }); }); </script> // PHP adapter-fetch.php $global_user_id = 5; $find_query = $db->prepare("SELECT date_available FROM user_dates WHERE user_id = :user_id"); $find_query->bindParam(':user_id', $global_user_id); $find_query->execute(); $result_find = $find_query->fetchAll(PDO::FETCH_ASSOC); $dates = []; if(count($result_find) > 0) { foreach($result_find as $row) $dates[] = $row['date_available']; } echo json_encode($dates); So now the above code will highlight all the dates I fetch from the database table. And my original code will insert and delete dates entries when clicking a date on the Calendar. Having said that I have a new issue. Here's the full code combined. <script> $(document).ready(function() { function formattedDate(d = new Date) { let month = String(d.getMonth() + 1); let day = String(d.getDate()); const year = String(d.getFullYear()); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; return `${year}-${month}-${day}`; } // THIS CODE WILL HIGHLIGHT THE DATES $.ajax({ type: "POST", url: 'snippets/adapter-fetch.php', data: {}, dataType: 'json', success: function(highlighted) { $('#datetimepicker1').datepicker({ dateFormat: "yy-mm-dd", multidate: true, beforeShowDay: function(date) { newDate = formattedDate(date); for(let x=0; x < highlighted.length; x++) { if (highlighted[x] === newDate) { return [true, 'Highlighted', '']; // that 3rd index is the tooltip text } } return [true, '']; // default if not a highlight } }); } }); // THIS CODE WILL INSERT/DELETE THE DATES $('#datetimepicker1').datepicker({ dateFormat: "yy-mm-dd", multidate: true, onSelect: function () { var getDate = $("#datetimepicker1").val(); $.ajax({ type: "POST", //or GET. Whichever floats your boat. url: "snippets/adapter-set.php", data: { date: getDate }, success: function(data) { // alert(data); }, error: function() { alert("Error."); } }); } }); }); </script> It seems like the ajax code that highlights the dates won't work if I have both of them together. Can you tell me why I can't use the "$('#datetimepicker1').datepicker" twice in the same script? It doesn't return any errors. It just doesn't highlight the dates.
×
×
  • 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.