im trying to echo out two different column inside a while loop
and the output will be inside <td>
here is my code:
while($row = mysql_fetch_array($stmt)){
$doc_number = $row['doc_number'];
$place_of_issue = $row['place_of_issue'];
$crew_rank_code = $row['crew_rank_code'];
$full_name = $row['full_name'];
$date_of_birth = $row['date_of_birth'];
$place_of_birth = $row['place_of_birth'];
$date_issue = $row['date_issue'];
$date_expiry = $row['date_expiry'];
$place_of_birth = $row['place_of_birth'];
echo '<tr>';
echo "<td>$crew_rank_code</td>";
echo "<td>$full_name</td>";
echo "<td>$date_of_birth</td>";
echo "<td>$place_of_birth</td>";
echo "<td>". (($row['doc_type'] == '1') ? "$doc_number" : "") . "</td>";
echo "<td>". (($row['doc_type'] == '1') ? "$date_issue" : "") . "</td>";
echo "<td>". (($row['doc_type'] == '1') ? "$date_expiry" : "") . "</td>";
echo "<td>". (($row['doc_type'] == '1') ? "$place_of_issue" : "") . "</td>";
echo "<td>". (($row['doc_type'] == '2') ? "$doc_number" : "") . "</td>";
echo "<td>". (($row['doc_type'] == '2') ? "$date_issue" : "") . "</td>";
echo "<td>". (($row['doc_type'] == '2') ? "$date_expiry" : "") . "</td>";
echo "<td>". (($row['doc_type'] == '2') ? "$place_of_issue" : "") . "</td>";
echo '</tr>';
}
right now, the code gives me an output up to PASSPORT PLACE ISSUE
column but after that, my code echo "<td>". (($row['doc_type'] == '2') ? "$doc_number" : "") . "</td>";
is no longer working
how can I make the echo "<td>". (($row['doc_type'] == '2') ? "$doc_number" : "") . "</td>";
working?
thank in advance guys
EDIT:
i added mysql
$stmt = mysql_query("select * from info join crew_documents_table on info.id = crew_documents_table.document_crew_id join crew_rank on info.crew_rank = crew_rank.crew_rank_id where crew_rank in ('1','2','3') and crew_status = '$crew_status' and vessel = '$vessel_name' and date_issue in (select max( date_issue ) from crew_documents_table where crew_documents_table.document_crew_id = info.id)")or die(mysql_error());
edit: Sorry my last answer was wrong, you should maybe rewrite your sql to end up with something like this
echo "<td>". $row['doc_type'][1]['$doc_number'] . "</td>";
Ternary operators do not necessarily need ()
braces, just a first note on your readability of code. Moving on, have you yet debugged what the row doc_type
actually contains by doing a simple output of its content? Your condition:
echo '<td>' . (int)$row['doc_type'] == 2 ? $doc_number : '' . '</td>';
Will only return the $doc_number
, aka. $row['doc_number']
, if the doc_type
is set as 2. Also, for this, always check numerical values as data-type int. It will automatically parse the doc_type
in the condition with the delimiter (int)
.
Ternary expressions are brilliant, but in your case, you're pro-longing it. For readability you could simply use conditional expressions and short-hand operators to output:
<?php
while($r = mysql_fetch_array($stmt)):
$doc_number = $r['doc_number'];
/* [...] */
if( (int) $r['doc_type'] == 1 ): ?>
<td> <?= $doc_number ?> </td>
<!-- [...] -->
<?php else if( (int)$r['doc_type'] == 2 ): ?>
<td> <?= $doc_XXXX ?> </td>
<!-- [...] -->
<?php else:
// debug console to check what `doc_type` value was if no condition was met
echo '<script>console.log("[DOC_TYPE_DEBUG] ' . $r['doc_type'] . '");</script>';
endif;
endwhile;