Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have this code below and it works fine, other than the fact that only the first row is put into a table row and the rest of the rows are not. Am I missing something obvious? I've been trying to fix this for a while and I just cant see the error/problem!

Help please?

PHP
		$messageFrom = $_REQUEST['messageFrom'];
		$messageTo = $_REQUEST['messageTo'];
        $db = Database::getConnection();
		$query = "SELECT * FROM messages WHERE messageTo = :to AND messageFrom = :from OR messageFrom = :to AND messageTo = :from ORDER BY messageDate DESC LIMIT 5";
		$output = $db->query($query);
		$output->bindParam(':to', $messageTo);
		$output->bindParam(':from', $messageFrom);
		$output->execute();
		$results = $output->fetchAll(PDO::FETCH_ASSOC);
		if (count($results) > 0){
			echo "<table>\n";
			echo "<tr><th colspan=2>Conversation</th></tr>\n";
foreach($results as $result){
				$from = $result['messageFrom'];
				$to = $result['messageTo'];
				$message = $result['message'];
				$messageID = $result['messageID'];
				echo "<tr>\n";
					if ($from == $messageFrom){
					echo "<td><div><p>$messageFrom</p>$message</div></td>";
					echo "</tr>\n";
					} else {
					echo "<td><div><p>$messageTo</p>$message</div></td>";
					echo "</tr>\n";
					}
				echo "</table>";
			}
Posted

1 solution

Before you complete your for
looping you have echo table
close tag.

It has to be like as below.

PHP
echo "<tr><th colspan=2>Conversation</th></tr>\n";
foreach($results as $result){
                $from = $result['messageFrom'];
                $to = $result['messageTo'];
                $message = $result['message'];
                $messageID = $result['messageID'];
                echo "<tr>\n";
                    if ($from == $messageFrom){
                    echo "<td><div><p>$messageFrom</p>$message</div></td>";
                    }
            else {
                    echo "<td><div><p>$messageTo</p>$message</div></td>";
                    }//end of if else
                echo "</tr>\n";
                }//end of for loop
echo "</table>";
}//end of your if count
 
Share this answer
 
v3
Comments
jba1991 30-Mar-15 12:38pm    
That is correct... Embarrassing as well! Ha. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900