Page 1 of 1

POST image and text system

Posted: Sun Jan 26, 2014 6:47 pm
by raigiano
Hello guys, i want to learn how to make users post images and text. And that they can follow each other and that their own posts, and the posts of the person they follow will be shown on their homepage. Something similar to Tumblr. This is something i really want to learn because i need it for future projects. I understand that is a lot of work but i really want to know how it's done.

Thanks!

Re: POST image and text system

Posted: Mon Jan 27, 2014 3:57 am
by Temor
Start by watching these tutorials:

Blog:
http://betterphp.co.uk/playlist.html?pi ... BA2F21ED08

Register & Login:
http://betterphp.co.uk/playlist.html?pi ... AE4FE98280

there is quite a bit of work if you're completely new to php, but if you start somewhere I'll be happy to help.

Re: POST image and text system

Posted: Mon Jan 27, 2014 5:53 am
by killfrog47
raigiano wrote:Hello guys, i want to learn how to make users post images and text. And that they can follow each other and that their own posts, and the posts of the person they follow will be shown on their homepage. Something similar to Tumblr. This is something i really want to learn because i need it for future projects. I understand that is a lot of work but i really want to know how it's done.

Thanks!


As Temor said its a lot of work but its also a lot of fun to learn!

Re: POST image and text system

Posted: Wed Jan 29, 2014 2:57 pm
by raigiano
Yes i know! That is the main reason why no one wants to help me. I'm not completely new to PHP, I know a thing or two about PHP. But this is something i can't do (yet). So any help is welkom.

Re: POST image and text system

Posted: Wed Jan 29, 2014 10:38 pm
by Temor
The thing about this forum is we won't actually write any code for you. You'll have to do all that by yourself. If I wanted to write code for people I would take up (another) freelancing job, and get paid for it.

You write as much code as you can manage by yourself, and me and the others here will come with tips, tricks, ideas and pointers as to where you should be looking next. That is the only way you'll learn writing code, which is the point of this forum.

Re: POST image and text system

Posted: Thu Jan 30, 2014 10:03 am
by raigiano
hahah, I understand that. I am busy writing the code to see if i can do it my self, but if i need any help, i will ask.

Thanks :)

Re: POST image and text system

Posted: Thu Jan 30, 2014 2:22 pm
by Temor
You're welcome :)
No matter how stupid your questions may be, don't be afraid to ask them. I am happy to help! :)

Re: POST image and text system

Posted: Thu Jan 30, 2014 6:01 pm
by raigiano
I am creating a text posting system were users can follow each other en see the posts of the person they follow. I see the post of myself and the user i follow but is till get this error:

Warning: implode(): Invalid arguments passed in C:\xampp\htdocs\website\core\functions\post-message-system.php on line 12
select user_id,body, stamp from posts where user_id in () order by stamp desc
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\website\core\functions\post-message-system.php on line 27
select user_id,body, stamp from posts where user_id in (21,22) order by stamp desc limit 5

This is the function code:

[syntax=php]function show_posts($user_id,$limit=0){
$posts = array();

$user_string = implode(',', $user_id);
$extra = " and user_id in ($user_string)";

if ($limit > 0){
$extra = "limit $limit";
}else{
$extra = '';
}

$sql = "select user_id,body, stamp from posts
where user_id in ($user_string)
order by stamp desc $extra";
echo $sql;
$result = mysql_query($sql);

while($data = mysql_fetch_object($result)){
$posts[] = array( 'stamp' => $data->stamp,
'user_id' => $data->user_id,
'body' => $data->body
);
}
return $posts;

}[/syntax]


And this is the code to show the post on the homepage of the user:


[syntax=php]<?php
$posts = show_posts($_SESSION['user_id']);
$users = show_users($_SESSION['user_id']);
if (count($users)){
$myusers = array_keys($users);
}else{
$myusers = array();
}
$myusers[] = $_SESSION['user_id'];

$posts = show_posts($myusers,5);

if (count($posts)){
?>
<table border='1' cellspacing='0' cellpadding='5' width='500'>
<?php
foreach ($posts as $key => $list){
echo "<tr valign='top'>\n";
echo "<td>".$list['user_id'] ."</td>\n";
echo "<td>".$list['body'] ."<br/>\n";
echo "<small>".$list['stamp'] ."</small></td>\n";
echo "</tr>\n";
}
?>
</table>
<?php
}else{
?>
<p><b>You haven't posted anything yet!</b></p>
<?php
}
?>[/syntax]

I don't know what i am doing wrong

Re: POST image and text system

Posted: Thu Jan 30, 2014 6:25 pm
by ScTech
Implode requires an array as the second parameter. If it's not an array it returns that error. Mysql_fetch_object is failing because your query is failing.

Re: POST image and text system

Posted: Thu Jan 30, 2014 6:54 pm
by Temor
You should not write your code in all lowercase letters. It makes it extremely hard to follow your code.
SQL is especially hard to read when all lowercase.

Here's a few pointers:
Make the "keywords" all caps.
[syntax=php]$sql = "SELECT user_id,body, stamp FROM posts
WHERE user_id in ($user_string)
ORDER BY stamp DESC $extra";[/syntax]

Use backticks (``) around your column and table names.
[syntax=php]$sql = "SELECT `user_id`,`body`, `stamp` FROM `posts`
WHERE `user_id` in ($user_string)
ORDER BY `stamp` DESC $extra";[/syntax]

Create new lines after every column name, to make it easier to make out what data you're actually fetching.

[syntax=php]$sql = "SELECT
`user_id`,
`body`,
`stamp`
FROM `posts`
WHERE
`user_id` in ($user_string)
ORDER BY [syntax=php][/syntax]
`stamp` DESC $extra";[/syntax]

You also need to put semiquotes ( ' ' ) around your strings. The only time you can ignore the quotes is when you're dealing with integer values. Strings always need quotes around them.

[syntax=php]$sql = "SELECT
`user_id`,
`body`,
`stamp`
FROM
`posts`
WHERE
`user_id` in ('$user_string')
ORDER BY
`stamp` DESC '$extra'";[/syntax]

See how much easier it is to read with only a few tab indentations and a couple of new lines?
It makes it MUCH easier to spot typos and other errors when all your code is easily readable like this.


/Edit; I just now saw that $user_string is in fact not a string, but an integer, so you don't have to put quotes around it.
I would however suggest changing the name of that variable. You never want to call a variable "string" when it isn't. Call it something with ID in it to make it instantly obvious what the variable is for.

Re: POST image and text system

Posted: Fri Jan 31, 2014 12:59 pm
by raigiano
yeah i admit that i was a little bit sloppy :P, but i will start over and use the tips you gave me.

thanks :)