Jump to content

Archived

This topic is now archived and is closed to further replies.

DaVuLf

Dynamic Form Dropdown?

Recommended Posts

Is it possible to have the dropdown area of a form be dynamic? By this I mean equivalent to the names of different rows in a database?

Suppose I have a database with a column named 'names':

[code]
Names:
Bob
Jill
Tina
[/code]

Every time someone buys something from me, their name gets added to the list. Is there any way I could have a dropdown menu that lists all the people in the table? (Grouped of course).

Thanks so much,
DaVuLf :D

[b]Quick Edit:[/b] Another quick question I had was if I could make my .php redirect after it's finished. I have a form, when I submit it, it posts everything and runs a php. Then it is just left on a blank php page. How can I have it redirect back to the start?

Share this post


Link to post
Share on other sites
yes, (example makes the customer's id the value, and the name shows in the box):

[code]
$dropdown = '<select name="product_name">';

while($custinfo = mysql_fetch_array($query)) {
   $custid = $custinfo['custid'];
   $custname  = $custinfo['name'];
   $dropdown .= "<option value='".$custid."'>".$custname."</option>";
}
$dropdown .= '</select>';
[/code]

and yes, you can redirect with a header function like so:

[code]
header(Location: blah.php);
[/code]

please note that that function will not work if there is ANY html output before it.

Share this post


Link to post
Share on other sites
What do you mean by

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--](Grouped of course)[/quote]

Share this post


Link to post
Share on other sites
i think what he means is that his table has rows for every time someone buys something so their name will be repeated throughout the table so he will need to do a group by in order to return the names only once? anyways, that's a query string thing. my code assumes he has all that sorted out.

Share this post


Link to post
Share on other sites
Right. Thanks a lot for the code Violet. That's exactly what I'm looking for.

Here is another issue I'm having along the same lines:

[code]
//Grab variables from our form.
$team1=$_POST['team1'];
$team2=$_POST['team2'];
$team3=$_POST['team3'];
$team4=$_POST['team4'];
$team5=$_POST['team5'];

$i=1;

while ( $i <= 5 ){
    $increment="team$i";
    $name="$".$increment;
    echo $name;
    $query = "INSERT INTO teams VALUES ('$name','')";
    mysql_query($query);
    $i++;
}
[/code]

I'm trying to increment the variable while in the loop in order to insert the $team variables into the table. I could technically just copy and paste it.. But I'm sure there must be a way to do this. My echo returns:

[code]
$team1$team2$team3$team4$team5
[/code]

So its getting the $team right, but it isn't registering that this is a variable. Any suggestions?

Thanks,
DaVuLf

Share this post


Link to post
Share on other sites
[!--quoteo(post=375347:date=May 19 2006, 09:48 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 19 2006, 09:48 PM) [snapback]375347[/snapback][/div][div class=\'quotemain\'][!--quotec--]
i think what he means is that his table has rows for every time someone buys something so their name will be repeated throughout the table so he will need to do a group by in order to return the names only once? anyways, that's a query string thing. my code assumes he has all that sorted out.
[/quote]

In that case,

[code]SELECT DISTINCT name FROM tablename[/code]

will eliminate duplicates.

As for second problem, naming the formfields "team[]" will remove the need for $team1, $team2 etc. The fields will be posted in an array EG

[code]<?php
if (isset($_POST['submit'])) {
    foreach ($_POST['team'] as $teamname) {
         if ($teamname)
        mysql_query("INSERT INTO teams (name) VALUES ('$teamname')");
    }
}

?>
<FORM method='POST'>
Team 1 <input type="text" name="team[]">
Team 2 <input type="text" name="team[]">
Team 3 <input type="text" name="team[]">
Team 4 <input type="text" name="team[]">
Team 5 <input type="text" name="team[]">
<input type="submit" name="submit" value="Submit">
</FORM>[/code]

Share this post


Link to post
Share on other sites
Thanks Barand.

So, the top portion of that snippet is for the .php, and the bottom is for the .html, right?

[b]Edit:[/b] Wait wait wait... Are you telling me you can have a form send to itself?... Wow... Okay, so if that is true, how would I tell it to forward somewhere after you click on that?

Share this post


Link to post
Share on other sites
Yes if you have separate files. In which case you need to specify "action=somepage.php" in the form tag.

You could put it all in one .php file as I have.

Share this post


Link to post
Share on other sites
Okay, so I tried Crayon's method with the dropdown, but I couldn't get it to work. I went with this instead:

[code]
<?
                $phpEx = 'php';
                $root_path = './';
                include($root_path . 'includes.'.$phpEx);
                
                $res=mysql_query("SELECT * FROM teams order by name") or die(mysql_error());
                echo "<select name="."team".">";
                while($row=mysql_fetch_assoc($res)) {
                echo "<option value=$row[ID]>$row[name]</a></option>";
                }
                echo "</select>";
            ?>
[/code]

I have the name set to 'team'. I then try to access it using this:

[code]
$team=$_POST['team'];
[/code]

But this returns nothing. I'm not sure why it does this. Any ideas?

Thanks again,
DaVuLf [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]

Share this post


Link to post
Share on other sites
okay now i'm getting kinda confused here.

are you saying that you want 5 dropdown menus, 1 for each team and each one is populated with their own stuff like this:

label [dropdownbox] (choices offered)
team1 [..................] (selection choices: 1a,1b, 1c, 1d)
team2 [..................] (selection choices: 2a, 2b, 2c, 2d)
team3 [..................] (selection choices: 3a, 3b, 3c, 3d)
team4 [..................] (selection choices: 4a, 4b, 4c, 4d)
team5 [..................] (selection choices: 5a, 5b, 5c, 5d)

and you want to select an option from each box, hit submit, and it inserts a new row into your table for each one?

Share this post


Link to post
Share on other sites
[!--quoteo(post=375411:date=May 19 2006, 09:11 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 19 2006, 09:11 PM) [snapback]375411[/snapback][/div][div class=\'quotemain\'][!--quotec--]
okay now i'm getting kinda confused here.

are you saying that you want 5 dropdown menus, 1 for each team and each one is populated with their own stuff like this:

label [dropdownbox] (choices offered)
team1 [..................] (selection choices: 1a,1b, 1c, 1d)
team2 [..................] (selection choices: 2a, 2b, 2c, 2d)
team3 [..................] (selection choices: 3a, 3b, 3c, 3d)
team4 [..................] (selection choices: 4a, 4b, 4c, 4d)
team5 [..................] (selection choices: 5a, 5b, 5c, 5d)

and you want to select an option from each box, hit submit, and it inserts a new row into your table for each one?
[/quote]

Not quite. Its one dropdown box, with values populated by a mySql database. Then, when I submit the form, I have to use whatever value was selected as a variable.

The site is here:
[a href=\"http://www.briancomeau.net/wdt/input.php\" target=\"_blank\"]http://www.briancomeau.net/wdt/input.php[/a]

The 'Team' dropdown is the one in question. If I run it with the current code, everything works, but when I try to access the $team variable (acquired from the POST method) it returns blank, and so I cannot use it to access more tables.

Thanks for the help :).

Share this post


Link to post
Share on other sites
well i went to the linkie and entered in some stuff and clicked the button and i got this:

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/content/b/r/i/briancomeau/html/wdt/insert.php on line 25

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/content/b/r/i/briancomeau/html/wdt/insert.php on line 28

which means either your insert query string is wrong or else you failed to establish a connection or something. what are those lines?

Share this post


Link to post
Share on other sites
[!--quoteo(post=375413:date=May 19 2006, 09:19 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 19 2006, 09:19 PM) [snapback]375413[/snapback][/div][div class=\'quotemain\'][!--quotec--]
which means either your insert query string is wrong or else you failed to establish a connection or something. what are those lines?
[/quote]

[code]
//Make a table if it doesn't already exist.
$query="CREATE TABLE $team (id int(6) NOT NULL auto_increment,team varchar(25) NOT NULL,stock varchar(4) NOT NULL,type varchar(20) NOT NULL,time int(6) NOT NULL,quantity int(20) NOT NULL,value double NOT NULL, worth double NOT NULL, balance double NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
$result=mysql_query($query);

//If we created a table, then set the last balance to $100,000
//If we didn't create a table, then we need to look up the last balance.
if($result){
    $last_balance = 100000;
} else {
    echo $team;
    $query="SELECT * FROM $team";//row 23
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    $find_balance="SELECT balance FROM $team WHERE id = '$num'";
    $find_result= mysql_query($find_balance);
    $last_balance= mysql_result($find_result,0,0);//row 28
};
[/code]

Rows 13-29 (unfortunately it doesn't write the numbers)

Because row 23 references $team, I will post how that is found:
[code]
//Grab variables from our form.
$team=$_POST['team'];
echo $team;
$stock=$_POST['stock'];
$type=$_POST['type'];
$quantity=$_POST['quantity'];
[/code]

It is found from the POST method. Unfortunately, it doesn't return anything, which is why line 23 shows an error, as clearly a null table doesn't exist. What I'm not understanding is the 'why' behind it.

Thanks for helping out, I know this is bothersome. [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]

Share this post


Link to post
Share on other sites
Viewing the source of your site the option values aren't getting printed which is why you aren't able to use the teams values in the next query you do.

Check your mysql query when you list the the team name and id. Is id really ID?

[code]
while($row=mysql_fetch_assoc($res)) {
                echo "<option value=$row[ID]>$row[name]</a></option>";
                }
[/code]

Share this post


Link to post
Share on other sites
[!--quoteo(post=375418:date=May 19 2006, 09:42 PM:name=yonta)--][div class=\'quotetop\']QUOTE(yonta @ May 19 2006, 09:42 PM) [snapback]375418[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Viewing the source of your site the option values aren't getting printed which is why you aren't able to use the teams values in the next query you do.

Check your mysql query when you list the the team name and id. Is id really ID?

[code]
while($row=mysql_fetch_assoc($res)) {
                echo "<option value=$row[ID]>$row[name]</a></option>";
                }
[/code]
[/quote]

Hah! I love you. I noticed that too, but thought it was just one of those mySql global things. Thanks alot man.

Share this post


Link to post
Share on other sites
Okay, so I've got most of that working, but for some reason I've run into a problem with code that used to work. The weird thing is that the output seems right:

[code]
//Make a table if it doesn't already exist.
$query="CREATE TABLE $team (id int(6) NOT NULL auto_increment,team varchar(25) NOT NULL,stock varchar(4) NOT NULL,type varchar(20) NOT NULL,time int(6) NOT NULL,quantity int(20) NOT NULL,value double NOT NULL, worth double NOT NULL, balance double NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
$result=mysql_query($query);
$result_find = mysql_fetch_assoc($result) or die(mysql_error());;

echo $result_find;

//If we created a table, then set the last balance to $100,000
//If we didn't create a table, then we need to look up the last balance.
if(!$result){
    $query="SELECT * FROM $team";
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    $find_balance="SELECT balance FROM $team WHERE id = '$num'";
    $find_result= mysql_query($find_balance);
    $last_balance= mysql_result($find_result,0,0);
} else {
    $last_balance = 100000;
};
[/code]

Output:
[code]
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/b/r/i/briancomeau/html/wdt/insert.php on line 15
Table 'Paco' already exists
[/code]

Now, I originally wrote this code because the tables weren't created prior to this. It seems like I've created the tables already. I'm just wondering how I could write a condition like 'if $num=0, $last_balance =100000'..........

I think I just answered my own question. Thanks :).

Share this post


Link to post
Share on other sites
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]I'm just wondering how I could write a condition like 'if $num=0, $last_balance =100000'

I think I just answered my own question. Thanks :).[/quote]
As long as you begin it with == for the comparison operator [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

Share this post


Link to post
Share on other sites
[!--sizeo:5--][span style=\"font-size:18pt;line-height:100%\"][!--/sizeo--]mysql_num_rows[!--sizec--][/span][!--/sizec--]

Share this post


Link to post
Share on other sites

×

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.