idris
idris

Reputation: 1049

Getting only one notification result

So I have a notification system, here's the tables layout

+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| id        | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| from_user | varchar(255)     | YES  |     | NULL    |                |
| to_user   | varchar(255)     | YES  |     | NULL    |                |
| type      | varchar(255)     | YES  |     | NULL    |                |
| date      | varchar(255)     | YES  |     | NULL    |                |
+-----------+------------------+------+-----+---------+----------------+

And this is what one row looks like

+----+-----------+---------+--------+------+
| id | from_user | to_user | type   | date |
+----+-----------+---------+--------+------+
| 32 | allex     | scott   | hgjghj | NULL |
+----+-----------+---------+--------+------+

Now here's how I'm getting the results

    //Check if any records of notifications exists & loop em' back
$stmt = $con->prepare("SELECT * FROM notifications WHERE to_user = :user");
$stmt->bindValue(':user', $username, PDO::PARAM_STR);
$stmt->execute();

$notifications = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $notifications[] =  array(
        'id' => $row['id'],
        'from_user' => $row['from_user'],
        'to_user' => $row['to_user'],
        'type' => $row['type'],
        'date' => $row['date']
    );
}

And now finally my issue, say I had another row, and the type was different, what happens is once I loop the query I get the same type returned throughout the whole, so instead of

Alelx hgjghj scott,

and say I had another result that said

Alelx ghfjhgj scott

I wouldn't be able to see it because the Type from the first result ends up being "dominant". Any help would be great.

Upvotes: 0

Views: 43

Answers (1)

Anatoliy Kim
Anatoliy Kim

Reputation: 756

According to your code your $notifications array will look like this:

Array
 0 => (... type=>'hgjghj' ...)
 1 => (... type=>'ghfjhgj' ...)

Every row of your DB will get another element to this array. So, you need to loop through that array and see what values you get.

Upvotes: 1

Related Questions