Jump to content

If Elseif


gunthertoody

Recommended Posts

I am  a novice and had this code below working fine until I added the If / elseif to "plan_id".  Prior, the column would just list out the numbers from the database, but I want the numbers to echo out the text. Any help is appreciated.

 

while($row = mysql_fetch_array($result)){

 

  $bg = ($bg=='#ffffff' ? '#e7e8e8' : '#ffffff'); //For Alternate Row Colors

 

echo '<tr bgcolor="' . $bg . '"><td>' . $row['customers_firstname'] . " " . $row['customers_lastname'] . '</td><td>' . date("M. d, Y", strtotime($row['date'])) . '</td><td>' . if ($row['plan_id'] == 1) {

echo "1 DVD";

}

elseif ($row['plan_id'] == 2) {

echo "2 DVDs";

}

else {

echo "3 DVDs";

}

. '</td></tr>';}

echo '</table>';

echo '<br />';

Link to comment
Share on other sites

what output are you getting? does it display 1,2 or 3 DVD's?

 

Anyhow, try this code:

 

$idx = 0;
while($row = mysql_fetch_array($result))
{
    $idx++;
    $bg = ($bg=='#ffffff' ? '#e7e8e8' : '#ffffff'); //For Alternate Row Colors
   
    echo '<tr bgcolor="' . $bg . '">
            <td>' . $row['customers_firstname'] . $row['customers_lastname'] . '</td><td>' . date("M. d, Y", strtotime($row['date'])) . '</td><td>';
            if ($idx == 1) 
            {
                echo $idx.' DVD';
            }
            else {
                echo $idx.' DVDs';
            }

    echo '</td></tr>';
}
echo '</table><br />';

 

$idx = 0; is what defines the variable

$idx++; increments the value by one each time the loop loops.

the if statement checks if there is one dvd and prints the non plural form of dvd, if it's not one it displays the plural form.

hope this helps.

Link to comment
Share on other sites

Alrighty then, go ahead and use this code

<?php
while($row = mysql_fetch_array($result))
{
    $bg = ($bg=='#ffffff' ? '#e7e8e8' : '#ffffff'); //For Alternate Row Colors
   
    echo '<tr bgcolor="' . $bg . '">
            <td>' . $row['customers_firstname'] echo '</td><td>';
    if ($row['plan_id'] == 1) 
    {
        echo '1 DVD';
    }
    elseif ($row['plan_id'] == 2) 
    {
        echo '2 DVDs';
    }
    else {
        echo '3 DVDs';
    }     
    echo '</td></tr>';
}
echo '</table><br />';
?>['php]

Link to comment
Share on other sites

Thanks but it's still not working :'(

 

Second looks appreciated.

 

The "plan_id" column currently holds either 1, 2 or 3 as values. I just want to change the value from a number to text.

 

Desired results:

when plan_id = 1    echo "1 DVD"

when plan_id = 2    echo "2 DVDs"

when plan_id = 3    echo "3 DVDs"

 

$bg = '#ffffff'; //For Initial Row Color

 

  while($row = mysql_fetch_array($result)){

   

    $bg = ($bg=='#ffffff' ? '#e7e8e8' : '#ffffff'); //For Alternate Row Colors

 

  echo '<tr bgcolor="' . $bg . '"><td>' . $row['customers_firstname'] . " " . $row['customers_lastname'] . '</td><td>' . date("M. d, Y", strtotime($row['date'])) . '</td><td>' . if ($row['plan_id'] == 1) {

  echo '1 DVD';

  }

  elseif ($row['plan_id'] == 2) {

  echo '2 DVDs';

  }

  else {

  echo '3 DVDs';

  }

  . '</td></tr>';}

      echo '</table>';

      echo '<br />';

Link to comment
Share on other sites

Try something like this:

<?php
while($row = mysql_fetch_array($result))
{
    $bg = ($bg=='#ffffff' ? '#e7e8e8' : '#ffffff'); //For Alternate Row Colors
    $dvd_string = ($row['plan_id'] == 1)?'DVD':'DVDs';   
    echo "<tr bgcolor='$bg'><td>{$row['customers_firstname']}</td><td>{$row['plan_id']} $dvd_string</td></tr>\n";
}
echo '</table><br />';
?>

 

Ken

Link to comment
Share on other sites

Ken,

 

Your code works great, unfortunately, I still need help. What I didn't mention, for the sake of brevity, was that I have 3 more plan_id's and two don't match up. id5 = 6 and id6 =5. That is why I thought that if elseif was the best way to go. I know there's got to be a way, but I'm probably just overlooking proper syntax.

 

David

Link to comment
Share on other sites

It's looks like you have an error in the code. This:

 

...
echo '<tr bgcolor="' . $bg . '"><td>' . $row['customers_firstname'] . " " . $row['customers_lastname'] . '</td><td>' . date("M. d, Y", strtotime($row['date'])) . '</td><td>' . 

...

. '</td></tr>';
...

 

Should be:

 

...
echo '<tr bgcolor="' . $bg . '"><td>' . $row['customers_firstname'] . " " . $row['customers_lastname'] . '</td><td>' . date("M. d, Y", strtotime($row['date'])) . '</td><td>';

...

echo '</td></tr>';
...

 

Notice the semicolon after the first echo statement and "echo" was missing from the second.

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.