2

I need to store different table markup code in a php array, as a string. Some of the code that needs to be stored in an array needs to be tested using an if-statement. Before I knew I needed some of the code needs to be tested in such a way, everything was working flawlessly, but now that I know that the string stored is based on an 'if-statement' I am not sure how to code it. Because I need to end the markup for the current slot in order to insert the php conditional statement. As a result, I am unable to continue storing data in the 'current' slot, which is what I need.

I have been trying to find work arounds for over an hour now, I appreciate any help with this.

$favorites[] = '
        <tr>
          <td><a href="#">'.$files_row['name'].'</a></td>
          <td><a href="#"><img class="table-button" src="images/smallglobe.png"></a></td>
          <td>
            <img src="images/e-mail-icon.PNG">';
                if(strcmp($_files_row['email'],'none') != 0){
                    $favorites[] = '<div style="display:none;"><a href="#" >'.$files_row['email'].'</a></div>';
                }
$favorites[]='        
         </td>
          <td class="table-button-cell">
            <img class="table-button" src="images/eicon.png">
            <div class="phonepopup" style="display:none;">Call: '.$files_row['phone'].'</div>
          </td>
          <td class="table-button-cell"> .... etc....

3 Answers 3

2

try to use a temporary variable for the current slot and assign it to the array, after the if-statements have been evaluated. so you can concatenate the necessary strings:

$temp_var = '
    <tr>
      <td><a href="#">'.$files_row['name'].'</a></td>
      <td><a href="#"><img class="table-button" src="images/smallglobe.png"></a></td>
      <td>
        <img src="images/e-mail-icon.PNG">';
            if(strcmp($_files_row['email'],'none') != 0){
                $temp_var .= '<div style="display:none;"><a href="#" >'.$files_row['email'].'</a></div>';
            }
$temp_var .='        
     </td>
      <td class="table-button-cell">
        <img class="table-button" src="images/eicon.png">
        <div class="phonepopup" style="display:none;">Call: '.$files_row['phone'].'</div>
      </td>
      <td class="table-button-cell"> .... etc....';

$favorites[] = $temp_var;
Sign up to request clarification or add additional context in comments.

Comments

1

You can concatenate text or instructions inline:

$favorites[5] = 'I prefer '.($age > 18 ? : 'natural' : 'chocolate').' milk';

The (condition?true:false) syntax does miracles ;)

Comments

0

You have two options:

Option one: Append the string using concatenation.

  $favorites[index] = '
    <tr>
        <td><a href="#">'.$files_row['name'].'</a></td>
        <td><a href="#"><img class="table-button" src="images/smallglobe.png"></a></td>
        <td>
            <img src="images/e-mail-icon.PNG">';
    if(strcmp($_files_row['email'],'none') != 0){
        $favorites[index] .= '<div style="display:none;"><a href="#" >'.$files_row['email'].'</a></div>';
    }
    $favorites[index] .= '        
         </td>
         <td class="table-button-cell">
             <img class="table-button" src="images/eicon.png">
             <div class="phonepopup" style="display:none;">Call: '.$files_row['phone'].'</div>
         </td>
         <td class="table-button-cell"> .... etc....

Option 2: Use PHP's ternary operator to do the conditional code inline.

    $favorites[index] = '
        <tr>
          <td><a href="#">'.$files_row['name'].'</a></td>
          <td><a href="#"><img class="table-button" src="images/smallglobe.png"></a></td>
          <td>
            <img src="images/e-mail-icon.PNG">' . ((strcmp($_files_row['email'],'none') != 0) ? '<div style="display:none;"><a href="#" >'.$files_row['email'].'</a></div>' : '') . '        
         </td>
          <td class="table-button-cell">
            <img class="table-button" src="images/eicon.png">
            <div class="phonepopup" style="display:none;">Call: '.$files_row['phone'].'</div>
          </td>
          <td class="table-button-cell"> .... etc....

1 Comment

Well, I cant seem to fix the formatting, but you should be able to understand the gist of what I'm saying...

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.