wkilc Posted August 15, 2010 Share Posted August 15, 2010 I've made a few attempts at this, but I'm likely not making much sense.... I'm working on how to phrase the question. I had a MySQL database where I used to combine user first AND last names in a single string... I called that string "Member" So if I filtered: www.mysite.com/members.php?Member=Peter It would find and display all Peters: Peter Rabbit and Peter Griffith and Pope Peter. This is what I want. I'm now keeping each name (first and last) in separate strings (as separate variables?), and I am combining them in an array to display on the website: $First = $First; $Last = $Last; $member = array($Last, $First,); foreach ($Member as $key => $v ) if (!$v) unset ($Member[$key]); $Member = implode(', ', $Member); echo $Member; Now I've lost the ability to search all the members for Peter. So if I filtered: www.mysite.com/members.php?First=Peter ...it would work.... for Peter Rabbit and Peter Griffith, but not Pope Peter I cannot query the entire array? I can no longer check members (first and last names) for Wayne? www.mysite.com/members.php?Member=Peter I'm betting I can, but there's a lot more to it... and 8 hours of Googling haven't helped much. Thanks for listening. ~Wayne Link to comment https://forums.phpfreaks.com/topic/210755-url-query-string/ Share on other sites More sharing options...
ngreenwood6 Posted August 15, 2010 Share Posted August 15, 2010 There is a very simple solution. Keep it the way it was with ?member=Peter. Then in your query change it to check the firstname and lastname like this: $member = 'Peter'; "SELECT * FROM users WHERE firstname LIKE '%{$member}%' OR lastname LIKE '%{$member}%'" It just checks the firstname and lastname against peter instead of just the full name. Link to comment https://forums.phpfreaks.com/topic/210755-url-query-string/#findComment-1099419 Share on other sites More sharing options...
wkilc Posted August 15, 2010 Author Share Posted August 15, 2010 Thank you very much. I can't for the life of me figure out how to implement that, though. Obviously I don't want to hard-code "Peter"... I want that to be a variable. Here's my exisiting query: $query = "SELECT user_stuff.First,user_stuff.Last,user_stuff.Phone,user_stuff.Email, FROM user_stuff WHERE ((user_stuff.First LIKE '$First%') and (user_stuff.Last LIKE '$Last%') and (user_stuff.Phone LIKE '$Phone%') and (user_stuff.Email LIKE '$Email%')) ORDER BY user_stuff.$sort LIMIT $eu, $limit "; $result = mysql_query($query); And here's my form: <?php // If the form has been submitted... if(isset($_POST['submit'])) { // Set $name as the value of the name field in your form ($_POST just references the form) $name = $_POST['name']; } ?> <form name="form" method="post" action=""> <input type="text" name="<?php $member ?>" value="<? echo $name; ?>" /> <input type="submit" name="submit" value="Search Names" /> </form> If anyone can help me implement this I'd be eternally grateful. Thanks again. ~Wayne Link to comment https://forums.phpfreaks.com/topic/210755-url-query-string/#findComment-1099428 Share on other sites More sharing options...
ngreenwood6 Posted August 15, 2010 Share Posted August 15, 2010 Your text field has a wrong name and u arent using the variable name that you created. try something like this: query $query = "SELECT user_stuff.First,user_stuff.Last,user_stuff.Phone,user_stuff.Email, FROM user_stuff WHERE ((user_stuff.First LIKE '$name%') and (user_stuff.Last LIKE '$name%') and (user_stuff.Phone LIKE '$Phone%') and (user_stuff.Email LIKE '$Email%')) ORDER BY user_stuff.$sort LIMIT $eu, $limit "; $result = mysql_query($query); form <?php // If the form has been submitted... if(isset($_POST['submit'])) { // Set $name as the value of the name field in your form ($_POST just references the form) $name = $_POST['member']; } ?> <form name="form" method="post" action=""> <input type="text" name="member" value="<? echo $name; ?>" /> <input type="submit" name="submit" value="Search Names" /> </form> Link to comment https://forums.phpfreaks.com/topic/210755-url-query-string/#findComment-1099431 Share on other sites More sharing options...
wkilc Posted August 15, 2010 Author Share Posted August 15, 2010 Thank you... I gave it a shot. Now, if I search one letter "p", it will only return folks who have a first and last name that both start with "p". Weird... that must have to do with the "%"? If I search more than two letter I get no results. Will keep working on it. Nothing happens if I post the query in the URL, still. (www.mysite.com/members.php?member=p) I think the purpose of this was to hold the value of what was submitted in the text field after the form was submitted. // Set $name as the value of the name field in your form ($_POST just references the form) $name = $_POST['name']; Thanks again. ~Wayne Link to comment https://forums.phpfreaks.com/topic/210755-url-query-string/#findComment-1099434 Share on other sites More sharing options...
ngreenwood6 Posted August 15, 2010 Share Posted August 15, 2010 Sorry didnt even notice that from the code you posted because I just copied and pasted what you had and modified it. The % symbol needs to be in before and after the variable in the query so it should look like this: LIKE '%$name%' You will have to update them all to reflect that change and it should work as expected. Link to comment https://forums.phpfreaks.com/topic/210755-url-query-string/#findComment-1099448 Share on other sites More sharing options...
wkilc Posted August 15, 2010 Author Share Posted August 15, 2010 Thank you again. I had already tried updating the query: $query = "SELECT user_stuff.First,user_stuff.Last,user_stuff.Phone,user_stuff.Email, FROM user_stuff WHERE ((user_stuff.First LIKE '%$name%') and (user_stuff.Last LIKE '%$name%') and (user_stuff.Phone LIKE '$Phone%') and (user_stuff.Email LIKE '$Email%')) ORDER BY user_stuff.$sort LIMIT $eu, $limit "; $result = mysql_query($query); The result now is if I search a letter (say, "m") it will return memebrs who have an "m" somewhere in both their first and last names (i.e. William Smith). But if I search for "Smith", nothing is found. ~Wayne Link to comment https://forums.phpfreaks.com/topic/210755-url-query-string/#findComment-1099478 Share on other sites More sharing options...
ngreenwood6 Posted August 15, 2010 Share Posted August 15, 2010 Wow I didnt even notice something else, it was late last night when I was posting so I am going to blame it on that. The part that I gave you is working properly, however the logic that you used is where the problem lies. You are checking where the firstname is like whatever AND lastname is like whatever. The problem is the AND, you need to change that to an OR. You may need to check the other ones for phone and email, not sure how you want it to match. Link to comment https://forums.phpfreaks.com/topic/210755-url-query-string/#findComment-1099523 Share on other sites More sharing options...
wkilc Posted August 15, 2010 Author Share Posted August 15, 2010 BLESS YOU! Thanks again. ~Wayne Link to comment https://forums.phpfreaks.com/topic/210755-url-query-string/#findComment-1099536 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.