<?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)
{
}
    ?>

<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'];
        $_SESSION['quotes'][$id]['person'] = $row['person'];
        $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"><pre>'."\n";
    echo 'Quote(\''.$_SESSION['quotes'][$i]['person']."', \n";
    $count = $_SESSION['quotes'][$i]['count'];
    for ($j = 0; $j < $count; $j++)
    {
        $speaker = $_SESSION['quotes'][$i][$j]['speaker'];
        $message = $_SESSION['quotes'][$i][$j]['message'];
        echo "Line(";
        if ($speaker != "0")
        {
            echo "'".$speaker."', ";
        }
        else
        {
            echo "'', ";
        }
        echo "'".$message."')";
        echo ',';
    }
    echo ')'."</pre></div>\n";
}
            ?>
        </p>
<?php print_selector($settings); ?>
    </body>
</html>
