Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

IF statements with form variables

Guest
Apr 14, 2007 Apr 14, 2007
I have a jump box on a page with three values, I want my record set to change depending on what is selected in the jump box but so far all I've got is errors. I basically plan on outputting a table where the column matches the one specified in the form, I might be doing this completely wrong as I think the IF statement selects the contents of a column where a match is found but I'm a little clueless.

Heres the code I'm trying to use. I've been looking into the CASE statement but cant make much sense of it.

IF ("'.$_POST['TASKS']."' = Service) THEN SELECT Service FROM DETAILS
ELSE IF ("'.$_POST['TASKS']."' = Physical) THEN SELECT Physical FROM DETAILS
ELSE ("'.$_POST['TASKS']."' = Skill) THEN SELECT Skill FROM DETAILS

Obviously all my select statements would be different in the final version but at the moment I'm just trying to get it to work.

Thanks
TOPICS
Server side applications
437
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 14, 2007 Apr 14, 2007
On Sat, 14 Apr 2007 18:40:27 +0000 (UTC), "slowpoke115"
<webforumsuser@macromedia.com> wrote:

>That kinda works but the reason I wanted to use an if statement was because i
>need some other information from the matching record set. With the jump box I
>want three things to be selectable, when one is selected it is used as a field
>(all this works with the code you provided me with) BUT I also need other
>information dependant upon the jump box, in this case it is
>service/physical/skill co...
Translate
LEGEND ,
Apr 14, 2007 Apr 14, 2007
On Sat, 14 Apr 2007 14:08:57 +0000 (UTC), "slowpoke115"
<webforumsuser@macromedia.com> wrote:

> IF ("'.$_POST['TASKS']."' = Service) THEN SELECT Service FROM DETAILS
> ELSE IF ("'.$_POST['TASKS']."' = Physical) THEN SELECT Physical FROM DETAILS
> ELSE ("'.$_POST['TASKS']."' = Skill) THEN SELECT Skill FROM DETAILS

Not sure what you're trying to do, but what about this?

$query="SELECT $_POST[TASKS] FROM DETAILS";

Gary
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 14, 2007 Apr 14, 2007
That kinda works but the reason I wanted to use an if statement was because i need some other information from the matching record set. With the jump box I want three things to be selectable, when one is selected it is used as a field (all this works with the code you provided me with) BUT I also need other information dependant upon the jump box, in this case it is service/physical/skill completion date. My database is made in such a way that each one is just service_date/physical_date/skill_date. could I just do "$_POST[TASKS] _date" or something fancy like that? Whats the best way of doing this? Sorry its so unclear

Thanks
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 14, 2007 Apr 14, 2007
On Sat, 14 Apr 2007 18:40:27 +0000 (UTC), "slowpoke115"
<webforumsuser@macromedia.com> wrote:

>That kinda works but the reason I wanted to use an if statement was because i
>need some other information from the matching record set. With the jump box I
>want three things to be selectable, when one is selected it is used as a field
>(all this works with the code you provided me with) BUT I also need other
>information dependant upon the jump box, in this case it is
>service/physical/skill completion date. My database is made in such a way that
>each one is just service_date/physical_date/skill_date. could I just do
>"$_POST[TASKS] _date" or something fancy like that? Whats the best way of doing
>this? Sorry its so unclear

Okay, I'm still a bit in the dark about what you want, but perhaps this
will get you closer:

IF ("'.$_POST['TASKS']."' = Service) THEN SELECT Service FROM DETAILS
ELSE IF ("'.$_POST['TASKS']."' = Physical) THEN SELECT Physical FROM
DETAILS
ELSE ("'.$_POST['TASKS']."' = Skill) THEN SELECT Skill FROM DETAILS

switch($_POST['TASKS']){
case 'Service':
// build your query here
break;
case 'Physical':
// build your query here
break;
case 'Skill':
// build your query here
break;
}

Gary
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 14, 2007 Apr 14, 2007
Well I've been playing with my code a bit and ogt this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF ('SERVICE' = Service) THEN SELECT FIRSTNAME, SURNAME, SERVICE, SERVICE_DATE F' at line 1

With this code: "IF ('".$_POST['TASKS']."' = Service) THEN SELECT FIRSTNAME, SURNAME, SERVICE, SERVICE_DATE FROM DETAILS

Theres some more else statements to go with it too (obviously) but there all structured the same way. Thanks so much for all your help!


I'll try to explain what I'm trying to do more: the site is intended for staff members at a school who wish to see which students have completed certain tasks (physical, skill or whatever). I've made a drop down box containing all the relevant values and upon pressing submit I wanted my next page to display the full name of the student, the task theyve completed and the date they did it. In the SKILLS field for example there will be stuff like piano and under skill_date it will be the date they played the piano. So my output would be John Smith Piano 2007-11-11 (and all other students who have specified a date and completed a skill). So I need some not null code for empty fields where students havent completed the task but thats beyond what I'm doing.


Thanks once again,
sorry for being such a pain in the arse! I'm kinda knew to PHP but I realise I've set myself a huge challenge! I owe you one Gary 😄
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 14, 2007 Apr 14, 2007
On Sat, 14 Apr 2007 22:03:08 +0000 (UTC), "slowpoke115"
<webforumsuser@macromedia.com> wrote:

> With this code: "IF ('".$_POST['TASKS']."' = Service) THEN SELECT FIRSTNAME,
>SURNAME, SERVICE, SERVICE_DATE FROM DETAILS

That couldn't possibly work. For one thing, the test for equality
requires a double equal sign and for another, you don't have quotes
around the SELECT and never really do anything with it. Did you try the
other code I posted with the switch in it?

Gary
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 14, 2007 Apr 14, 2007
Just got it working mate, your a life saver, fantastic coding thank you so much!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 15, 2007 Apr 15, 2007
LATEST
On Sun, 15 Apr 2007 00:26:29 +0000 (UTC), "slowpoke115"
<webforumsuser@macromedia.com> wrote:

>Just got it working mate, your a life saver, fantastic coding thank you so much!

You're welcome.

Gary
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines