Saving a value to a database then appending a value to it - php

So im doing a project and i have made a webpage, on this page i have a total value which is a float inside of a div called total. What i want to do is save this value to a database on my webspace (im hosted by 123 reg) so each user saves the total that they get and then the total in the database in the website is constantly updated and displayed again on the website as a "Global total".
I have no idea about how i would go about doing this any takers?

Ok so basically you might want to take a look at Javascript, especially how to submit a form using javascript:
http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
Then you want to take a look on how to process this data in PHP.
The data should come in within the $_GET or $_POST variables.
And you might want to learn about SQL INSERT statements.
http://www.ntchosting.com/mysql/insert-data-into-table.html (somewhere around the bottom)
And then you might want to learn about using the MySQL SELECT-Statement. With that you can use a summation operation to get the sum of all totals and then using PHP echo that into the page again.
Or using AJAX you could load it into the page in a somewhat "live" action.

Related

How to autoload data from a database in laravel PHP

I have a question on how to achieve something from a development standpoint in Laravel. I am looking for ways to use autoload to return data from a database by just supplying a few character entry. For example, if I need to search for the word 'Johnson' which is already stored in a table in the database, supplying the word: 'Joh' in the search bar should bring up Johnson and other names like Johnson. I am very new to PHP and laravel, does anyone have any suggestions?
You are going to have to use Ajax and php to do this.
Basically the logistics will be as such:
a key is typed into an input which will load a javascript function that does a post to a php file. That php file will do a query for "Joh%". All records will come back matching Joh%. Print them out with php and it will be delivered back to the javascript function which called the php file in the first place. Now that javascript function will have to manage adding and subtracting entries from the dropdown on the input box you are typing in.
The question your asking is basically a lot of code and you should take it step by step. First start by getting a php query to work where you get records matching "Jon%", then continue on to another step.

How do I implement an Angular-like filter function in php?

I have a page that is full of tables (generated by a foreach loop) and every row displays, among other things, a month.
Now I want to give the user the ability to choose a month and have only the table rows for that current month displayed (or no table at all if that month is missing from the table). A bit like the filtering function in Angular JS.
I'm still a bit new to php and the only solution I can up with is to handle the whole thing somewhat clumsily with modifying CSS classes and having the superfluous rows hidden by CSS.
What would be a more efficient way to do this?
Your problem is related with the filter alternate of angular into php.
As you know php is server side. So, if you want to get the filter after a reload or by ajax method, you need a $_GET variable.
Use a different query string in your url for each click of each button. Then retrieve the GET variable and use it to get data from database for relevant category.

PHP - Changing content dynamically

Alright, so I have a PHP app that, in essence, fills up an array with references to elements in an XML file, does a shuffle() on the array to randomise it, then cycles through the array, displaying the data from the array (and ergo the XML file) on the screen.
My current code works fine - It fills the array, shuffles it, and displays the 0th index (which -is- random). My problem lies in the fact that I want to be able to reload the content on the page dynamically, without losing the data in the arrays, but not link to a different page. More specifically, I want to be able to cycle through the array on the click of a button (without totally reloading the page, losing the valuable data in the array).
I tried using some hidden form fields to load the values of the array into a temporary array, then feed them back in as the page reloads, but to no avail. I find a lot of bugs this way, and it's quite clearly a quick-n-dirty way of doing it.
Essentially, I want the code to do something like this:
$heaps_array = array(...); // Populated by, let's say 3 strings, for argument's sake
shuffle($heaps_array);
echo $heaps_array[0];
// User clicks the Next button...
// Get rid of the $heaps_array[0] from the page content, and...
echo $heaps_array[1];
// User clicks the Next button...
// Get rid of the $heaps_array[1] from the page content, and...
echo $heaps_array[2];
// User clicks the Next button...
...
PHP has a / is request based interpreter language so you need to run a script every time you need something from server. You can't have those arrays always in memory without using some storage engine (memcached, MySQL, you name it).
As others have said, you can send the array to the frontend and manipulate it in JavaScript. That way you won't load the server and have all data in memory.
Cant you send the array to the client side (the dirty way, not ajax) ?
Then display values with js.
i mean:
echo 'var array = new Array('.implode(',', $array).');';
Otherwise you will need more sofisticated method, the best is to build a mini webservice and get values through an AJAX query.
You have a few options here, but you need to understand that PHP is a server side language that runs and finishes before the browser even sees the page content. To do dynamic content, thats where javascript and ajax come into play. I recommend a javascript framework such as jQuery that makes AJAX calls simple.
Some ways to do it:
1) Output the entire php array into a javascript variable on page. Do all of the array sorting using javascript. No AJAX.
2) Use php to sort the array, store it in a session variable. Use AJAX to request new data from the array in session memory, resort the array if necessary and return the value you need.
3) Use an AJAX call to request and return the entire sorted PHP array. Use javascript to do with it as you wish.
Depending on the size of the data you wish to return, you may want to minimise amount of data request through AJAX and the client side processing, and just use PHP to do all of the array stuff then return the exact values you need.
Just throwing PHP's session management out there since no one has. http://php.net/session_start

How do I permanently change the contents of a div with JavaScript & PHP?

Okay. So I don't have any example code to show, but after doing a bit of research and learning the basics of PHP, I think the answer to my question should be pretty simple.
Here is the scenario, as I would like it to be:
On the homepage there will be several team names, with scores next to them. Like "house-points" in Harry Potter.
Below the score is a small text-field. Below that is a submit button.
The user will put a number in the text-field, press submit, and that number will be added to the team's total score.
NOW. I know how to achieve all of that with JavaScript. Easy. What I want to know IS:
How do I make that new number (the new score total) STAY there. I need to permanently alter the HTML when that submit button is pressed.
All I know is that I need to use PHP. I know the basics of PHP, so whatever the answer is, just throw it at me and I'll figure it out.
Sounds like what you want to do is submitting forms. First drop the JavaScript, you won't need it. What you need is to put your text fields in a form and when you submit you can fetch your values with $_<GET|POST|REQUEST>['<name_of_field>'].
Then you will need to store it somehow. The best way to do it is to use a database like MySQL or MongoDB to store it, but it could be a bit tricky if you are just learning this, so maybe you would like to stick to files. You could do this with INI files and PHP's INI functions.
Lastly you will need to print out the correct values to the website. Now this is easy: Just edit your HTML file to do something like
<?php echo $score['team1']; ?>
for each team after retrieving the correct values at the top or something. (Don't forget to rename the HTML file to .php as well).
Now you should be all set to save your scores. =)
If you mean really permanent you'll have to send it to a database via Ajax (combination of PHP and Javascript). OR write it to a text-document, which is less good.

Save information to database after jquery callback

I'm new here and I'm very new at programming but I need some serious hand-by-hand help here.
I was searching jquery and found a script to drag and drop stuff on the screen, basicly i just want to move some divs around, thats the easy part, the script I found has a callback function that writes onto the div that you just moved "dropped", this is exactly what I need but instead of writting dropped I want to save the 2 postion variables into a database (mysql), this is so that if I close the browser and open it again the div's will be on the last place I dropped them.
Can you help? Is there a jquery user interface with this already built in ?
I think this is easy to do with jquery ajax functions right? basicly I should send the serialized data (json right?) into a page that processes that data once its feed into it, then jason returns the handler with success or even with some output right?
It would be cool for the dragabble div to have a handler with last know position retrieve by jason from an external page that acts like a buffer to the database.
Is this the correct pipeline?
Best Regards
Joricam
you have kind of a vague question here, but I can try to help you get closer to the answer.
Imagine two sides of the puzzle:
When the page loads, the two (or more) DIVs are drawn on the screen. If you want them to draw in a specific order, you need to keep track of that in the database. So be sure your db has a field called something like display_order, and then display the DIVs in that order. (You can usually just add ORDER BY display_order to the SQL, so they are retrieved in the order you want, and then draw the DIVs right out in a loop.)
When someone drags and drops a DIV, you use AJAX/JSON/etc to tell your PHP script the new order. In this case, when that happens, rather than draw the word 'dropped' in the DIV, you should instead immediately update the display_order fields in the database. This way you are remembering each DIV's position.
Does that help/make sense?
UPDATED: thinking more about your question, here is the pseudo code:
in "display.php":
Fetch the contents for each DIV from the database, with ORDER BY display_order on the rows.
Draw them on the screen, looping through each database row.
Also in this HTML, use the jQuery script you already have to call another PHP script (dragged.php) when a row is dragged.
in "dragged.php":
This script is called when a row is dragged on the screen.
Currently it puts the word "dropped" in the DIV that is dragged. That's not helpful, so remove that.
Instead, you now know (from the variables passed to you) that a specific DIV needs to be in a specific place.
So grab a list of the DIVs from the database, then change the order of them (by altering the display_order column) based on the new position(s) you know.
Save that back to the database, so when display.php is called again next time, it draws the DIVs in the order you want.
Hope this helps explain further. If you are still struggling, I respectfully suggest you try to write the code, and post a more specific question about the part that you're stuck on. This will help you get a good answer quickly. (You may also want to Google this one a lot; I'm sure there are code samples out there showing how to do all this.)

Categories