0

I'm making a simple private messaging system. I try to make all conversations appear in the user's inbox, I use a foreach loop for this. However, the loop doesn't seem to print out anything. It only gives the error: Warning: illegal string offset.

Below is an example of the code (in my original code I get these values from my database with a query)

$result = array();
$result['conversation_id'] = '3';
$result['conversation_subject'] = 'test112';
$result['conversation_last_reply'] = '1485701808';

print_r($result);

foreach($result as $conversation){
    echo $conversation['conversation_subject'];
}

When I use print_r the correct results print (3, test112 and 1485701808) but in the foreach loop I keep getting the warning. Can anyone help me with this problem? Thanks!!

EDIT: relevant PHP code

$sql = $db->prepare("SELECT 
                        conversations.conversation_id, 
                        conversations.conversation_subject, 
                        MAX(conversations_messages.message_date) AS conversation_last_reply 
                        FROM conversations 
                        LEFT JOIN conversations_messages ON conversations.conversation_id = conversations_messages.conversation_id
                        INNER JOIN conversations_members ON conversations.conversation_id = conversations_members.conversation_id
                        WHERE conversations_members.user_id = {$_SESSION['user_id']}
                        AND conversations_members.conversation_deleted = 0
                        GROUP BY conversations.conversation_id
                        ORDER BY conversation_last_reply DESC");
$sql->execute();

$result = $sql->fetch(PDO::FETCH_ASSOC);

foreach($result as $conversation){
    ?>
    <div class="conversations">
        <h4><a href=""><?php echo $conversation['conversation_subject']; ?> </a></h4>
        <p>Last reply: </p>
    </div>
    <?php
}

EDIT: I also get 2 warnings when I try to echo the 'conversation_last_reply: date() expects parameter 2 to be integer, string given in AND Warning: illegal string offset.

This is what I tried to echo the date:

 echo date('d/m/Y H:i:s', $conversation['conversation_last_reply']);
C. T.
  • 3
  • 2
  • Possible duplicate of [How to loop through an associative array and get the key?](http://stackoverflow.com/questions/1951690/how-to-loop-through-an-associative-array-and-get-the-key) – Jan Franta Jan 29 '17 at 16:37
  • `$conversation['conversation_subject']` is wrong, `$conversation` is not an array. Simply do `echo $conversation;`. Or do `echo $result['conversation_subject'];` – Rajdeep Paul Jan 29 '17 at 16:38
  • @RajdeepPaul when I do this I get the following output: 3test1121485701808, but I only want test112 – C. T. Jan 29 '17 at 16:39
  • @C.T. You don't need *that* `foreach` loop, access the elements directly from `$result` array - `echo $result['conversation_subject'];` – Rajdeep Paul Jan 29 '17 at 16:41
  • @RajdeepPaul the problem is that I want a list of all conversations, so I need a foreach loop. When there is only 1 conversation it doesn't matter. But when the user has multiple conversations I want to loop over these, create a
    for all conversations and put the conversation subject as a

    – C. T. Jan 29 '17 at 16:43
  • @C.T. Please include the *relevant* PHP code(including the SQL query). You're probably generating the `$result` array in the wrong way. – Rajdeep Paul Jan 29 '17 at 16:47
  • @RajdeepPaul I added some code, hope this is enough. – C. T. Jan 29 '17 at 16:50
  • @C.T. I've given an answer below, hopefully this will resolve your issue. – Rajdeep Paul Jan 29 '17 at 16:58

1 Answers1

0

From OP's comment,

I want a list of all conversations, so I need a foreach loop. When there is only 1 conversation it doesn't matter. But when the user has multiple conversations I want to loop over these, create a for all conversations and put the conversation subject as a <h2>

Use while() loop instead of foreach loop to loop through all the conversations from the result set.

// your code
$sql->execute();
while($conversation = $sql->fetch(PDO::FETCH_ASSOC)){
    ?>
    <div class="conversations">
        <h2><a href=""><?php echo $conversation['conversation_subject']; ?> </a></h2>
        <p>Last reply: </p>
    </div>
    <?php
}
Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • @C.T. You're welcome! :-) Please *accept* the answer to close the question. [How to accept answer on Stack Overflow?](http://meta.stackexchange.com/a/5235) – Rajdeep Paul Jan 29 '17 at 18:17