<?php 
//You're gonna need to query
include load_header('sqlHeader.php');
//2. You just submitted a post, this file will "recieve" it
include root('/bulletin/recieveMessage.php');

//3 .Always display bulletin and submit option if logged in 
$result = mysqli_query($Dbcon,"SELECT * FROM messages");

//Find out what page we are on, this will be used to display messages later 
$page = 1;
if(isset($_GET['page'])){
	$page = $_GET['page'];
}

//I don't know yet how to find how many matches there are for fetch_array
//So Ill loop through it once and count it
$total_messages=0;
while($row = mysqli_fetch_array($result)){
	$total_messages++;
}

//Now Ill see how many pages we need.  Dividing by 10 and flooring will see how many times we
//have a full 10 messages, and we will set aside that many pages
$total_page_links = floor($total_messages/10.0);
//then, if there are any leftover messages, found out with %, we will add another page for them
if($total_messages%10){
	$total_page_links++;
}
//sets the data pointer on mysqli_fetch_array back to 0, ie back to the first match
mysqli_data_seek($result,0);

//LOOPS and displays all messages, taking into consideration your page
$current_message = 0;
while($row = mysqli_fetch_array($result)){
	$current_message++;
	//Every page displays 10 messages. If we are between last pages last, and 10 from this page,
	//then display
	if($current_message > ($page-1)*10  && $current_message <= ($page)*10){ 
		echo '<b>'. $row['author_name'] . ': </b>'. $row['message'];
		echo '<br>';
	}
}
//Displays a link to view other pages 
$current_page = 1;
while($total_page_links){
	echo '<b><a href="index.php?page='.$current_page.'">' . $current_page . '</a>  </b>';
	$total_page_links --;
	$current_page++;
}
//form to submit message
echo '
	<form action="index.php" method="post">
	Message:<input type="text" name="message">
	<input type=submit>
	</form>
';
?>