-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrud_function.php
More file actions
93 lines (80 loc) · 3.14 KB
/
crud_function.php
File metadata and controls
93 lines (80 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
//define bucket file
define(bucketFile, 'gs://example-khang-bucket-1/project.csv');
function readData() {
if (($handle = fopen(bucketFile, "r")) !== FALSE) {
fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 9999, ",")) !== FALSE) {
{
echo "<tr>",
"<th scope='row'>",
"<form action='/' method='POST'>"
, "<button type='submit' value='" . $data[0] . "' name='deleteProject' class='btn btn-danger btn-block'>Delete</button>"
, "</form>"
, "<form onsubmit=\"updateForm(event, ".$data[0].")\">"
, "<button type='submit' value='" . $data[0] . "' name='viewOne' onClick='Window.href='#formtitle'' class='btn btn-primary btn-block'>Update</button>"
, "</form>"
, "</th>";
foreach ($data as $field) {
echo "<td class ='text-justify'>" . $field . "</td>";
}
echo "</tr>";
}
}
fclose($handle);
}
}
function createData() {
$handle = file_get_contents(bucketFile);
$newid=0;
if (empty($handle)) {
$id = 1;
} else {
$rows = str_getcsv($handle, "\n");
$id = ((int)str_getcsv(end($rows), ",")[0]);
$newid = $id+ 1;
}
$str = sprintf('%d,"%s","%s","%s","%s","%s","%s","%s"' . "\n", $newid,$_POST['form1'], $_POST['form2'], $_POST['form3'], $_POST['form4'], $_POST['form5'], $_POST['form6'], $_POST['form7']);
$handle.=$str;
$file = fopen(bucketFile, 'w');
fwrite($file, $handle);
fclose($file);
}
function deleteData(){
$id= $_POST['deleteProject'];
$dataArray = file(bucketFile, FILE_IGNORE_NEW_LINES);
$row=0;
foreach($dataArray as $key=>$project) {
$projectObject = explode(",", $project);
if ($projectObject[0] == $id) {
if (count($projectObject)<24){
unset($dataArray[$key+1]);
}
unset($dataArray[$key]);
$csvFileBody = implode("\n", $dataArray);
$csvFileBody .="\n";
$csvFile = fopen(bucketFile, "w");
fwrite($csvFile , $csvFileBody);
fclose($csvFile );
break;
}
$row++;
}
}
function editData()
{
$id = $_POST['editProject'];
$replaceString = sprintf('%s,"%s","%s","%s","%s","%s","%s","%s"',$_POST['id'], $_POST['form1'], $_POST['form2'], $_POST['form3'], $_POST['form4'], $_POST['form5'], $_POST['form6'], $_POST['form7']);
$arrayObjects=file(bucketFile, FILE_IGNORE_NEW_LINES);
foreach($arrayObjects as $key=>$project) {
$projectObject = explode(",", $project);
if ($projectObject[0] == $id) {
$arrayObjects[$key] = $replaceString;
$csvFileBody = implode("\n", $arrayObjects);
$csvFile = fopen(bucketFile, "w");
fwrite($csvFile , $csvFileBody);
fclose($csvFile );
}
}
}
?>