I'm working out a social tool like twitter, but I got stuck on "who also tweet this twitter."
There's a list of twitters on my page, and I wanna display who also tweeted this for each tweet.
I'm using MySQL + php + Smarty.
Here's the code:
/* ---- php ---- */
// Get twitters that tweeted over 200 times.
$sql = mysql_query("SELECT id, content, user, tweets FROM twitter_list WHERE tweets > '200'");
$twitter_array = array();
while($tweet_row = mysql_fetch_array($sql)){
array_push($twitter_array, $tweet_row);
// Get the users who tweeted these twitters.
$twitter_id = $tweet_row['id'];
// Find out 5 friends who also tweeted this twitter. twitter_relation stores who tweets what.
$friends_who_also_tweet = mysql_query("SELECT tid, twitter_id, user_id FROM tweet_relation WHERE twitter_id = '$twitter_id' ORDER BY tid DESC LIMIT 5");
$friends_who_also_tweet_array = array();
while($friends_who_also_tweet_row = mysql_fetch_array($friends_also_tweet_array)){
array_push($friends_who_also_tweet_array, $friends_who_also_tweet_row);
}
if($friends_who_also_tweet_array){
$sm->assign("fwat", $friends_who_also_tweet_array);
}
}
if($twitter_array){
$sm->assign("twitter", $twitter_array);
}
$smarty->display('twt.html');
/* ---- HTML: twt.html ---- */
...
{section name=twitter loop=$twitter}
<div class="content">{$twitter[twitter].content}</div>
<div class="who_also_tweet">
{section name=who loop=$fwat}
<div class="i_also_tweet">{$fwat[who].user_id}</div>
{/section}
</div>
{/section}
I wish to get the array of twitter, and for each twitter, retrieve the 5 users who tweets it. But it only display the first twitter's users, I suspect while() might be wrong but cannot find it out. Anyone could lend me a hand? Many thanks.
$twitter_array = array();
then:
if($twitter_array){
$sm->assign("twitter", $twitter_array);
}
You never added anything to that array. I'm assuming you mean to do it here (since $tweet_array is never declared):
array_push($tweet_array, $tweet_row);
In fact, this variable also gets assigned and then never referred to as well:
$friends_who_also_tweet
Your variable naming is really confusing, and I think it may be contributing to your problems.
Edit: Also, why do you assign this with every iteration of the first While loop? It is just going to overwrite the previous values:
if($friends_who_also_tweet_array){
$sm->assign("fwat", $friends_who_also_tweet_array);
}
Related
I need to make an PHP application where anybody can define a page name and get the public information of that page, specifically the followers count.
For example, Google's Facebook page (https://www.facebook.com/Google/) has 28.318.253 likes and 33.390.596 followers. How can I, programatically, get those numbers?
I want my final code to look something like this:
$page = "google";
// Some code logic, API calls or anything else
$likes = $pageData->likes_count;
$followers = $pageData->followers_count;
echo $likes; // Should output "28318253"
echo $followers; // Should output "33390596"
I am currently trying to set up a custom sortable list. Without getting into too much detail as this would become a very large post, I would like to use the PHP foreach function to show the list.
I currently have
$video_id = $row['videos'];
$res = preg_replace("/[^0-9,.]/", "",$video_id);
$exclude = explode(',', $res);
} ?>
....
<ul id="sortable2" class="connectedSortable">
<?php foreach ($video_list as $key => $item): ?>
<?php if(in_array($item['id'], $exclude)){ ?>
<li id="video-<?php echo $item['id'] ?>"><?php echo $item['id'],
$item['title'] ?></li>
<?php } ?>
<?php endforeach; ?>
</ul>
This generates a list of items from the database excluding items with an ID from an array which is also fetched from another table.
In this example, it will only show videos with the id 3,4 and 2. This is all working as expected. My issue is once I have reordered the items and refreshed the page they are loaded in numerical order. I need these to be load in the order specified (3,4 and 2 for this example).
Any help would be highly appreciated.
Thanks
I think what you are looking for is ORDER BY FIELD (MySql), this lets you order by a field in a specific way.
I need these to be load in the order specified (3,4 and 2 for this example).
Sounds like a good fit to me!
For example
SELECT * FROM table WHERE id IN(3,4,2) ORDER BY FIELD(id, 3,4,2)
This will select rows from table where the id is in the list and then order the results by id in the order they are place there, 3 then 4 then 2.
excluding items with an ID
This might cause some issue, the Field order by assumes you know at least some of the values in the field. Excluding implies that you know what you don't want but not necessarily what you do want, if that makes sense.
Maybe it's not an issue, I don't have enough information on your particular use case to determine that, so I am just mentioning it as a cautionary thing.
Thanks!
[edited] I need to display posts from database sorted in a kinda specific way.
There is a page which at set time interval query database via ajax to pull 180 latest post. Each post in db has column "source" which can be "instagram" or "twitter". Ajax returns 30 sets (or less) x 6 posts each (less if there is not enough posts). Than with help of js all sets are hidden and only one set (6 posts) is displayed at a time. After few seconds set is hidden and next one is shown. Think of it like a typical slideshow but with posts instead of images on infinite loop.
At the begining it wasn't important what kind of post were in sets. It could be all 6 posts from twitter or instagram or mixed. But as usual when project was scheduled to finish, I was asked to change the way sets are generated. Now client wants to have only 2 types of sets with posts in particular order (described below).
So my question is how change this (simplified example from my ajax file):
DB details for ref.: table POSTS: id / source / user / date etc.
$sql="SELECT * FROM posts ORDER BY id DESC LIMIT 180";
...
$stmt->setFetchMode(PDO::FETCH_OBJ);
$set=1;
while($row = $stmt->fetch()) {
if($set==1){
echo '<!--start set--><div class="set">';
}
if($row->source="twitter"){
echo '<div class="post twitter">';
echo '{all stuff related to this twitter post}';
echo '</div>';
} else if($row->source="instagram") {
echo '<div class="post instagram">';
echo '{all stuff related to this instagram post}';
echo '</div>';
}
if($set==6) {
echo '</div> <!--//END set-->';
$set=0;
}
}
$set++
}
into something what let me generate those 30 sets but with posts distributed among them using this pattern:
<!--First set should has post in order: --
<div class="set>
<div class="post twitter">{stuff}</div> {instagram post} {instagram post}
{instagram post}{instagram post}{twitter post}
</div> <!--//END set-->
<!--Second set order:-->
<div class="set">
{instagram} {instagram} {twitter}
{twitter} {instagram} {instagram}
</div>
etc.
Of course there is no guarantee that there would be enough post to create all 30 sets with those patterns so I need to create as many sets following those two patterns and leftovers put in sets as before simply in order they are pulled from db.
Thats all.
Thanks in advance for any suggestions / ready solutions ;) etc.
You could try something with a split between the two kind of posts using array_filter.
$posts = $stmt->fetchAll();
$twitterPosts = array_filter($posts, function($post){
return $post->source == "twitter";
});
$instagramPosts = array_filter($posts, function($post){
return $post->source == "instagram";
});
$set=1;
while(!empty($twitterPosts) && !empty($instagramPosts))
{
if($set%2 == 1)
{
$instagramPost1 = array_shift($instagramPosts);
$instagramPost2 = array_shift($instagramPosts);
$twitterPost1 = array_shift($twitterPosts);
}
else
{
$instagramPost1 = array_shift($instagramPosts);
$twitterPost1 = array_shift($twitterPosts);
$twitterPost2 = array_shift($twitterPosts);
}
/* display them */
if($set==6) {//your condition
echo '</div> <!--//END set-->';
$set=0;
}
$set++;
}
/* now one of them is empty, you can just display the other one */
if(!empty($twitterPosts))
/* Display all remaining twitter posts */
elseif(!empty($instagramPosts))
/* Display all remaining instagram posts */
Of course you should add validation that there is still something in the array between 2 array_shift.
I hope this helps !
EDIT : sorry I missed the condition for only 3 item per row, adding it now.
I have worked on this for a few days now.
This snippet finds the correct row(s) from the database. It works fine. However, when the Button is clicked, I only want for the comment it is next to targeted, not all the comments on that page. It's hard to explain.. Let me show you image.
What I am doing currently is selecting the database rows and outputting them for debugging, instead of removing them (I don't want to go around deleting sections of my database with broken code)
$db is mysqli connection to the database
Snippet of the while loop:
TL/TR
I want to do the following with this in the end:
Remove the comment from the database.
Give the feedback to an admin.
This may sound like a noob question, but please help me.
Many thanks!
Just can't wrap my head around this, made like 6 pages single-handed and now I am stuck in a thing this "easy" for a d**n week and i still can't get it right.
EDIT
Latest snippet (Still doing it, What the hell!?!)
$query = $db->query("SELECT * FROM comments WHERE post_id='$id'");
while($row = $query->fetch_object()){
echo "<h5>".$row->name."</h5>","<br>";
$strip_comment = strip_tags($row->comment);
$delComment = $row->comment_id;
$strip_comment_shlashes = stripslashes($strip_comment);
echo "<blockquote>".$strip_comment_shlashes,"<br><br></blockquote>";
//button stuff
if($is_admin){
$query1 = $db->prepare("SELECT comment_id FROM comments WHERE comment_id = '$delComment'");
$query1->execute();
$query1->bind_result($commId);
while($row2 = $query1->fetch()):
?>
<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"?>" method="post">
<input type="submit" class="closeButton" name="deleteComment" value="<?php echo $commId; ?>" />
</form>
<?php
if(isset($_POST['deleteComment'])&& $is_admin){
if($is_admin && $commId){
echo "Comment ID <b>$commId</b> Removed";
}
}
endwhile;
}
Edit: NON-OBJECT error..
$delComment_2 = $_POST['deleteComment'];
$query2 = $db->prepare("SELECT * FROM comments WHERE comment_id='$delComment_2'");
$query2->execute();
Edit 2: (31/10)
Please, Could someone fix this snippet and post it? I don't usually ask for working snippets, but this one is driving me crazy. I just am too noob to understand how this goes. Thanks.
Change the input to this:
<input type="submit" class="closeButton" name="deleteComment" value="$comment_id_here" />
And then just use the comment id to delete a specific one from the database. (check $_POST['deleteComment'])
Of course, there are numerous other ways to do this - but the point is the same: you need to pass the comment id (not the post id) from your page to the query string. Whatever way you use to accomplish that is up to you, I just gave you an example.
Hi i have a sorting question to ask.
My database has images with "Photoname" as one of the columns in mySQL table.
Database:
Photoname
Apple
Bear
Cat
Orange
So in my catalog.php, I would display all the images from the database. Then, i have a navigation bar(div) to allow viewers to filter the images. So users can view by A | B | C... | Z|.
My question is with regards to the URL and approarch. I would create imagesort.php which would handle all the mySQL in a general way( so that i don't have to waste time creating for A - Z).
Should the imagesort.php be something like imagesort.php?sort=A? Then in imagesort.php how can i get the value A? For example:
select photoname, date from image where photoname LIKE 'a%'
And also if the above way is correct, how can i parse this variable A to the link in catalog.php?
Here is what i've done so far in my catalog.php:
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
...
</ul>
Do i really have to do this for 26 entries? or is there a simpler method?
You can use something like this...
<ul>
<?php for($i=65;$i<90;$i++) { ?>
<li>&#<php echo $i;?>;</li>
<?php } ?>
</ul>
You'll need to get the value of sort from the $_GET array. Make sure you sanitize your input before using it in your SQL query.
$match = "";
if(isset($_GET['sort']))
{
// Escape the string to guard against SQL injection
$match = mysql_real_escape_string($_GET['sort']);
}
$query = "select photoname, date from image where photoname LIKE '$match%'";
This code has the effect of working for any value of sort, including an empty string. If you want to limit the valid strings to a single uppercase alpha character, you'll have to check separately for that, like so:
if(strlen($match) != 1 || ctype_upper($match) == false)
{
$match = "";
// Maybe some other error condition
}
If the links are formatted like that then they will end up in the $_GET super global:
$sort = $_GET['sort'];
For the second question, you can easily use a loop in php to run through the letters of the alphabet, to generate the pagination content you need:
$link_base = 'imagesort.php';
$pagination_content = '<ul>';
for($i=65; $i<=90; $i++)
{
$pagination_content .= '<li>'.chr($i).'</li>';
}