I have an HTML table being dynamically generated from database. The solution for achieving this can be seen at this question Adding rows to a HTML table with dynamic columns.
This works fine, except I want to indicate all sessions a person attended for each week in the same row - with the code below, an additional session attendance becomes another row appended to the HTML table. So what I want is like:
DB tables are like ('week', 'cohort' and 'attendance' tables)
+---------+-----------+----------+-----------+
| week_pk | week_name | sessions | cohort_fk |
+---------+-----------+----------+-----------+
| 1 | Week 1 | 3 | 1 |
| 2 | Week 2 | 2 | 1 |
| 3 | Week 3 | 1 | 1 |
+---------+-----------+----------+-----------+
+-----------+-------------+-------------+-------------+
| cohort_pk | cohort_name | cohort_code | cohort_year |
+-----------+-------------+-------------+-------------+
| 1 | Some name | MICR8976 | 2014 |
+-----------+-------------+-------------+-------------+
+---------------+-----------+-------------+---------+-----------+---------+---------+
| attendance_pk | person_id | given_names | surname | cohort_fk | week_fk | session |
+---------------+-----------+-------------+---------+-----------+---------+---------+
| 1 | 123456 | Bill | Smith | 1 | 1 | 2 |
| 2 | 123456 | Bill | Smith | 1 | 2 | 2 |
| 3 | 753354 | Fred | Jones | 1 | 1 | 1 |
| 4 | 753354 | Fred | Jones | 1 | 2 | 1 |
| 5 | 753354 | Fred | Jones | 1 | 3 | 1 |
+---------------+-----------+-------------+---------+-----------+---------+---------+
And the code that I'm using:
$cohort = $_POST['cohort'];
$year = $_POST['year'];
$query = "SELECT * FROM cohort, week
WHERE week.cohort_fk = cohort.cohort_pk
AND cohort.cohort_year = '$year'
AND cohort.cohort_pk = '$cohort'
ORDER BY week.week_pk";
$result = mysql_query($query, $connection) or die(mysql_error());
echo "<table width='100%' cellpadding='4' cellspacing='0' class='attendance_table'>";
echo "<tr><td class='theadings'></td>";
$second_row = "<tr><td class='theadings'></td>";
$totalcolumn = 1;
while( $row = mysql_fetch_assoc($result) ){
$weekname = $row["week_name"];
$n_session = $row["sessions"];
$weekpk = $row["week_pk"];
$totalcolumn += $n_session;
echo "<td class='theadings' colspan='$n_session'>$weekname</td>";
for($i=1; $i<=$n_session; $i++){
$second_row .= "<td class='theadings_lab'>Lab $i</td>";
$weeksession[$weekpk][$i] = $totalcolumn - $n_session + $i;
}
}
echo "</tr>";
echo $second_row . "</tr>";
$query = "SELECT * FROM cohort, week, attendance
WHERE week.cohort_fk = cohort.cohort_pk
AND attendance.week_fk = week.week_pk
AND attendance.cohort_fk = cohort.cohort_pk
AND cohort.cohort_year = '$year'
AND cohort.cohort_pk = '$cohort'
ORDER BY attendance.attendance_pk";
$result = mysql_query($query, $connection) or die(mysql_error());
while( $row = mysql_fetch_assoc($result) ){
$name = $row["given_names"] . " " . $row["surname"];
$weekpk = $row["week_pk"];
$sno = $row["session"];
echo "<tr><td class='tborder_person_left'>$name</td>";
for($i=2; $i<=$totalcolumn; $i++){
if( $weeksession[$weekpk][$sno] == $i )
echo "<td class='tborder_person_attended'>✔</td>";
else
echo "<td class='tborder_person'>-</td>";
}
echo "</tr>";
}//end while
echo "</table>";
#Kickstart below is an example of what the table looks like with your code. You can see for example Melody Chew and Kit Yeng Melody Chew (same person) have two seperate rows. The unique identifier needs to be on the person_id which exists in the attendance table (apologies for not showing this before! my BAD Note also the additional columns on the right of the table with the crosses which should be under the week 2 column.
I personally would go through nested arrays.
I give you an example. Note that this is only in principle, as I do not have the database, I have not tested. :)
1 -> statement of the array $liste_name = array();
2 -> Replace your second treatment by :
while( $row = mysql_fetch_assoc($result) ){
$name = $row["given_names"] . " " . $row["surname"];
$weekpk = $row["week_pk"];
$sno = $row["session"];
$person_id = $row["person_id"]; //add person_id
$liste_name[$person_id] = $name; // to have the last name
$tab[$person_id][1] = "<td class='tborder_person_left'>$name</td>"; // to have the last name
for($i=2; $i<=$totalcolumn; $i++){
if( $weeksession[$weekpk][$sno] == $i )
$tab[$person_id][$i] = "<td class='tborder_person_attended'>✔</td>";
}
}//end while
3 -> Fill the empty areas :
foreach ($liste_name as $person_id => $name){
$tab2[$person_id][1] = $tab[$person_id][1];
for($i=2; $i<=$totalcolumn; $i++){
if (!isset($tab[$person_id][$i]) || ($tab[$person_id][$i] != "<td class='tborder_person_attended'>✔</td>"))
$tab[$person_id][$i] = "<td class='tborder_person'>-</td>";
$tab2[$person_id][$i] = $tab[$person_id][$i];
}
}
4 -> Use the array :
foreach($tab2 as $person_id => $col){
echo "<tr>";
foreach ($col as $i => $value){
echo $value;
}
echo "</tr>";
}
echo "</table>";
I try with fictional data and it works. :)
I would be tempted to use a couple of CROSS JOINs to get all the possible combinations of session / week / person, and then left join that against attendance.
SELECT unique_names.given_names,
unique_names.surname,
week_sessions.week_pk,
week_sessions.session,
attendance.attendance_pk
FROM
(
SELECT week_pk, sub1.i AS session, week_name, week.cohort_fk, cohort.cohort_year
FROM week
INNER JOIN cohort
ON week.cohort_fk = cohort.cohort_pk
INNER JOIN
(
SELECT 1 i UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10
) sub1
ON week.sessions >= sub1.i
WHERE cohort.cohort_year = 2014
AND cohort.cohort_pk = 1
) week_sessions
CROSS JOIN
(
SELECT DISTINCT given_names, surname
FROM attendance
) unique_names
LEFT OUTER JOIN attendance
ON week_sessions.week_pk = attendance.week_fk
AND week_sessions.cohort_fk = attendance.cohort_fk
AND week_sessions.session = attendance.session
AND unique_names.given_names = attendance.given_names
AND unique_names.surname = attendance.surname
ORDER BY unique_names.given_names,
unique_names.surname,
week_sessions.week_pk,
week_sessions.session
I have knocked up an SQL fiddle for this:-
http://www.sqlfiddle.com/#!2/4a388/7
You can then loop around this easily (although adding the titles is a bit messy - following would be half the size without the titles). I have used the mysql_* functions as that is what you are already using.
<?php
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test_area") or die(mysql_error());
$sql = "SELECT unique_names.person_id,
unique_names.FullName,
week_sessions.week_pk,
week_sessions.session,
attendance.attendance_pk
FROM
(
SELECT week_pk, sub1.i AS session, week_name, week.cohort_fk, cohort.cohort_year
FROM week
INNER JOIN cohort
ON week.cohort_fk = cohort.cohort_pk
INNER JOIN
(
SELECT 1 i UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10
) sub1
ON week.sessions >= sub1.i
WHERE cohort.cohort_year = 2014
AND cohort.cohort_pk = 1
) week_sessions
CROSS JOIN
(
SELECT person_id, MAX(CONCAT_WS(' ', given_names, surname)) AS FullName
FROM attendance
GROUP BY person_id
) unique_names
LEFT OUTER JOIN attendance
ON week_sessions.week_pk = attendance.week_fk
AND week_sessions.cohort_fk = attendance.cohort_fk
AND week_sessions.session = attendance.session
AND unique_names.person_id = attendance.person_id
ORDER BY unique_names.person_id,
unique_names.FullName,
week_sessions.week_pk,
week_sessions.session";
$result = mysql_query($sql, $connection) or die(mysql_error());
$prev_person_id = 0;
$first = true;
$header = array('Name'=>array('Session'));
$output = array();
while( $row = mysql_fetch_assoc($result) )
{
if ($prev_person_id != $row['person_id'])
{
if ($prev_person_id != 0)
{
$first = false;
}
$prev_person_id = $row['person_id'];
$output[$prev_person_id] = array();
}
if ($first)
{
$header["Week ".$row['week_pk']]["S".$row['session']] = "S".$row['session'];
}
$output[$prev_person_id][] = (($row['attendance_pk'] == '') ? ' ' : 'X');
}
$header1 = '';
$header2 = '';
foreach($header as $key=>$value)
{
$header1 .= "<td colspan='".count($value)."'>$key</td>\r\n";
foreach($value as $key1=>$value1)
{
$header2 .= "<td>$value1</td>\r\n";
}
}
echo "<table border='1'>\r\n";
echo "<tr>\r\n$header1</tr>\r\n";
echo "<tr>\r\n$header2</tr>\r\n";
foreach($output as $name=>$value)
{
echo "<tr><td>$name</td>";
foreach($value as $key1=>$value1)
{
echo "<td>$value1</td>\r\n";
}
echo "</tr>";
}
echo "</table>\r\n";
?>
Related
This question already has answers here:
How do I iterate over the results in a MySQLi result set?
(2 answers)
Closed 1 year ago.
I want to query data from my database with mysqli_fetch_array and mysqli_fetch_field, but it's not working at all.
I have the tbl_student table like this:
| id | firstname | lastname |
| -- | --------- | -------- |
| 1 | first_A | last_A |
| 2 | first_B | last_B |
| 3 | first_C | last_C |
PHP code:
$query = "select * from tbl_student";
$result = mysqli_query($db, $query);
while ($row = mysqli_fetch_array($result)) {
while ($col = mysqli_fetch_field($result)) {
echo $col->name . " = " . $row[$col->name];
echo "<br>";
}
echo "<br>";
}
As my wish, I want the result like this one:
id = 1
firstname = first_A
lastname = last_A
id = 2
firstname = first_B
lastname = last_B
id = 3
firstname = first_C
lastname = last_C
But not, I only have the fist record:
id = 1
firstname = first_A
lastname = last_A
How can I do this?
mysqli_fetch_field is designed to get detailed information about the fields in the query, not to get information about each field in a record.
Use the following code to get your desired result.
while ($row = mysqli_fetch_array($result)) {
foreach ($row as $key => $value) {
echo $key . " = " . $value;
echo "<br>";
}
echo "<br>";
}
I want to dynamically generate an HTML table based on MySql data, and the user would be able to update the table directly on the page (through JQuery and Ajax).
Week rows will be inserted for the entire year all at once in table schedule.
Each week has no more than 2 users assigned to.
A user can be removed at any time and replaced with another user.
The first table column of each row contains all the users and then each column represents a week from tuesday to tuesday for the entire year. Expected HTML result :
+-------+--------------------------+--------------------------+--------------------------+
| Users | 2019-03-19 to 2019-03-26 | 2019-03-26 to 2019-04-02 | 2019-04-02 to 2019-04-09 |
+-------+--------------------------+--------------------------+--------------------------+
| 1 | | | |
+-------+--------------------------+--------------------------+--------------------------+
| 2 | X | X | |
+-------+--------------------------+--------------------------+--------------------------+
| 3 | X | | |
+-------+--------------------------+--------------------------+--------------------------+
| 4 | | | |
+-------+--------------------------+--------------------------+--------------------------+
| 5 | | X | |
+-------+--------------------------+--------------------------+--------------------------+
Schema :
table schedule
+-------------+------------+------------+------------+------------+
| schedule_id | user1 | user2 | dateStart | dateEnd |
| PK AUTO_INC | FK int(11) | FK int(11) | date | date |
+-------------+------------+------------+------------+------------+
| 1 | 3 | 2 | 2019-03-19 | 2019-03-26 |
+-------------+------------+------------+------------+------------+
| 2 | 5 | 2 | 2019-03-26 | 2019-04-02 |
+-------------+------------+------------+------------+------------+
| 3 | null | null | 2019-04-02 | 2019-04-09 |
+-------------+------------+------------+------------+------------+
Users are gradually being assigned. The user1 and user2 columns allow for null and later on someone can enter the users for a particular week.
I need to check with PHP if one or two users are already assigned, then someone can update a row with either a user id or null (removing a user).
For this purpose I use variables $set1 and $set2 and data-attributes (for later use with jQuery) telling me which week has a user in user1 or user2.
I can echo the table <th> with all the necessary data.
Now my question is how to echo the table <td>. I need to find a way to generete <tr> for each users and <td> for each <th> while matching the user id value.
Therefore, if the week inserted in <th> has user 2 assigned, I will add an 'X' to the <td> below that <th> on user 2 row (see expected result above).
I was thinking myabe I can run another MySql query and while loop to echo by users and order by dates?
My code so far :
// Connect to database decyb
include 'dbconnect.php';
// MySQL query
$getUpdateDispoEnq = "
SELECT dispo_enq.*,
u1.firstName as enq1First,
u1.lastName as enq1Last,
u2.firstName as enq2First,
u2.lastName as enq2Last
FROM dispo_enq
JOIN users u1 ON dispo_enq.userEnq1 = u1.user_id
JOIN users u2 ON dispo_enq.userEnq2 = u2.user_id;";
// Prepared statements
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $getUpdateDispoEnq)){
echo '<div class="red-text" style="margin-top: 1em">SQL error!</div>';
exit;
} else {
mysqli_stmt_execute($stmt);
$results = mysqli_stmt_get_result($stmt);
$resultCheck = mysqli_num_rows($results);
$week = 0;
echo '<table class="dispo-enq">';
echo '<tr>';
echo '<th class="regular"><div><span>EnquĂȘteurs</span></div></th>';
echo '<th class="regular"><div><span>Cellulaire</span></div></th>';
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($results)) {
$enq1 = $row['userEnq1'];
$enq2 = $row['userEnq2'];
$dateStart = $row['dateStart'];
$dateEnd = $row['dateEnd'];
$week++;
// Check if users are set
if ($enq1 > 0){
$set1 = 1;
} else {
$set1 = 0;
}
if ($enq2 > 0){
$set2 = 1;
} else {
$set2 = 0;
}
// Display table headers
echo '<th class="rotate" data-week="'.$week.'" data-enq1="'.$set1.'" data-enq2="'.$set2.'"><div><span>'.$dateStart.' to '.$dateEnd.'</span></div></th>';
}
}
echo '</tr>';
echo '</table>';
exit;
}
You have a task similar to mine in a way but since I can get the code I used there I decided to simply post what I can get out of my brain under such a short notice.
// MySQL query
$getUpdateDispoEnq = "
SELECT dispo_enq.*, u1.firstName as enq1First, u1.lastName as enq1Last, u2.firstName as enq2First, u2.lastName as enq2Last
FROM dispo_enq
JOIN users u1 ON dispo_enq.userEnq1 = u1.user_id
JOIN users u2 ON dispo_enq.userEnq2 = u2.user_id;";
$html = '';
// Prepared statements
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $getUpdateDispoEnq)){
$html .= '<div class="red-text" style="margin-top: 1em">SQL error!</div>';
exit;
} else {
mysqli_stmt_execute($stmt);
$results = mysqli_stmt_get_result($stmt);
$resultCheck = mysqli_num_rows($results);
$week = $set1 = $set2 = 0;
$dateStart = $dateEnd = '';
$html .= '<table class="dispo-enq"><tr>';
$html .= '<th class="regular"><div><span>EnquĂȘteurs</span></div></th>';
$html .= '<th class="regular"><div><span>Cellulaire</span></div></th>';
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($results)) {
$enq1 = $row['userEnq1'];
$enq2 = $row['userEnq2'];
$dateStart = $row['dateStart'];
$dateEnd = $row['dateEnd'];
$week++;
// Check if users are set
if ($enq1 > 0) $set1 = 1;
if ($enq2 > 0) $set2 = 1;
// Display table headers
$html .= '<th class="rotate" data-week="'.$week.'" data-enq1="'.$set1.'" data-enq2="'.$set2.'">';
$html .= '<div><span>'.$dateStart.' to '.$dateEnd.'</span></div></th>';
}
}
$html .= '</tr>';
$result = mysql_query("SELECT firstName, lastName FROM users");
while ($rows = mysql_fetch_assoc($result)) {
$html .= '<tr>';
$html = '<td>'.$week.'</td>';
foreach ($rows as $row) $html .= '<td>'.$row.'</td>';
$html = '<td>'.$dateStart.'</td>';
$html = '<td>'.$dateEnd.'</td>';
$html .= '</tr>';
}
$html .= '</table>';
exit;
}
return $html;
I am getting result as...............
Column 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.
.
So On
I need to display result like this ...... new column after every 10 rows.
col 1 | col 2 | .....
abc | 11 | 21
jjj | 12 | .
jhjjk | 13 | .
jhbjj | ... | .
..... | ... | .
..... | ... | .
10 | 20 | 30
have done this much .....
<?php
mysql_select_db("$database");
$sql = "select test, rate FROM product";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
echo <<<EOL
<input type="checkbox" name="name[]" value="{$row['test']}-{$row['rate']}" /> {$row['test']}-{$row['rate']}<br />
EOL;
}
?>
Any help will be appreciated.
If you could survive with lists rather than separate columns, then this is not too bad in SQL. That would be:
1,11,21,31
2,12,22,32
But the values would all be in one column.
The idea is to enumerate the values (using variables) and then aggregate:
select group_concat(column order by rn)
from (select t.column, (#rn := #rn + 1) as rn
from t cross join
(select #rn := 0) params
order by ?? -- do you want an ordering?
) t
group by (rn - 1) % 10
order by min(rn);
You'll need to implement your logic in PHP code like this:
<?php
$input_fields = array();
$rows_per_column = 10;
mysql_select_db("$database");
$sql = "select test, rate FROM product";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
$input_fields[] =<<<EOL
<input type="checkbox" name="name[]" value="{$row['test']}-{$row['rate']}" /> {$row['test']}-{$row['rate']}
EOL;
}
echo "<table><tr>";
foreach ($input_fields as $i => $value){
if ($i % $rows_per_column == 0){
echo '<td valign="top">';
}
else{
echo '<br>';
}
echo $value;
if ($i % $rows_per_column == $rows_per_column - 1 || $i == count($input_fields) - 1){
echo '</td>';
}
}
echo "</tr></table>";
I do a simple SQL-Query:
SELECT `name`, `likes`
FROM `social`
WHERE `month` = '2015-01'
ORDER BY `likes` DESC
then I add a "Rank" wich is an intenger with ++
$data = array();
$rank = 0;
while ($table_row = mysqli_fetch_assoc($table)) {
$rank++;
$data[$table_row['name']] = $table_row;
$data[$table_row['name']]['rank'] = $rank;
}
The result is left and what I want on the right side
+------+------+-------+ +------+------+-------+
| rank | name | likes | | rank | name | likes |
+------+------+-------+ +------+------+-------+
| 1 | foo | 123 | | 1 | foo | 123 |
| 2 | mfoo | 33 | | 2 | mfoo | 33 |
| 3 | xfoo | 33 | | 2 | xfoo | 33 |
| 4 | yfoo | 30 | | 4 | yfoo | 30 |
| 5 | zfoo | 29 | | 5 | zfoo | 29 |
+------+------+-------+ +------+------+-------+
how do I get the right side table? is there a way to solve it in the query?
EDIT:
There I am standing now:
select IF(#likes=s.likes, #rownum, #rownum:=#rownum+1) rank2,
s.domain_name, s.likes,
(#likes:=s.likes) dummy
from social s,
(SELECT #rownum:=0) x,
(SELECT #likes:=0) y
WHERE `month` = '2015-01'
order by likes desc
but the rank is not 100% correct because I want to skip a rank instead of counting through
I see what's happening here.
I dont know about best practices but I'd probably do something like this myself:
$data = array();
$rank = 0;
$lastlike = 1;
$currentlike = 0;
$i = 0;
while ($table_row = mysqli_fetch_assoc($table)) {
$name = $table_row['name'];
$currentlike = $table_row['likes'];
if ($currentlike != $lastlike) $rank++;
$data[$i] = array('rank'=>$rank,'name'=>$name,'likes'=>$currentlike);
$lastlike = $currentlike;
$i++;
}
I've not checked it but you're welcome to try it out and see.
You can do this with SQL or with PHP (you can test to know which one is the faster).
Note : this 2 solutions will give you a rank which start at 0.
SQL :
SELECT
`name`,
`likes`,
(SELECT COUNT(*) FROM `social` AS S2 WHERE S1.likes > S2.likes) AS `rank_2`
FROM `social` AS S1
WHERE `month` = '2015-01'
ORDER BY `likes` DESC
PHP :
$data = array();
$rank = 0;
$likes_pre = -1;
while ($table_row = mysqli_fetch_assoc($table)) {
$likes_cur = $table_row['likes'];
if ($likes_pre > $likes_cur) {
$rank++;
}
$data[$table_row['name']] = $table_row;
$data[$table_row['name']]['rank'] = $rank;
$likes_pre = $likes_cur;
}
Not tested, but it should work.
Try this... rank should be the same for all rows with the same number of likes
$data = array();
$rank = 0;
$last_likes =0;
while ($table_row = mysqli_fetch_assoc($table)) {
if ($last_likes != $row['likes']) {
$rank++;
}
$data[$table_row['name']] = $table_row;
$data[$table_row['name']]['rank'] = $rank;
}
I'm making a basic notification system for a practice project. I have several tables, two of which look something like the following:
> Table 1: "Users"
>
userid | username | Firstname | Lastname
1 | Daffy | Daffy | Duck
2 | Flinstone | Fred | Flinstone
3 | dduck | Donald | Duck
>
> Table 2: "Notifications"
>
Notification_id | from_user_id | to_user_id | SeenOrUnseen | type
1 | 1 | 2 | 1 | fRequest
> 2 | 1 | 2 | 0 | comment
> 3 | 1 | 2 | 1 | comment
> 4 | 3 | 1 | 1 | fRequest
> 5 | 2 | 3 | 1 | fRequest
> 6 | 2 | 3 | 0 | comment
> 7 | 3 | 2 | 0 | comment
I then need data from both these tables, and would normally JOIN the tables on the user_id and from_user_id before sending of an sql query. However, a join seems to return multiple values because in the second table there are multiple instances of from_user_id. Instead, i'm querying the database, returning the data in a while loop, and within that while loop sending out another query to the database for a different tables' information:
include('../../db_login.php');
$con = mysqli_connect("$host", "$username", "$password", "$db_name");
$tbl_name = "Profile";
$tplname = "profile_template.php";
$tplname2 = "public_profile_template.php";
$myid = $_SESSION['identification'];
//CHECK CONNECTION
if(mysqli_connect_errno($con)) {
echo "failed to connect" . mysql_connect_error();
}else {
$result = mysqli_query($con, "SELECT * FROM Notifications WHERE to_user_id='$myid'");
$count = mysqli_num_rows($result);
if($count == 0){
$style = "display:none;";
} else {
echo "<ul class='notifications'>";
while($row = mysqli_fetch_array($result)){
$from_user_id = $row['from_user_id'];
$to_user_id = $row['to_user_id'];
$seen = $row['seen'];
$nature = $row['nature'];
$result2 = mysqli_query($con, "SELECT * FROM users WHERE id='$from_user_id'");
$count2 = mysqli_num_rows($result2);
if($count2 != 0){
while($row2 = mysqli_fetch_array($result2)){
$fromFirstname = $row2['Firstname'];
$fromLastname = $row2['Lastname'];
}
if($nature == 'fRequest'){
echo "<li> You have received a friend request from" . $fromFirstname . " " . $fromLastname . "</li>";
}
}
}
echo "</ul>";
}
mysqli_close($con);
}
echo "<div id='NoNotification'></div>";
echo "<div id='Notification' style='" . $style . "'></div>";
?>
Is there a better way of doing this?
Thanks for any help!
You can do something like this:
SELECT n.*, u.*
FROM Notifications n
JOIN users u ON n.from_user_id=u.id
WHERE n.to_user_id=$myid
ORDER BY n.id, u.id
You will get all the data you need. The notification data and the user data. It is better to define which fields you want to retrieve in stead of * so you got a better view of what your using and your not sending data which you don't use.
Als you are using an integer (id) as a string, you can just put the $myid there. Also beaware this is not a safe query for MySQL injection. For more info.