1

I have an array that i pull from a database that looks like this

id - name - shortlink - downloadurl - count - date

Now I want to display that array as a table so the administartor of the site can se the content of the database. For that, I'm using this code:

function build_table($array){
                    // start table
                    $html = '<table id="usertable">';
                    // header row
                    $html .= '<tr class="header">';
                    foreach($array[0] as $key=>$value){
                            $html .= '<th>' . $key . '</th>';
                        }
                    $html .= '</tr>';

                    // data rows
                    foreach( $array as $key=>$value){
                        $html .= '<tr>';
                        foreach($value as $key2=>$value2){
                            $html .= '<td>' . $value2 . '</td>';
                        }
                        $html .= '</tr>';
                    }

                    // finish table and return it

                    $html .= '</table>';
                    return $html;

And it works great. The problem is that i wan't do display some of the columns with a different code. E.g. 'downloadurl' which is an webadress, i want to make clickable. I just can't figure out how to split up the function so that i can write the code for the individual columns.

3
  • Check the $key and use conditionals (assuming $array is associative, indexed will be a bit more cryptic to future developers). Commented Feb 21, 2016 at 13:53
  • You can use a switch conditional to have a $key printed like you want. Commented Feb 21, 2016 at 13:54
  • Thanks for quick response! Can you show me how to do it? I pretty new to php so i'm not shure of what you guys mean. Commented Feb 21, 2016 at 13:56

2 Answers 2

1

You could try something similar to the following:

using conditionals

This is probably the most straight forward way to go about handling different attributes per column - not without its drawbacks (noted below)

// data rows
foreach( $array as $key => $value) {
  $html .= '<tr>';
  foreach($value as $key2 => $value2) {
    if ($key2 == 'downloadurl') {
      $html .= '<td><a href="' . $value2 . '">Download</a></td>';
    } else {
      $html .= '<td>' . $value2 . '</td>';
    }
  }
  $html .= '</tr>';
}

using switch statement

If you decide on this route, it may be a bit easier to manage in the long run as if () elseif () else() can become difficult to read over time.

foreach($value as $key2 => $value2) {
  switch($key2) {
    case 'downloadurl':
      $html .= '<td><a href="' . $value2 . '">Download</a></td>';
    break;

    default:
      $html .= '<td>' . $value2 . '</td>';
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot! Could only make the switch statement work for the first link. The rest just displayed the adress. But using the conditionals worked.
0

use this function to generate urls:

function getURL($downloadURL)
{
    $html = "<a href = '$downloadURL'>Download</a>";
    return $html;
}

And your function build_table():

function build_table($array){
    // start table
    $html = '<table id = "usertable">';
    // header row
    $html .= '<tr class = "header">';
    foreach($array[0] as $key => $value){
         $html .= '<th>' . $key . '</th>';
    }
    $html .= '</tr>';
    // data rows
    foreach( $array as $key => $value){
         $html .= '<tr>';
         foreach($value as $key2 => $value2){
              $value2 = ($key2 == 'downloadurl') ? getURL($value2) : $value2;
              $html .= '<td>' . $value2 . '</td>';
         }
         $html .= '</tr>';
      }
      // finish table and return it
      $html .= '</table>';
      return $html;
}

1 Comment

I'm not sure I agree with this approach. I appreciate that you're attempting to separate concerns but, this is really just unnecessarily convoluting code. getURL() doesn't actually get anything outside of what's provided. Furthermore, you're still calculating when to invoke within the loop cycle - And this approach leaves OP with less room to perform different views per column. Overall, if the point was separating out view, I would have suggested a view templating framework rather than forcing it into an adjunct answer.

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.