Location: PHPKode > scripts > PHP Address book > php-address-book_v_1.02_eng/overview.php
<?php 
include_once 'includes/header.inc.php';

if(!$session->getRole() == 1 or !$session->getRole() == 2){
    header("location: login.php");
}

//see if there is a beginletter
if(isset($_GET['q'])){
    $q = $_GET['q'];
    $q = $q . "%";
} else {
    $q = "%";
}

//count total records
$query_count = $mysqli->prepare("SELECT 
                                    *
                                FROM 
                                    `cal_contacts` 
                                WHERE 
                                    `cal_contacts`.`firstname` 
                                LIKE 
                                    ?
                                ");
$query_count->bind_param(i, $q);
$query_count->execute();
$total = $query_count->affected_rows;
$query_count->store_result();

?>
<legend>Contacts overview</legend>
<?php
if($total == 0){
    ?>
    <div class="alert alert-info">
        There are no contacts here yet
    </div>
    <?php
} else {
    ?>
    <table class="table table-striped">
        <tr>
            <th>Firstname</th>
            <th>Lastname</th>
            <th>E-mail</th>
            <th>Birthday</th>
            <th>Age</th>
        </tr>
    <?php
    //check if there is a start
    if(isset($_GET['start'])){
        $start = $_GET['start'];
    } else {
        $start = 0;
    }

    $limit = 15;


    $query = $mysqli->prepare("SELECT 
                                    `cal_contacts`.`id`, 
                                    `cal_contacts`.`firstname`, 
                                    `cal_contacts`.`lastname`, 
                                    `cal_contacts`.`email`, 
                                    `cal_contacts`.`phone_home`, 
                                    `cal_contacts`.`phone_cell`, 
                                    `cal_contacts`.`address`, 
                                    `cal_contacts`.`zipcode`, 
                                    `cal_contacts`.`city`, 
                                    `cal_contacts`.`birthday`,
                                    EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(),`birthday`))))+0 AS `age`, 
                                    DATE_FORMAT(`cal_contacts`.`birthday`, '%d') AS `date`, 
                                    DATE_FORMAT(`cal_contacts`.`birthday`, '%m') AS `month`, 
                                    DATE_FORMAT(`cal_contacts`.`birthday`, '%Y') AS `year` 
                               FROM 
                                    `cal_contacts` 
                               WHERE 
                                    `cal_contacts`.`firstname` 
                               LIKE 
                                    ? 
                               ORDER BY 
                                    `firstname` 
                               LIMIT 
                                    ?, ?
                               ");
    $query->bind_param(sii, $q, $start, $limit);
    $query->bind_result($q_id, $q_firstname, $q_lastname, $q_email, $q_phone_home, $q_phone_cell, $q_address, $q_zipcode, $q_city, $q_birthday, $q_age, $q_date, $q_month, $q_year);
    $query->execute();
    
    while($query->fetch()){
        ?>
        <tr>
            <td><a href="<?php echo $url; ?>details.php?id=<?php echo $q_id; ?>"><?php echo $q_firstname; ?></a></td>
            <td><?php echo $q_lastname; ?></td>
            <td><?php if($q_email != ""){ echo $q_email; } else { echo "Unknown"; } ?></td>
            <td><?php if($q_date != "00"){ echo $q_date . " - " . $q_month; } else { echo "Unknown"; } ?></td>
            <?php if($q_year == "0000"){
                ?><td>Unknown</td><?php
            } else {
                ?><td><?php echo $q_age; ?></td><?php
            }
            ?>
        </tr>
        <?php
    }
    $query->store_result();
    ?>
    </table>
<div class="pagination">
  <ul>
      
    <?php if($start != 0){ ?>
        <li><a href="?start=<?php echo $start-$limit; ?>">Vorige</a></li>
    <?php }
    
        $query = $mysqli->prepare("SELECT SUBSTR(firstname,1,1) as `shorten` FROM `cal_contacts` GROUP BY `shorten`");
        $query->bind_result($shorten);
        $query->execute();
        
        while($query->fetch()){
            ?><li><a href="?start=<?php echo $start; ?>&q=<?php echo $shorten; ?>"><?php echo $shorten; ?></a></li><?php
        }
        $query->store_result();
        
    if(($start + $limit) <= $total ){ ?>
        <li><a href="?start=<?php echo $start+$limit; ?>">Volgende</a></li>
    <?php } ?>
  </ul>
</div>
<?php 
}
include_once 'includes/footer.inc.php'; ?>
Return current item: PHP Address book