I am building a custom social network in php and mysqli. I have a page called notifications.php that shows the user site notifications as well as friend requests! put simply all I want to do is allow the user to purge his notifications list by clicking on a button!
in my html I have this...
<p><span id="purgeList"><?php echo $purgeList; ?></span></p>
<h2>Notifications</h2><?php echo $notification_list; ?></div>
and so far all I have for the button is this....
<?php
$purgeList = '<button disabled>Purge your List</button>';
if ($notification_list == true){
$purgeList = '';
}
?>
The notifications themselves gets pulled in using this script!
$notification_list = "";
$sql = "SELECT * FROM notifications WHERE username LIKE BINARY '$log_username' ORDER BY date_time DESC LIMIT 5";
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if($numrows < 1){
$notification_list = "You do not have any notifications";
} else {
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$noteid = $row["id"];
$initiator = $row["initiator"];
$app = $row["app"];
$note = $row["note"];
$date_time = $row["date_time"];
$date_time = strftime("%b %d, %Y", strtotime($date_time));
$notification_list .= "<p><a href='user.php?u=$initiator'>$initiator</a> | $app<br />$note</p>";
}
}
mysqli_query($db_conx, "UPDATE users SET notescheck=now() WHERE username='$log_username' LIMIT 1");
If you are looking for a way to remove their notifications on the fly, you will need to do an XHR request (also known as AJAX)
for documentation on this you can visit jQuery API for more info on that.
you can cause them to reload the page to go to delete their notifications then use a header redirect to send them back to their 'news feed'
In my opinion i would use ajax because it does not cause a page reload and it can happen dynamically.
Hope this helps =)
Related
I'm developing an api that can detect simultaneous request (For example 5 request is supported).
I have a sql table with rows limit and requests_made.
I have my attempt here the sleep(30) is just a delay for example.
<?php
include_once('connectdb.php');
$api_key_url = $_GET['api_key'];
$sql = "SELECT * FROM table WHERE api_key = '$api_key_url'";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($query)){
$account1 = $row['email'];
$requests_made= $row['requests_made'];
$limit = $row['limit'];
}
if ($check_speed > $check_limit){
echo"Your request couldn't be served. Your account is already running at maximum requests";
exit();
}
mysqli_query($conn, "UPDATE hash SET check_speed = requests_made + '1' WHERE email = '$account1';");
echo"Request Is Served";
sleep(30);
mysqli_query($conn, "UPDATE hash SET check_speed = requests_made - '1' WHERE email = '$account1';");
?>
This works perfectly until the user decides to exit the tab until it finishes loading because the counter will update +1 on the requests_made. Any suggestions for these ?
Use the ignore_user_abort function.
I have a table called flagged_posts in my database, and it has the following columns:
id
thought_id
flagged_by_id
What I am trying to do is that if the logged in user has already flagged the post, then don't allow them to flag the post again, and I am trying to achieve this by removing the anchor link and replacing it by a message.
Here is a snippet of my code:
<?php
$query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE added_by='$user' AND shared ='yes' "."ORDER BY id DESC LIMIT {$start}, {$limit}");
while ($row = mysqli_fetch_array($query)) {
$thought_id = $row['id'];
$message_content = $row['message'];
$date_of_msg = $row['post_details'];
$thoughts_by = $row['added_by'];
$attachent = $row['attachment'];
$shared = $row['shared'];
// getting the id of the user who is logged in.
$see_if_flagged_q = mysqli_query($connect, "SELECT id FROM users WHERE username = '$username'");
$getting_deets = mysqli_fetch_assoc ($see_if_flagged_q);
$logged_in_user_id = $getting_deets ['id'];
echo "
<div class='more_options' style='float: right;'>";
$see_if_flagged_q2 = mysqli_query($connect, "SELECT * FROM flagged_posts WHERE flagged_by_id ='$logged_in_user_id' ");
while ($getting_deets2 = mysqli_fetch_assoc ($see_if_flagged_q2)){
$flagged_post_by_id = $getting_deets2 ['flagged_by_id'];
// If the user logged in has not flagged the post, i.e. there is no data in the database ..
// .. which says their user id has flagged this thought_id.. then display the link...
if ($logged_in_user_id == $flagged_post_by_id){
echo "<a href='/inc/flagged_post.php?id=$thought_id'> Flag </a>";
}
// if there is data stating this user has flagged this thought_id, then echo a message
if ($logged_in_user_id != $flagged_post_by_id) {
echo "Flagged";
}
}
echo " </div>";
}
?>
So assume I am logged in as Conor. Conor has an id of 8 (id obtained from users table). Conor flags a post with an id of 209 (thought_id obtained from user_thoughts table). So in my flagged posts table, I will see the following row:
id: 1
thought_id: 209
flagged_by_id: 8
At the moment, neither link nor the message is appearing. If I change my query, i.e. $see_if_flagged_q2 = mysqli_query($connect, "SELECT * FROM flagged_posts "); (removed the WHERE clause) then I get the message Flagged echo'd four times (because there are four rows in the flagged_posts table and they are echo's on every post, even those which have not been flagged by the logged in user.
Update:
Here is the updated code first of all:
$see_if_flagged_q2 = mysqli_query($connect, "SELECT * FROM flagged_posts WHERE flagged_by_id = '$logged_in_user_id'");
$test_num = mysqli_num_rows ($see_if_flagged_q2);
$getting_deets2 = mysqli_fetch_assoc ($see_if_flagged_q2);
$flagged_post_by_id = $getting_deets2['flagged_by_id'];
if ($flagged_post_by_id == $logged_in_user_id){
echo "<a href='/inc/flagged_post.php?id=$thought_id'> Flag </a>";
echo $test_num;
}
if ($flagged_post_by_id != $logged_in_user_id) {
echo "Flagged";
}
With the above, the link appears for all posts now, even if they are flagged. I have echo'd both $flagged_post_by_id and '$logged_in_user_id', which both echo the value of 12 (the id of Conor from users table). The values are correct and the number of rows returned by $test_num is also correct.
Ok, here's a reworking of your original code. I moved the data gathering part up front, so we have a setup section before we run the while loop on the thoughts. I changed a variable name here and there. Basically, we build a list of flagged entries, and then in the while loop the job is simpler. If the current row id is in the flagged_posts array, it's flagged, else present the link.
// get the id of the current user
$user_id_q = mysqli_query($connect, "SELECT id FROM users WHERE username = '$username'");
$getting_deets = mysqli_fetch_assoc($user_id_q);
$logged_in_user_id = $getting_deets['id'];
// build array of posts flagged by current user
$flagged_posts_q = mysqli_query($connect, "SELECT thought_id FROM flagged_posts WHERE flagged_by_id = '$logged_in_user_id'");
$flagged_posts = array();
while ($row = mysqli_fetch_array($flagged_posts_q)) {
$flagged_posts[] = $row['thought_id'];
}
$query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE added_by='$user' AND shared ='yes' "."ORDER BY id DESC LIMIT {$start}, {$limit}");
while ($row = mysqli_fetch_array($query)) {
//You could just use $row['foo'] down below, and skip all this
/*
$thought_id = $row['id'];
$message_content = $row['message'];
$date_of_msg = $row['post_details'];
$thoughts_by = $row['added_by'];
$attachent = $row['attachment'];
$shared = $row['shared'];
*/
echo "<div class='more_options' style='float: right;'>";
if (in_array($row['id'], $flagged_posts)){
echo "Flagged";
} else {
echo "<a href='/inc/flagged_post.php?id=".$row['id']."'> Flag </a>";
}
echo "</div>";
}
I am trying to make a follower post kind of thing in PHP and whenever Ajax refreshes my page, they are not in the correct order. The output I am getting is that the content of a post always sticks to whoever posts it, for example if I post something, then someone else posts something, then I post something again, it should be like this my post, their post, my post, instead it is like this, my post, my post, their post. So I am basically having an issue ordering what is coming out of the while loop. Here is the code I have.
<?php
require("scripts/connect.php");
session_start();
$my_id = $_SESSION["wave"]["id"];
$query = mysql_query("SELECT * FROM `follows` WHERE `follower_id` = '$my_id'")or die(mysql_error());
if(mysql_num_rows($query) >= 1)
{
while($row = mysql_fetch_assoc($query))
{
$following = $row["following_id"];
$new_query = mysql_query("SELECT * FROM `posts` WHERE `user_id` = '$following' ORDER by `id` DESC");
while($new_row = mysql_fetch_assoc($new_query))
{
echo $new_row["username"]."<br />".$new_row["content"]."<hr>";
}
}
}else
{
echo "You are not following anyone; There are no new Ripples for you.";
}
?>
I'm getting a conflict with these 2 sections of code:
first one to validate that the log in session is in the database tab
include "../scripts/connectpage.php";
$sql = mysql_query("SELECT * FROM login WHERE id='$managerID' AND username='$manager' AND password='$password' LIMIT 1");
$existCount = mysql_num_rows($sql);
if ($existCount == 0) {
echo "Your login session data is not on record in the database.";
exit();
and this one that list all content of another tab
$product_list ="";
$sql = mysql_query("SELECT * FROM table ORDER BY date_added DESC");
$productCount = mysql_num_rows($sql);
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$product_list .= and so on.........
I can log in to the page and it seems fine but I if I refresh it or execute any other stuff on the page, the first code don't validate anymore with the database and I get the echo. If I remove either of the codes they both work fine independently.
Most likely because when you refresh the page, variables:
$managerID, $manager,$password
are not visible anymore on the page.
How you assign value to those variables, through get or post request ?
I need to update database rows and display the change without reloading the page.
This is what I use currently to display the information:
<?php
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = "SELECT * FROM craffyposts ORDER by time DESC $limit";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo $row['like']; // This is what needs to be changed when database is updated.
?>
<?php
};
?>
like.php:
<?php
$cid = $_GET['id'];
database_connect();
$query2 = "SELECT * FROM craffyposts WHERE id = '".$cid."'";
$result2 = mysql_query($query2) or die(mysql_error());
while ($row2 = mysql_fetch_assoc($result2)) {
$lk = $row2['like'];
};
$nlk = $lk + "1";
mysql_query("UPDATE craffyposts SET `like` = '".$nlk."' WHERE id = '".$cid."'") or die(mysql_error());
echo "<script type='text/javascript'>window.location='index.php';</script>";
?>
Any help?
Without reloading the page? You'll have to use a client-side request to fetch new data from the server. the most common of such would be JavaScript's AJAX. You can use AJAX to fetch data from the server (presumably in some quickly parsed format such as JSON) and then load the data into the page with JavaScript.
use JavaScript (or a JS Framework, I prefer jQuery) and use Ajax-Methods to display the data.