How do I preform an If Then statement using PHP and MySql?

  • Thread starter Thread starter Richter B
  • Start date Start date
R

Richter B

Guest
Below is my script. Currently, the way its set up works perfectly. I am able to pull records off my sql database and display info only. I need to add the ability to assign values based on the displayed values. For instance, Our students are registered for 3 classes, 1A, 2C, 3D. I need to show the actual name of the classes (vs what’s in the system) so when they log in to view their info, they wont see "You are registered for: 1C" but rather "You are registered for: 1C - Business Development" Below are both my sets of code. I need to join them but am unsure as to how. Any help you can provide would be greatly appreciated. Thank you.

<?
$username="name";
$password="pass";
$database="mydatabase";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";
$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$class=mysql_result($result,$i,"class");

echo "<b>$first $last</b><br>
Class: $class";

$i++;
}

?>

I want to add this

$Class = "1A";
if ( $Class == "1A" )
{
echo "Business Development";
}
 
Just to expand on Steve's answer....since you already put it in your question...

if ($class=="1A"){
echo "- Business Development"; ' You want it to echo not redefine $class here
}elseif ($class=="2C"){
echo "- Whatever"; ' You want it to echo not redefine
}

$i++;
}

It seems you are doing it in an odd way though...do you know you can do a while with the results? E.g.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$first= $row[0];
$last= $row[1];
$class = $row[2]
If ($class == "1A") {
echo "<b>$first $last</b><br>
Class: $class - Business Development";
} else if ($class == "2B") {

echo "<b>$first $last</b><br>
Class: $class - Whatever";
}

}

instead of getting the number of rows and trying to use a variable ($i) to increase.
 
Back
Top