1

So! I am trying to make a simple shortcode to insert a table where I want it and I have everything working except I don't understand how to put the foreach loop in the return.

Basically I need all of this easily storeable in the dataTypeAllowed function so that it can be called for the shortcode add_shortcode('data_allowed', 'dataTypeAllowed');

Right here I tried just dropping in the foreach right into the return but of course I'm getting an error.

<?php

// Data Types Array
        $data_types_array = array( // the order: name, common reference, slug, ACF value, ACF label
        array('Personally Identifiable Information', 'PII', 'pii', $pii['value'], $pii['label']), 
        array('Protected Health Information', 'PHI', 'phi', $phi['value'], $phi['label']),
        );


function dataTypeAllowed() {
    
        return '<table>
                    <tr>
                        <th>Permission</th><th>Data Type</th><th>Common Reference</th>
                    </tr>' .
            
                    foreach($data_types_array as $data_type){
                echo '<tr>
                        <td' . $small_center . '>' . 
                            (($data_type[3] == 1) ? $allowed : '') . 
                            (($data_type[3] > 1 ) ? $questionable : '') . 
                            (($data_type[3] == 0) ? $not_allowed : '') . 
                        '</td>
                        <td> <a href="data-type/'. $data_type[2] . '">' . $data_type[0] . '</a>' .
                            (($data_type[3] > 1 ) ? '<br> <span style="font-size:11px">' . $data_type[4] . '</span>' : '') .
                        '</td>
                        <td>' . $data_type[1] . '</td>
                    </tr>'; } . 
                    '</table>';
}
    add_shortcode('data_allowed', 'dataTypeAllowed');

?>

Alternatively I thought I could wrap it all up in a variable but it also gives an error.

$foreach_data = foreach($data_types_array as $data_type){
                echo '<tr>
                        <td' . $small_center . '>' . 
                            (($data_type[3] == 1) ? $allowed : '') . 
                            (($data_type[3] > 1 ) ? $questionable : '') . 
                            (($data_type[3] == 0) ? $not_allowed : '') . 
                        '</td>
                        <td> <a href="data-type/'. $data_type[2] . '">' . $data_type[0] . '</a>' .
                            (($data_type[3] > 1 ) ? '<br> <span style="font-size:11px">' . $data_type[4] . '</span>' : '') .
                        '</td>
                        <td>' . $data_type[1] . '</td>
                    </tr>'; };



function dataTypeAllowed() {
    
        return '<table>
                    <tr>
                        <th>Permission</th><th>Data Type</th><th>Common Reference</th>
                    </tr>' .
                        $foreach_data
                     . 
                    '</table>';
}
    add_shortcode('data_allowed', 'dataTypeAllowed');

?>

Any suggestions?

edit 2:

Hmm I used your suggestion and cleaned up the code but it is only returning the table header row <table><tr><th>Permission</th><th>Data Type</th><th>Common Reference</th></tr>

Here is the block:

$allowed = '<img';
$not_allowed = 'img';
$questionable = 'img';

$data_types_array = array( // the order: name, common reference, slug, ACF value, ACF label
        array('Personally Identifiable Information', 'PII', 'pii', $pii['value'], $pii['label']), 
        array('Protected Health Information', 'PHI', 'phi', $phi['value'], $phi['label']));

function dataTypeAllowed(){
    $response = "";
    $response .= "<table>
        <tr>
            <th>Permission</th><th>Data Type</th><th>Common Reference</th>
        </tr>";
    
    foreach ($data_types_array as $data_type) {
        $response .= "<tr><td  align='center' width='60px' >";
        $response .= ($data_type[3] == 1) ? $allowed : '';
        $response .= ($data_type[3] > 1) ? $questionable : '';
        $response .= ($data_type[3] == 0) ? $not_allowed : '';
        $response .= "</td><td> <a href='data-type/'. $data_type[2] . ''>  $data_type[0] </a>";
        $response .= ($data_type[3] > 1) ? "<br> <span style='font-size:11px'> $data_type[4] </span>" : '';
        $response .= "</td><td> $data_type[1] </td> </tr>";
    }
    $response .= "</table>";
    return $response;
}
add_shortcode('data_allowed', 'dataTypeAllowed');
ashueyUTK
  • 23
  • 4
  • 2
    Build up your string into a variable, **then** return it – aynber Feb 22 '23 at 18:52
  • I posted an edit with a follow-up on this advice, if you could take another look I'd super appreciate it! – ashueyUTK Feb 22 '23 at 19:29
  • Hooboy, that's even worse. I'd suggest reading up on [variable scope](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and), and using a good IDE/Editor with syntax highlighting and error checking to see the visible issues. – aynber Feb 22 '23 at 19:43
  • The pain of self-taught. Thanks for the reference it might be the thing I need! – ashueyUTK Feb 22 '23 at 19:50

1 Answers1

1

You need to do something like that:

function dataTypeAllowed()
{
    $response = "";
    $response .= "
    <table>
        <tr>
            <th>Permission</th><th>Data Type</th><th>Common Reference</th>
        </tr>
    ";
    foreach ($data_types_array as $data_type) {
        $response .= "<tr>
                <td $small_center >";
        $response .= ($data_type[3] == 1) ? $allowed : '';
        $response .= ($data_type[3] > 1) ? $questionable : '';
        $response .= ($data_type[3] == 0) ? $not_allowed : '';
        $response .= "</td>
                <td> <a href='data-type/'. $data_type[2] . ''>  $data_type[0] </a>";
        $response .= ($data_type[3] > 1) ? "<br> <span style='font-size:11px'> $data_type[4] </span>" : '';
        $response .= "</td>
                <td> $data_type[1] </td>
            </tr>";
    }
    $response .= "</table>";
    return $response;
}
add_shortcode('data_allowed', 'dataTypeAllowed');

And also you need to define all variables in your dataTypeAllowed() function. ($data_types_array, $small_center, $allowed, $questionable, $not_allowed)

Krunal Bhimajiyani
  • 1,115
  • 3
  • 12