Jump to content

"If" conditional help needed


simflex

Recommended Posts

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;

 

?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 } ?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
} 
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

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.

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.