1

I have an array of arrays. Each array contains a user's information (name, email, username, etc). When a user is logged in, the page needs to display the arrays in a table. (This is working fine)

<table>

    <?php
        if(isset($_SESSION['logged_in_user'])) {
            echo '<tr>';
            for ($i = 0; $i < count($item_names); $i++) {
                if ($i == 8) continue;
                echo "<th scope=\"col\">$item_names[$i]</th>";
            }
            echo '</tr>';
            $altrow = false;
            foreach ($contacts as $contact) {
                echo ($altrow) ? '<tr class="alt">' : '<tr>';
                $altrow = !$altrow;
                for ($i = 0; $i < count($contact); $i++) {
                    if ($i == 8) continue;
                    echo "<td>$contact[$i]</td>";
                }
                echo '</tr>'; 
            }
        }
    ?> 
</table>

But I would also like the table to highlight the array(row) of the specific logged in user.

I have tried (and failed):

<table>
    <?php
        if(isset($_SESSION['logged_in_user'])) {
            echo '<tr>';
            for ($i = 0; $i < count($item_names); $i++) {
                if ($i == 8) continue;
                echo "<th scope=\"col\">$item_names[$i]</th>";
            }
            echo '</tr>'
            $altrow = false;
            foreach ($contacts as $contact) {
                if ($contact[1] == $_SESSION['logged_in_user']) {
                    echo '<tr class="highlight">' : '<tr>'; 
                } else {
                    echo ($altrow) ? '<tr class="alt">' : '<tr>';
                    $altrow = !$altrow;
                    for ($i = 0; $i < count($contact); $i++) {
                        if ($i == 8) continue;
                            echo "<td>$contact[$i]</td>";
                        }
                    }
                    echo '</tr>'; 
                }
            }
    ?> 
</table>

Am I not even close? Any help is appreciated!

3
  • 1
    Your indenting is confusing. The ending bracket should at least be at the same indentation as the beginning block. Not before it. Commented Nov 1, 2012 at 4:58
  • I'm going to attempt to indent your code properly. Note that proper indentation allows errors to be more easily spotted. Commented Nov 1, 2012 at 5:12
  • I also recommend replacing tabs with spaces. Commented Nov 1, 2012 at 5:18

2 Answers 2

0

hi yaar simply check tour loop foreach ($contacts as $contact=>$value) { //manipulate $value here and or you can check by printing $value here

}

4
  • This does not help the question. Commented Nov 1, 2012 at 5:03
  • Hi can u post bit peace of array then i can explain u how to display if your array is like array([0]=>array('name'=>'john')) and if you apply foreach ($contacts as $contact=>$value) { $value['name'] give u value john....... } Commented Nov 1, 2012 at 5:05
  • The array goes like: `$contacts = array ( array(1000, "Garrett", "Salinas", ... array(1001, "Xander", "Ryan", ... Commented Nov 1, 2012 at 5:30
  • well as i get your array is like array ( array(1000, "Garrett", "Ryan"), array(1001, "Xander", "Ryan")....) You write foreach ($contacts as $_contact){ echo $_contact[0]; //this give u id} Hope it help! Commented Nov 2, 2012 at 11:13
0

Please try code given below

<table>
   <?php
    if(isset($_SESSION['logged_in_user'])) {
        echo '<tr>';
        for ($i = 0; $i < count($item_names); $i++) {
            if ($i == 8) continue;
                echo "<th scope=\"col\">$item_names[$i]</th>";
         }
        echo '</tr>';

      $altrow = false;

      foreach ($contacts as $contact) {
        if ($contact[1] == $_SESSION['logged_in_user']) {
            echo '<tr class="highlight">'; 
        } else {
            echo ($altrow) ? '<tr class="alt">' : '<tr>';
         }   
           $altrow = !$altrow;
            for ($i = 0; $i < count($contact); $i++) {
                if ($i == 8) continue;
                     echo "<td>$contact[$i]</td>";
             }

            echo '</tr>'; 
         }
      }
   ?> 
</table>

There was some bracket mistake in your code like } else {}.

I hope it will be working for you.

thanks

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.