dynamic pages with php

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 861 Views - Last Post: 28 December 2013 - 10:28 AM

#1 wakinews  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 10-November 13

dynamic pages with php

Posted 30 November 2013 - 04:29 AM

Hello guys. I am fairly new to programming and web development. I am building a simple microblogging site . I want users to be able to fill out a form about a song they are listening to,artist name and a short comment and then click share. just like a twitter tweet. I have already created a database of users and posts.

How do i make the post appear on the page below the form? and when another one is posted how do i make it appear on top of the other? I know abit of php,javascript,css and other web technologies but i dont know which technologies to use and how.

thank you very much in advance

Is This A Good Question/Topic? 0
  • +

Replies To: dynamic pages with php

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,988
  • Joined: 08-June 10

Re: dynamic pages with php

Posted 30 November 2013 - 05:37 AM

Hey.

What you need for that is a server-side language, like PHP, ASP.NET or Python, as well as a database. - You have a database already? If so, then which database system?

What you are asking to do is actually pretty simple, once you learn the basics of your chosen language. You could no doubt learn enough to accomplish this in a matter of hours. If you know some PHP, that would be a good choice. There are plenty of good tutorials out there, starting with the one on the the official manual, as well as many more. I quite like how the one on Codeacademy is set up; it's more interactive than most of the tutorials out there.
Was This Post Helpful? 0
  • +
  • -

#3 ge∅  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 482
  • Joined: 21-November 13

Re: dynamic pages with php

Posted 30 November 2013 - 05:38 AM

First things first. Do you know how to store the comments in a database ?
Was This Post Helpful? 0
  • +
  • -

#4 wakinews  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 10-November 13

Re: dynamic pages with php

Posted 30 November 2013 - 05:53 AM

@Atli. I know java well and am learning php so its easy for me to understand it. I have a Mysql database and I have created two tables and i know how to write to the database with PHP and Mysql. The proble is on writing back to the page in a manner that is like the twitter pages like i said.

@ge. Yes i know how to store the comments, i know how to register new users and have them post something. but then to bring the posts to the page as they are added is the issuee
Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,988
  • Joined: 08-June 10

Re: dynamic pages with php

Posted 30 November 2013 - 06:02 AM

OK, what is the problem then, exactly? Please describe it more clearly. "like the twitter pages" doesn't tell us much. (Especially those of us who don't really use Twitter.)
Was This Post Helpful? 0
  • +
  • -

#6 wakinews  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 10-November 13

Re: dynamic pages with php

Posted 30 November 2013 - 06:15 AM

Ok the thing is: when a user opens the page for posts, they should see what others have shared starting with the most recent ones goin down to the old ones. Also on the same page if one creates a post it should appear immediately on the top of the others.see the picture below of my twitter page.As i write on the left and submit it,it goes on the right to the other old messages/tweets.Attached Image
Was This Post Helpful? 0
  • +
  • -

#7 wakinews  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 10-November 13

Re: dynamic pages with php

Posted 30 November 2013 - 06:32 AM

is it goin to be something like this?
<?
//database connection



$result = mysql_query("SELECT * FROM posts order by created_at 
desc",$linkme); 
 
while($row = mysql_fetch_array($result)) 
{ 
?> 
Song:<? echo $row['song'] ?>;echo "<br />";
<artist:<? echo $row['artist'] ?>;echo "<br />";
 <? echo$row['comment']?>;echo "<br />";

  
 } 
mysql_close($linkme); 
?>


Was This Post Helpful? 0
  • +
  • -

#8 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,988
  • Joined: 08-June 10

Re: dynamic pages with php

Posted 30 November 2013 - 06:50 AM

That's the basic idea, yes. Fetch the posts in reverse order, and print them.

Three things about that code, however, that should be mentioned.

  • The old MySQL API functions - like mysql_query and mysql_real_escape_string - are obsolete. They shouldn't be used anymore. Instead, you should be using PDO or MySQLi.

  • You generally want to avoid using the short-tags version of the PHP tags (<? ... ?>). Support for them is optional, so unless you're 110% sure you'll always have full control over the server the code is working on (and you intend to handle it's configuration yourself) then you should use the full version: <?php ... ?>.

  • The mysql_close() is redundant. PHP does that automatically for all connections when it closes the script.


Also, I'd like to point out that the PHP+HTML infusion code style is highly problematic for anything except very small projects. Maintenance and improvement for such code is extremely difficult to do efficiently. You may want to look into code separation, OOP and more advanced design patterns if you intend to do anything large-scale. (Those concepts are not language specific, so you may well know much of that from your Java development?)

Edit:
It just occurred to me that Twitter no doubt uses AJAX to update the post feed client-side, without having to reload the page. So when you submit a tweet, it'll appear on the right immediately without the entire page refreshing. - Is that what you are after?

This post has been edited by Atli: 30 November 2013 - 06:50 AM

Was This Post Helpful? 1
  • +
  • -

#9 wakinews  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 10-November 13

Re: dynamic pages with php

Posted 30 November 2013 - 07:09 AM

Thats what i was looking for. I thought its possible with php alone. so does it mean if I am going to show the newp post after saving it i have to add a code that reloads the page upon saving?

thanks alot for your comments about my code. i will do code separation and the rest. thanks alot
Was This Post Helpful? 0
  • +
  • -

#10 ge∅  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 482
  • Joined: 21-November 13

Re: dynamic pages with php

Posted 30 November 2013 - 07:33 AM

You don't need to add any code to reload the page if you submit a HTML form. The page will be redirected to the location of the form's action attribute automatically.

What Atli meant is that you can send a HTTP request in Javascript instead. It will retrieve the server response in background without redirecting the user.
Was This Post Helpful? 0
  • +
  • -

#11 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,988
  • Joined: 08-June 10

Re: dynamic pages with php

Posted 30 November 2013 - 07:41 AM

PHP is a server-side language. It only executes as the HTTP server is preparing a response to a HTTP request. As such, anything you do in PHP requires a request to the server. Normally that means that the page needs to be redirected or refreshed in order for PHP to be able to update anything.

However with AJAX, you can do a HTTP request in Javascript; on the client-side, in the background, so that the page itself never has to be reloaded. The PHP code that processes the new post and gets you the updated post list can happen in response to a background request. The user never has to know that a request for data was sent to the server.
Was This Post Helpful? 0
  • +
  • -

#12 wakinews  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 10-November 13

Re: dynamic pages with php

Posted 30 November 2013 - 08:04 AM

Ok thanks .I am reading about JQuery now. I know abit fo javascript. I hope the learning curve wont be that steep.
Was This Post Helpful? 0
  • +
  • -

#13 wakinews  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 10-November 13

Re: dynamic pages with php

Posted 30 November 2013 - 08:21 AM

By the way how can i present this on my page. I sulk at design dont laugh at me.
i want a poster's picture then to the right:
song: songname
artist:artistname
[commentboxhere]
ofcourse with the timestamp of the creation.
more like the image below of the twitter page
Attached Image
Was This Post Helpful? 0
  • +
  • -

#14 ge∅  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 482
  • Joined: 21-November 13

Re: dynamic pages with php

Posted 30 November 2013 - 11:10 AM

You don't need jQuery just for function that can fit in 15 lines.

Bellow is an example

function POST(action, data, callback){
	var xhr = new XMLHttpRequest(), encodedParams = "";
    for(input in data){
        encodedParams+=input+"="+encodeURIComponent(data[input])+"&";
    }
	xhr.onreadystatechange = function(){
		if (xhr.readyState==4 && xhr.status==200 && callback){
			callback(xhr.responseText);
		}
	};
	xhr.open("POST", action, true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send(encodedParams);
}



you can use it this way

POST("script.php", {
    user: 52,
    song: "song",
    artist: "artist",
    comment: "Hello Wolrd"
});



you can also add a callback function to display the server response for example

POST("script.php", {
user: 52,
song: "song",
artist: "artist",
comment: "Hello Wolrd"
}, function(serverResponse){
alert(serverResponse);
});
[/code]
Was This Post Helpful? 0
  • +
  • -

#15 wakinews  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 10-November 13

Re: dynamic pages with php

Posted 30 November 2013 - 11:38 AM

:dozingoff:

I am confused there. the POST i know how to use is the one we use in forms i.e POST=(page.php) The way its use here requires me to look further into it. I dont understand where exactly to place this code as well. Sorry for being too naive
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2