<?php ini_set ("session.use_cookies", "true"); session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
      "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
      
<?php

include "mysql.inc.php";
$settings = array();

// set default settings

$settings['lang'] = substr ($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$settings['start'] = 0;
$settings['count'] = 20;
$settings['requery'] = false;

$settings = array_merge ($settings, $_GET);

function print_selector($settings)
{
    ?>
        <p class="selector">
            <?php
if ($settings['lang'] == "de")
{
    echo "Seite: ";
}
else
{
    echo "Page: ";
}

$overall = $_SESSION['overall'];
if ($overall > $settings['count'])
{
    $sitecount = (int) ($overall / $settings['count']);
   
    if ($settings['count'] * $sitecount < $overall)
    {
        $sitecount++;
    }
    for ($i = 0; $i < $sitecount; $i++)
    {
        if ($i*$settings['count'] == $settings['start'])
        {
            echo "<b>";
            echo $i+1;
            echo "</b> ";
        }
        else
        {
            echo "<a href=\"?start=".$i*$settings['count']."&count=".$settings['count']."\">";
            echo $i+1;
            echo "</a> ";
        }
    }
}
else if ($settings['start'] > 0)
{
    echo '<a href="?start=0&count='.$settings['count'].'">';
    echo "1</a>";
    ?>
            <b>2</b>
    <?php
}
else
{
    ?>
            <b>1</b>
    <?php
}
            ?>
            <br />
            <?php if ($settings['lang'] == "de") { echo "Zitate pro Seite: "; } else { echo "Items per page: "; } ?>
            <?php echo '<a href="?start='.$settings['start'].'&count=5">' ?>5</a>
            <?php echo '<a href="?start='.$settings['start'].'&count=10">' ?>10</a>
            <?php echo '<a href="?start='.$settings['start'].'&count=15">' ?>15</a>
            <?php echo '<a href="?start='.$settings['start'].'&count=20">' ?>20</a>
            <?php echo '<a href="?start='.$settings['start'].'&count=25">' ?>25</a>
            <?php echo '<a href="?start='.$settings['start'].'&count=50">' ?>50</a>
        </p>
<?php
}
    ?>

<html>
    <head>
        <title>Zitate.</title>
        <link rel="stylesheet" type="text/css" media="all" href="style.css">
        <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
    </head>
    <body>
        <?php
if ($settings['requery'] || $settings['start'] == 0)
{
    $_SESSION['queried'] = false;
}

if ($settings['start'] > 0)
{
    $settings['requery'] = false;
}
            
if (!isset ($_SESSION['queried']) || !$_SESSION['queried'])
{
    $_SESSION['quotes'] = array();
    $db_link = @mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS);
        
    if (!$db_link)
    {
        die ("Database error. Sorry.\n");
    }
    
    mysql_select_db(MYSQL_DATABASE);
    
    if (isset($settings['person']))
    {
        
        $query_string = "SELECT * FROM quotes WHERE person = ".'\''.$settings['person'].'\''.";";
    }
    else
    {
        $query_string = "SELECT * FROM quotes;";
    }
    
    $result = mysql_query($query_string);
    
    if (!$result)
    {
        echo ("Database error (2). Sorry.\n");
        echo "MySQL error:".mysql_error();
        die();
    }
    
    $id = 0;
    $overall = 0;
    
    while ($row = mysql_fetch_assoc ($result))
    {
        $id = $row['id'];
        $no = $row['no'];
        $_SESSION['quotes'][$id][$no]['speaker'] = $row['speaker'];
        $_SESSION['quotes'][$id][$no]['message'] = $row['message'];
        $_SESSION['quotes'][$id]['count'] = $row['count'];
        $overall = $id + 1;
    }
    
    $_SESSION['overall'] = $overall;
    $_SESSION['queried'] = true;
}
        ?>
<?php print_selector($settings); ?>
        <p>
            <?php
for ($i = $settings['start']; $i < $settings['start'] + $settings['count']; $i++)
{
    if (!isset ($_SESSION['quotes'][$i]))
    {
        continue;
    }
    
    echo '<div class="quote">'."\n";
    $count = $_SESSION['quotes'][$i]['count'];
    for ($j = 0; $j < $count; $j++)
    {
        $speaker = $_SESSION['quotes'][$i][$j]['speaker'];
        $message = $_SESSION['quotes'][$i][$j]['message'];
        if ($speaker != "0")
        {
            echo '<div class="speaker">';
            echo $speaker.":";
        }
        else
        {
            echo '<div class="emptyspeaker">';
        }
        echo '</div>'."\n";
        echo '<div class="message">';
        echo $message;
        echo '</div>'."\n";
    }
    echo '</div>'."\n";
}
            ?>
        </p>
<?php print_selector($settings); ?>
    </body>
</html>
