cougar23 Posted June 27, 2008 Share Posted June 27, 2008 i'm building a HTML table base don the contents of a database row returned from a mysql PDO query. I have some columns that don't have data in them every time, and in the table it doens't put data in the cell and therefore messes up the look of the table because the border for the cell with missing data doesn't get output. I can fix the problem by putting whitespace in teh cell. Right now i have the following line: echo '<td>'.$row['course_name'].' </td>'; What I'd like to do is some one-liner like the following, that if the column contains no data echoes the whitespace char, otherwise the data in the column. Does anyone if this is possible and if so how to do it? echo (!empty($row['course_name']) ? $row['course_name'] : '$nbsp;'; Link to comment https://forums.phpfreaks.com/topic/112137-can-you-use-echo-with-if-statements/ Share on other sites More sharing options...
mrbuter Posted June 27, 2008 Share Posted June 27, 2008 You could probably do something like $query = mysql_query(SELECT * FROM ....) $rows = mysql_num_rows($query) if ($rows == 0) { echo "..."; } else { echo "..."; } Does that work for you? Link to comment https://forums.phpfreaks.com/topic/112137-can-you-use-echo-with-if-statements/#findComment-575679 Share on other sites More sharing options...
bluejay002 Posted June 27, 2008 Share Posted June 27, 2008 <?php (!empty($row['course_name'])) ? echo $row['course_name'] : echo '$nbsp;'; ?> cheers, Jay Link to comment https://forums.phpfreaks.com/topic/112137-can-you-use-echo-with-if-statements/#findComment-575715 Share on other sites More sharing options...
haku Posted June 27, 2008 Share Posted June 27, 2008 Does anyone if this is possible and if so how to do it? You can do it almost exactly how you did it: echo (!empty($row['course_name'])) ? $row['course_name'] : 'nbsp;'; Link to comment https://forums.phpfreaks.com/topic/112137-can-you-use-echo-with-if-statements/#findComment-575728 Share on other sites More sharing options...
cougar23 Posted June 27, 2008 Author Share Posted June 27, 2008 Ok this was my code before: foreach($results as $row) { if($counter % 2 == 0) echo '<tr>'; else echo '<tr class="lightGrey">'; $counter += 1; echo '<td>'.$row['course_name'].' </td>'; echo '<td>'.$row['term_1_grade'].' </td>'; echo '<td>'.$row['term_2_grade'].' </td>'; echo '<td>'.$row['mid_term_exam'].' </td>'; echo '<td>'.$row['semester_1_grade'].' </td>'; echo '<td>'.$row['term_3_grade'].' </td>'; echo '<td>'.$row['term_4_grade'].' </td>'; echo '<td>'.$row['final_exam'].' </td>'; echo '<td>'.$row['semester_2_grade'].' </td>'; echo '<td>'.$row['course_grade'].' </td>'; echo '</tr>'; } And this is my code after, and now the table is not formatted, it's just outputting the vaules all together outside of the table, instead of in the table in the cells: foreach($results as $row) { if($counter % 2 == 0) echo '<tr>'; else echo '<tr class="lightGrey">'; $counter += 1; echo '<td>'.(!empty($row['course_name'])) ? $row['course_name'] : ' '.'</td>'; echo '<td>'.(!empty($row['term_1_grade'])) ? $row['term_1_grade'] : ' '.'</td>'; echo '<td>'.(!empty($row['term_2_grade'])) ? $row['term_2_grade'] : ' '.'</td>'; echo '<td>'.(!empty($row['mid_term_exam'])) ? $row['mid_term_exam'] : ' '.'</td>'; echo '<td>'.(!empty($row['semester_1_grade'])) ? $row['semester_1_grade'] : ' '.'</td>'; echo '<td>'.(!empty($row['term_3_grade'])) ? $row['term_3_grade'] : ' '.'</td>'; echo '<td>'.(!empty($row['term_4_grade'])) ? $row['term_4_grade'] : ' '.'</td>'; echo '<td>'.(!empty($row['semester_2_grade'])) ? $row['semester_2_grade'] : ' '.'</td>'; echo '<td>'.(!empty($row['course_grade'])) ? $row['course_grade'] : ' '.'</td>'; echo '</tr>'; } Any way to fix this so that it works via the second method? Link to comment https://forums.phpfreaks.com/topic/112137-can-you-use-echo-with-if-statements/#findComment-575737 Share on other sites More sharing options...
haku Posted June 27, 2008 Share Posted June 27, 2008 It's not going to work that way - you can't embed this type of if statement. You need to do it like this: echo '<td>'; echo (!empty($row['course_name'])) ? $row['course_name'] : ' '; echo '</td>'; Or you could do it like this: echo (!empty($row['course_name'])) ? '<td>' . $row['course_name'] . '</td>': '<td> </td>'; Link to comment https://forums.phpfreaks.com/topic/112137-can-you-use-echo-with-if-statements/#findComment-575742 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.