Tom10 Posted May 15, 2015 Share Posted May 15, 2015 Hi, i've never created a search in php so i'm not exactly sure on the method you can use do create a search, basically i have created an admin panel and it has a list of users but if there are too many users i will need to create a search. Please can someone tell me how i can do this? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
CroNiX Posted May 15, 2015 Share Posted May 15, 2015 (edited) Depends on what you need to search for, but let's say you want to search for the users last name. Create a form with an input for the last name. When submitting the form, just do a search for the last name in the db using WHERE lastname = 'lastname_from_form' to get an exact match, or WHERE lastname LIKE 'lastname_from_form%' to get all that start with lastname_from_form (the % at the end is a wildcard). Then display the list of matched users Edited May 15, 2015 by CroNiX Quote Link to comment Share on other sites More sharing options...
Tom10 Posted May 15, 2015 Author Share Posted May 15, 2015 Depends on what you need to search for, but let's say you want to search for the users last name. Create a form with an input for the last name. When submitting the form, just do a search for the last name in the db using WHERE lastname = 'lastname_from_form' to get an exact match, or WHERE lastname LIKE 'lastname_from_form%' to get all that start with lastname_from_form (the % at the end is a wildcard). Then display the list of matched users Thanks, CroNiX i will try this! Quote Link to comment Share on other sites More sharing options...
Tom10 Posted May 15, 2015 Author Share Posted May 15, 2015 Hi i'm trying to echo all usernames from sql to html my code is working but it's echoing to the page as an array $select = "SELECT username FROM users"; $stmt = $handler->prepare($select); $stmt->execute(); if($stmt->rowCount()) { $res = $stmt->fetchAll(); print_r($res); } Quote Link to comment Share on other sites More sharing options...
CroNiX Posted May 15, 2015 Share Posted May 15, 2015 (edited) Because $res IS an array of all usernames, which is what your query asked for by using fetchAll(). So you'd need to iterate over that array. foreach($res as $user) { echo $user['username'] . "<br>"; } Edited May 15, 2015 by CroNiX Quote Link to comment Share on other sites More sharing options...
Tom10 Posted May 15, 2015 Author Share Posted May 15, 2015 Because $res IS an array of all usernames, which is what your query asked for by using fetchAll(). So you'd need to iterate over that array. foreach($res as $user) { echo $user['username'] . "<br>"; } Ah i get you Quote Link to comment Share on other sites More sharing options...
Tom10 Posted May 15, 2015 Author Share Posted May 15, 2015 <a href="<?php echo htmlspecialchars(htmlspecialchars($_SERVER['PHP_SELF'])) . '?page=manage&new='.hash('ripemd128', rand()).'' ?>"> + New User </a> Hi i'm trying to load a create page by adding &new= to the url but it won't work if i create case 'new': ? 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.