gunthertoody Posted February 9, 2011 Share Posted February 9, 2011 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 />'; Quote Link to comment https://forums.phpfreaks.com/topic/227115-if-elseif/ Share on other sites More sharing options...
Skepsis Posted February 9, 2011 Share Posted February 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227115-if-elseif/#findComment-1171634 Share on other sites More sharing options...
gunthertoody Posted February 9, 2011 Author Share Posted February 9, 2011 Thanks, but I'll clarify. 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" Quote Link to comment https://forums.phpfreaks.com/topic/227115-if-elseif/#findComment-1171637 Share on other sites More sharing options...
Skepsis Posted February 9, 2011 Share Posted February 9, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/227115-if-elseif/#findComment-1171643 Share on other sites More sharing options...
gunthertoody Posted February 9, 2011 Author Share Posted February 9, 2011 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 />'; Quote Link to comment https://forums.phpfreaks.com/topic/227115-if-elseif/#findComment-1171690 Share on other sites More sharing options...
kenrbnsn Posted February 9, 2011 Share Posted February 9, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/227115-if-elseif/#findComment-1171697 Share on other sites More sharing options...
gunthertoody Posted February 9, 2011 Author Share Posted February 9, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/227115-if-elseif/#findComment-1171758 Share on other sites More sharing options...
cyberRobot Posted February 9, 2011 Share Posted February 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227115-if-elseif/#findComment-1171768 Share on other sites More sharing options...
gunthertoody Posted February 9, 2011 Author Share Posted February 9, 2011 THANK SO MUCH CYBER, that did the trick. So I guess the rule is if I start a conditional statement resulting in another echo, I must close out the previous echo statement with a semi-colon? David Quote Link to comment https://forums.phpfreaks.com/topic/227115-if-elseif/#findComment-1171825 Share on other sites More sharing options...
cyberRobot Posted February 9, 2011 Share Posted February 9, 2011 Yep Quote Link to comment https://forums.phpfreaks.com/topic/227115-if-elseif/#findComment-1171836 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.