Jump to content

can you use echo with " if ? : "statements?


cougar23

Recommended Posts

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

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?

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>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.