PNewCode Posted March 11, 2023 Share Posted March 11, 2023 Hello. First... WOW I learn so much on here. Today I'm facing a new task I am bringing upon myself to take something I already have, and make it more specific. I have a drop down menu that selects a user name, from a list of users in the database. This works great. However, now that the list is getting longer, I am attempting to have this populate with only one specific user instead of all of them.This is the scenerio I want to achieveI am at the website, and I want to go to "bob"s profile page. When I get there, I see I can send him a gift (or a message). So I click on that button but it sends me to a page where I have to scroll through over 400 names to get to it. So now I am annoyed and leave the website. I wish the developer (hehe) just made it so his name was there. Bob's id in the database is 215 What I have now works great to select a person and move on to the next page and send them something based on the name because it also gets their id. So how can I do this to capture ONLY the id of the page I'm visiting, and have that as the only option in the list? (I can't just have only 215 and bob in the list because this will depend on which page is being visited)Note: Considering that everything works solid with the rest of the page with a full drop down list, and that is the part I want to edit, I am assuming that is all I should show on here to reduce clutter. If I should show more than I apologize and I am willing to post whatever else is needed to be seen <select name="gname" style="font-size:18px;"> <option>Choose A Member</option> <?php $sqli = "SELECT * FROM users ORDER BY fname ASC"; $result = mysqli_query($con, $sqli); while ($row = mysqli_fetch_array($result)) { # code... echo '<option value="'.$row['id'].'">'.$row['fname'].'</option>'; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/315999-selecting-a-specific-user-from-drop-down-in-php-mysql/ Share on other sites More sharing options...
kicken Posted March 11, 2023 Share Posted March 11, 2023 You'd pass the user's ID as a parameter to the page then use that to filter your query results using a WHERE clause, like in your previous thread. 1 Quote Link to comment https://forums.phpfreaks.com/topic/315999-selecting-a-specific-user-from-drop-down-in-php-mysql/#findComment-1606444 Share on other sites More sharing options...
mac_gyver Posted March 11, 2023 Share Posted March 11, 2023 if you are asking how to make the selected option 'sticky', you would output the selected attribute for the option choice that matches any existing gname input value. if you are asking how to narrow down the choices, this is called autocomplete/typeahead and uses javascript and ajax requests to populate a dropdown list with entries from your database that match the portion of text that has been typed in a text input field. you can then just click on the choice you want to complete the entry in the field. there are countless example scripts posted on the web. 1 Quote Link to comment https://forums.phpfreaks.com/topic/315999-selecting-a-specific-user-from-drop-down-in-php-mysql/#findComment-1606447 Share on other sites More sharing options...
Barand Posted March 11, 2023 Share Posted March 11, 2023 An easy way to iplement a similar search is to use a datalist. For example Pupil ID : <input type='text' name='name' id='name' list='pupils'> <datalist id="pupils"> <option value="1">Adam Simms</option> <option value="2">Allan Blair</option> <option value="3">Anna Hamilton</option> <option value="4">Anne Bailey</option> <option value="5">Anthony Bell</option> <option value="6">Caroline Freeman</option> <option value="7">David Powell</option> <option value="8">Emma Watson</option> <option value="9">George Wilson</option> <option value="10">Henry Irving</option> <option value="15">Jack Williams</option> <option value="11">Jane Morrison</option> <option value="12">John Patterson</option> <option value="13">John Tully</option> <option value="14">John Watson</option> <option value="16">Margaret Norton</option> <option value="17">Mary Blake</option> <option value="18">Mary Sheldon</option> <option value="19">Mary Whitehouse</option> <option value="20">Michael Grove</option> <option value="21">Peter Adamson</option> <option value="22">Peter Appleby</option> <option value="23">Wayne Jones</option> <option value="24">William Smith</option> </datalist> The list is initially hidden by default. Entering "ma" in the input displays the names that contain "ma" When Margaret is selected, the input field then contains the ID of her record (16) 1 Quote Link to comment https://forums.phpfreaks.com/topic/315999-selecting-a-specific-user-from-drop-down-in-php-mysql/#findComment-1606448 Share on other sites More sharing options...
PNewCode Posted March 12, 2023 Author Share Posted March 12, 2023 I decided to go a different route because to be honest, with the rest of the pages design in html, the drop down menu just looked ugly lol. So I just made it an auto filled form with the id passed to it. HOWEVER, I have a new problem with this. The page also needs to grab the id of the user that is logged in, to ALSO send in part of the form. There is a "vew profile" link like in my previous post. But this one serves as a purpose for the user to view their own profile to see if they want to make any changes. In the top menu, I use once again<a href="my-profile.php?id='.$row['id'].'">Your Profile</a> But the results are my-profile.php?id= What was built on the previous post works because it's sending the id of someone else that is in a table that has many users. This doesn't because I don't seem to understand where to grab the id of the user logged in, instead of the id of someone else. Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/315999-selecting-a-specific-user-from-drop-down-in-php-mysql/#findComment-1606489 Share on other sites More sharing options...
kicken Posted March 12, 2023 Share Posted March 12, 2023 26 minutes ago, PNewCode said: I don't seem to understand where to grab the id of the user logged in If they are logged in, then presumably their ID would be stored within your session, so grab it out of $_SESSION when you need it. Quote Link to comment https://forums.phpfreaks.com/topic/315999-selecting-a-specific-user-from-drop-down-in-php-mysql/#findComment-1606490 Share on other sites More sharing options...
PNewCode Posted March 12, 2023 Author Share Posted March 12, 2023 EUREKA!!! Just to share, here's the winning solution. I chaged it to <a href="my-profile.php?id='.$myid.'">Your Profile</a> and added $myid = $row["id"]; To the while($row = $result->fetch_assoc()) {Okay so I'm learning so much and I'm really rather excited. Especially when things WORK! lol Quote Link to comment https://forums.phpfreaks.com/topic/315999-selecting-a-specific-user-from-drop-down-in-php-mysql/#findComment-1606491 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.