Echoing Sorted Multidimensional Array - php

Ok, so I am creating a web app with php and mysqli.
I have a table friends which is a simple set up:
f_id int(11)
uid int(11)
fids TEXT
now its basically like a row for each user with the fids consisting of a lot of numerical values (other userids) separated by commas like: 1,2,3
so I use this function to get each user's friends:
function getFriends($db, $userid)
{
$q = $db->query("SELECT fids FROM friends WHERE uid='$userid'");
$ar = $q->fetch_assoc();
$friends = $ar['fids'];
$fr = explode(",", $friends);
return $fr;
}
but each posts comments that appear to each of their friends. my problem comes from trying to sort these comments by the time they were posted.
lets say my comments table is:
c_id int(11)
uid int(11)
c_text TEXT
c_time int(11)
I want to be able to get the comments posted by each 'friend' put them all into an array together, then sort them from their c_time value, then all the values from that particular row in the comments table.
The problem comes from my how I've set up my friends table.
I'm using:
$fr = getFriends($db, $userid);
$updates = array();
$i = 0;
foreach( $fr as $friend)
{
// Get Updates from friends and from self
$q = $db->query("SELECT up.*, u.* FROM updates up
LEFT JOIN users u ON u.id = '$friend'
WHERE (up.userid = '$userid') ORDER BY up.up_id DESC");
while($ar = $q->fetch_array(MYSQLI_BOTH))
{
$updates[$i] = $ar;
$i++;
}
}
$sortArray = array();
foreach($updates as $update){
foreach($update as $key=>$value){
if(!isset($sortArray[$key])){
$sortArray[$key] = array();
}
$sortArray[$key][] = $value;
}
}
$orderby = "up_id";
array_multisort($sortArray[$orderby],SORT_DESC,$updates);
$updates_limit = array_slice($updates, 0, 20);
to get the comments from each friend, sorting it by time, then slicing it to the first 20.
However when I var_dump($updates_limit) it takes the last row in the comments table, and then makes it look like each friend posted the same comment.
Can anyone see the problem or a better way of addressing this issue?

I'd completely refactor the friends table to look something more like this: (Also, use english - Characters are cheap :c))
CREATE TABLE friends (
user_id int FOREIGN KEY REFERENCES user(id)
, friend_id int FOREIGN KEY REFERENCES user(id)
, PRIMARY KEY (user_id, friend_id)
);
Then you can take essentially the same comment table:
CREATE TABLE comment (
comment_id int PRIMARY KEY
, user_id int FOREIGN KEY REFERENCES user(id)
, comment_text text
, comment_time datetime
);
And your "query for friend's comments" becomes:
SELECT comment_id, comment.user_id, comment_text, comment_time
FROM friends
INNER JOIN comment
ON comment.user_id = friends.friend_id
WHERE friends.user_id = ? #Target it
ORDER BY comment_time DESC
LIMIT 0, 20;
You can even speed this up by adding a few indexes - like comment(user_id).

Related

Using a variable in a select query

The Problem
I am trying to create a website, where users can post posts and then their friends can see these posts. Similar to facebook, twitter etc.
I got to the point where users can be friends and they can post things. However, I am stuck restricting the shown posts to just the user's friends' posts. At the moment every user can see every post.
I am using PDO in a MVC architecture.
The database structure
TABLE `friends` (
`friendship_id` int(11) NOT NULL AUTO_INCREMENT,
`user_one` int(11) NOT NULL,
`user_two` int(11) NOT NULL,
PRIMARY KEY (`friendship_id`),
FOREIGN KEY (user_one) REFERENCES users(user_id),
FOREIGN KEY (user_two) REFERENCES users(user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
In the table above, depending on who sent the friend request to who, 'user_one' can be either myself or my friend or 'user_two' can be myself or my friend.
TABLE `posts` (
`post_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`post_text` text NOT NULL,
`user_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
The Model
/**
* Get all posts from the suer's friends
* #return array an array with several objects (the posts)
*/
public static function getAllPosts()
{
$my_id = Session::get('user_id');
$database = DatabaseFactory::getFactory()->getConnection();
// get all of the user's friends
$friendsQuery = $database->prepare("
SELECT user_one FROM friends WHERE user_two = '$my_id';
SELECT user_two FROM friends WHERE user_one = '$my_id';
");
$friendsQuery->execute();
$friends = $friendsQuery->fetchAll();
$friendsQuery->closeCursor();
$query = $database->prepare("SELECT * FROM posts WHERE user_id = '$my_id' OR user_id = '$friends'");
$query->execute(array());
return $query->fetchAll();
}
In the above code my problem lies in the $friends variable. If I replace it manually with the user_id of my friend it works as intended.
E.g.:
"SELECT * FROM posts WHERE user_id = '$my_id' OR user_id = '1'";
The Controller
/**
* Gets all posts (of the user and his friends).
*/
public function index()
{
$this->View->render('post/index', array(
'posts' => PostModel::getAllPosts()
));
}
The View
<?php if ($this->posts) { ?>
<?php foreach($this->posts as $key => $value) { ?>
<p><?= htmlentities($value->post_text); ?></p>
<?php } ?>
<?php } ?>
Summary
I am not able to create a sql query, which just selects posts, which were posted by myself or my friends and does not select any other posts.
I would be very thankful, if somebody could help me out with this!
UPDATE
My model now looks like this:
/**
* Get all posts from the suer's friends
* #return array an array with several objects (the posts)
*/
public static function getAllPosts()
{
$my_id = Session::get('user_id');
$database = DatabaseFactory::getFactory()->getConnection();
// get all of the user's friends
$friendsQuery = $database->prepare("
SELECT user_one FROM friends WHERE user_two = '$my_id';
SELECT user_two FROM friends WHERE user_one = '$my_id';
");
$friendsQuery->execute();
$friends = $friendsQuery->fetchAll();
print_r($friends);
$friendsQuery->closeCursor();
foreach($friends as $friend)
{
$friend_ids[$friend->key] = $friend->val;
}
print_r($friend_ids);
$friendsSQL = implode(',',$friend_ids);
$query = $database->prepare("SELECT * FROM posts WHERE user_id = '$my_id' OR user_id IN('$friendsSQL')");
$query->execute();
return $query->fetchAll();
}
This is my output:
Array ( [0] => stdClass Object ( [user_one] => 1 ) )
Notice: Undefined property: stdClass::$key
Notice: Undefined property: stdClass::$val
Array ( [] => )
You can add all friends_id into an array and then use this code.
//$friends is an array.
$friendsSQL = implode(',',$friends);
$query = $database->prepare("SELECT * FROM posts WHERE user_id = '$my_id' OR user_id IN($friendsSQL)");
You should note that $friendsQuery->fetchAll() will return an Array. Also, if PDO::FETCH_ASSOC is supplied as an additional argument [fetchAll(PDO::FETCH_ASSOC)], it would be an associative array.
The structure of the array could look like this :
Array ( [int] datasetNumber =>
Array (
[string] "friendship_id" => [int] 1,
[string] "user_one" => [int] 1,
[string] "user_two" => [int] 1
)
)
I gave some more Information about returning data from PDO Statements in this post.
your $friends variable is being returned by a fetchAll() function so will probably be a list of results. Your then using that list as a string in the query which won't work. You need to extract the ID of the friend from the $friends list.
if you're using PDO then use the variable handling built into PDO ie:
$query = $database->prepare("SELECT * FROM posts WHERE user_id = :variable_ID OR user_id = '1'");
$query->bindValue( ':variable_ID', $my_id, PDO::PARAM_STR ] );
$query->execute();

PHP Recursive Find siblings

working with this for a while.. cant get my head right.. sooo... help here ;-)
It is quite simple I am sure..
Table:
CREATE TABLE IF NOT EXISTS `items` (
`item_id` bigint(20) NOT NULL AUTO_INCREMENT,
`item_parent_id` bigint(20) NOT NULL COMMENT 'itemid which this item belongs to',
`item_name` varchar(255) NOT NULL,
`item_serialnumber` varchar(255) NOT NULL,
`item_model` varchar(255) NOT NULL,
PRIMARY KEY (`item_id`),
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
I am trying to create an array of item_id and the item_id that it belongs to - via the item_parent_id - recursivly -
so that even if you find a child to a parent, check if the child is a parent to others.
Tried with something like this:
function get_item($item_id, $menu)
{
$sql = "
SELECT
items.*,
customers.*
FROM
assets
LEFT JOIN item_customer_rel USING(item_id)
LEFT JOIN customers USING(customer_id)
WHERE
items.item_parent_id = '".$parent."'
ORDER BY
items.item_name
";
$res = mysqli_query($db, $sql) or die("ERROR: SQL Select a2a ancestor", $sql, mysqli_error($db) , $_SESSION["u_id"]);
while ($items = mysqli_fetch_assoc($res))
$menu = build_ancestor_array($parent, $menu);
}
function build_ancestor_array($parent, $menu)
{
GLOBAL $db;
$sql = "
SELECT
items.*,
customers.*
FROM
items
LEFT JOIN item_customer_rel USING(item_id)
LEFT JOIN customers USING(customer_id)
WHERE
items.item_parent_id = '".$parent."'
";
$res = mysqli_query($db, $sql) or cc("ERROR: SQL Select a2a ancestor", $sql, mysqli_error($db) , $_SESSION["u_id"], $this_document);
while ($items = mysqli_fetch_assoc($res))
{
if ($ancestor_item_array[$parent] == $items["item_id"])
$menu = build_ancestor_array($parent, $menu);
$ancestor_item_array[$parent] = $items["item_id"];
// Creates entry into items array with current menu item id ie. $menu['items'][1]
$menu['items'][$items['item_id']] = $items;
$menu['items'][$items['item_id']]["connection_type"] = 2;
// Creates entry into connected_to array. connected_to array contains a list of all items with connected_to
$menu['connected_to'][$items['item_parent_id']][] = $items['item_id'];
}
return $menu;
} // end build item array
It only goes one "level" down.
Refer, the 2 links below, I had recently posted answers for these, this is done in pure SQL
Recursive MySQL Query with relational innoDB
and
How to find all child rows in MySQL?
Recursive worked.. Just needed to try manually with pen and paper ;-)
function get_item_data($parent, $menu, $ancestor_item_array = "")
{
GLOBAL $db;
$sql = "
SELECT
items.*,
customers.*
FROM
items
LEFT JOIN item_customer_rel USING(item_id)
LEFT JOIN customers USING(customer_id)
WHERE
items.item_parent_id = '".$parent."'
ORDER BY
items.item_name
";
$res = mysqli_query($db, $sql) or cc("ERROR: SQL Select a2a ancestor", $sql, mysqli_error($db) , $_SESSION["u_id"], $this_document);
while ($items = mysqli_fetch_assoc($res))
{
$ancestor_item_array[] = $items["item_id"];
if (!in_array($items["item_parent_id"], $ancestor_item_array))
$menu = get_item_data($items["item_id"], $menu, $ancestor_item_array);
// Creates entry into items array with current menu item id ie. $menu['items'][1]
$menu['items'][$items['item_id']] = $items;
$menu['items'][$items['item_id']]["connection_type"] = 2;
// Creates entry into connected_to array. connected_to array contains a list of all items with connected_to
$menu['connected_to'][$items['item_parent_id']][] = $items['item_id'];
}
}
It wont work on pure SQL.
You should take a look at stored procedures, the sql you're trying to make will only go 'inwards' one level because all the relations will be shown as if they're first level connections.
for example.
parent->son->grandson->ggson
parent.item_parent_id = null
son.item_parent_id = parent
grandson.item_parent_id = son
ggson.item_parent_id = grandson
even tough grandson is a lower level connection, he will show up as a first level connection.
it cant be done with pure sql, sadly..
that's one of the reasons that made me go to NOSQL databases.

Combine several SQL querys to one

I know this is probably really simple but I have tried to find some similar examples and failed.
The problem is that I would like to list 8 random images from a gallery in a database, sorted by added date. And I have managed to do this, but only with several iterating query's that becomes really slow. So if someone would be so kind to teach me about combining them for faster speed, I guess UNION is the way to go? Here is my working (but slooow code)
<?php
$latestPictures = mysql_query("SELECT pictureID, addedDate FROM picture ORDER BY addedDate DESC LIMIT 8");
$latestConcerts = mysql_query("SELECT concertID, addedDate FROM concert WHERE pictureID is null ORDER BY addedDate DESC LIMIT 8");
// Add concerts and pictures to array
while($curFestival = mysql_fetch_object($latestPictures))
{
$array[$curFestival->addedDate] = "p" . $curFestival->pictureID;
}
while($curConcert = mysql_fetch_object($latestConcerts))
{
$array[$curConcert->addedDate] = "c" . $curConcert->concertID;
}
// Order array by key
krsort($array);
$latestArray = array_slice($array, 0, 8);
foreach($latestArray as $key => $value) {
$type = substr($value, 0, 1);
$ID = substr($value, 1);
// If type == picture
if($type == 'p')
{
$picturesPicturesID = mysql_query("SELECT concertID, name FROM photo WHERE concertID IN(SELECT concertID FROM concert WHERE pictureID = $ID) ORDER BY photoID");
// Get random picture
$curRandomPicture = rand(0, (mysql_num_rows($picturesPicturesID) - 1));
$curPictureConcertID = mysql_result($picturesPicturesID, $curRandomPicture, "concertID");
$curPictureName = mysql_result($picturesPicturesID, $curRandomPicture, "name");
$curPicture = mysql_fetch_object(mysql_query("SELECT c.URL, p.name FROM concert c, picture p WHERE p.pictureID = c.pictureID AND c.concertID = $curPictureConcertID"));
echo "Some image";
}
// If type == concert
if($type == 'c')
{
$concertPicturesID = mysql_query("SELECT concertID, name FROM photo WHERE concertID = $ID ORDER BY photoID");
// Get random picture
$curRandomPicture = rand(0, (mysql_num_rows($concertPicturesID) - 1));
$curPictureConcertID = mysql_result($concertPicturesID, $curRandomPicture, "concertID");
$curPictureName = mysql_result($concertPicturesID, $curRandomPicture, "name");
$curPicture = mysql_fetch_object(mysql_query("SELECT URL, name FROM concert WHERE concertID = $curPictureConcertID"));
echo "Some image";
}
}
?>
I realized that I forgot to include the tables, here they are:
TABLE OF photo:
PKEY: photoID
FKEY: concertID
name
TABLE OF concert:
PKEY: concertID
FKEY: pictureID
name
URL
addedDate
TABLE OF picture
PKEY: pictureID
name
date
So every post is part of TABLE photo AND concert, but only some is part of picture witch is only used sometimes to group differens albums together. When they are grouped together I whant a random name post from that grouping ID (picture) and if they are by them self a random name post from there (concert).
Accckkk! (In the infamous words of Bill the Cat.)
It's hard to figure out what result set your code really wants from the database.
This query isn't the most efficient, but it will return 8 random rows from the photos table, with those rows ordered by addedDate:
SELECT r.*, c.*
FROM (SELECT p.*
FROM photo p
WHERE p.concertid IS NOT NULL
ORDER BY RAND()
LIMIT 0,8
) r
JOIN concert c
ON c.concertid = p.concertid
ORDER BY r.addedDate ASC
If you have a really large photo table, this is going to be slow, because that RAND() function has to get called for every single row in the table, and MYSQL has to produce a temporary result set (a copy of the table) and then sort it on that derived column.
(NOTE: I'm assuming here that it's the photo table that you want to return "random" rows from, and I'm assuming that concertid is the primary key on the concert table, and a foreign key from the photo table. It's apparent that you have three tables... concert, picture and photo, but it's not clear which columns are the primary keys and which columns are the foreign keys, so it's likely I have it wrong.)
(NOTE: replace the p.* and c.* with a list of expressions you actually want to return.)
There are more efficient approaches to returning a single random row. In your case, you want exactly eight rows, and you presumably don't want to return a duplicate.

Problems with a SQL SELECT [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
one variable row inside another variable row
I have script something like -
$sql = "SELECT * FROM `users`"
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$sql1 = "SELECT * FROM `other_table`";
$q1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($q1);
$item = $row1[$row['username']];
How can I set one variable row inside another, since it don't work. Basically, I need to select username, and then select column with user username from other table, in which is written user points.
I was thinking about adding -
$sql = "SELECT * FROM `users`"
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$sql1 = "SELECT `".$row['username']."` FROM `other_table` WHERE `uid` = 1";
$q1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($q1);
$item = $row1[xxxxxxxxxx]; // DONT KNOW HOW TO DEFINE IT, so it takes out found variable (there is only one).
I have question already, but not with full info, and I don't know how to delete it :(!
Here is my table -
Users
'id'--'name'
'1'--'Bil'
'2'--'Conor'
'3'--'Ilian'
Other_table (which holds points for users)
'id'--'Bil'--'Conor'--'Ilian'
'1'--'2'--'3'--'55'
Don't ask, why I don't hold the points in the same table, since if I could, I would do that ;)!
It sounds like you need to redesign the tables to start with, I would suggest a table structure like this:
Table Users
ID | Name
Table Points
UserID | Points
This way you can add a foreign key constraint between the tables and then do a simple join query like:
SELECT U.Name, P.Points FROM Users AS U INNER JOIN Points AS P ON U.ID = P.UserID
Then your php code would look like:
$sql = "SELECT U.Name, P.Points FROM Users AS U INNER JOIN Points AS P ON U.ID = P.UserID";
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
Here are the create table queries for you:
CREATE TABLE Users (ID INT NOT NULL AUTO_INCREMENT,
Name VARCHAR(50) NOT NULL, PRIMARY KEY (ID))
CREATE TABLE Points (UserID INT NOT NULL, Points INT,
FOREIGN KEY(UserID) REFERENCES Users(ID) ON UPDATE CASCADE ON DELETE CASCADE)
EDIT:
This should allow you to get the value (NOTE: untested as I don't have a php/mysql instance to mess with currently), but for a more robust solution you really should look into redesigning your table schema.
$userName = $row['username'];
$sql1 = "SELECT [". $userName ."] FROM other_table WHERE [uid] = 1";
$q1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($q1);
$item = $row1[$userName];
SELECT users.id, users.name, other_table.points FROM users LEFT JOIN other_table ON other_table.name=users.name

PHP and MySQL problems

I'm trying to count how many times a certain article has been graded for example how many times have users_articles_id 3 been graded by my members.
I'm also trying to count the points for a certain article for example users_articles_id 3 is related to the ratings database by its ratings_id the rating points should be a total of 13.
I was wonder if I was doing this right because to me it looks all wrong? I was hoping if some one can help me fix this? And where should my code go exactly?
I'm using PHP and MySQL?
Here is my MySQL tables
CREATE TABLE articles_grades (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
ratings_id INT UNSIGNED NOT NULL,
users_articles_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
date_created DATETIME NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE ratings (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
points FLOAT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (id)
);
Database Input
articles_ratings
id ratings_id users_articles_id user_id date_created
1 3 2 32 2010-01-13 02:22:51
2 1 3 3 2010-01-13 02:23:58
3 2 3 45 2010-01-13 02:24:45
ratings
id points
1 10
2 3
3 5
Here is the PHP code I'm trying to fix.
// function to retrieve rating
function getRating(){
$sql1 = "SELECT COUNT(*)
FROM articles_ratings
WHERE users_articles_id = '$page'";
$result = mysql_query($sql1);
$total_ratings = mysql_fetch_array($result);
$sql2 = "SELECT COUNT(*)
FROM ratings
JOIN ratings ON ratings.id = articles_ratings.ratings_id
WHERE articles_ratings.users_articles_id = '$page'";
$result = mysql_query($sql2);
$total_rating_points = mysql_fetch_array($result);
if(!empty($total_rating_points) && !empty($total_ratings)){
// set the width of star for the star rating
$rating = (round($total_rating_points / $total_ratings,1)) * 10;
echo $rating;
} else {
$rating = 100;
echo $rating;
}
}
Well I think there are several issues here.
1) You are defining a table called articles_grades, not articles_ratings, as in your code.
2) Why do articles_grades and ratings need to be in separate tables? There is a one-to-one correspondence between those tables.
3) You need to do sum(points) in your second query.
4) You can combine both queries into a single query.
Here's how I would do it if you don't change the schema:
<?php
mysql_connect('localhost','root','fake123123');
mysql_select_db('test');
$result = mysql_query('SELECT users_articles_id,count(*),sum(r.points)
FROM articles_grades ag,ratings r
WHERE ag.ratings_id = r.id
GROUP BY users_articles_id');
if (!$result)
die('invalid');
else{
echo '<table><tr><th>Article Id</th><th>#Ratings</th><th>#Points</th></tr>';
$a = mysql_fetch_row($result);
while($a){
echo '<tr><td>'.implode('</td><td>',$a).'</td></tr>';
$a = mysql_fetch_row($result);
}
echo '</table>';
}
?>
You can run this as a CGI script. It should return a table of the results.
Let me know if this helps.

Categories