Forked from anonymous/PHP_store_and_search_data_for_start_learning.php
Created
October 12, 2017 05:59
-
-
Save techsharif/4efebeb4939d64f1143d5ca38ce0a988 to your computer and use it in GitHub Desktop.
PHP_store_and_search_data_for_start_learning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// for data add | |
function add_data($id, $name, $phone) | |
{ | |
$data = sprintf('%10d%60s%15s', $id, $name, $phone); | |
$file = fopen('db.txt', 'a'); | |
fwrite($file, $data); | |
fclose($file); | |
} | |
if (isset($_GET['add-data'])) { | |
$id = $_GET['id'] + 0; | |
$name = $_GET['name']; | |
$phone = $_GET['phone']; | |
add_data($id, $name, $phone); | |
} | |
?> | |
<?php | |
// for data show | |
function get_data() | |
{ | |
$file = fopen('db.txt', 'r'); | |
$filesize = filesize('db.txt'); | |
$data = fread($file, $filesize); | |
fclose($file); | |
// echo $data; | |
$ln = strlen($data); | |
$students = []; | |
for ($i = 0; ; $i++) { | |
$start = $i * 85; | |
$extra = 85; | |
if ($start + $extra > $ln) { | |
break; | |
} | |
$student_info = substr($data, $start, $extra); | |
$id = trim(substr($student_info, 0, 10)); | |
$name = trim(substr($student_info, 10, 60)); | |
$phone = trim(substr($student_info, 70, 15)); | |
// echo "<br>id: $id - Name: $name - Phone: $phone"; | |
$student['name'] = $name; | |
$student['id'] = $id+0; | |
$student['phone'] = $phone; | |
$students[] = $student; | |
} | |
return $students; | |
} | |
function show_data(){ | |
$students = get_data(); | |
foreach ($students as $student){ | |
echo $student['id']. '<br>'; | |
} | |
} | |
function show_data_by_id($id){ | |
// $flag = 0; | |
$students = get_data(); | |
foreach ($students as $student){ | |
if ($student['id'] == $id) { | |
echo $student['name'] . '<br>'; | |
return; | |
} | |
} | |
// if ($flag == 0){ | |
echo "No Data Found at roll $id"; | |
// } | |
} | |
if (isset($_GET['search-data'])){ | |
$id = $_GET['search-id']; | |
show_data_by_id($id); | |
}else{ | |
show_data(); | |
} | |
?> | |
<form action="" method="GET"> | |
<input type="number" name="id" placeholder="id"> | |
<input type="text" name="name" placeholder="name"> | |
<input type="text" name="phone" placeholder="phone"> | |
<input type="submit" name="add-data" value="Add"> | |
</form> | |
<br> | |
<br> | |
<br> | |
<form action="" method="GET"> | |
<input type="number" name="search-id" placeholder="id"> | |
<input type="submit" name="search-data" value="search"> | |
</form> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment