Jump to content


Photo

Displaying Information from a Database in a Drop down form?


  • Please log in to reply
10 replies to this topic

#1 Andrew R

Andrew R
  • Members
  • PipPipPip
  • Advanced Member
  • 158 posts
  • LocationIreland

Posted 03 August 2006 - 12:23 PM

Hello

I was wondering how I could display all the users from my database in the drop down list so when I submit the form (viewusers.php) it displays what user I selected from the drop down form in viewusers.php.

At the top of the page I have all the database information and the sql query.

<table width="925" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr>
    <td width="104" height="24" valign="middle">User:</td>
    <th width="221" valign="middle" scope="col"> <form action="viewusers.php" method="post" name="user" id="user">
        <div align="left">
          <select name="formquery" class="table" id="formquery">
          </select>
          <input name="formtype" type="hidden" id="formtype" value="user">
          <input name="Submit" type="submit" class="table" value="Go!">
        </div>
      </form></th>
    <th width="592">&nbsp;</th>
  </tr>
</table>


Help would be much appreciated.

Cheers


#2 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 03 August 2006 - 12:34 PM

I hope this helps

$query = "SELECT * FROM users WHERE username LIKE '%".$usersearch."'";
$result = mysql_query($query);

if(mysql_num_rows($result) == 0){

echo "I am afraid no users were found";
}else{
echo "<form method='post' action='page.php'>\n"
."<select name='users'>\n";
while($row = mysql_fetch_assoc($result)){
echo "<option>".$row["username"]."</option>\n";
}
echo "</select>\n"
."</form>\n";

}

Tell me the problem, I will try tell you the solution

#3 Andrew R

Andrew R
  • Members
  • PipPipPip
  • Advanced Member
  • 158 posts
  • LocationIreland

Posted 03 August 2006 - 01:37 PM

Cheers for the help onlyican although I was trying to do it this way (Below).  The problem I am having using the below script is that the drop down form only displays one username, not all the users in the database.  Any ideas on how to fix it?

<?php
include 'db.php';

$query_users = "SELECT * FROM users";
$users = mysql_query($query_users) or die(mysql_error());
$row_users = mysql_fetch_assoc($users);
php ?>
<table width="925" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr> 
    <td width="104" height="24" valign="middle">Day:</td>
    <td width="438" valign="top"><form action="viewusers.php" method="post" name="user" id="user">
        <div align="left"> 
          <input name="formtype" type="hidden" id="formtype" value="users">
          <select name="formquery" id="formquery">
            <option value="<?php echo $row_users['username']?>"><?php echo $row_users['username']?></option>
          </select>
          <input name="Submit" type="submit" class="table" value="Go!">
        </div>
      </form></td>
    <td width="383">&nbsp;</td>
  </tr>
</table>


#4 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 03 August 2006 - 01:43 PM

you need to loop it in a while loop
<select name="formquery" id="formquery">
<?
while ($row = mysql_fetch_assoc($users){
echo "<option>".$row["users"]."</option>\n";
}
?>
</select>

Note: if the value of the option is the same as whats between the two options, you dont need to add a value. It will still work
Tell me the problem, I will try tell you the solution

#5 CTM

CTM
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 03 August 2006 - 01:47 PM

That's because you weren't looping through all the results, simply using the first one. Now, onlyican's solution works, but you could always do something like :

<?php
include 'db.php' ;  // Including the DB configuration, connection and such I presume

$query_users = "SELECT username FROM users" ;   // Instead of *, since you're only using the username field, only fetch that (for memory issues)
$users = mysql_query ( $query_users ) or die ( mysql_error ( ) ) ;

$select_options = array ( ) ;   // Instead of using a string, we'll use an array into which we'll store every result (in the <option> tag form)
while ( $row_user = mysql_fetch_assoc ( $users ) )   // Loop it up
{
   // The [] after select_options indicates to append a new value to the select_options array
   $select_options[] = "<option value = '" . $row_user['username'] . "'>" . $row_user['username'] . "</option>" ;
}

$select_options = implode ( "<br />", $select_options ) ; // I'm simply using this (and the array form) for formatting preferences (when viewing the HTML source)

// Now, you have a long string which contains every option that goes inside your select element.

?>

<table width="925" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr> 
    <td width="104" height="24" valign="middle">Day:</td>
    <td width="438" valign="top"><form action="viewusers.php" method="post" name="user" id="user">
        <div align="left"> 
          <input name="formtype" type="hidden" id="formtype" value="users">
          <select name="formquery" id="formquery">
            <?php print $select_options ; ?>
          </select>
          <input name="Submit" type="submit" class="table" value="Go!">
        </div>
      </form></td>
    <td width="383">&nbsp;</td>
  </tr>
</table>

Anyway, onlyican's should work fine too, but it can't be re-used.

#6 Andrew R

Andrew R
  • Members
  • PipPipPip
  • Advanced Member
  • 158 posts
  • LocationIreland

Posted 03 August 2006 - 02:38 PM

Cheers guys, I tried both scripts, both worked well although I also want to add another drop down box containing the location of all of the members, the problem is USA and UK are being displayed over and over again in the drop down box because they are where most our members are from, how would I stop that so the USA and the UK will only display once in the drop down?

Cheers.

#7 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 03 August 2006 - 02:39 PM

use another query, and add GROUP BY country

Tell me the problem, I will try tell you the solution

#8 CTM

CTM
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 03 August 2006 - 06:06 PM

You wouldn't really need a second query if using my method, but then again, a second query would speed the process of treating the country info. I'm not sure in memory usage which one would be faster, but the second query is definitively simpler.

#9 Andrew R

Andrew R
  • Members
  • PipPipPip
  • Advanced Member
  • 158 posts
  • LocationIreland

Posted 03 August 2006 - 08:08 PM

Thanks guys for your help.  To add a second select_options array would I simply add

$select_options[2] = "<option value = '" . $row_user['username'] . "'>" . $row_user['username'] . "</option>" ;

below the first select array?

Cheers

#10 CTM

CTM
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 03 August 2006 - 08:15 PM

No. And I'm not sure what you want. You'd want an exact replica of the dropdown options above? Or another one with different info?

As I explained my comments, the $select_options[] lines is simply to indicate that you're appending a new value into the $select_options array (which, in that instance, was the usual "<option value='username'>username</option>"). $select_options[2] will simply return the third iteiration of that array, which means a singleton of type "<option value='username'>username</option>".

Anyway, explain what you want.

#11 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 03 August 2006 - 08:20 PM

Try this please.

<?php
include 'db.php';

$query_users = "SELECT * FROM users";
$users = mysql_query($query_users) or die(mysql_error());
while($row_users = mysql_fetch_assoc($users)) {
php ?>
<table width="925" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr> 
    <td width="104" height="24" valign="middle">Day:</td>
    <td width="438" valign="top"><form action="viewusers.php" method="post" name="user" id="user">
        <div align="left"> 
          <input name="formtype" type="hidden" id="formtype" value="users">
          <select name="formquery" id="formquery">
            <option value="<?php echo $row_users['username']?>"><?php echo $row_users['username']?></option>
          </select>
          <input name="Submit" type="submit" class="table" value="Go!">
        </div>
      </form></td>
    <td width="383">&nbsp;</td>
  </tr>
</table>
<?}?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users