Jump to content

Archived

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

sjones

Need help with Dynamic list menu??

Recommended Posts

Hello, I have a page that I am working on. In the admin section I have it setup so the administrator can create their own categories. Also they will be able to create vendors, The [b]vendors [/b]table in the database has a field for [b]cat_id[/b], allowing me to cross reference the vendor to the category that they are in. I am working on the [b]add_vendor.php [/b]page.
I would like to query the [b]categories[/b] table to get the [b]cat_id [/b]and [b]cat_name[/b], then create a list menu of the [b]cat_names[/b], so that they could be selected while retaining the [b]cat_id [/b]in some type of [b]hidden field[/b] so that when they submit the new name of the vendor it would add the [b]cat_id [/b]and [b]vendor_name[/b] to the[b] vendors [/b]table in the database. If you could take a look and give some suggestions. Or start me in the direction of how to create a dynamic list from the database.
[a href=\"http://www.uswebproducts.com/country_savings/admin/csmag_admin.php\" target=\"_blank\"]http://www.uswebproducts.com/country_savin...csmag_admin.php[/a]

Share this post


Link to post
Share on other sites
Um, well something like this?:

[code]if(empty($_POST['submit'])) {
    echo "<span class='content_blank'><strong>Welcome....</strong></span>";
} else {
    if(!empty($_POST['vendorid']) & !empty($_POST['name'])) {
        $qry = mysql_query("INSERT INTO vendors (cat_id,vendor_name) VALUES ('".$_POST['vendorid']."','".$_POST['name']."');");
        if($qry) { echo "<span class='content_blank'><strong>New vendor has been added</strong></span>"; }
        else { echo "<span class='content_blank'><strong>Error inserting new vendor</strong></span>"; }
    } else {
        echo "<span class='content_blank'><strong>Error: something was left blank</strong></span>";
    }
}

    } else {
        echo "<span class='content_blank'><strong>Error: something was left blank</strong></span>";
echo "<table width='100%'  border='0' cellspacing='0' cellpadding='0'>
<tr>
<td>
   <span class='content_blank'>Please choose a category and type in the name for the new vendor.</span><br><br>
   <form method='post' action='csmag_admin.php?c=add_vendor'>
   <select name='vendorid'>
   <option>- Please select one -</option>\n";
   $qry = mysql_query("SELECT * FROM categories;");
   while($rows = mysql_fetch_assoc($qry)) {
       echo "<option value='".$rows['cat_id']."'>".$rows['cat_name']."</option>\n";
   }
   echo "</select>
   <input name='name' type='text' size=20>
   <input name='submit' type='submit' value='Submit'>
   </form>
</td>
</tr>
</table>";[/code]

Share this post


Link to post
Share on other sites
OK, Thank you for your help so far.
Below I will add the code that I have now. I am not understanding how I can call the [b]cat_id [/b]from the [b]categories[/b] table and hold that value until the submit button is active and then put that value into the [b]vendors[/b] table. So what I need to do is get [b]categories.cat_id [/b]and put that value in [b]vendors.cat_id [/b]Then the text field will enter a string in the [b]vendor_name [/b]field of the [b]vendors [/b]table and the [b]vendor_id[/b] is auto_increment and will be added with the query??
I have'nt added everything in the second post because I don't understand it. Here's what I have so far. If anyone can help???
[a href=\"http://www.uswebproducts.com/country_savings/admin/csmag_admin.php?c=add_vendor\" target=\"_blank\"]http://www.uswebproducts.com/country_savin...hp?c=add_vendor[/a]

<?php
include ('../connections/mysql_connect.php');
if(empty($_POST['submit'])) {
echo "<span class='content_blank'><strong>Welcome....</strong><BR></span>
<span class='content_blank'>You can enter a new Vendor by selecting the category that they belong<BR></span>
<span class='content_blank'>and entering the name of the new vendor</span><BR><BR>";
}
echo "<table>
<tr>
<td valign='top'><span class='content_blank'><strong>Select a category</strong></span><BR>";

echo "<form action='csmag_admin?c=add_vendor.php' method='post' name='category'>
<select name='category'>
<option>- Please select one -</option>\n";
$qry = mysql_query("SELECT * FROM categories;");
while($rows = mysql_fetch_assoc($qry)) {
echo "<option value='".$rows['cat_id']."'>".$rows['cat_name']."</option>\n";
}
echo "</select>";

echo "</td>
<td valign='top' class='form_padding'>
<span class='content_blank'><strong>Enter Vendors Name</strong></span><BR><BR>";

echo "<input name='vendor_name' type='text' size=40>
<input name='submit' type='submit' value='Submit'>";
echo "</form>";
echo "</td>
</tr>
</table>";
?>

Share this post


Link to post
Share on other sites
The <select>'s options have the cat_id as the value when you choose them, therefore you don't need to get it out of the database when you are inserting it. What you actually see in the select is the cat_name.
When you insert the new vendor, yes it will auto increment the vendor_id.

PS: the form needs to be submitted to [b]'cs_mag.php?c=add_vendor'[/b] not [b]'cs_mag?c=add_vendor.php'[/b]

Share this post


Link to post
Share on other sites
OK, First of all I would like to thank you for your help.
This is the issue I am having now. When I use this code the page is blank.
?>[a href=\"http://www.uswebproducts.com/country_savings/admin/csmag_admin.php?c=add_vendor\" target=\"_blank\"]Click Here![/a]

Any Ideas??

<?php
include ('../connections/mysql_connect.php');
if(empty($_POST['submit'])) {
echo "<span class='content_blank'><strong>Welcome....</strong><BR></span>
<span class='content_blank'>You can enter a new Vendor by selecting the category that they belong<BR></span>
<span class='content_blank'>and entering the name of the new vendor</span><BR><BR>";
}else{
if(!empty($_POST['vendor_id']) & !empty($_POST['name'])) {
$qry = mysql_query("INSERT INTO vendors (cat_id,vendor_name) VALUES ('".$_POST['vendor_id']."','".$_POST['name']."');");
if($qry) { echo "<span class='content_blank'><strong>New vendor has been added</strong></span>"; }
else { echo "<span class='content_blank'><strong>Error inserting new vendor</strong></span>"; }
} else {
echo "<span class='content_blank'><strong>Error: something was left blank</strong></span>";
}
}

} else {
echo "<span class='content_blank'><strong>Error: something was left blank</strong></span>";
echo "<table width='100%' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td>
<span class='content_blank'>Please choose a category and type in the name for the new vendor.</span><br><br>
<form method='post' action='csmag_admin.php?c=add_vendor'>
<select name='vendor_id'>
<option>- Please select one -</option>\n";
$qry = mysql_query("SELECT * FROM categories;");
while($rows = mysql_fetch_assoc($qry)) {
echo "<option value='".$rows['cat_id']."'>".$rows['cat_name']."</option>\n";
}
echo "</select>
<input name='name' type='text' size=20>
<input name='submit' type='submit' value='Submit'>
</form>
</td>
</tr>
</table>";

Share this post


Link to post
Share on other sites
You copied the else two times, use this:

[code]include ('../connections/mysql_connect.php');
if(empty($_POST['submit'])) {
    echo "<span class='content_blank'><strong>Welcome....</strong><BR></span>
          <span class='content_blank'>You can enter a new Vendor by selecting the category that they belong<BR></span>
          <span class='content_blank'>and entering the name of the new vendor</span><BR><BR>";
}else{
    if(!empty($_POST['cat_id']) & !empty($_POST['name'])) {
        $qry = mysql_query("INSERT INTO vendors (cat_id,vendor_name) VALUES ('".$_POST['cat_id']."','".$_POST['name']."');");
        if($qry) { echo "<span class='content_blank'><strong>New vendor has been added</strong></span>"; }
        else { echo "<span class='content_blank'><strong>Error inserting new vendor</strong></span>"; }
    } else {
        echo "<span class='content_blank'><strong>Error: something was left blank</strong></span>";
    }
}

echo "<table width='100%'  border='0' cellspacing='0' cellpadding='0'>
<tr>
<td>
   <span class='content_blank'>Please choose a category and type in the name for the new vendor.</span><br><br>
   <form method='post' action='csmag_admin.php?c=add_vendor'>
   <select name='cat_id'>
   <option>- Please select one -</option>\n";
   $qry = mysql_query("SELECT * FROM categories;");
   while($rows = mysql_fetch_assoc($qry)) {
       echo "<option value='".$rows['cat_id']."'>".$rows['cat_name']."</option>\n";
   }
   echo "</select>
   <input name='name' type='text' size=20>
   <input name='submit' type='submit' value='Submit'>
   </form>
</td>
</tr>
</table>";[/code]

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.