0

I've checked all kinds of previously answered similar questions and nothing's working. I have a "Inbox" type application where a person of [email protected] might have 5 messages waiting for them. They log in and get a screen of those five messages, the sender email, the date sent, and the first 15 chars of the message.

In mysql, I have: id, emailaddress, senderemail, datesent, fullmessage.

I'm using SELECT * FROM db WHERE emailaddress='[email protected]' to pull out, let's say, 5 new messages. $result equals the mysql query. mysql_num_rows does a great job of telling me there are 5 rows. It works fine up to that point.

My problem is that I can't (despite hours of trying) find the right syntax for making this into a multidimensional array so that I can iterate table rows in a for loop and print it all out. I'm envisioning:

<table>
for ($a = 0; $a < $numrows; $a++) {
<tr>
     the php code would go here and would print the associated sender, date, and message in <td> cells.
</tr>
}
</table>

Any help would be fantastic.

I'm sure this is not hard but have just not gotten anywhere for most of the afternoon. Can anyone out there tell me what a simple syntax for doing this would be?

3

1 Answer 1

3

Information that comes from the database is in an array when you use mysql_fetch_array() or mysql_fetch_assoc(). So, you can do something like:

<table>
<?php
$storage_array = array();
$result = mysql_query("SELECT * FROM db WHERE emailaddress='[email protected]'" or die(mysql_error());

while($data = mysql_fetch_assoc($result))
{
?>
<tr>
  <td><?php echo $data['sender'] ?></td>
  <td><?php echo $data['date'] ?></td>
  <td><?php echo $data['message '] ?></td>
</tr>
<?php
  $storage_array[] = $data;
}
?>
</table>

You will obviously need to join some tables for messages if they are on different tabels or use LIMIT to only get 5 rows. You will also need to determine your ORDER BY.

You will need to know which loop added your data to $storage_array and remember it starts with $storage_array[0].

Sign up to request clarification or add additional context in comments.

5 Comments

This is exactly what I'm looking for except that there's multiple rows that have "[email protected]" and I want to get ALL the rows. This would just be if there was one row with emailaddress='[email protected]', right?
Okay, actually, this works great. :-) But now I'm wondering how to access that data so I can assign it to variables. Or maybe the question is whether it's better to put it first into a multidimensional array and then create the table using those variables? Either way, THANK YOU for the help! :-)
This is creating a table by looping through the data. Are you needing to pull two arrays and loop through them? You can access the current row by calling $data[''] and the [''] part is the name of the table column.
How do I access NON-current row data? I ended up doing exactly what you've got only instead of "echo" I store the data in variables. Then access by "$array[$row#]['columntitle']. But it seems like there must be a more elegant or php-did-the-work-for-me way.
What do you need the non-current row for? You could save it to an array with each pass to have it available. I edited my answer to show it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.