Jump to content

imgrooot

Members
  • Posts

    383
  • Joined

  • Last visited

  • Days Won

    1

Everything posted by imgrooot

  1. So based on your answer, here's my new query. WHERE MATCH(items.item_title) AGAINST(:title_query IN BOOLEAN MODE) OR MATCH(brands.brand_name) AGAINST(:brand_query_ IN BOOLEAN MODE) ORDER BY items.item_id DESC LIMIT :limit OFFSET :offset"); $get_records->bindParam(':title_query', $search_query); $get_records->bindParam(':brand_query', $search_query); The above gives me the error below. The line 117 being the WHERE query. Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1191 Can't find FULLTEXT index matching the column list' in C:\xampp\htdocs\..... on line 117
  2. In my database connection, I had this. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); I replaced that with with this. $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); Is that correct? After doing that, the query does work with offset and limit parameters. "Items" and "type_1" are 2 different tables. Would that not be considered multiple tables? Below is my full code for the search page. I have 4 seperate tables(items, type_1, brands, images). They are all connected. Currently the search works by matching the user's search query against the "item_title" in the items table. But what I would like to do is for the search_query to ALSO search the brand_name in the brands table and retrieve results based on on the matching of both brand name item title. I have tried maxxd's method above and it doesn't work. Like this. WHERE MATCH(items.item_title, brands.brand_name ) AGAINST(:search_query IN BOOLEAN MODE) ORDER BY items.item_id DESC LIMIT :limit OFFSET :offset Original code. $search_query = trim(preg_replace('/[^\p{L}\p{N}_]+/u', ' ', $_GET['sq'])); $count_records = $db->prepare("SELECT COUNT(*) FROM items WHERE MATCH(item_title) AGAINST(:search_query IN BOOLEAN MODE)"); $count_records->bindParam(':search_query', $search_query); $count_records->execute(); $total = $count_records->fetchColumn(); // How many items to list per page $limit = 8; // How many pages will there be $pages = ceil($total / $limit); // What page are we currently on? $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array ( 'options' => array( 'default' => 1, 'min_range' => 1, ), ))); // Calculate the offset for the query $offset = ($page - 1) * $limit; // Some information to display to the user $start = $offset + 1; $end = min(($offset + $limit), $total); if($page > 0) { $offset = ($page - 1) * $limit; } else { $offset = 0; } $get_records = $db->prepare("SELECT items.*, type_1.*, brands.*, images.* FROM items LEFT JOIN type_1 ON items.type_1 = type_1.type_1_id LEFT JOIN brands ON items.brand_id = brands.brand_id LEFT JOIN images ON items.item_id = images.item_id WHERE MATCH(items.item_title) AGAINST(:search_query IN BOOLEAN MODE) ORDER BY items.item_id DESC LIMIT :limit OFFSET :offset"); $get_records->bindParam(':search_query', $search_query); $get_records->bindParam(':limit', $limit); $get_records->bindParam(':offset', $offset); $get_records->execute(); $result_records = $get_records->fetchAll(PDO::FETCH_ASSOC); if(count($result_records) > 0){ foreach($result_records as $row) { // loop for items } } else { echo '<div id="message-flash">No results found.</div>'; }
  3. Good to know. In terms of your example with prepared parameters, it only works if I set the search_query parameter. If I add offset and limit, it will give an error. Now my question is, how is your prepared statement different from mine below? Are both correct methods? $items_stmt = $db->prepare("SELECT items.*, type_1.* FROM items LEFT JOIN type_1 ON items.type_1 = type_1.type_1_id WHERE MATCH(items.item_title) AGAINST(':search_query' IN BOOLEAN MODE) ORDER BY items.item_id DESC LIMIT {$limit} OFFSET ".$offset); $items_stmt->bindParam(':search_query', $search_query); $items_stmt->execute(); $result_items = $items_stmt->fetchAll(PDO::FETCH_ASSOC); if(count($result_items) > 0){ } Also using the above query, can you give me an example of the MATCH AGAINST per table you're talking about? Because I've tried many different methods and so far no luck.
  4. I tried that but it gives me an error like this. I ready somewhere that Fulltext search might not allow multiple tables to be searched. Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1210 Incorrect arguments to MATCH' in... In terms of validating and sanitizing the variable, I have it like this. I don't know if that's sufficient enough. $search_query = trim(preg_replace('/[^\p{L}\p{N}_]+/u', ' ', $_GET['sq']));
  5. I meant to say from multiple TABLES not columns. I know you can search from more than one column in the same table. But I have having an issue searching from 2 left joined tables at the same time. Also I want to do the same thing as above with the COUNT query. $count_records = $db->prepare("SELECT COUNT(*) FROM items WHERE MATCH(item_title) AGAINST('$search_query' IN BOOLEAN MODE)"); $count_records->execute(); $total = $count_records->fetchColumn();
  6. I currently have a fulltext search that works. It matches against a item title column in mysql database. Now I would like to include another column to match against. How can that be done properly? Here's my code. Where it says MATCH, I would like to include the type_1 column as well; like this(type_1.type_1_name). $get_records = $db->prepare("SELECT items.*, type_1.* FROM items LEFT JOIN type_1 ON items.type_1 = type_1.type_1_id WHERE MATCH(items.item_title) AGAINST('$search_query' IN BOOLEAN MODE) ORDER BY items.item_id DESC LIMIT {$limit} OFFSET ".$offset); $get_records->execute(); $result_records = $get_records->fetchAll(PDO::FETCH_ASSOC); if(count($result_records) > 0){ foreach($result_records as $row) { // get results } }
  7. Never mind. I found the solution. When I output the variable, I have to use htmlentities like this htmlentities($variable_output).
  8. I have an html form that inserts a record into database. I also have a an html edit form that retrieves the data from the database. Normally everything works. But I just noticed that I have an issue if I use double quotes in the form field. It'll insert into the database fine. The problem arises when outputting the variable in the form field. If I echo it outside the form field, it'll show up fine with the double quotes. But inside the field, I only geta partial string. For eg. $variable_input = '5 3/4" Glitter Concealed Platform Pump'; The above will insert to the database. But if I retrieve it in the form field below, it'll return with one number only. <input type="text" name="title" value="5" /> Is there a way to fix the variable output of the wording that includes the double quotes?
  9. I have this jquery code below. It basically shows a new div when mouse hover over another div. It works. But I noticed that on page load, it sometimes shows 1 or 2 products already hovered and showing the ".show-details" content. This doesn't happen on every page and sometimes it's only 1 product and sometimes it's 2 products. And they vary in the positions on a page. Do you know why it's doing that and how to fix it? By the way, I have ".show-details" set to display: none by default in the CSS file. <script> $(document).ready(function() { $(".product-small").hover(function() { $(this).find('.show-details').fadeIn("fast").css({ display: 'block'}); }, function(){ $(this).find('.show-details').fadeOut("fast").css({ display: 'block'}); }); }); </script>
  10. Yes It's all about the pagination. The way I have it set up now is more manual work for me. The solution works but I am trying to find a way to improve the pagination code so that it's more efficient.
  11. Alright so here's the full picture. I am trying to get a pagination working. I am trying to modify the script I already had to match the new look. The new pagination looks roughly like this (<) 1 2 3 4 5 (>). I have it working but I find there has to be a more efficient method out there to get the same results. Here is the full process code for one of the pages. I didn't update it with the new code mentioned in the above comments. <?php $type_id = (!empty($_GET['type']) ? $_GET['type'] : null); $price_filter = (!empty($_GET['price']) ? $_GET['price'] : null); $get_page = (!empty($_GET['page']) ? $_GET['page'] : null); ?> <div id="breadcrumb"> <div id="breadcrumb-child"> <h2>Shop</h2> <div id="price-filter"> Price: <a <?php if($price_filter == 'low'){ ?>class="filter-highlight"<?php } ?> href="shop?type=<?php echo $type_id; ?>&name=<?php echo $get_type_3_name_slug; ?>&price=low">Low</a> | <a <?php if($price_filter == 'high'){ ?>class="filter-highlight"<?php } ?> href="shop?type=<?php echo $type_id; ?>&name=<?php echo $get_type_3_name_slug; ?>&price=high">High</a> </div> </div> </div> <div class="collection-list"> <?php $count_records = $db->prepare("SELECT COUNT(*) FROM items WHERE type_3 = :type_3"); $count_records->bindParam(':type_3', $type_id); $count_records->execute(); $total = $count_records->fetchColumn(); // How many items to list per page $limit = 3; // How many pages will there be $pages = ceil($total / $limit); // What page are we currently on? $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array ( 'options' => array( 'default' => 1, 'min_range' => 1, ), ))); // Calculate the offset for the query $offset = ($page - 1) * $limit; // Some information to display to the user $start = $offset + 1; $end = min(($offset + $limit), $total); if($page > 0) { $offset = ($page - 1) * $limit; } else { $offset = 0; } if($price_filter == 'high') { $get_records = $db->prepare("SELECT items.*, type_1.*, type_3.* FROM items LEFT JOIN type_1 ON items.type_1 = type_1.type_1_id LEFT JOIN type_3 ON items.type_3 = type_3.type_3_id WHERE items.type_3 = :type_3 ORDER BY items.item_price DESC LIMIT {$limit} OFFSET ".$offset); $get_records->bindParam(':type_3', $type_id); $get_records->execute(); $result_records = $get_records->fetchAll(PDO::FETCH_ASSOC); if(count($result_records) > 0){ foreach($result_records as $row) { // There are other variables but I am only showing you the ones relevant to the pagination $get_type_1_id = intval($row['type_1']); $get_type_3_name_slug = trim($row['type_3_name_slug']); // This shows all the individual products on a page. include 'snippets/product-loop.php'; } } else { echo '<div id="message-flash">Items coming soon.</div>'; } } else if($price_filter == 'low') { $get_records = $db->prepare("SELECT items.*, type_1.*, type_3.* FROM items LEFT JOIN type_1 ON items.type_1 = type_1.type_1_id LEFT JOIN type_3 ON items.type_3 = type_3.type_3_id WHERE items.type_3 = :type_3 ORDER BY items.item_price ASC LIMIT {$limit} OFFSET ".$offset); $get_records->bindParam(':type_3', $type_id); $get_records->execute(); $result_records = $get_records->fetchAll(PDO::FETCH_ASSOC); if(count($result_records) > 0){ foreach($result_records as $row) { // There are other variables but I am only showing you the ones relevant to the pagination $get_type_1_id = intval($row['type_1']); $get_type_3_name_slug = trim($row['type_3_name_slug']); // This shows all the individual products on a page. include 'snippets/product-loop.php'; } } else { echo '<div id="message-flash">Items coming soon.</div>'; } } else { $get_records = $db->prepare("SELECT items.*, type_1.*, type_3.* FROM items LEFT JOIN type_1 ON items.type_1 = type_1.type_1_id LEFT JOIN type_3 ON items.type_3 = type_3.type_3_id WHERE items.type_3 = :type_3 ORDER BY items.item_id DESC LIMIT {$limit} OFFSET ".$offset); $get_records->bindParam(':type_3', $type_id); $get_records->execute(); $result_records = $get_records->fetchAll(PDO::FETCH_ASSOC); if(count($result_records) > 0){ foreach($result_records as $row) { // There are other variables but I am only showing you the ones relevant to the pagination $get_type_1_id = intval($row['type_1']); $get_type_3_name_slug = trim($row['type_3_name_slug']); // This shows all the individual products on a page. include 'snippets/product-loop.php'; } } else { echo '<div id="message-flash">Items coming soon.</div>'; } } // Pagination starts here // It looks like this format: (<) 1 2 3 4 5 (>) if($price_filter == 'low') { $prevlink = ($page > 1) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">‹</span>'; $page1 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=1">1</a>'; $page2 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=2">2</a>'; $page3 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=3">3</a>'; $page4 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=4">4</a>'; $page5 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=5">5</a>'; $page6 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=6">6</a>'; $page7 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=7">7</a>'; $page8 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=8">8</a>'; $page9 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=9">9</a>'; $page10 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=10">10</a>'; $nextlink = ($page < $pages) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=low&page=' . ($page + 1) . '" title="Next page">›</a>' : '<span class="disabled">›</span>'; } else if($price_filter == 'high') { $prevlink = ($page > 1) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">‹</span>'; $page1 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=1">1</a>'; $page2 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=2">2</a>'; $page3 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=3">3</a>'; $page4 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=4">4</a>'; $page5 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=5">5</a>'; $page6 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=6">6</a>'; $page7 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=7">7</a>'; $page8 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=8">8</a>'; $page9 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=9">9</a>'; $page10 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=10">10</a>'; $nextlink = ($page < $pages) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&price=high&page=' . ($page + 1) . '" title="Next page">›</a>' : '<span class="disabled">›</span>'; } else { $prevlink = ($page > 1) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">‹</span>'; $page1 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=1">1</a>'; $page2 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=2">2</a>'; $page3 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=3">3</a>'; $page4 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=4">4</a>'; $page5 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=5">5</a>'; $page6 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=6">6</a>'; $page7 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=7">7</a>'; $page8 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=8">8</a>'; $page9 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=9">9</a>'; $page10 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=10">10</a>'; $nextlink = ($page < $pages) ? '<a class="circle-link" href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=' . ($page + 1) . '" title="Next page">›</a>' : '<span class="disabled">›</span>'; } ?> <div id="pagination"> <ul> <li><?php echo $prevlink; ?></li> <?php if($page <= 5) { ?> <li class="page-numbers <?php if($get_page == 1) { echo "active-page-number";} ?>"><?php echo $page1; ?></li> <li class="page-numbers <?php if($get_page == 2) { echo "active-page-number";} ?>"><?php echo $page2; ?></li> <li class="page-numbers <?php if($get_page == 3) { echo "active-page-number";} ?>"><?php echo $page3; ?></li> <li class="page-numbers <?php if($get_page == 4) { echo "active-page-number";} ?>"><?php echo $page4; ?></li> <li class="page-numbers <?php if($get_page == 5) { echo "active-page-number";} ?>"><?php echo $page5; ?></li> <?php } else if($page <= 10) { ?> <li class="page-numbers <?php if($get_page == 6) { echo "active-page-number";} ?>"><?php echo $page6; ?></li> <li class="page-numbers <?php if($get_page == 7) { echo "active-page-number";} ?>"><?php echo $page7; ?></li> <li class="page-numbers <?php if($get_page == { echo "active-page-number";} ?>"><?php echo $page8; ?></li> <li class="page-numbers <?php if($get_page == 9) { echo "active-page-number";} ?>"><?php echo $page9; ?></li> <li class="page-numbers <?php if($get_page == 10) { echo "active-page-number";} ?>"><?php echo $page10; ?></li> <?php } else ?> <li><?php echo $nextlink; ?></li> </ul> </div> </div> <?php
  12. That's great. How do I get the "$pageNumber" from the first loop to show up in the second loop? Should I just put another foreach loop inside the second foreach loop above? Essentially I am trying to get both the a href link with incremented pages and the incremented page numbers individually.
  13. That's great. Now my next question is, how do I echo all 100 of those a href links outside the for loop?
  14. Here's a line. I want to add 100 more of same lines and increment the page numbers as shown below. I know I can just manually add 100 lines but Is there a way to do this with PHP that would make it easy? Or is this more of a javascript thing? // original $page1 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=1">1</a>'; // new $page1 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=1">1</a>'; $page2 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=2">2</a>'; $page3 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=3">3</a>'; $page4 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=4">4</a>'; $page5 = '<a href="?type='. $type_id .'&name='. $get_type_3_name_slug .'&page=5">5</a>'; ...etc
  15. The only other solution I can think of is to turn the table into "varchar" and use a unique text like "none" to replace "0".
  16. So what's the solution? The column I am inserting into is "int" based. So I need an int number to distinguish from all the other numbers. Is there no way to add zero to it?
  17. I am finding that if I have "0"(zero) value in form select option, it won't select this option or submit data. If I change this value to any other number to text, it will work. Is there a way to fix this? I have to have an option where I am able to choose to submit "0" value to the database table. <option value="0" <?php if(empty($_POST['special'])) {} else { if($_POST['special'] == 0) { echo 'selected'; } } ?> >None</option>
  18. Ah yes. I did add the return and it now works. function getIt($var) { $pos = strpos($var, ' - '); return substr($var, $pos+3); }
  19. Here's what I am trying to do. Retrieve "title" and "slug title" from all the rows in a table. Update them with new "title", "slug title" and "brand name". Here's my query. It updates the table with the brand names for each row. But for some reason, it removes the title and slug title from all the rows in the table. Is there a reason why it's doing that? function getIt($var) { $pos = strpos($var, ' - '); echo(substr($var, $pos+3)."\n"); } if(isset($_POST['submit'])) { $get_record = $db->prepare("SELECT * FROM items"); $get_record->execute(); $result_record = $get_record->fetchAll(PDO::FETCH_ASSOC); if(count($result_record) > 0){ foreach($result_record as $row) { $get_item_id = intval($row['item_id']); $get_item_title = trim($row['item_title']); $get_item_title_slug = trim($row['item_title_slug']); $new_title = getIt($get_item_title); $new_title_slug = Output::createSlug($new_title); $brand_name = substr($get_item_title, 0, strpos($get_item_title, ' - ')); $update_record = $db->prepare("UPDATE items SET brand_name = :brand_name, item_title = :item_title, item_title_slug = :item_title_slug WHERE item_id = :item_id"); $update_record->bindParam(':item_id', $get_item_id); $update_record->bindParam(':brand_name', $brand_name); $update_record->bindParam(':item_title', $new_title); $update_record->bindParam(':item_title_slug', $new_title_slug); if(!$update_record->execute()) { $errors[] = 'There was a problem updating the item.'; } } } else { $errors[] = 'No item found.'; } }
  20. Yes I am doing it so that in the future I can I filter out products by "Brand" name. Currently the brand name is directly in the title. I can still have it in the title when I output the variables but in the database, it would be better to have it in an individual column.
  21. I think I have it working for the 2nd step. $variable_1 = 'Lulu-free - Swimsuit swimwear red'; $variable_2 = 'Pap & Jones - Bard Mini Dress'; $str1 = substr($variable_1, 0, strpos($variable_1, ' - ')); // results: Lulu-free $str2 = substr($variable_2, 0, strpos($variable_2, ' - ')); // results: Pap & Jones No unfortunately it is not once but several hundred rows.
  22. That works perfectly. One more thing. Basically I have a long list of items in MySQL database with the title in the format I specified above. Basically what I am trying to do is do 2 things. 1. Remove everything before the " - " to create a new title. This works thanks to you. 2. The string that is removed before the " - ", I want to add it to a new table column. Now my next question is, how do I get these strings(Lulu-free, Pap & Jones) as a variable?
  23. Below I have "Input" where it shows the original string. Basically I want to remove everything before " - ". I would like to know how can I get the "Output" results using PHP? Input $variable_1 = Lulu-free - Swimsuit swimwear red $variable_2 = Pap & Jones - Bard Mini Dress Output $variable_1 = Swimsuit swimwear red $Variable_2 = Bard Mini Dress
×
×
  • 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.