looking for a point in the right direction

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpstudent
Forum Newbie
Posts: 10
Joined: Wed Feb 04, 2009 11:54 pm

looking for a point in the right direction

Post by phpstudent »

Just started using php this week, and have had success with creating a form i can enter data into, get it sent into a mysql table, and get it to display on various pages on my site. took me a while, but i got a pretty good grasp of how to do this.

now im ready for the next step, and have searched google and this site for a couple hours, but cant seem to find what im after. i think maybe i dont know the proper terms to search for what im after, maybe you could point me in the right direction for some tutorials

in the form i have now, it is taking 2 simple fields... a date, and a news item. what i would like, would be to have this form with the same two input fields, but a checkbox that would tell the script which table to send the data to. i would have two tables with the same structure, but want two different tables so i can pull one type of news to certain pages, and other news to other pages.

is it possible to have a checkbox under the form that indicates which table the data is to be sent to?

if you can point me in the right direction i would greatly appreciate it

this is the code i have for the table i set up and is working, just want to make the choice of which script the data is sent to:

Code: Select all

<FORM ACTION="formindb.php" METHOD="POST" NAME="whatsnew_form">
<TABLE>
 <TR>
  <TD>Date:</TD>
  <TD> <input type="text" name="date"></TD>
 </TR>
 <TR>
 <TD>News:</TD>  <TD>
    <input type=text name="news" style="width: 371px; height: 22px"></TD>
 </TR>
 <TR>
  <TD><input type="submit" value="Submit" name="Submit"></TD>
  <TD><input type="reset" value="Reset" name="Reset"></TD> 
 </TR>
</TABLE>
</FORM>
 
thanks again
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: looking for a point in the right direction

Post by Skoalbasher »

Do you mean the checkbox will decide witch table to query from in th db?
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: looking for a point in the right direction

Post by watson516 »

If you only want the ability to use one table, you could use radio buttons and then on the process page, use a switch statement on the values of the radio buttons.

Code: Select all

<?php
$radioButton=$_POST['usetable'];
switch($radioButton)
{
     case "table1":
           $q="INSERT INTO table1...";
           break;
     default:
           $q="INSERT INTO table2...";
}
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: looking for a point in the right direction

Post by califdon »

First of all, congratulations on making that much progress in your first week.

There's an underlying issue that I'll address here. You can certainly do what you described, but the more important question is Why would you want to? It is bad database design. Your table should have another field for NewsType, probably an Integer field that refers to another table, if there are more than 3 or 4 types. Then when you're searching for news of type 3, your SQL query just includes "...WHERE NewsType=3". You should not split data into different tables for that kind of a purpose. Any time there are 2 (or more) tables with exactly the same structure, it's an immediate alarm signal that's it's not designed correctly.

With one table, as described above, your question now becomes, How do I search for one particular type of news? The usual approach is to use a <SELECT><OPTION> HTML element to make a dropdown list from which the user selects the desired type. Typical coding would be like this:

Code: Select all

echo "<select name='NewsType'>";
echo "  <option value=1>International</option>";
echo "  <option value=2>National</option>";
echo "  <option value=3>Local</option>";
echo "</select>";
Now you have a new form variable "NewsType" with a value of 1, 2, or 3, which you can use in your SQL to query the desired data from the database.
phpstudent
Forum Newbie
Posts: 10
Joined: Wed Feb 04, 2009 11:54 pm

Re: looking for a point in the right direction

Post by phpstudent »

thanks, i took your advice and created my second table to have different structure, at least different field names.

i solved the problem i was having by using multiple submit boxes, and for each submit button i changed the INSERT INTO table command.

thanks again for your advise, im sure i will be back for more ideas soon :)
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: looking for a point in the right direction

Post by califdon »

Yeh, the problem with hypothetical projects like that is that there are a myriad of ways you might accomplish something, and for each real-life situation, a different solution would be appropriate. Your solution would be fine for some situations, but not for others. As a long-time teacher of college database courses, my slant on it is that you will do better by concentrating on some realistic projects, rather than just making up theoretical databases. Not that there's anything wrong with making things up, but you'll be exercising your thought processes on situations that may not be at all similar to anything that you would ever encounter in the real world. In the long run, that's not an efficient way to learn what you will later need. Just my perspective.
Post Reply