d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Simple Php Exercise I Need To Do > Help Me Please
Prev123Next
Add Reply New Topic New Poll
Member
Posts: 16,004
Joined: Jul 2 2009
Gold: Locked
Dec 20 2016 07:04am
Quote
<html>
<link rel="stylesheet" type="text/css" href="style.css">

<?php

echo '<table>';

$openFile = fopen("daten.csv", "r");
$i = 0;
while (($calendarWeek = fgetcsv($openFile, 1000, ";")) !== FALSE) {
if ($i == 0) {
for ($cW = 0; $cW < count($calendarWeek); $cW++) {
echo '<th>' . $calendarWeek[$cW] . '</th>' . "\n";
}
} else {
echo '<tr>' . "\n";
for ($cW = 0; $cW < count($calendarWeek); $cW++) {
if ($calendarWeek[$cW] == 1) {
//echo '<td>' . redGreen() . '</td>' . "\n";
echo '<td><a href="index.php?method=change&uid=1_40&value=0">' . redGreen() . '</a></td>' . "\n";
} else if ($calendarWeek[$cW] == '' || $calendarWeek[$cW] == '0') {
echo '<td>' . redGreen() . '</td>' . "\n";
} else {
echo '<td>' . $calendarWeek[$cW] . '</td>' . "\n";
}
}

echo '</tr>' . "\n";
}
$i++;
}

fclose($openFile);

echo '</table>';

function redGreen()
{
if (isset($_GET['change'])) {
$change = $_GET['change'];

if ($change == "changegreen") {
return "<form action=\"index.php\" method=\"get\">
<input type=\"Submit\" class=\"btnDone\" value=\"changered\" name = \"change\">
</form>";
} else {
return "<form action=\"index.php\" method=\"get\">
<input type=\"Submit\" class=\"btnNotDone\" value=\"changegreen\" name = \"change\">
</form>";
}
} else {
return "<form action=\"index.php\" method=\"get\">
<input type=\"Submit\" class=\"btnNotDone\" value=\"changered\" name = \"change\">
</form>";
}
}

?>
</html>


This is the code I have for the overview, where there is a button. When I press one of the buttons, all the buttons change color from red to green and vice versa. I need however for each button to have an individual input for each week and each user(Azubi).

What would be the best approach?

I am thinking of multidimensional arrays, but I can't figure out how to change the arrays and save them in the csv by the click of a button.

This is another piece of code I use to get the current calendar week.

Quote
<?php
date_default_timezone_set('Europe/Berlin');
$timeStamp = time();
$dateToday = date("d.m.Y", $timeStamp);
$calendarWeek = DateTime::createFromFormat("d.m.Y", $dateToday);
$stringCW = $calendarWeek->format('o-W');
$subStrCW = substr($stringCW, 5);
$subStrCWInt = intval($subStrCW);
var_dump($subStrCWInt);
//$calendarArray[0]='test';
//var_dump ($calendarArray)."\n";
$calendarArray = range($subStrCWInt, 0, [-1]);
var_dump($calendarArray);
$fp = fopen('daten.csv', 'a');
fputcsv($fp, $calendarArray, ";");
fclose($fp);


?>



Sorry for the bad code..
maybe this explains my problem a bit better.
Member
Posts: 16,004
Joined: Jul 2 2009
Gold: Locked
Dec 20 2016 07:04am
Quote (garrettthegreat @ Dec 20 2016 01:29am)
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 is kind of confusing to me, as I dont get how it will change the value in the csv.
I am really appreciating your help though man! It's gotten me far.

This post was edited by bochakaboy on Dec 20 2016 07:05am
Member
Posts: 735
Joined: Jan 23 2007
Gold: 725.94
Dec 20 2016 12:31pm
Quote (bochakaboy @ Dec 20 2016 07:04am)
This is kind of confusing to me, as I dont get how it will change the value in the csv.
I am really appreciating your help though man! It's gotten me far.


Sorry, I was leaving it open ended so you'd be able to work through it yourself. You've got most of the hard work done! And, if this is a homework assignment, I'd be hurting you in the long run if I say too much.

Open the CSV like normal into your array. Edit the array with the changes from the form. You'll just add some code before your first output and after you've loaded the old CSV into the array to detect if a form is submitted (check your $_POST or $_GET). If so, make the edits to the array, then use implode with a comma delimiter, and write the new CSV one line at a time. Once that's done, print out your table like you have been doing.
Member
Posts: 16,004
Joined: Jul 2 2009
Gold: Locked
Dec 21 2016 04:32am
Quote (garrettthegreat @ Dec 20 2016 06:31pm)
Sorry, I was leaving it open ended so you'd be able to work through it yourself. You've got most of the hard work done! And, if this is a homework assignment, I'd be hurting you in the long run if I say too much.

Open the CSV like normal into your array. Edit the array with the changes from the form. You'll just add some code before your first output and after you've loaded the old CSV into the array to detect if a form is submitted (check your $_POST or $_GET). If so, make the edits to the array, then use implode with a comma delimiter, and write the new CSV one line at a time. Once that's done, print out your table like you have been doing.


could you maybe give me a hint how to save a multidimensional array into the csv file? Because that seems really hard to em at the moment. I googled it but theres so many different answers and mostly they use functions or terms that I haven't heard yet.

:)


/e:
or better yet, if I want to change just a single cell of my csv, lets say for user 1 and week 53, how can I do this? I am thinking fputcsv, however how can I choose exactly the line and row that I want to edit?

This post was edited by bochakaboy on Dec 21 2016 04:52am
Member
Posts: 16,004
Joined: Jul 2 2009
Gold: Locked
Dec 21 2016 07:37am
Quote (garrettthegreat @ Dec 20 2016 06:31pm)
Sorry, I was leaving it open ended so you'd be able to work through it yourself. You've got most of the hard work done! And, if this is a homework assignment, I'd be hurting you in the long run if I say too much.

Open the CSV like normal into your array. Edit the array with the changes from the form. You'll just add some code before your first output and after you've loaded the old CSV into the array to detect if a form is submitted (check your $_POST or $_GET). If so, make the edits to the array, then use implode with a comma delimiter, and write the new CSV one line at a time. Once that's done, print out your table like you have been doing.


I am taking a completely new approach, if you can maybe you can have a look:

Quote
<?php

echo '<table>';

$openFile = fopen("daten.csv", "r");

$i = 0;
// i = Zeile
while (($calendarWeek = fgetcsv($openFile, 1000, ";")) !== FALSE) {
if ($i == 0) {
for ($cW = 0; $cW < count($calendarWeek); $cW++) {
echo '<th>' . $calendarWeek[$cW] . '</th>' . "\n";
}
} else {
echo '<tr>' . "\n";
// cW = Spalte
for ($cW = 0; $cW < count($calendarWeek); $cW++) {
//echo '<td>' . redGreen() . '</td>' . "\n";
echo '<td>' . changeState($i, $cW, $calendarWeek[$cW]) . '</td>' . "\n";

}

echo '</tr>' . "\n";
}
$i++;
}

fclose($openFile);

echo '</table>';

?>


then on the same page:

Quote
function changeState($row, $col, $state)
{
"<a href=\"index.php?row=".$row."&col=".$col."&state=".$state."\">";
if ($col == 0) {

}
else{

}


}


the plan is to create a link for each entry, which reflects the row and column (user and week), and based on that knows whether to change the entry for both user and week specifically. SO if it was a 1 it will turn to 0 when clicked, if it was a 0 it will turn to a 1.

What do you think? a co-worker brought me this way.
Member
Posts: 735
Joined: Jan 23 2007
Gold: 725.94
Dec 21 2016 09:41pm
Quote (bochakaboy @ Dec 21 2016 07:37am)
What do you think? a co-worker brought me this way.


How you could do it:

Code
$source = fopen('file.csv', 'r'); // old file handle
$dest = fopen('temp.csv', 'w'); // new file handle
$i = 0; // current row number in the file

while(($row = fgetcsv($source, ...)) { // iterate through each row
if ($i == $_GET['row']) { // (TODO SANITIZE USER INPUT!!) the row to edit is the same as the current row
$row[$_GET['col']] = $_GET['state']; // (TODO SANITIZE USER INPUT!!) edit the row with the new information
}

fputcsv($dest, $row); // write back the row data

// snip
}

// eventually, overwrite 'file.csv' with 'temp.csv'


A word of caution; your code is starting to get complex enough where it's getting hard to read. If you start breaking it up into functions, it could be a lot easier to read:

Code
// snip
$currentRow = 0;
$rowNum = isset($_GET['row']) && is_numeric($_GET['row']) && $_GET['row'] > 0 ? intval($_GET['row']) : -1;

while(($row = fgetcsv($source, ...)) {
if ($currentRow == 0) {
printHeaders($row);
} else {
if ($currentRow == $rowNum) {
updateRow($row);
}

writeRow($row);
printRow($row);
}
}

// snip
Member
Posts: 1
Joined: Jan 14 2007
Gold: 0.00
Dec 25 2016 06:51pm
Is there a reason you don't use json for this? seems like it would be fairly easy to build an array, json_encode it and write to file, json_decode from the file and make changes. Or is this exercise specifically to make you do dumb things in PHP? :)

also composer require league/csv :)
Member
Posts: 16,004
Joined: Jul 2 2009
Gold: Locked
Dec 27 2016 05:36am
Quote (diabme69 @ Dec 26 2016 12:51am)
Is there a reason you don't use json for this? seems like it would be fairly easy to build an array, json_encode it and write to file, json_decode from the file and make changes. Or is this exercise specifically to make you do dumb things in PHP? :)

also composer require league/csv :)


Yes I think the point is for me to do dumb thigns in PHP, but too be honest I am thinking of just using or some shit because this stuff is so complicated. I'm not able to edit a single cell in the csv. I can edit a whole line but not one cell.
Member
Posts: 36,389
Joined: Jul 18 2008
Gold: 3,192.00
Dec 27 2016 08:58am
Here's how I would do it:

Read from csv into multi dimensional array
Modify array values
Write to new csv
Replace old csv with new csv
Member
Posts: 16,004
Joined: Jul 2 2009
Gold: Locked
Dec 27 2016 09:31am
Quote (Mastersam93 @ Dec 27 2016 02:58pm)
Here's how I would do it:

Read from csv into multi dimensional array
Modify array values
Write to new csv
Replace old csv with new csv


I tried doing that but its hard to write a mutlidimensional array into a csv. I tried looking for hints online but everything was vague.

What would you suggest to use? fputcsv or some other command?
Go Back To Programming & Development Topic List
Prev123Next
Add Reply New Topic New Poll