<?php
error_reporting(0);
$dbh = new PDO("mysql:host=localhost;dbname=test", "root", "");
/*
-- phpMyAdmin SQL Dump
-- version 4.8.3
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Apr 12, 2019 at 04:09 AM
-- Server version: 10.1.36-MariaDB
-- PHP Version: 7.2.10

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";




--
-- Database: `test`
--

-- --------------------------------------------------------

--
-- Table structure for table `contacts`
--

CREATE TABLE `contacts` (
  `id` int(5) NOT NULL,
  `fullname` varchar(255) NOT NULL,
  `phone` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `comments` text NOT NULL,
  `lastmodified` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `contacts`
--

INSERT INTO `contacts` (`id`, `fullname`, `phone`, `email`, `comments`, `lastmodified`) VALUES
(1, 'Kris', '5551212', 'kdsecor@gmail.com', 'Test Comments', '2019-04-11 00:00:00'),
(2, 'amy lee', '989999080', '9809@wew.cxom', 'qwdhuihwiuw', '2019-04-09 00:00:00'),
(3, 'Madeline', 'qwdqdqw', 'qwdwqdd', 'qwdqwdqdq', '2019-04-17 00:00:00'),
(4, 'amy lee', '989999080', '9809@wew.cxom', 'qwdhuihwiuw', '2019-04-09 00:00:00'),
(5, 'Madeline', 'qwdqdqw', 'qwdwqdd', 'qwdqwdqdq', '2019-04-17 00:00:00'),
(6, 'Kris', '5551212', 'kdsecor@gmail.com', 'Test Comments', '2019-04-11 00:00:00'),
(7, 'amy lee', '989999080', '9809@wew.cxom', 'qwdhuihwiuw', '2019-04-09 00:00:00'),
(8, 'Madeline', 'qwdqdqw', 'qwdwqdd', 'qwdqwdqdq', '2019-04-17 00:00:00'),
(9, 'amy lee', '989999080', '9809@wew.cxom', 'qwdhuihwiuw', '2019-04-09 00:00:00'),
(10, 'Madeline', 'qwdqdqw', 'qwdwqdd', 'qwdqwdqdq', '2019-04-17 00:00:00');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `contacts`
--
ALTER TABLE `contacts`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `contacts`
--
ALTER TABLE `contacts`
  MODIFY `id` int(5) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;
COMMIT;

*/
?>
<script>
function changenum(){
    var num = document.getElementById("numperpage").value;
    
    window.location = 'contactcms.php?numperpage=' + num;
    
    //document.getElementById("controlnum").submit();
}

</script>


<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$fullname = $_POST["fullname"];
$phone = $_POST['phone'];
$email = $_POST['email'];
if (isset($_POST['comments'])){
$comments  = addslashes(nl2br($_POST['comments']));
}
}
$function = 'Enter';
//update
if ($_POST['submit'] == 'Update'){
    $newupdateid = $_POST['updateid'];
     $updatesql = $dbh->prepare("update contacts set fullname = '$fullname',phone='$phone',email='$email',comments='$comments',lastmodified=now() where id = $newupdateid");
    $updatesql->execute();
     echo $fullname.' updated! '; 
    //print_r($updatesql->errorInfo());
} 




//insert
if ($_POST['submit'] == 'Enter'){
    
    $inssql = $dbh->prepare("insert into contacts (fullname,phone,email,comments,lastmodified) values ('$fullname','$phone','$email','$comments',now())");
    $inssql->execute();
    echo 'Record added!';
} 

//delete
if ($_GET['deleteid']){
    $delid = $_GET['deleteid'];
    $delsql = $dbh->prepare("delete from contacts where id = '$delid'");
    $delsql->execute();
    echo $_GET['fullname']." Deleted";
}
?>
control number per page
<form id="controlnum" method="get" action="contactcms.php"> 
<select id="numperpage" onchange="changenum();">
    <option value="">Choose Below</option>

<option value="2" >2</option>
<option value="5" >5</option>
<option value="10">10</option>
</select>
</form>    


<?php

//pagination needs to go here
if ($_GET['numperpage'] != ''){
$numperpage = $_GET['numperpage'];
}
else {
    $numperpage = 5;
}

//this is how many records per page you want
$start = $numperpage * $_GET['start']; //we need to use the url window to see what link the user clicked on. That will populate our limit tag.

//in order to find the number of links in our pagination, we need to find the total number of records and divide by the numperpage

$getrecords= $dbh->prepare("select count(id) from contacts");//count is super handy for getting the number of records
$getrecords->execute();//this enacts the query
$getrow = $getrecords->fetch();//this grabs the data
$num = $getrow[0];//this extracts the number
$links = $num/$numperpage;//this tells me the number of links I need
echo "numperpage is ".$numperpage;
$prev = $_GET['start'] - 1;
$next = $_GET['start'] + 1;

$y=1;

//show prev (only if there is a prev ($start > 0))
  if ($prev > -1 ){
    echo '<a href="contactcms.php?start='.$prev.'">Prev</a> '."\n";
  }
//output links:
for ($i=0;$i<$links;$i++){
    if ($_GET['start'] == $i) $style= ' style="color:#ff0000;text-decoration:none" ';
    else { $style = '';}
    echo '<a href="contactcms.php?start='.$i.'" '.$style.'>'.$y.'</a> '."\n";
    $y++;
    //url will be ?start=$i
}
//show next (only if there is a next ($start < $num - 1))
 if ($next < $links){
    echo '<a href="contactcms.php?start='.$next.'">Next</a> '."\n";
  }

echo "select * from contacts limit $start,$numperpage <br>";
//show contacts
$sql = $dbh->prepare("select * from contacts limit $start,$numperpage");
$sql->execute();
while ($row = $sql->fetch()){
  $id = $row['id'];    
  echo $id .':'.$row['fullname'].' <a href="contactscms.php?fullname='.$row['fullname'].'&deleteid='.$id.'" onclick= "return confirm(\'are you sure you want to delete '.$row['fullname'].'? \')">Delete</a> <a href="contactscms.php?updateid='.$id.'">Update</a><br>';   
}

//populate value
if ($_GET['updateid']){
   $upid = $_GET['updateid'];
   $function = 'Update';    
   $upsql = $dbh->prepare("select fullname,phone,email,comments from contacts where id = '$upid'");
   $upsql->execute();
   $uprow = $upsql->fetch();
   $upname = $uprow['fullname'];
   $upemail = $uprow['email'];
   $upphone = $uprow['phone'];   
   $upcomments = $uprow['comments'];            

echo '<a href="contactscms.php">&lt;&lt;Return to add mode</a>';    
}


?>


<form action = "<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Name: <input type="text" name="fullname" value="<?php 
if (isset($upname)){
    echo $upname;
}?>" required ><br>
Phone: <input type="tel" name="phone" value="<?php echo $upphone;?>" required ><br>
Email: <input type="email" name="email" value="<?php echo $upemail;?>" required ><br>
Comments: <textarea name="comments"><?php echo $upcomments;?></textarea> <br>  
<input type="hidden" name="updateid" value ='<?php echo $upid;?>'>   
<input type="submit" name="submit" value = "<?php echo $function;?>"> 
</form>
    