Jump to content

Selecting a specific user from drop down in php Mysql


PNewCode

Recommended Posts

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 achieve

I 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>

 

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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"

image.png.8edbb3d481152525d9a8dbd6d6b89061.png

When Margaret is selected, the input field then contains the ID of her record (16)

  • Like 1
Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.