Jump to content

Editing Form with Select Field


Xtremer360

Recommended Posts

This code is apart of my edit form and for this select field I'm wanting to know how do I find out which is the right style that matches against the database put in the database.

 

<select name="style" class="dropdown">
                        <option value="0">- Select -</option>';
                        <?php
                        $query = 'SELECT name FROM efed_list_styles LIMIT 2';
                        $result = mysql_query ( $query );
                        while ( $row = mysql_fetch_assoc ( $result ) ) 
                        {
                            print "<option value=\"".$row['name']."\">".$row['name']."</option>\r";
                        }
                        ?>
                        </select>

Link to comment
Share on other sites

Perhaps none of that made sense.

 

Say I have a form that calls the data of a particular user and in that data is from a column called style and the value from that column is singles.

 

Now with the select box I want it to scroll through the list of options it has with its little query and select the option called singles to match what's in the DB.

Link to comment
Share on other sites

Heya

 

I'm not 100% sure what you're asking unfortunately.

 

What do you mean by style?  Are we talking CSS, or are we talking about best practices for programming?  Typically, you don't want to have your SQL/database code inline with your presentation code (your html).  It still works, it's just not the best style.

 

Once again - not 100% sure what the question is :)

 

Is your code not working?

Link to comment
Share on other sites

So you have a select box in a form. The values from the select box are pulled from the 'efed_list_styles' table. You have a user table that has a column called style. You want to find out which of the styles from 'efed_list_styles' is in the user table column 'style'. Right so far?

 

If so, all you have to do is add an if statement to check if the user column 'style' matches the 'efed_list_styles' column.

 

I'm going to assume you have already pulled the information from the user's table and it is stored in $user.

 

<select name="style" class="dropdown">
                        <option value="0">- Select -</option>';
                        <?php
                        $query = 'SELECT name FROM efed_list_styles LIMIT 2';
                        $result = mysql_query ( $query );
                        while ( $row = mysql_fetch_assoc ( $result ) ) 
                        {
                            print "<option value=\"".$row['name'] . "\";
                            if ($row['name'] == $user['style']) {
                                  print " SELECTED";
                            }
                            print ">" . $row['name']."</option>\r";
                        }
                        ?>
                        </select>

 

**Untested

 

Should be something like that. Might need to tweak it a little bit.

 

Jeremy

Link to comment
Share on other sites

That is exactly what i'm looking for however what does the user part stand for? This is where my code is from:

 

case 2:
		echo $e;
            require_once('../backstagefunctions.php');
		require_once('../backstageconfig.php');
		$id = $_GET['id']; 
		$query = mysql_query("SELECT * FROM `efed_list_titles` WHERE `id` = '" . $id . "'");
		$row = mysql_fetch_array($query); 
		?>
            <h1 class="backstage">Title Management</h1><br />
            <h2 class="backstage">Edit Title</h2><br />
            <form name="edittitle" method="post">
                <input type="hidden" name="action" value="title" />
                <table width="100%" class="table2">
                    <tr>
                        <td width="120" class="rowheading" valign="center">Short Name:</td><td class="row3"><input type="text" name="shortname" class="fieldtext140" value="<?=$row['shortname'];?>"></td>
                    </tr>
                    <tr>
                        <td width="120" class="rowheading" valign="center">Name:</td><td class="row3"><input type="text" name="name" class="fieldtext490" value="<?=$row['name'];?>"></td>
                    </tr>
                    <tr>
                        <td width="120" class="rowheading" valign="center">Style:</td><td class="row3">
                        <select name="style" class="dropdown">
                        <option value="0">- Select -</option>';
                        <?php
                        $query = 'SELECT name,id FROM efed_list_styles LIMIT 2';
                        $result = mysql_query ( $query );
                        while ( $row = mysql_fetch_assoc ( $result ) ) 
                        {
                            print "<option value=\"".$row['id']."\";
						if ($row['name'] == $user['style']) {
                                  print " SELECTED";
                            }
						print ">" . $row['name']."</option>\r";
                        }
                        ?>
                        </select></td>
                    </tr>
                    <tr>
                        <td width="120" class="rowheading">Status:</td><td class="row3">
                        <select name="status" class="dropdown">
                        <option value="0">- Select -</option>
                        <?php
                        $query = 'SELECT name FROM efed_list_status';
                        $result = mysql_query ( $query );
                        while ( $row = mysql_fetch_assoc ( $result ) ) 
                        {
                            print "<option selected=\"selected\" value=\"".$row['id']."\">".$row['name']."</option>\r";
                        }
                        ?>
                        </select></td>
                    </tr>
                </table><br />
                <input type="checkbox" name="deletetitle"><span class="table1heading">Delete Title?</span><br /><br />
                <input type="hidden" name="oldname" value="<?php echo $row['name']; ?>">
			<input type="hidden" value="true" name="editted" />
			<input type="submit" value="Edit Title" class="button"><br /><br />
                <input type="button" value="Return to Title List" class="button200"><br /><br />
                </form>
		<?php
		returnmain();
	break;

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.