Jump to content


Photo

Need help with Dynamic list menu??


  • Please log in to reply
6 replies to this topic

#1 sjones

sjones
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 01 March 2006 - 07:56 PM

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 vendors table in the database has a field for cat_id, allowing me to cross reference the vendor to the category that they are in. I am working on the add_vendor.php page.
I would like to query the categories table to get the cat_id and cat_name, then create a list menu of the cat_names, so that they could be selected while retaining the cat_id in some type of hidden field so that when they submit the new name of the vendor it would add the cat_id and vendor_name to the vendors 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]

#2 dcro2

dcro2
  • Members
  • PipPipPip
  • Advanced Member
  • 489 posts

Posted 01 March 2006 - 10:26 PM

Um, well something like this?:

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>";


#3 sjones

sjones
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 02 March 2006 - 03:16 AM

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 cat_id from the categories table and hold that value until the submit button is active and then put that value into the vendors table. So what I need to do is get categories.cat_id and put that value in vendors.cat_id Then the text field will enter a string in the vendor_name field of the vendors table and the vendor_id 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>";
?>


#4 dcro2

dcro2
  • Members
  • PipPipPip
  • Advanced Member
  • 489 posts

Posted 02 March 2006 - 02:48 PM

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 'cs_mag.php?c=add_vendor' not 'cs_mag?c=add_vendor.php'

#5 sjones

sjones
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 02 March 2006 - 04:41 PM

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>";


#6 dcro2

dcro2
  • Members
  • PipPipPip
  • Advanced Member
  • 489 posts

Posted 02 March 2006 - 06:43 PM

You copied the else two times, use this:

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>";


#7 sjones

sjones
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 02 March 2006 - 07:00 PM

YOU ARE A GENIUS.
THAT WORKED GREAT!

THANK YOU




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users