Jump to content

notification badge in php


I-AM-OBODO
Go to solution Solved by I-AM-OBODO,

Recommended Posts

That really depends. Usually you'd just query to see if there are any new notifications for the user on each page (like the menu part of your code), and if so use the appropriate CSS. Like for bootstrap: http://getbootstrap.com/components/#badges

hmmm. thanks. but how can one query for new notification thats the issue. doing a select alone can't get the new notification. my idea is you have to have a last count and any other entry that follows will be deemed as new. but the problem is I can't codify it.

Link to comment
Share on other sites

you need to store the id of the latest displayed information, so that you can query for new information with id's greater than that stored value on the next page request. for a value that needs to persist, even if someone logs out, you would need to store it in the user information in a database.

 

see the following posts and the threads they are in for some discussion about this - http://forums.phpfreaks.com/topic/294082-highlight-newest-post-between-refresh/?do=findComment&comment=1503553 and http://forums.phpfreaks.com/topic/292925-if-mysql-table-updated-since-page-load/?do=findComment&comment=1498777

Link to comment
Share on other sites

you need to store the id of the latest displayed information, so that you can query for new information with id's greater than that stored value on the next page request. for a value that needs to persist, even if someone logs out, you would need to store it in the user information in a database.

 

see the following posts and the threads they are in for some discussion about this - http://forums.phpfreaks.com/topic/294082-highlight-newest-post-between-refresh/?do=findComment&comment=1503553 and http://forums.phpfreaks.com/topic/292925-if-mysql-table-updated-since-page-load/?do=findComment&comment=1498777

 can u gimme an example on how to get the last displayed id?

Link to comment
Share on other sites

I tried to get the last id but its giving me error

    $stmt = $pdo->prepare("SELECT * FROM $tbl_name ");
    $stmt->execute();
    $num_rows = $stmt->rowCount();
    echo $num_rows Record(s) Found.;
    $last_id = $stmt->lastInsertId();
    echo $last_id;

but its giving this error:

Fatal error: Call to undefined method PDOStatement::lastInsertId()

 

 

it seems the lastInsertId() can only be used on an insert statement?

Edited by Mr-Chidi
Link to comment
Share on other sites

can u gimme an example on how to get the last displayed id?

 

 

it depends, as CroNIX stated, on what information you are displaying. but it's the highest id of the data you are displaying, at the time you displayed it. it's not the highest id in a table because the number of rows in the table are changing asynchronous to your process where you are trying to find newer rows to display.

 

in whatever data you are retrieving and displaying, find and store the highest id present in that data, either store it in a session variable, in a column in the user's row in a database table, or even in a get parameter in a link, whatever is appropriate for what you are doing, so that you have that value on the next page request where you want to find and display newer rows then what were displayed on the last page request.

Link to comment
Share on other sites

Hello

 

I ended up with my own idea. On the form, i create a hidden field as new_entry and on my table as well.

So when the entry is clicked, i update the new_entry to old_entry.

I created a css to make the new entry font-weight as bold and old entry font weight as normal. but the problem now is assigning the entries to my while condition,

 i.e if entry == old_entry the class = bold else class = normal

my code

 

 

 
    $stmt = $pdo->query("SELECT * FROM $tbl_name");
    $stmt->execute();
    $num_rows = $stmt->rowCount();
    print "<p>$num_rows Record(s) Found.</p>";
 
echo "<table width='100%' border='1' bordercolor='#0cc' cellspacing='0' cellpadding='2'>";
echo "<tr>
    <th bgcolor='#444444' align='center'><font color='#fff'>Surname</font></th>
    <th bgcolor='#444444' align='center'><font color='#fff'>Firstname</font></th>
    <th bgcolor='#444444' align='center'><font color='#fff'>Email / Username</th>
    <th bgcolor='#444444' align='center'><font color='#fff'>Phone</th>
    <th bgcolor='#444444' align='center'><font color='#fff'>Date Registered</th>
    <th bgcolor='#444444' align='center'><font color='#fff'>Status</th>
    <th bgcolor='#444444' align='center'><font color='#fff'>Create Account</th>
    <th bgcolor='#444444' align='center'><font color='#fff'>Delete Account</th>
    </tr>";
// keeps getting the next row until there are no more to get
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

    $clas = $row['entry'];
    if($clas == 'NEW'){
        $style = "<div style='font-weight:bold'></div>";
    }else{
    $style = "<div style='font-weight:normal'></div>";    
    }
  
    // Print out the contents of each row into a table
    echo "<tr><td>";
    echo ucfirst($row['surname']);
    echo "</td><td>";
    echo ucfirst($row['firstname']);
    echo "</td><td>";
    echo $row['email'];
    echo "</td><td>";
    echo $row['phone'];
    echo "</td><td>";
    echo $row['today'];
    echo "</td><td>";
    echo $row['status'];
    echo "</td><td>";
    echo "<a href='create-account.php?id={$row['id']}'>Create Account</a>";
    echo "</td><td>";
    echo "<a href='account-delete.php?id={$row['email']}'>Delete Account</a>";
    echo "</td></tr>";
}
echo "</table>";
 

 

how do i assign it to suit my need.

 

Thanks
 

Link to comment
Share on other sites

by storing the 'NEW' value in with the data, you will have two problems -

 

1) this can only be used by one visitor to your site. when the 'NEW' values are cleared, due to that one visitor viewing the entries, no other visitor will ever see any bold entries. you must store the value that tells your code what has been viewed or not, separately for each visitor.

 

2) by not using the id of the highest row at the time you displayed the data, you can have a race condition where new entries can be  inserted at the same time you are getting and display the data, that won't be displayed on that visit to your page, and that your display logic will clear the 'NEW' value for. they will be displayed the next time you visit the page without being in bold, but they are new rows that where not displayed the last time you visited the page.

 

edit: also, your 'css' isn't css. you are using in-line styling and typing it for each element you are styling. to use css, you define styling rules, that you then apply to elements on your page using a selector, typically a class selector, so that multiple same elements can be styled without repeating the actual styling every place it is used. this allows you to make a change in one place, rather than to go through all your code and change every instance of the in-line styling. it also reduces the amount of markup and clutter you have in the code on your page.

Link to comment
Share on other sites

by storing the 'NEW' value in with the data, you will have two problems -

 

1) this can only be used by one visitor to your site. when the 'NEW' values are cleared, due to that one visitor viewing the entries, no other visitor will ever see any bold entries. you must store the value that tells your code what has been viewed or not, separately for each visitor.

 

2) by not using the id of the highest row at the time you displayed the data, you can have a race condition where new entries can be  inserted at the same time you are getting and display the data, that won't be displayed on that visit to your page, and that your display logic will clear the 'NEW' value for. they will be displayed the next time you visit the page without being in bold, but they are new rows that where not displayed the last time you visited the page.

 

edit: also, your 'css' isn't css. you are using in-line styling and typing it for each element you are styling. to use css, you define styling rules, that you then apply to elements on your page using a selector, typically a class selector, so that multiple same elements can be styled without repeating the actual styling every place it is used. this allows you to make a change in one place, rather than to go through all your code and change every instance of the in-line styling. it also reduces the amount of markup and clutter you have in the code on your page.

On the css rule, i just used that as an example but on the actually page i used a i did not use an in-line styling.

I do not know how else to get it done, but this seem to work for me. Since i do not have an alternative, guess i'll stick with what i have and what i did was once the link is clicked, it updates the 'NEW' to 'OLD' thereby allowing only the clicked link to appear as normal text and not-clicked link always appear bold regardless of the number of views. Once it's not clicked it stays bold forever.

I've tested it so many times and it didnt work-short of what i wanted. What's left is the notification stuff.,

 

 

Thanks

Link to comment
Share on other sites

  • Solution

Finally this is what worked for me. I've tested it several times and it worked. mac_gyver was right but in my own case only the admin visits the site and not multiple users.

 

Had to device my own means. Thanks mac_gyver and cronix for the inspiration!!!

 
    $stmt = $pdo->query("SELECT * FROM $tbl_name");
    $stmt->execute();
    $num_rows = $stmt->rowCount();
    print "<p>$num_rows Record(s) Found.</p>";
 
echo "<table width='100%' border='1' bordercolor='#0cc' cellspacing='0' cellpadding='2'>";
echo "<tr>
    <th bgcolor='#444444' align='center'><font color='#fff'>Surname</font></th>
    <th bgcolor='#444444' align='center'><font color='#fff'>Firstname</font></th>
    <th bgcolor='#444444' align='center'><font color='#fff'>Email / Username</th>
    <th bgcolor='#444444' align='center'><font color='#fff'>Phone</th>
    <th bgcolor='#444444' align='center'><font color='#fff'>Date Registered</th>
    <th bgcolor='#444444' align='center'><font color='#fff'>Status</th>
    <th bgcolor='#444444' align='center'><font color='#fff'>Create Account</th>
    <th bgcolor='#444444' align='center'><font color='#fff'>Delete Account</th>
    </tr>";
// keeps getting the next row until there are no more to get
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

    $clas = $row['entry'];
    if($clas == 'NEW'){
        $style = " style='font-weight:bold'";
    }else{
    $style = " style='font-weight:normal'";    
    }
  
    // Print out the contents of each row into a table
    echo "<tr{$style}><td>";
    echo ucfirst($row['surname']);
    echo "</td><td>";
    echo ucfirst($row['firstname']);
    echo "</td><td>";
    echo $row['email'];
    echo "</td><td>";
    echo $row['phone'];
    echo "</td><td>";
    echo $row['today'];
    echo "</td><td>";
    echo $row['status'];
    echo "</td><td>";
    echo "<a href='create-account.php?id={$row['id']}'>Create Account</a>";
    echo "</td><td>";
    echo "<a href='account-delete.php?id={$row['email']}'>Delete Account</a>";
    echo "</td></tr>";
}
echo "</table>";
Edited by Mr-Chidi
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.