Jump to content

Help setting up variable array


stangen

Recommended Posts

Ok I am not sure if this is even the proper way to do it.

 

I have a form setup to add data to mysql.  Right now I have a table called "messages" and inside it I have "id,name,message,category"

 

I have the "category" column setup to be a numeric value between 1-4, which is selected in the intial form as shown here:

<form action="addmessage.php" method="post">
Title:<input type="text" id="name" name="name" /></input><br>
Message:       <textarea id='message' name="message" cols="30" rows="2"></textarea><br>
Category: <select name="category" id="category"><option value="1">User Related</option><option value="2">Guide related</option><option value="3">Code Related</option><option value="4">Misc.</option></select>
<input type="submit" value="submit" />
</form>

So what I am wanting to do now is make it to where the numeric value displays a variable defined in another table. Say I started a table called 'categories' where the id corresponds to a text value.

 

The whole idea is to have another form setup to add a name for the category so that the user can change category names as needed. There will only be a 4 values and these will only be called on for the drop down in the above code and on anothe page that displays all the data from the messages table.

 

Am I over thinking this process?

Link to comment
Share on other sites

After you set up the category table you will need to do the following:

 

1. For the existing form above you would create a process to query the category table to dynamically create the select options. If the form will be used to edit existing records, then you will need to build that process to set the selected value based on the current record.

 

2. For the pages that display the records, you should already have a query to get those records. You will want to change those queries to JOIN the category table so you have the name of the category. Here is an example

 

SELECT messages.name, messages.message, categories.category_name
FROM messages
JOIN categories ON messages.category_id = categories.category_id

 

3. Create a management page for the categories. Since the list will be very short I would just have a single page to edit all of the categories at one time. Just create a form with an input field for each category name. The input field could look something like this:

 

<input type="text" name="category[{$idFromDB}]" value="{$valueFromDB}" />

The name will be an array and the id will be the numerical primary key value for each category. Then when the page is submitted, loop through each record in the array to get the ID and the value and update the records in the database.

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.