d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Simple Php Exercise I Need To Do > Help Me Please
123Next
Add Reply New Topic New Poll
Member
Posts: 16,004
Joined: Jul 2 2009
Gold: Locked
Dec 2 2016 09:00am
Okay, so I was given the task to create a php project in which I can:

View all the apprentices at my work
View all the weeks they had to hand in their weekly homework.
This can be displayed in a simple chart.

The hard part for me is:
Our trainer wants me to add some feature, where he can simply press a button on the interface, and the "status" of the specified week will change from "not done" to "done" for exmaple. So when somebody does his homework he can simply press a button and the status changes automatically ("he wanted a color change from red to green").

I can't use a database for this.

Any tips what commands I should be looking at?
He told me that it is doable with a file that can be changed through the push of the button.

Help is greatly appreciated!

/edit:
He said I should be using a .csv file for this!

This post was edited by bochakaboy on Dec 2 2016 09:04am
Member
Posts: 20,253
Joined: Apr 30 2008
Gold: 5,267.97
Dec 2 2016 09:34am
Which part of the file IO is difficult for you?
Member
Posts: 6,742
Joined: Mar 23 2008
Gold: 45.00
Dec 2 2016 10:49am
"I can't use a database for this."

AJAX call using JQuery. You'll undoubtedly have the PK id on the page, so simply send that in the call to your PHP page.

Pull the record with the id to ensure it's valid, determine if it's currently flagged as completed or not and return that to the client.

From there, changing the color is easy. $('(insert selector)').css('color','green'); in your success: function(data) { } (one of the $.ajax object parameters). Keep in mind you're returning JSON, so you'll need to parse it. var blah=$.parseJSON(data); for example.

Code
$.ajax example:
//get the pk either using getElementById or $('-')
$.ajax({
datatype: "json",
method: "post", // you'll receive the values you send in the post superglobal
url: "your php page",
data: {
record_id: id //accessed as $_POST['record_id'] with the value of id on receiving page
},
timeout: 15000,
async: false,
success: function(data) {
var blah=$.parseJSON(data);
if ( blah['completed'] ) {
//We completed within php page
$('selector').css('color','green');
} else {
$('selector').css('color','red');
}
}
});


return values from the php page to the client using print json_encode($value);

As for writing it to a CSV, that's fairly straight forward. Create a buffer string with your headers separated by commas. Write it to the file. Rewrite buffer with your values in a loop, write to file every iteration.

This post was edited by Blizane on Dec 2 2016 10:50am
Member
Posts: 16,004
Joined: Jul 2 2009
Gold: Locked
Dec 2 2016 11:41am
Quote (Blizane @ Dec 2 2016 04:49pm)
"I can't use a database for this."

AJAX call using JQuery. You'll undoubtedly have the PK id on the page, so simply send that in the call to your PHP page.

Pull the record with the id to ensure it's valid, determine if it's currently flagged as completed or not and return that to the client.

From there, changing the color is easy. $('(insert selector)').css('color','green'); in your success: function(data) { } (one of the $.ajax object parameters). Keep in mind you're returning JSON, so you'll need to parse it. var blah=$.parseJSON(data); for example.

Code
$.ajax example:
//get the pk either using getElementById or $('-')
$.ajax({
datatype: "json",
method: "post", // you'll receive the values you send in the post superglobal
url: "your php page",
data: {
record_id: id //accessed as $_POST['record_id'] with the value of id on receiving page
},
timeout: 15000,
async: false,
success: function(data) {
var blah=$.parseJSON(data);
if ( blah['completed'] ) {
//We completed within php page
$('selector').css('color','green');
} else {
$('selector').css('color','red');
}
}
});


return values from the php page to the client using print json_encode($value);

As for writing it to a CSV, that's fairly straight forward. Create a buffer string with your headers separated by commas. Write it to the file. Rewrite buffer with your values in a loop, write to file every iteration.


The issue is that I can't use anything outside of PHP. No Java no SQL no nothing :/ I shouldn't even use CSS for this..


Quote (Leevee @ Dec 2 2016 03:34pm)
Which part of the file IO is difficult for you?


what is file IO? :unsure:
Member
Posts: 6,742
Joined: Mar 23 2008
Gold: 45.00
Dec 2 2016 12:41pm
Quote (bochakaboy @ Dec 2 2016 05:41pm)
The issue is that I can't use anything outside of PHP. No Java no SQL no nothing :/ I shouldn't even use CSS for this..




what is file IO? :unsure:


Yeah... pretty sure you misheard the instructions, man
Member
Posts: 16,004
Joined: Jul 2 2009
Gold: Locked
Dec 3 2016 03:58am
Quote (Blizane @ Dec 2 2016 06:41pm)
Yeah... pretty sure you misheard the instructions, man


I particularly asked him again regarding only PHP, as it seems very easy for me using SQL.

He said to work with a csv file or text document, there are commands like fopen fwrite etc, I should be using some of those right? But I don't understand how to implement buttons in a static php page.
Member
Posts: 735
Joined: Jan 23 2007
Gold: 725.94
Dec 4 2016 02:19pm
Put the buttons in a <form> and then use the method and action attributes to pass the information you need along. When the button is pressed, the form action will be loaded in the user's browser, via the method given. If you use GET, then you can use $_GET and POST for PHP $_POST.

Example:

Code
<form action="get" method="file.php">
<input type="hidden" name="what_to_do" value="do_something" />
<input type="submit" value="submit" />
</form>


And in PHP
Code
if ($_GET["what_to_do"] == "do_something") {
do_something();
}


I would stick with a CSV file because you can edit it with Excel or OpenOffice, Libreoffice, etc. Think of a CSV as a spreadsheet.

Code
APPRENTICE NAME, WEEK 1 IS DONE, WEEK 2 IS DONE, ..., WEEK N IS DONE
garrettthegreat, true, true, ...


Read the entire file into a variable. Split it along the newline character via explode(). Split those string along the comma character via explode(). Make a multidimensional array such that you have $var["user"]["week"].

When changes are made, save the file back to the CSV.
Member
Posts: 16,004
Joined: Jul 2 2009
Gold: Locked
Dec 5 2016 01:26pm
Quote (garrettthegreat @ Dec 4 2016 08:19pm)
Put the buttons in a <form> and then use the method and action attributes to pass the information you need along. When the button is pressed, the form action will be loaded in the user's browser, via the method given. If you use GET, then you can use $_GET and POST for PHP $_POST.

Example:

Code
<form action="get" method="file.php">
<input type="hidden" name="what_to_do" value="do_something" />
<input type="submit" value="submit" />
</form>


And in PHP
Code
if ($_GET["what_to_do"] == "do_something") {
do_something();
}


I would stick with a CSV file because you can edit it with Excel or OpenOffice, Libreoffice, etc. Think of a CSV as a spreadsheet.

Code
APPRENTICE NAME, WEEK 1 IS DONE, WEEK 2 IS DONE, ..., WEEK N IS DONE
garrettthegreat, true, true, ...


Read the entire file into a variable. Split it along the newline character via explode(). Split those string along the comma character via explode(). Make a multidimensional array such that you have $var["user"]["week"].

When changes are made, save the file back to the CSV.


i will give this a try, thanks a lot!
Member
Posts: 16,004
Joined: Jul 2 2009
Gold: Locked
Dec 19 2016 04:25pm
Quote (garrettthegreat @ Dec 4 2016 08:19pm)
Put the buttons in a <form> and then use the method and action attributes to pass the information you need along. When the button is pressed, the form action will be loaded in the user's browser, via the method given. If you use GET, then you can use $_GET and POST for PHP $_POST.

Example:

Code
<form action="get" method="file.php">
<input type="hidden" name="what_to_do" value="do_something" />
<input type="submit" value="submit" />
</form>


And in PHP
Code
if ($_GET["what_to_do"] == "do_something") {
do_something();
}


I would stick with a CSV file because you can edit it with Excel or OpenOffice, Libreoffice, etc. Think of a CSV as a spreadsheet.

Code
APPRENTICE NAME, WEEK 1 IS DONE, WEEK 2 IS DONE, ..., WEEK N IS DONE
garrettthegreat, true, true, ...


Read the entire file into a variable. Split it along the newline character via explode(). Split those string along the comma character via explode(). Make a multidimensional array such that you have $var["user"]["week"].

When changes are made, save the file back to the CSV.


So I was finally able to make the csv etc, however it is not possible for me to have a button for EACH entry that is either red or green. I use 0's for not done(red), and 1's for the done(green) tasks.
Anybody can give me some hints what to use for entry to have their own button based on their value(0 or 1), and if I press that button it changes that value to the opposite?
Member
Posts: 735
Joined: Jan 23 2007
Gold: 725.94
Dec 19 2016 07:29pm
Quote (bochakaboy @ Dec 19 2016 04:25pm)
So I was finally able to make the csv etc, however it is not possible for me to have a button for EACH entry that is either red or green. I use 0's for not done(red), and 1's for the done(green) tasks.
Anybody can give me some hints what to use for entry to have their own button based on their value(0 or 1), and if I press that button it changes that value to the opposite?


If you can only use PHP, it won't be pretty, but you can probably just do it with forms:

Code
<form action="script.php" method="post">
<table>
<tr>
<td>student name</td>
<td>week 1 is done</td>
<td>week 2 is done</td>
</tr>
<tr>
<td>garrettthegreat</td>
<td>
<span style="color: green">Done!</span>
<button name="edit" type="submit" value="garrettthegreat,1" />
</td>
<td>
<span style="color: red">Not Done!</span>
<button name="edit" type="submit" value="garrettthegreat,2" />
</td>
</tr>
</table>
</form>


Now, when you go to PHP, you'll get a string with the name and week number, separated by a comma. You can use explode() again to separate them. The trick is to use name="edit" for all submit buttons, Since only one submit button can be pressed at a time, you'll end up with only one element in $_POST named "edit". Of course, commas must be forbidden in user names and week numbers to prevent misinterpreting this data.

This post was edited by garrettthegreat on Dec 19 2016 07:33pm
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll