simflex Posted January 27, 2010 Share Posted January 27, 2010 Greetings experts: I am an extreme newbie in PHP and so, please, please be gentle. I gobbled up this script that prints out employee name, sex, and dept. What I really would like help on is how to add an if conditional. For instance, rather than display the records as they are being displayed now like: Employee Name EmploymentType Dept John Peters PT IT Jane Doe FT Administration, I would like an if statement like: If EmploymentType = 'PT' Then $Employee is in IT else $Employee is in Administration end if Then I will list out employees and the depts they are in like: Jane Doe is in Administration John Peters is in IT etc etc I am having the biggest problem with integrating the IF statement with the code below. Thanks a lot in advance. <?php $db_conn = new COM("ADODB.Connection"); $connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("./DB/tbltDiv.mdb").";"; $db_conn->open($connstr); $sql = 'SELECT EployeeName, employmentType, Dept FROM DIVISIONS ORDER BY Dept'; $rs = $db_conn->Execute($sql); ?> <table> <tr> <th>Employee Name</th> <th>employment Type</th> <th>Dept</th> </tr> <?php while (!$rs->EOF) { ?> <tr> <td><?php echo $rs->Fields['EmployeeName']->Value ?></td> <td><?php echo $rs->Fields[employmentType]->Value ?></td> <td><?php echo $rs->Fields['Dept']->Value ?></td> </tr> <?php $rs->MoveNext() ?> <?php } ?> </table> <?php $rs->Close(); $db_conn->Close(); $rs = null; $db_conn = null; ?> Quote Link to comment https://forums.phpfreaks.com/topic/190014-if-conditional-help-needed/ Share on other sites More sharing options...
gwolgamott Posted January 27, 2010 Share Posted January 27, 2010 change this entire block of code here <table> <tr> <th>Employee Name</th> <th>employment Type</th> <th>Dept</th> </tr> <?php while (!$rs->EOF) { ?> <tr> <td><?php echo $rs->Fields['EmployeeName']->Value ?></td> <td><?php echo $rs->Fields[employmentType]->Value ?></td> <td><?php echo $rs->Fields['Dept']->Value ?></td> </tr> <?php $rs->MoveNext() ?> <?php } ?> </table> To something like this <?php while (!$rs->EOF) // TABLE HTML NOT NEEDED ELIMINATE IT // RUN WHILE LOOP UNTIL END OF FILE { echo $rs->Fields['EmployeeName']->Value; //ECHO OUT THE NAME echo" is in "; //ECHO THE text between the two variables including spaces echo $rs->Fields['Dept']->Value; //ECHO OUT THE DEPARTMENT echo "<br>"; //ECHO AN HTML NEW LINE CHARACTER $rs->MoveNext() //MOVE TO NEXT FIELDS } ?> Could be streamlined but this may help you understand the whys and hows first then make it cleaner later. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/190014-if-conditional-help-needed/#findComment-1002518 Share on other sites More sharing options...
taquitosensei Posted January 27, 2010 Share Posted January 27, 2010 Here's one way. put something like this before your while loop $depts=array("PT"=>"IT","FT"=>"Administration"); then <?php while (!$rs->EOF) { ?> <tr> <td><?php echo $rs->Fields['EmployeeName']->Value; ?></td> <td><?php echo $rs->Fields[employmentType]->Value; ?></td> <td><?php echo $depts[$rs->Fields['employementType']->Value]; ?></td> </tr> <?php $rs->MoveNext() ?> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/190014-if-conditional-help-needed/#findComment-1002520 Share on other sites More sharing options...
simflex Posted January 27, 2010 Author Share Posted January 27, 2010 Thanks a bunch guys for your prompt response and for your kindness. I appreciate the code very much. What I really would like to do before printing our results is to perform a check. If employmentType = FT then employee is in IT else employee is in Administration. I don't see the if. You may have done it differently but my newbieness may be the reason why I can't see. Also, when I tested taquitosensei's script, it displayed all the names but only displayed one dept. For instance, the result looks like this: John Peters FT Jane Doe PT IT See that Dept is not being displayed for those employees who are Fulltime(FT). gwolgamott, I will test yours in a sec. Again, thanks to both you, very much Quote Link to comment https://forums.phpfreaks.com/topic/190014-if-conditional-help-needed/#findComment-1002528 Share on other sites More sharing options...
gwolgamott Posted January 27, 2010 Share Posted January 27, 2010 I don't see the if. You may have done it differently but my newbieness may be the reason why I can't see. Also, when I tested taquitosensei's script, it displayed all the names but only displayed one dept. For instance, the result looks like this: John Peters FT Jane Doe PT IT Overlooked what you wanted sorry... but that code I gave is a start. I just modified this... but give me a few and I'll get you something... but first do you want it in a table listing name, ft or pt, then list only a dept if in IT... right? Quote Link to comment https://forums.phpfreaks.com/topic/190014-if-conditional-help-needed/#findComment-1002530 Share on other sites More sharing options...
simflex Posted January 27, 2010 Author Share Posted January 27, 2010 hi gwolgamott, Yes, that is correct. There are several employees. The goal is to run a check to say, if employmentType is FT, then IT else Administration. Then these results are printed out line by line. Thanks again, very, very much Quote Link to comment https://forums.phpfreaks.com/topic/190014-if-conditional-help-needed/#findComment-1002531 Share on other sites More sharing options...
gwolgamott Posted January 27, 2010 Share Posted January 27, 2010 Ok then just a slight mode to this and maybe that is what you wanted then... my bad... was a bit more simple then what I did... ...DUMMY ME I just put the wrong code up... give me a sec. <?php while (!$rs->EOF) // TABLE HTML NOT NEEDED ELIMINATE IT // RUN WHILE LOOP UNTIL END OF FILE { echo $rs->Fields['EmployeeName']->Value; //ECHO OUT THE NAME echo" is "; //ECHO THE text between the two variables including spaces echo $rs->Fields[employmentType]->Value; //ECHO WHETHER FT OR PT if( ($rs->Fields[employmentType]->Value) == "FT") {echo " & in the IT Dept!";} if($rs->Fields[employmentType]->Value) == "PT") {echo " is in Administration!";} echo "<br>"; //ECHO AN HTML NEW LINE CHARACTER $rs->MoveNext() //MOVE TO NEXT FIELDS } ?> Quote Link to comment https://forums.phpfreaks.com/topic/190014-if-conditional-help-needed/#findComment-1002533 Share on other sites More sharing options...
simflex Posted January 27, 2010 Author Share Posted January 27, 2010 I really appreciate your help. AND NO, you can't be this good and be a dummy at same time. What I have learned early in life is that the sharpest people beat themselves up a lot. I don't know why. Quote Link to comment https://forums.phpfreaks.com/topic/190014-if-conditional-help-needed/#findComment-1002536 Share on other sites More sharing options...
gwolgamott Posted January 27, 2010 Share Posted January 27, 2010 That will display something like: Bob is FT & in the IT Dept! Jan is PT & in Administration! Hopefully that is close to what you want. Quote Link to comment https://forums.phpfreaks.com/topic/190014-if-conditional-help-needed/#findComment-1002537 Share on other sites More sharing options...
simflex Posted January 27, 2010 Author Share Posted January 27, 2010 Yes, very very close. Just having a minor issue. I am getting results like: It is not showing the dept for those in FT which is IT. It just reads, John Peters is male (Nothing else is showing). Those in Administration are showing correctly as Jane Doe is female & is in Administration. And while we are at it, is possible to incorporate one very nice idea from taquitosensei? He made this declaration first: $depts=array("PT"=>"IT","FT"=>"Administration"); Again, thanks very much. Quote Link to comment https://forums.phpfreaks.com/topic/190014-if-conditional-help-needed/#findComment-1002551 Share on other sites More sharing options...
simflex Posted January 27, 2010 Author Share Posted January 27, 2010 hi gwolgamott, Please ignore the first part of my issue which says the those who are FT are not getting their departments printed. That is working. It turns out that PHP is case sensitive. I didn't know that till but then again, I am just using for the second day in my entire life. I still need help with the array. Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/190014-if-conditional-help-needed/#findComment-1002554 Share on other sites More sharing options...
gwolgamott Posted January 27, 2010 Share Posted January 27, 2010 And while we are at it, is possible to incorporate one very nice idea from taquitosensei? He made this declaration first: $depts=array("PT"=>"IT","FT"=>"Administration"); Yeah this makes an array called $dept with elements of PT and FT So $dept[PT] = "IT" and $dept[FT] = "Administration" Also it could be looked at as such Array ( [PT] => IT [FT] => Administration) But depends on how and why you want to use it on how to set up an array. It basically eliminates the need for the if statements that is. Either way works. Quote Link to comment https://forums.phpfreaks.com/topic/190014-if-conditional-help-needed/#findComment-1002564 Share on other sites More sharing options...
simflex Posted January 27, 2010 Author Share Posted January 27, 2010 Ok, in that case, let's leave it like it is now. I thank you so very much for your help. You have been and angel. My first time here and I am really impressed. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/190014-if-conditional-help-needed/#findComment-1002574 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.