Jump to content

Question regarding "if" conditional statement


soulreaver418

Recommended Posts

Hi and thanks for taking the time to read through this first and foremost. I have a bit of code that I have managed to piece together with help from other sites and what it does is connect to our local ms sql server and retrieve a few tables from our database. The info is relating to property management and it basically displays the unit address, current date and its rental status. (Occupied or Vacant)

 

This makes it easy for us to at a glance check the status of our rentals instead of having to log into our software and navigate through all that.. anyways, I have two issues I need help with.

 

First problem, in my code, is it possible to write a conditional statement that would check for a certain status in a view and then if it is there, print a simple yes or no in a table with that appropriate property.

 

For example, we have property 1000 name street and it is currently vacant, however the viewPendingLease shows us information regarding future bookings. I would like to be able to look through that view with the code, check property 1000 name street for any future pending leases, and if there is one, print out a "Yes" in a Future Booked column that I would create.

 

If any of that makes sense, I can post the print out of the tables of the viewPendingLease if it makes it easier for anyone to see the data output to understand what I am meaning.

 

The second issue, is it possible to change the color of the text according to a variable, say a word in a generated table with html or css? I would like to color the words vacant or occupied green and red respectively just to make it much easier to zero in on the vacant properties. Or even maybe just color every other row a background color just to help distinguish between the different properties.

 

Here is my code that pulls up my current tables to show Property, Street and current rented status.

 

<?php 

//connect to a DSN "*******" 
$conn = odbc_connect('*******','*****','************'); 

if ($conn) 
{ 
//the SQL statement that will query the database 
$query = "Select unit.PropertyUnitID, unit.Street, unit.Status, from unit";
//perform the query 
$result=odbc_exec($conn, $query); 

echo "<table border=\"1\"><tr>"; 

//print field name 
$colName = odbc_num_fields($result); 
for ($j=1; $j<= $colName; $j++) 
{ 
echo "<th>"; 
echo odbc_field_name ($result, $j ); 
echo "</th>"; 
} 

//fetch tha data from the database 
while(odbc_fetch_row($result)) 
{ 
echo "<tr>"; 
for($i=1;$i<=odbc_num_fields($result);$i++) 
{ 
echo "<td>"; 
echo odbc_result($result,$i); 
echo "</td>"; 
} 
echo "</tr>"; 
} 

echo "</td> </tr>"; 
echo "</table >"; 

//close the connection 
odbc_close ($conn); 
} 
else echo "odbc not connected"; 
?>

 

I currently have the viewPendingLease.Active (which shows if there is a future pending lease) taken out due to a repeating data issue I am also trying to figure out...

Link to comment
Share on other sites

Also, regarding the psuedo code you posted. I think I did not clarify what exactly I am wanting. in the table unit.Status it will show "Vacant" or "Occupied", I would like to color those words, then use the if to check the viewPendingLease.StatusName for either a pending status or not.

 

If it has no data I would like it to return a No in Column Future Booked, which I would have to create. If it has a pending status I would like for it to print a Yes in Column Future Booked.

 

Thanks for the quick reply, I feel that I am on the right track and with help can get this figured out or done a different way. Going to include small ss of table so you can see print out of the table unit.

 

Thanks again!

 

th_Capture.jpg

 

 

Link to comment
Share on other sites

Is it me, or should you be getting a syntax error here:

} 
else echo "odbc not connected"; 
?>

 

It should read:-

} 
else{
echo "odbc not connected"; 
}
?>

The braces were missing off the 'else' part of the if!

 

General format for if/else is:

 

if(conditions){

//case true

}

else{

//case false

}

 

Check out:If statements

 

Rw

Link to comment
Share on other sites

Perhaps being an old guy I am in a bit of a fog :)

 

Presuming db table unit.Status contains either the word 'Vacant' or 'Occupied' then in the HTML table cell

 

 

<td>
<?PHP 
if(unit.Status == "Vacant') {
?>
  <font color="somecolor">Vacant</font>
<?PHP
}else{
?>
  <font color="anothercolor">Occupied</font>
  <?PHP
}
?>
</td>

The IF for the other is independent of the color IF

Link to comment
Share on other sites

That makes more sense now with the color, however I am getting a syntax error when inserting. I am probably putting it in the wrong place, sorry to be so helpless but I am trying to self teach php. I find it very interesting I just don't fully understand the language and the order of it yet.

 

Think I need to go to books a million and find an idiot's guide to php...

Link to comment
Share on other sites

Ok, got the color issue sorted out. Ended up changing this section here,

 

$resultfield = odbc_result($result,$i); 
echo "<td class='$resultfield'>";
echo $resultfield;

 

and then created

 

a .css file with the .occupied and .vacant in it with the proper colors.

 

Thanks for the assistance to get the gears slowly turning here.

 

Now on to making the Future Booked column and populating it according to what is in the StatusName table.

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.