Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
1  
Your indenting is confusing. The ending bracket should at least be at the same indentation as the beginning block. Not before it. – Daedalus Nov 1 '12 at 4:58
I'm sorry. I'm a mega noob. – BDV Nov 1 '12 at 4:59
I'm going to attempt to indent your code properly. Note that proper indentation allows errors to be more easily spotted. – Daedalus Nov 1 '12 at 5:12
I also recommend replacing tabs with spaces. – Daedalus Nov 1 '12 at 5:18
Thank-you very much – BDV Nov 1 '12 at 5:20

2 Answers

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

}

share|improve this answer
This does not help the question. – Daedalus Nov 1 '12 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....... } – Santosh kumar Nov 1 '12 at 5:05
The array goes like: `$contacts = array ( array(1000, "Garrett", "Salinas", ... array(1001, "Xander", "Ryan", ... – BDV Nov 1 '12 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! – Santosh kumar Nov 2 '12 at 11:13

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.