1

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.

Zack Bluem
  • 83
  • 1
  • 2
  • 11

2 Answers2

3

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

WebNovice
  • 2,230
  • 3
  • 24
  • 40
  • I do have a category table that is seperate that has the category name and the catid, but I ran into a problem on another page when trying to get the info I needed, because I was trying to show on the post the category name and have that hyperlink to pull all posts under that category it wasn't working properly. So, I had to do this, even though I agree it should be better. At this point I just need a hack to make it work. – Zack Bluem Oct 08 '13 at 10:40
  • Updated my answer to use JOINS – WebNovice Oct 08 '13 at 10:43
  • I truly appreciate your advice. But I really can't seem to follow. If I could just add the catid to the posts table and keep categories in there as it as (for now) ....then I can complete what I need to achieve. I just need to find out how I can pass the cat id based on the selection. I don't understand how to join as you illustrated. I am displaying all the posts with the category name on them, once clicked the category name shows all posts in that category by using the catid. I just need the catid in the same table. – Zack Bluem Oct 08 '13 at 11:00
  • If you are going to use both the `catid` and the `categoryname` in the `posts` table, then why do you even have a `category` table? – WebNovice Oct 08 '13 at 11:04
  • because it was orginally intended to work differently, until I ran into this problem. So, creating this hack will render that table useless for now. But, I will have to fix later. Right now, I just need to complete. – Zack Bluem Oct 08 '13 at 11:09
  • got your "update 2" to work. Many Thanks. I was trying javascript unsuccessfully like this : `` and adding this to my select `onchange="category.value=options[selectedIndex].value;categoryval.value=options[selectedIndex].text"` – Zack Bluem Oct 08 '13 at 12:33
  • Glad I could help. I don't know what your javascript does. But you cannot send two values from an SELECT element. – WebNovice Oct 08 '13 at 13:13
  • Amazing answer, this saves my time. – Sandeep Londhe Dec 15 '16 at 20:08
0

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>
  • then how would I enter just the catid into its own column in that table? That is what is needed, because I am displaying the category on the front end of the website. So I don't want it to be displayed as 1 Car. I want to be able to display how I currently have it "category name" and be able to create a hyperlink for that by grabbing the catid like this `{$posts[i].category|stripslashes}` – Zack Bluem Oct 08 '13 at 10:51