0

I have a form on a Wordpress page posting data to a table called 'members'. I have a second page displaying this data in a table. All is working well. The data is being displayed using:

$myrows = $wpdb->get_results( "SELECT first_name, surname, role, email, country, bio FROM members" );
foreach ( $myrows as $row ) {
echo "<tr><td>" . $row->first_name . "</td><td>" . $row->surname . "</td><td>" . $row->role . "</td><td>" . $row->email . "</td><td>" . $row->country . "</td></tr>";
}

Not all members will have bio's but what I would like to do is say "if the member has a bio, make the first name clickable to display a pop-up containing the bio."

So my question is how do I add an IF statement inside an echo as above?

Would it be easier to split the echo statement as below?

echo '<tr><td>';
if($row->bio) {
echo '<a href="#">' . $row->first_name . '</a>';
echo '<div class="bio" style="display: none;">' . $row->bio . '</div></td><td>';
} else {
echo $row->first_name;
echo '</td><td>';
}
echo $row->surname;
echo '</td><td>';
echo $row->role;
echo '</td><td>';
echo $row->email;
echo '</td><td>';
echo $row->country;
echo '</td></tr>';

Thanks, Adrian

1 Answer 1

0

Here is one approach:

$myrows = $wpdb->get_results( "SELECT first_name, surname, role, email, country, bio FROM members" );

foreach ( $myrows as $row ) {
    $first_name = $row->first_name;

    if ( ! empty( $row->bio ) ) {
        $first_name = '<a href="#">' . $first_name . '</a><div class="bio" style="display: none;">' . $row->bio . '</div>';
    }

    echo "<tr><td>" . $first_name . "</td><td>" . $row->surname . "</td><td>" . $row->role . "</td><td>" . $row->email . "</td><td>" . $row->country . "</td></tr>";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.