Question

I can't seem to find a workaround for this.

I have a smarty based script that im using.

I am trying somehow submit 2 values from a form that is on my submit post page. I am actually successfully posting several fields into the "posts" table already from my current form.

However, here's where my problem is, I need to get both the category name and the category id into to their own columns. Right now I can only choose one or the other.

My form contains this :

<select name="category" id="category" >
    {section name=i loop=$msgcat}
        <option value="{$msgcat[i].catname|stripslashes}">{$msgcat[i].catname|stripslashes}</option>
    {/section}
</select>

Which will render this in html as :

<select id="category" name="category">
<option value="Car">Car</option>
<option value="Truck">Truck</option>
<option value="Boat">Boat</option>
<option value="Motorcycle">Motorcycle</option>
<option value="RV">RV</option>
<option value="Other">Other</option>
</select>

Is there a way in this same form I can have a hidden input field called catid which could assign the associated category id "value" based on which "category" drop down selection is made?

Something like "if category selection = Car, then hidden input value = 1" (would have to include a rule for each dropdown selection)

Would this be done in php? javascript?

Is there a much better way to do what im trying to do?

Right now the "category" drop down field gets submitted properly to the "posts" table into the "category" column. Respectively, I need the catid to go into the "catid" column in the same table.

The catid is a defined value for each category, e.g. Car=1, Truck=2, Boat=3...

Where 1 is the catid for "Car" ...and so on.

Was it helpful?

Solution

So you have both category and a catid column in your posts table? You should normalize your table. Only have catid in your posts table and reference it in a categories table.

If you don't normalize, later on, what if you decide to rename your category Car to Sedan. Then you will have to change every category column in your posts table. If you normalize, you will only need to change one row in your categories table, and change that Car to Sedan and all your posts will show the updated category name.

Example posts table:

id | catid | title | content | date
1  | 1     | something about cars | the content of the post | 2013-10-01

Example categories table:

id | name
1  | Car
2  | Truck
3  | Boat

http://en.wikipedia.org/wiki/Database_normalization

If you don't want a normalized database, then in your select dropdown, you pass your catid, and in your PHP script, get the ID and look for the corresponding category name from the categories table.

UPDATE

Use MySQL JOIN to get the category name while pulling your post.

To get all posts:

SELECT posts.*, categories.name as category FROM posts p JOIN categories c ON p.catid = c.id

To get a post by ID:

SELECT posts.*, categories.name as category FROM posts p JOIN categories c ON p.catid = c.id WHERE posts.id = YOUR_ID

Then when display your post, you can just do:

echo $post['category'] and it will print out the category name

UPDATE 2

Then do as @jeff says. Put a pipe in your SELECT OPTION values.

<select name="category">
<option value="1|Car">
<option value="2|Truck">
</select>

In your PHP, do the following:

$category = $_POST['category'];
//Explode the category, this will generate an array 
$category = explode('|', $category);
//the $category will result in array('1', 'Car')
$cat_id = $category[0];
$cat_name = $category[1];
//Now you have both your category ID and Name

Read up on PHP Explode

OTHER TIPS

Just place the information within the value attribute. I use the | to separate the data and on the server-side split this information out. Assumming that you are using $msgcat[i].catid as your id variable

<select name="category" id="category" >
    {section name=i loop=$msgcat}
        <option value="{$msgcat[i].catid}|{$msgcat[i].catname|stripslashes}">{$msgcat[i].catname|stripslashes}</option>
    {/section}
</select>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top