Broad Design Implementation: PHP, MySQL, Knockout - javascript

Spent the last few weeks learning MySQL and PHP. Then I learned some basic knockout.js and I'm rewriting a lot of things.'
Just wanted to check before I go further if this is the most efficient way of doing things with PHP/MySQL/Knockout.js-
Pages are php with Knockout which is pulling and pushing data via ajax/Json to separate php files which then push to MySQL. The login system is PHP & MySQL. And I'm using $_SESSION variables to limit content (its a project management system) to what each user is allowed to access.
Is this the current way of doing things? Or am I still behind the times
thanks!
-newb

knockout is the way to go, maybe in combination with jquery in the client side you are good to go, but i recommend you to use a php framework for the server side like codeigniter, zend, symfony there are lots of them, i am currently using codeigniter + jquery + knockout and is boosting me a lot. hope this will help you

Related

Inserting and retrieving data from MySQL database from AngularJS site

As a learning project I have been using AngularJS to create a quiz website. I having a web hosting package with 123-reg.co.uk and managed to create a MySQL database with a table I hope to store peoples scores who complete the quiz.
Ultimately this means I will be able to build a scoreboard on my site. I am hoping to create two functions in my site, one which will retrieve the scores and one which will post new scores. I believe in order to achieve this I will need to use PHP to retrieve and post data however I am a little unsure where to start.
If anyone could provide me with some pointers I would be grateful as I am not particularly familiar with MySQL databases or PHP. I did find this guide on connecting to MySQL database from 123reg.co.uk however I am struggling.
Thanks,
Simon
The tutorial that you mentioned is outdated, relies on mysql_connect that was deprecated in PHP 5.5.0 and removed on PHP 7.0.0. Find a more up to date tutorial that uses mysqli or PDO.
Also you will need to learn some security concerns, I really recommend you to study a bit before doing this, since you can have serious problems if implement a insecure script to do this.
There are great PHP Frameworks that implement all of this in a secure and easy way, try searching for Laravel Framework. Isn't for begginers, but with a little knowlegend of PHP Object-oriented and MySQL (or any SQL supported by Laravel) you can do what you want.
There are also a nice site to learn Laravel, called Laracasts with very nice screen-casts teaching Laravel (for begginers or not).

How to build a REST client frontend for a REST API backend?

I've built a REST API backend using Django and am now at the stage of designing a client facing frontend. Though, I can't seem to understand how this client frontend should be structured and which languages it should use.
PHP is server-side, and is usually used as the language for building backends. When coupled with a framework such as Codeigniter, it can also be used to play around with sessions, route URLs, and decide which templates to use. Though, I don't believe it can be used to call my REST API to fetch resources (might be wrong here, correct me please if I am).
Javascript is client facing but is used only once the webpage has been fetched from the server. AngularJS is great, but from what I've read, it seems it only helps add very dynamic functionality into already rendered static pages.
I am really open to any ideas, suggestions, and advice based on your experiences creating client frontends. So, back to my original question, how does one structure a REST client frontend, which language is best for this goal, and if which frameworks should one consider to use?
Update 1
Someone asked whether this client frontend will be run in a browser -- the answer is yes, it will. I am trying to build a Twitter-like web client frontend that interacts with a Twitter-like REST API backend. Basically, everything you see there when you go on Twitter's website.
Since it is a browser frontend I would go with HTML/JavaScript only. No need to use PHP or any server side language IMHO. This has the advantage of being extremely portable.
I would also use a JS framework for that purpose ( the trend nowadays seems to be angular).
REST really, really isn't new. It's been a part of HTTP at least as far back as HTTP 1.1
Have a look at this question: Backbone.js frontend with RESTful Rails backend? the top answer lists 20 possible frameworks for building a front end.
Thanks for your help, everyone. Found exactly what I was looking for here:
http://docs.python-requests.org/en/latest/
A nice little library for Python that allows me to basically make calls to a REST backend from within a Django application, which serves as my frontend.
AngularJS will also be used for to make the static pages that Django returns more dynamic. Why? Because AngularJS by itself can be the complete solution only if your whole site consists of one page. If you have multiple pages where each one has it's own set of dynamic elements, you need a combination of Django and AngularJS.
Apparently REST is still quite new and it seems there aren't many people that have stumbled upon this very fundamental question like I have.
Once again, thanks!

Single Page Application - Should I use MVC Framework on the server side?

I'm developing a SPA application and I can't decide, whether to use server-side MVC framework or not.
I have chosen AngularJS as the client-side framework. I was thinking I could use Symfony 2 on the server side, but I also considered using pure OOP oriented PHP. Something like this - database class, model classes (User, UserDbManager, ...). I would use something like Slim to create the REST API.
It seems counterproductive to basically write the same code twice, once on the client side and once on the server side, if I could just use the server to serve the data from the database.
If anyone had done similar decision, I'd be happy, if you'd share your thoughts and tips about it.
I would decide depending on the complexity of the requirement.
If it's a very simple I don't see a huge difference in using a framework or plain old php.
But if it's a complex project, I would definitely go with a framework considering the fact that most of the core functions one might need are already there and quicker development.
Again, it would come down to personal preference too.
Codeigniter has been said easy to learn for beginners in framework.
Symfony and zend are more commercially used (I am not saying others are not, but I see more vacancies asking for symfony or zend development experience).
My suggestion will be if you are having angular js as clientside framework. Then take advantage of it and use some api on server side to just return some json kind of response. Why send html on network when you have angular js on clientside to work with dom. just send data from server to client.
I have also done same. I have jquery for clientside and i am using spring mvc to send response as json.

How can you read and write to a MySQL database starting at a JavaScript web game?

First of all, I'm not much of a programmer if at all. This question may seem silly but I would honestly like some advice from real programmers.
I'd like to make a bit of an adventure game on a webpage.
Could I make it by having a MySQL database setup to store variables while JavaScript, HTML and CSS is used for the user interface and JavaScript for the game programming and PHP to communicate with MySQL.
I don't entirely understand it but I followed a tutorial and got it working. It also showed me how you can replace text on the screen by giving that text an elementid and then just setting its value to other text.
In this tutorial script, it has it so when the JavaScript file wants to communicate with php, it will open the php file with a ?=value at the end of the hyperlink where the value part turns into some kind of MySQL search value.
Something like this:
xmlhttp.onreadystatechange=function()
xmlhttp.open("GET","index.php?q="+str,true);
and then in the PHP file:
$q=$_GET["q"];
$sql="SELECT * FROM user WHERE id = '".$q."'";
This means you will always search by specific id number. The problem with this is that the php file is always set to look at the exact same table.
Sometimes you want to look at different tables, or multiple values from multiple tables, etc. Basically, you should be able to select each value like it's a record from one of those automatic DJs that radio stations used to have. Also, sometimes you'd want to write or append the database like when variables change and need to be updated and all of that has to happen securely.
The only thing I can think of is to have a ton of php files that work the same way and call the appropriate file when you want a certain kind of response. But then if I have a file on my website that has a php file that lets me write TO the database then someone can just read the javascript code, see that, and then basically hijack the mysql database.
So how can I securely do this?
I would recommend you to look into using jQuery and Ruby on Rails.
jQuery is a JavaScript library that will make easy your interaction with a server (MySQL) and will help you to get code that works in a lot of different web browsers.
Ruby on Rails is a web framework that will encapsulate everything you need to store state (game data) to a database (MySQL) and handle secure communication, as well as a host of other needs you may eventually face.
In addition to jQuery and Ruby on Rails, there are tons of other comparable frameworks you could use.
YUI3 (http://yuilibrary.com) and Django are two more examples. Express (for Node.js) is a JavaScript back end framework (like Ruby on Rails) that you could use with your existing JavaScript knowledge.
Anyway, good luck!
Theoretically, without moving towards different frameworks, here are a few things to think about...
I think you have the right idea with this what you are trying to do. The PHP file is used as server side logic. It should not be available to the user.
What the user can see is that there is a function available to make changes to something. This he will see from your AJAX call in JavaScript ( xmlhttp.onreadystatechange=function() xmlhttp.open("GET","index.php?q="+str,true); ).
Your responsibility, in the PHP (server side logic) is to make sure you scan the parameters to this function before you allow any changes to be made on the Database.
As with any requests to a database, you need to make sure you are escaping the parameters before any call is made to prevent SQL Injections.
As with previous answer, there are some libraries that exist that have some tools already built in. Some poeple prefer certain tools/languages/libraries over others, but they can all pretty much all do the same thing. What changes is a bit of how it's done. I think you are on the right path, just need to protect those PHP pages of injections and inputs/parameters you do not want.
If you are using multiple PHP pages for different actions, it is possible to have the same PHP script accessed from all other pages. Therefore your escaping (preventing SQL Injections) can be done in the same script and don't need to include it in every single PHP page that makes a database call.
Hope this helps a bit!

how to develope CRUD by backbone.js by database

I want to use backbone.js .
http://documentcloud.github.com/backbone/examples/todos/index.html
In this example it store in the local storage.but i want to store in the database so for this what changes i need to do in the code.
Please suggest .
If I understand your question correctly it's something I was wondering about, I recently posted a question about RESTful persistence in PHP.
Since Javascript doesn't have access to the filesystem you can't use it to write to a database. You'll have to pick a backend server in another language like PHP, Rails, Ruby, whatever. There are a few suggestions for PHP at the link above.

Categories

Resources