Sensation Energy

Tutorials


Content


Advertising


Function 7

CSV PHP



Id Name Email Year Actiona

CSV PHP Documentation

Here I will explain how PHP is used with CSV files Function fgetcsv () parses a line from file for CSV fields.


Select (index.php)

Select allows us to list all the data in the table. Here, all columns from the table are selected unconditionally and printed


<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</a></th>
<th>Year</th>
<th>Actiona</th>
</tr>
</thead>
<tbody>
<?php
$lines = file('test.csv',FILE_IGNORE_NEW_LINES);
$i = 0;
foreach($lines as $line) {
$parts = explode(',',$line);
$id = $parts[0];
$name = $parts[1];
$email = $parts[2];
$year = $parts[3];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $year; ?></td>
<td><a href="pogledaj.php?i=<?php echo $i; ?>">View</a>
<a href="uredi.php?i=<?php echo $i; ?>">Edit</a>
<a href="obrisi.php?i=<?php echo $i; ?>" Delete</a></td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>

View (view.php)

The view allows us to see only the individual values from the table via id. From select.php, click View, and then go to the View.php page. It is important to emphasize that this ID is important if the value of id 1 shows all data related to 1


<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</a></th>
<th>Year</th>
</tr>
</thead>
<tbody>
<?php
$lines = file('test.csv',FILE_IGNORE_NEW_LINES);
$i = explode(',',$lines[$_GET['i']]);
?>
<tr>
<td><?php echo $i[0]; ?></td>
<td><?php echo $i[1]; ?></td>
<td><?php echo $i[2]; ?></td>
<td><?php echo $i[3]; ?></td>
</tr>
</tbody>
</table>

Insert (add.php)

Insert allows us to insert data into the database. It is important that the name = "add" long and ame = "name" input from the form match if (isset ($ _ POST ['add'])) {and $ name = $ _POST ['name'] ;, insert jus those values we set.


<form action="" method="post">
<div class="form-group">
<label">Id:</label>
<input type="text" name="id" class="form-input" />
<div class="form-group">
<label>Name:</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="form-group">
<label>Email:</label>
<input type="text" name="email" class="form-input">
</div>
<div class="form-group">
<label for="datum">Year</label>
<input type="number" name="year" class="form-input">
</div>
<div class="btn-group">
<button type="submit" name="add" class="btn-3" >POST</button>
</div>
</form>
<?php
if( $_POST['id'] != '' &&
$_POST['name'] != '' &&
$_POST['email'] != '' &&
$_POST['year'] != '') {
$f = fopen('test.csv','a');
fwrite($f,"{$_POST['id']},{$_POST['ime']},{$_POST['email']},{$_POST['godina']}\n");
fclose($f);
$_SESSION['message'] = array(
'text' => 'Added.',
'type' => 'success'
);
header('Location:index.php');
} else {
$_SESSION['POST'] = $_POST;
$_SESSION['message'] = array(
'text' => 'Error',
'type' => 'error'
);
header('Location:ubaci.php');
}
?>


Edit (edit.php)

Edit lets we edit data from the table using ID. Also from selekt.php we chose edit.php and in the value of the ID it shows all the data related to that ID.


<?php
$lines = file('test.csv', FILE_IGNORE_NEW_LINES);
$lines[$_POST['i']] = "{$_POST['id']},{$_POST['ime']},{$_POST['email']},{$_POST['godina']}";
$data_string = implode("\n",$lines);
$f = fopen('test.csv','w');
fwrite($f,$data_string);
fclose($f);
$_SESSION['message'] = array(
'text' => 'Edited.',
'type' => 'info'
);
header('Location:index.php');
$lines = file('test.csv',FILE_IGNORE_NEW_LINES);
$band = explode(',',$lines[$_GET['i']]);
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="i" value="<?php echo $_GET['i'] ?>" />
<div class="form-group">
<label for="thread_title"><h2>Id</h2></label>
<input type="text" name="id" class="form-input" value="<?php echo $band[0]; ?>">
</div>
<label for="thread_title"><h2>Name</h2></label>
<input type="text" name="name" class="form-input" value="<?php echo $band[1]; ?>">
</div>
<div class="form-group">
<label for="thread_title1"><h2>Email</h2></label>
<input type="text" name="email" class="form-input" value="<?php echo $band[2]; ?>">
</div>
<div class="form-group">
<label for="godina"><h2>Year</h2></label>
<input type="text" class="form-input" name="year" value="<?php echo $band[3]; ?>">
</div>
<div class="btn-group">
<div align="center">
<input type="submit" class="btn-1" name="edit" value="Edit">
</div>
</div>
</form>

Delete (delete.php)

Delete allows us to delete data from the table. Of course via id. From select.php we delete and click delete and all values related to the ID we selected will be deleted.


<?php
$lines = file('test.csv', FILE_IGNORE_NEW_LINES);
unset($lines[$_GET['i']]);
$data_string = implode("\n",$lines);
$f = fopen('test.csv','w');
fwrite($f,$data_string);
fclose($f);
$_SESSION['message'] =  array(
'text' => 'Deleted.',
'type' => 'error'
);
header('Location:index.php');
?>

Database (text.csv)


70,test,asdfasd@mail.com,44
51,zdravo,asdfasd@mail.com,55