I want to fetch and display posts of different users as they upload a post.. i can access their posts by userid but the posts are in sequence and not random or new posts. For example if 2 users have posted 4 times, 3 times first user and 1 time 2nd in this sequence 1st user post, 2nd user post, 1 user ,1 user, my query will return the 3 posts of user 1 first and then single post of user 2 irrelevant of the original sequence and time(old/new).
My query is
//this is my table -> user_post(userid,post_id,post,time)
$query = "SELECT * FROM user_post ORDER BY (time) DESC WHERE userid = '$user_id'";
and i want to load only 4 latest posts and when i next refresh the field the next 4 posts must be loaded..
this is a Android Client-Server Based application and i'm using volley to make the http calls and exchange data using JSON.
Did you try to use the limit clause ?
You code would be like :
$query = "SELECT * FROM user_post ORDER BY (time) DESC WHERE userid = '$user_id' LIMIT 4";
You need to include a LIMIT clause inside your query:
$query = "SELECT * FROM user_post ORDER BY (time) DESC WHERE userid = '$user_id' LIMIT 0,4";
On every request you maintain a count of how many rows have loaded in $lastLoadedRow. The next several times you load you would do:
... LIMIT $lastLoadedRow,4;
... LIMIT $lastLoadedRow,4;
... LIMIT $lastLoadedRow,4;
Related
I'm having an issues with my queried results. I have three tables, a follower table, a user table, and a posts table, while my logged in user = $user_name. My code calls all from follower table where the the follower is = to the logged in user, and calls the name of all the users their following ($followi). Then I call all from the posts table where the posters name is = to the username of the users the user is following. and the code works.
MY PROBLEM: I have tried to order my results by date, but MYSQL is placing precedence on the Followi, and displays in order of user who posted, then order of date. so if one user has 2 posts a month apart, it displays these two, then displays the next user, even if they posted 5 times in the last minute.
how do i display ONLY Based on date while keeping it to where it only displays the posts of users that the logged user is following?
heres my code
<?php
$follow = "SELECT followi from follow where follower = '$user_name'";
$runf = mysqli_query($con,$follow);
while($row=mysqli_fetch_array($runf)){
$followi = $row['followi'];
$sel_msg = "SELECT * from posts where poster_name = '$followi' ORDER by post_time desc";
$run_msg = mysqli_query($con,$sel_msg);
while($row_msg=mysqli_fetch_array($run_msg)){
echo "
<div id='post_box'>
".$row_msg['post_content']."
</div>
";
}}
?>
That's because your loop starts with the followersi data (the while loop). This is the order you are retrieving data and displaying it in the current logic:
Get posts for User A
User A Post 1
User A Post 2
User A Post 3
Get posts for User B
User B Post 1
User B Post 2
User B Post 3
What I believe you are trying to do is get the posts in date order but retrieve the user information to display at the same time. You can do that with a JOIN statement e.g.
$sel_msg = "SELECT posts.*, users.name, users.image
FROM posts
JOIN users ON posts.userId = users.id
ORDER BY post_time DESC";
$run_msg = mysqli_query($con,$sel_msg);
There is no need for the first sql statement in your code
I am not sure how to fix this issues.
Basicly, I need to be enable to retrieve data from a database.
But this needs to be limited to the users account plan.
For example: an user has a membership plan, which comes with 5 emails.
He will need to be enable to see 5 emails, instead of my whole email database getting extracted.
Code:
$SQLSelect = $odb -> query("SELECT * FROM `accounts` ORDER BY `ID` DESC LIMIT = '" . ($_POST['email']) . "'");
Desc limit is option what allows to display limited amount.
Example:
You have table with 5 rows and those rows have auto increment id.
wtih select * from users order by id desc limit 3, you will select users with id 1 , 2 , 3 because at begining you order it by id, so it select 1 ,2 ,3 ,4 ,5 and then u say that you need only 3 of them using desc limit. So it seems like you got desc limit wrong...
If i got your example then you need something like this :
$limit = 5;
query("SELECT * FROM `accounts` ORDER BY `ID` DESC LIMIT = ".$limit)
So here user will only see accounts with id 1 , 2 , 3 , 4 and 5.
If i didn't say what you need, i rly didn't get your question
I have created a posting system along with a liking system. I wanted to select the top three posts with the most likes but I don't know how to do that.
I have this code but I can't figure out how to select more than just one row.
$get_pop_posts = mysql_query( "SELECT MAX( likes ) AS popular_posts FROM `posts`;" );
while($fetch_pop_posts = mysql_fetch_array($get_pop_posts)){
$pop_posts = $fetch_pop_posts['popular_posts'];
echo $pop_posts;
}
this piece of code only fetches one row from the database.
SELECT *
FROM posts
order by likes desc
limit 3
SELECT MAX( likes ) AS popular_posts FROM posts
This query return single result because of the function MAX().
Try this
SELECT TOP 3 FROM posts
Firstly give unique id to each post that should be incremented automatically each time a new post comes.
Then,
SELECT id FROM posts order by likes desc limit 3
Pass these id's as arrays to get the complete post.
Hi everyone I am trying to write a sorting script
For this my user will click a move up button which posts the id of the current selection we want to move up to a new page where the script is processed.
So using the fetch functions below i am getting the sort id of the current row we want to move up
$sqlCurrent = "SELECT * FROM `contact` WHERE `contact_id` = $id LIMIT 0, 1 ";
$resultsCurrent= mysql_query($sqlCurrent) or die(mysql_error());
$rowC = mysql_fetch_row($resultsCurrent);
$currentSort =$rowC[9];
I then pulled out all the data in decending order using
Now if my current sort order is 6 I want to look for the row with the sort order with 3 or 4 or 5 in order so i used the decending order and the next sort scrip comes up in the next table.
$sql = "SELECT * FROM `contact` ORDER BY `contact`.`contact_sortOrder` DESC LIMIT 0, 30 ";
The question is how do I simply get data from that row using 1 or maybe 2 functions.
We cant simply look for the next sort order because it is possible it wont be there.
For this example i have used a database like this
rowId 1 Sort order 6
rowId 2 Sort Order 2
rowId 3 Sort Order 4
Now I am row Id 3 and want to replace it with the next one. So i need to pick up rowId 2 somehow using the shortest method.
Any help will be useful
Could be as simple as
$query = "
SELECT
x,y,z
FROM
contact
WHERE
contact_sortOrder < $currentSort
LIMIT
1
";
(assuming $currentSort is safe for "direct insertion" into the query. see http://docs.php.net/pdo.prepared-statements)
select prev.*
from contact as curr
left join contact as prev on prev.contact_sortOrder < curr.contact_sortOrder
where curr.contact_id = $id
order by prev.contact_sortOrder desc
limit 1
I am coding a blog post kind of thing, where the author will post the article and it will be displayed in the front end, my problem starts for selecting the posts as i have to meet certain conditions for posting the news in the front end,
I have 4 fields in the database namely
title
pic_title
pic_brief
pic_detail
you guessed it right apart from the title table the rest of three will hold the path to the images in varchar datatype, which will be used to display as the post, the format of the front end is such that
a) there will be total of eight post
displaying in the front end (eight
entries from the database)
b) there will be three post on the top which will include the value from
the table title, pic_title and
pic_brief (total of 3 values)
c) and the rest five will contain just the title and pic_title
(excluding the three entries of top)
Please NOTE: i want the second query to exclude the top 3 record
which already exist in the top i.e
(first query = 3 post in descending
order, second query = 8 - first 3 = 5
post)
The Order of the Post i want is by id DESC
EDIT: I took the first query as
SELECT * FROM news ORDER BY id DESC LIMIT 3
Now if i take the same second query and try populating the values by desc order again the same records will be accessed
In simple words i want a query that will skip the last three records order by id DESC
How do i achieve this feat in PHP?
If you just want the SQL, here it is:
First query
SELECT * FROM `table` LIMIT 3
Second query
SELECT * FROM `table` LIMIT 3,5
(where table is the name of your table of course. Of course you may want to add some ORDER BY clause. To execute these queries in PHP, I suggest reading the manual. If you have any specific problems after doing so, then you can post a new question.
This is a situation where I'd likely opt to select all eight records at once - the less trips to the database, the better.
SELECT t.title,
t.pic_title,
t.pic_brief
FROM TABLE t
ORDER BY t.id DESC
LIMIT 8
...because the rest is just presentation:
$query = sprintf("SELECT t.title,
t.pic_title,
t.pic_brief
FROM TABLE t
ORDER BY t.id DESC
LIMIT 8");
// Perform Query
$result = mysql_query($query) or die( mysql_error() );
$rowcount = 1;
// Use result
while ($row = mysql_fetch_assoc($result)) {
if(rowcount <= 3) {
echo $row['title']
echo $row['pic_title']
echo $row['pic_brief']
} else {
echo $row['title']
echo $row['pic_title']
}
++$rowcount;
}
first query will be like this
"select title, pic_title , pic_brief from table_name order by post_id desc limit 0 , 3"
and rest of five will be
"select title, pic_title from table_name order by post_id desc limit 3 , 5"
second query will exclude the three results returned by first query...
If you want more perfection you can collect all three Ids returned by first query and can add NOT IN in second query.
"select title, pic_title from table_name where post_id not in (1,2,3) order by post_id desc limit 0 , 5";