Sensation Energy

Tutorials


Function 7

OOP MVC PHP MySql




Id Name Email Year Actiona

MVC OOP PHP MySql

Object Oriented PHP with Model View Controller . It also connects to the database.


Connect (confing/confing.php)

Connect allows us to connect to the base.


<?php
class Connect {
public function veza() {
global $con;
$server = "localhost";
$korisnik = "root";
$lozinka = "root";
$imebaze = "7_2021";
$konekcija = new mysqli($server, $korisnik, $lozinka, $imebaze);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
return $con ;
}
}
$link = new Connect();
$link->veza();
?>

Database a database containing a table with fields that we will use in our CRUD. it is necessary to create a database named CRUD 2021 in PHPMyAdmin. When you do that, a new page will open and in the upper tab you should click on another SQL field where a page will open in which you will insert the previously copied lower code. And that’s how you made the base.


CREATE TABLE `users` (
`user_id` int(20) NOT NULL,
`name` varchar(100) NOT NULL,
`year` varchar(100) NOT NULL,
`email` int(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `users`
ADD PRIMARY KEY (`user_id`);
ALTER TABLE `users`
MODIFY `user_id` int(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=102;


MODEL

Select (model/index.php)

View (model/index.php)

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

There is also a single value view from user_id - "function one($user_id)".


<?php
require_once '../cofing/confing.php';
class Index{
function Single() {
global $con;
?>
<th>Id</th>
<th>Name</th>
<th>Last name</a></th>
<th>Email</th>
<th>Actiona</th>
</tr>
</thead>
<?php
$sql = "SELECT * from users";
$result = $con->query($sql);
while ($res = $result->fetch_assoc()) {
$array[] = $res;
}
return $array;
echo "no data";
}
function one($user_id) {
global $con;
$sql = "SELECT * from users where user_id='$user_id'";
$result = $konekcija->query($sql);
while ($res = $result->fetch_assoc()) {
$array[] = $res;
}
return $array;
echo "no data";
}
}
?>

Insert (model/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.


<?php
require_once '../cofing/confing.php';
class Mycrud {
private  $con;
public function add() {
global $con;
if(isset($_POST['Submit'])) {
$name =  $_POST['name'];
$year= $_POST['year'];
$email =  $_POST['email'];
$sql = "INSERT INTO user(name,year,email) VALUES('$name','$year','$email')";
if($con->query($sql) === true){
echo "<font color='green'>ADD is ok.<br>";
echo "<script>window.history.go(-2);</script>";
} else{
echo "<font color='red'>Eror<br>";
}
}
}
}
$get = new Mycrud();
$get->add();
?>
?>

Saerch (model/saerch.php)

Search allows us to search for words we have previously entered via the form.


<?php
require_once '../cofing/confing.php';
$v = "SELECT * from users";
$sql = $kon->query($v);
$search = $_GET['search'];
$min_length = 4;
if(strlen($search) >= $min_length){
?>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
$va = "SELECT * FROM users
WHERE (`user_id` LIKE '%".$search."%') OR (`name` LIKE '%".$search."%') OR (`yeare` LIKE '%".$search."%') OR (`email` LIKE '%".$search."%')";
$t = $con->query($va);
if(mysqli_num_rows($search) > 0){ // if one or more rows are returned do following
while ($value = $search->fetch_array()) {
?>
<tr>
<td><?php echo $value['user_id']; ?></td>
<td><?php echo $value['name']; ?></td>
<td><?php echo $value['year']; ?></td>
<td><?php echo $value['email']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
else{ // if there is no matching rows do following
echo "No results";
}
}
else{
echo "min_length ".$min_length;
}
?>


Edit (model/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
require_once '../cofing/confing.php';
class Mycrud {
function edit($user_id) {
global $con;
$sql = "SELECT * from users where user_id='$user_id'";
$result = $con->query($sql);
while ($res = $result->fetch_assoc()) {
$array[] = $res;
}
return $array;
}
public function edit(){
global $konekcija;
if(isset($_POST['submit'])) {
$name =  $_POST['name'];
$year = $_POST['year'];
$email =  $_POST['email'];
$user_id =  $_POST['user_id'];
$sql = "UPDATE users set name='$name',year='$year', email='$email' where user_id='$user_id'";
if($con->query($sql) === true){
echo '<script>window.history.go(-2);</script>';
echo "<font color='green'>USPESNO STE Azurirali. <br>";
} else{
echo "<font color='red'>Error<br> " ;
}
}
}
}
$lista = new Mycrud();
$lista->edit();
?>

Delete (model/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
require_once '../cofing/confing.php';
class Sensation{
function del() {
global $con;
if ( isset( $_GET['user_id'] ) ) {
$user_id= $_GET['user_id'];
$delete = "DELETE from users where user_id='$user_id'";
if($con->query($delete) === true){
echo "<script>window.history.back();</script>";
}
}
}
}
$user_id = $_GET['user_id'];
$list = new Sensation();
$list->del($user_id);
?>


CONTROLLER

Select (controller/index.php)

View (controllerindex.php)


<?php
require_once '../model/selekt.php';
class listarController{
private $list;
protected $user_id
public function cont(){
$list = new Index();
$list->Single();
$res = $list->Single();
foreach ($res as $value){
?>
<tbody>
<tr>
<td data-title="id"><?php echo $value['user_id']; ?></td>
<td><?php echo $value['name']; ?></td>
<td><?php echo $value['yeare']; ?></td>
<td><?php echo $value['email']; ?></td>
<td><a href="view.php?user_id=<?php echo $value['user_id']; ?>">View</a>
<a href="edit.php?user_id=<?php echo $value['user_id']; ?>">Edit</a>
<a href="../model/delete.php?user_id=<?php echo $value['user_id']; ?>">Delete></a></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
public function one(){
$user_id = $_GET['user_id'];
$list = new Index();
$list->one($user_id);
$res = $list->one($user_id);
foreach ($res as $value){
?>
<table>
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Last name</a></th>
<th scope="col">Email</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $value['user_id']; ?></td>
<td><?php echo $value['name']; ?></td>
<td><?php echo $value['year']; ?></td>
<td><?php echo $value['email']; ?></td>
</tr>
</tbody>
</table>

Insert (add.php)


<?php
require "../model/add.php" ;
if(isset($_POST['Submit'])) {
$name =  $_POST['name'];
$year = $_POST['year'];
$email =  $_POST['email'];
$conn= cuvaj($name,$year,$email);
if($con){
echo "<font color='green'>Ok<br>";
}else{
echo "<font color='red'>Error<br>";
}
}else{
echo "<font color='red'>Error<br>";
}
?>

Edit (controller/edit.php)


<?php
require "../model/uredi.php" ;
class Update{
private $list;
protected $user_id;
public function edit(){
$user_id = $_GET['user_id'];
$list = new Mycrud();
$list->edit($user_id);
$res = $list->edit($user_id);
foreach ($res as $value){
$user_id = $value['user_id'];
$name = $value['name'];
$year = $value['year'];
$email = $value['email'];
?>
<form  action="../model/uredi.php?id=<?php echo $value['id']; ?>" method="post" enctype="multipart/form-data"><!--enctype multipart encodes the form differently if we have to upload files etc-->
<div class="form-group">
<label>Name</label>
<input type="text" class="form-input" name="name" value="<?php echo $name;?>">
</div>
</div>
<div class="form-group">
<label>Last name</label>
<input type="text" class="form-input"  name="year" value="<?php echo $year;?>">
</div>
<div class="form-group">
<label>Email</label>
<input type="text" class="form-input"  name="email" value="<?php echo $email;?>">
</div>
<div class="form-group">
<input type="hidden" name="user_id" value=<?php echo $_GET['user_id'];?>>
<input type="submit" class="btn-1" value="submit" name="submit">
</div>
</form>


VIEW

Select (view/index.php)


<?php
require_once '../controller/index.php';
$lista1 = new listarController();
$lista1->cont();
?>

Insert (view/add.php)


<form action="../controller/dodaj.php" method="post" name="form1">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" class="form-input"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="year" class="form-input"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" class="form-input"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Add" class="btn-6">
</tr>
</table>
</form>

View (view/view.php)


<?php
require_once '../controller/index.php';
$user_id = $_GET['user_id'];
$lista1 = new listarController();
$lista1->one($user_id);
?>


Edit (edit.php)


<?php
require_once '../controller/uredi.php';
$lista1 = new Update();
$lista1->edit();
?>

Search Form (view/search.php)

Search form is clasic form which is used to enter the keywords that we will search.


<form action="../model/trazi.php" method="GET">
<input type="text" name="search" class="form-input"><br><br>
<input type="submit" value="Search" class="btn-7">
</form>