justlukeyou Posted June 17, 2012 Share Posted June 17, 2012 Hi, Im puzzled by one issue that could pose a real problem. I can query my MD using spaces (corner%20%tv%20unit) but I cant query it using hyphens (corner-tv-unit) The problem I have is that space actually appears in the link. (.com/corner tv unit) Is it possible to replace this with a hyphen or is their a much deeper problem? Quote Link to comment Share on other sites More sharing options...
requinix Posted June 18, 2012 Share Posted June 18, 2012 Sure, you can do that... if you write the code for it. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 18, 2012 Author Share Posted June 18, 2012 Thanks, Im surprised it allows spaces. I thought links would feature hyphens as standard. Would the code be in the HT access file. That's what I'm really struggling with, I can make pretty URLs that look really nice like .com/products/nintendo I just can't put the hyphen in .com/products/nintendo-wii But I can do .com/nintendo wii Its so bloody frustrating! Quote Link to comment Share on other sites More sharing options...
cpd Posted June 18, 2012 Share Posted June 18, 2012 Very simple. Assuming you want that match specifically you can do ^/?(a-zA-Z0-9-)?/?(a-zA-Z0-9-)?/?$ That matches lower and upper case letters, numbers and hyphens. You may also want \+ to include plus signs. Alternatively, just write ^(a-zA-Z0-9-\+)$ index.php/$1 And it'll map everything matching the criteria. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 18, 2012 Author Share Posted June 18, 2012 Hi, Many thanks, I tried those but couldn't get them to work. I have tried these also but they appear to work. RewriteRule ^products/extendingdiningtable/([a-zA-Z-]+)$ /products/extendingdiningtable.php?name=$1 RewriteRule ^products/extendingdiningtable/([a-zA-Z\-\s]+)$ /products/extendingdiningtable.php?name=$1 With this code just enter it as it is, does it all map from the the index file? ^(a-zA-Z0-9-\+)$ index.php/$1 Quote Link to comment Share on other sites More sharing options...
cpd Posted June 18, 2012 Share Posted June 18, 2012 I think you've misunderstood how that would function and how to implement it. If you want the website to redirect all content to one page you would do RewriteRule ^(.*)$ index.php/$1 [L] What are you actually trying to achieve? If you redirect something you must handle it on the page its being redirected too... Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 18, 2012 Author Share Posted June 18, 2012 Hi, I'm not looking to redirect to a page, I am trying to insert hyphens into query links. For example: .com/products/product.php?name=red-widget I can't seem to be able to make red-widget work. But I can make "red" work or "red%20widget" which leaves a space in the link. When I try "red-widget" It blocks the query. I just see why it wont work. Ive made about 30 different attempts to make it work. Quote Link to comment Share on other sites More sharing options...
cpd Posted June 18, 2012 Share Posted June 18, 2012 Perhaps you should post the code you have at the minute as I think I've confused the problem even further with previous posts. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 19, 2012 Author Share Posted June 19, 2012 Hi, This is the code I am using. Could be anything to do with the % I am using the code. There must be something block hyphens. <?php $name = mysql_real_escape_string($_GET['name']); $query = mysql_query(" SELECT name, product_id, rrp, discount, image_link FROM productdbase p INNER JOIN furniture_groups f ON f.long_name = p.furniture_group WHERE name LIKE '%{$name}%' LIMIT 15"); while ($query_row = mysql_fetch_assoc($query)) { ?> RewriteRule ^products/computerdesks/([a-zA-Z-\-\s]+)$ /products/computerdesks.php?name=$1 Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 20, 2012 Author Share Posted June 20, 2012 Hi does anyone have any suggestions on what I can try please? Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 20, 2012 Share Posted June 20, 2012 OK, so did you check what the actual query was that was run and the associated error message? $query = " SELECT name, product_id, rrp, discount, image_link FROM productdbase p INNER JOIN furniture_groups f ON f.long_name = p.furniture_group WHERE name LIKE '%{$name}%' LIMIT 15"); $result = mysql_query($query) or die ("Query: <br>$query<br>Error:<br>".mysql_error()); while ($query_row = mysql_fetch_assoc($result)) { Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 20, 2012 Author Share Posted June 20, 2012 Hi, Im not totally sure what you mean. I have echoed the result of the query and it displays fine if I use a single word such as "red". However, as soon as I introduce a hyphen it blocks the whole query and nothing is echoed onto the page. There are no error messages, its as if there is no code on the page once I introduce a hyphen. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 20, 2012 Share Posted June 20, 2012 How are you building these links? Do you actually have matching data in your database table? For a specific example, 'corner-tv-unit', what is the data in your two database tables that you expect the query to match? Perhaps for that specific name, your join query doesn't have a matching entry in the furniture_groups table? Edit: I just tried your rewrite rule and your example link and the correct value is received in the computerdesks.php file as the name, so this has nothing to do with that part of the process. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 20, 2012 Share Posted June 20, 2012 Im not totally sure what you mean. I have echoed the result of the query and it displays fine if I use a single word such as "red". However, as soon as I introduce a hyphen it blocks the whole query and nothing is echoed onto the page. There are no error messages, its as if there is no code on the page once I introduce a hyphen. Did you even LOOK at the code I provided? There is no such thing as a query being "blocked". Either the query runs or it doesn't. If it doesn't run an error will be generated. So, if the query doesn't run you should check 1) the actual content of the query and 2) the generated error. When a query has any dynamic content, don't assume you know what the query looks like. I provided revised code that added error handling so you can see #1 and #2 above. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 20, 2012 Author Share Posted June 20, 2012 Im not totally sure what you mean. I have echoed the result of the query and it displays fine if I use a single word such as "red". However, as soon as I introduce a hyphen it blocks the whole query and nothing is echoed onto the page. There are no error messages, its as if there is no code on the page once I introduce a hyphen. Did you even LOOK at the code I provided? There is no such thing as a query being "blocked". Either the query runs or it doesn't. If it doesn't run an error will be generated. So, if the query doesn't run you should check 1) the actual content of the query and 2) the generated error. When a query has any dynamic content, don't assume you know what the query looks like. I provided revised code that added error handling so you can see #1 and #2 above. Apologies Psycho, I've had about 2 hours sleep in last 24 hours and didn't notice the changes you made. I have run it, when I use a single word (red) it works fine. But when I enter a hyphen nothing is echoed. "corner tv unit" is in the database because I can create links with spaces in such as .com/products/corner tv unit - this works fine. But .com/products/corner-tv-unit doesn't run. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 20, 2012 Share Posted June 20, 2012 Do you not understand why querying for "tv-stand" will never produce a result when the value in the database is "tv stand"? Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 21, 2012 Author Share Posted June 21, 2012 No, I thought the HT Access file allowed for the insertion of hyphens. I cant insert hpyhens into the keywords. Or should I be doing that somehow? Is there a code like the one for spaces (%20) which allows me to insert hyphens. That would solve the problem! Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 21, 2012 Share Posted June 21, 2012 Is the following what you are trying to do: you want to put hyphens '-' in your urls, where your data actually has spaces and you want the submitted url (with hyphens) to match the spaces in your actual data? Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 21, 2012 Share Posted June 21, 2012 Im not totally sure what you mean. I have echoed the result of the query and it displays fine if I use a single word such as "red". However, as soon as I introduce a hyphen it blocks the whole query and nothing is echoed onto the page. There are no error messages, its as if there is no code on the page once I introduce a hyphen. Do you not understand why querying for "tv-stand" will never produce a result when the value in the database is "tv stand"? I think Pikachu has hit the nail on the head. The OP was erroneously suggesting that the query was failing. But, if the code I provided did not produce an error then it must be that he is incorrectly assuming that a hyphen would match a space. All these replies could have been avoided with accurate details provided. @OP: If you want the hyphen in a value to be treated as spaces then do a simple str_replace() on the value before you use it in your query to replace the hyphens with spaced. Alternatively, you could also replace the hyphens with an underscore and change the query to do a LIKE comparison. Then the underscore will be used to match ANY single character. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 21, 2012 Author Share Posted June 21, 2012 Thanks, Apologies but I am new to PHP. I thought the htaccess code allowed for hyphens as they are so widely used. In terms your suggestions does str_replace() read "corner tv units" and allow me to add a hyphen when the query. I am completely new to str_replace() To be 100% honest I thought this would be easy as hyphens are so widely used. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 21, 2012 Share Posted June 21, 2012 You realize your htaccess file is a completely different thing from your php script right? Re: str_replace - see my signature Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 21, 2012 Author Share Posted June 21, 2012 Thanks, I thought the HT Access would read a link such as .com/products/large-red-widgets and simply ignore the hyphen but the URL bar it showed the hyphen to make a pretty URL. What you have to remember is Im completely new to PHP but I am trying my hardest to use it so when you say "use a simple str_replace()", for me it isn't simple. The good news is my site is 98% complete. Just need to do this and finish off pagination. So string replace reads "corner-tv-units" and querys "corner tv units". If so should I be using this, if so what is $our_url. $our_url = str_replace( " ", "-", <%name%>); Name being the column name I am querying. This seems a very complicated way of doing it. Much more difficult than using a simple %20. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 22, 2012 Share Posted June 22, 2012 What you have to remember is Im completely new to PHP . . . Your registration date for this site shows February 18, 2007 (6 years ago). But, I'm questioning that date since the earliest post I can find for you is February 02, 2011 - but that is still well over a year ago. That's MORE than enough time to know that there is a manual at php.net and you can easily look up the details of a function with examples and such. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 22, 2012 Author Share Posted June 22, 2012 Thats very creepy, weird and slightly malicious. Thanks for that. I think. Care to publish anything else personal about me on the internet? I thought this was a well run forum. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 22, 2012 Share Posted June 22, 2012 All the information that was posted in this thread is public information. Your signup date is listed in your public profile. All your posts and threads can be searched for several different ways by anyone. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.