PDO将记录分成两半

时间:2013-12-07 14:08:08

标签: php mysql zurb-foundation

我一直在努力寻找一种方法,使用准备好的声明将我的记录分成两列,并且想知道是否有任何可以提供帮助

Split Records

<?PHP
$DB_NAME = '';
$DB_HOST = '';
$DB_USER = '';
$DB_PASS = '';

try {
    $dsn = "mysql:host=$DB_HOST;dbname=$DB_NAME";
    $db = new PDO($dsn, $DB_USER, $DB_PASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $query = "SELECT * FROM `snippets` ORDER BY _ID ASC"; // Split in half here
    $prep = $db->prepare($query);
    $prep->execute();   

    echo '  <div class="row">' . "\n";  // Display 1st half here
    echo '      <div class="large-12 columns">' . "\n";
    echo '          <div class="panel">' . "\n";
    while ($row = $prep->fetch()) {
        echo '                  <div class="row">' . "\n";
        echo '                      <div class="large-12 columns">' . "\n";
        echo '                  <b><a href="results.php?id=' . $row['_ID'] . '">' . $row['CODE_NAME'] . '</a></b><br/><br/>' . "\n";
        $timestamp = strtotime($row['CODE_TIMESTAMP']);
        echo '                          Date: ' . date("M d, Y h:m:s A", $timestamp).'<br/><br/>' . "\n";
        echo '                      </div>' . "\n";
        echo '                  </div>' . '<br/>' . "\n";
    }

    $db = null;
    }
catch (PDOException $e) {
    echo '<div class="row">';
    echo    '<div class="panel">';
    echo        'Connection failed: ' . $e->getMessage();
    echo    '</div>';
    echo '</div>';
}
echo '          </div>' . "\n";
echo '      </div>' . "\n";
echo '  </div>' . "\n";


 // Create a 2nd column here to display second half
?>

我想正确计算记录的总数,然后将其分成两半并在一列中显示一半而在另一列中显示另一列我已经想出我似乎无法将记录拆分然后显示

1 个答案:

答案 0 :(得分:0)

我设法通过执行以下操作来实现它

<!DOCTYPE html>
<!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<html>

<?php $title = 'ALL'; $page = 'all';include 'includes/header.php';?>

<body>

<?php include 'includes/nav.php'; ?>

<div class="row">

<div class="small-6 small columns">
<?php
$DB_NAME = '';
$DB_HOST = '';
$DB_USER = '';
$DB_PASS = '';

try {
    $dsn = "mysql:host=$DB_HOST;dbname=$DB_NAME";
    $db = new PDO($dsn, $DB_USER, $DB_PASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);



    $query = "SELECT * FROM `snippets` ORDER BY _ID ASC";
    $prep = $db->prepare($query);
    $prep->execute();
    $count = $prep->rowCount();
    $half = round($count/2);

    $dsn2 = "mysql:host=$DB_HOST;dbname=$DB_NAME";
    $db2 = new PDO($dsn, $DB_USER, $DB_PASS);
    $db2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);



    $query2 = "SELECT * FROM `snippets` ORDER BY _ID ASC Limit " . $half;
    $prep2 = $db2->prepare($query2);
    $prep2->execute();



    echo '  <div class="row">' . "\n";
    echo '      <div class="large-12 columns">' . "\n";
    echo '          <div class="panel">' . "\n";
    while ($row = $prep2->fetch()) {
        echo '                  <div class="row">' . "\n";
        echo '                      <div class="large-12 columns">' . "\n";
        echo '                  <b><a href="results.php?id=' . $row['_ID'] . '">' . $row['CODE_NAME'] . '</a></b><br/><br/>' . "\n";
        $timestamp = strtotime($row['CODE_TIMESTAMP']);
        echo '                          Date: ' . date("M d, Y h:m:s A", $timestamp).'<br/><br/>' . "\n";
        echo '                      </div>' . "\n";
        echo '                  </div>' . '<br/>' . "\n";
    }

    $db = null;
    }


catch (PDOException $e) {
    echo '<div class="row">';
    echo    '<div class="panel">';
    echo        'Connection failed: ' . $e->getMessage();
    echo    '</div>';
    echo '</div>';
}
echo '          </div>' . "\n";
echo '      </div>' . "\n";
echo '  </div>' . "\n";
?>
</div>

<div class="row">

<div class="small-6 small columns">
<?php
$DB_NAME = '';
 $DB_HOST = '';
$DB_USER = '';
 $DB_PASS = '';

try {

    $dsn = "mysql:host=$DB_HOST;dbname=$DB_NAME";
    $db = new PDO($dsn, $DB_USER, $DB_PASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);



    $query = "SELECT * FROM `snippets` ORDER BY _ID ASC";
     $prep = $db->prepare($query);
     $prep->execute();
    $count = $prep->rowCount();
    $half = round($count/2);

    $dsn2 = "mysql:host=$DB_HOST;dbname=$DB_NAME";
     $db2 = new PDO($dsn, $DB_USER, $DB_PASS);
    $db2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);



    $query2 = "SELECT * FROM `snippets` ORDER BY _ID ASC Limit " . $half . ',' . $half;
    $prep2 = $db2->prepare($query2);
    $prep2->execute();

    /*
    $dsn = "mysql:host=$DB_HOST;dbname=$DB_NAME";
    $db = new PDO($dsn, $DB_USER, $DB_PASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $query = "SELECT * FROM `snippets` ORDER BY _ID ASC LIMIT 20,19";
    $prep = $db->prepare($query);
    $prep->execute();
    */
    echo '  <div class="row">' . "\n";
    echo '      <div class="large-12 columns">' . "\n";
    echo '          <div class="panel">' . "\n";
    while ($row = $prep2->fetch()) {
        echo '                  <div class="row">' . "\n";
        echo '                      <div class="large-12 columns">' . "\n";
        echo '                  <b><a href="results.php?id=' . $row['_ID'] . '">' . $row['CODE_NAME'] . '</a></b><br/><br/>' . "\n";
        $timestamp = strtotime($row['CODE_TIMESTAMP']);
        echo '                          Date: ' . date("M d, Y h:m:s A", $timestamp).'<br/><br/>' . "\n";
        echo '                      </div>' . "\n";
        echo '                  </div>' . '<br/>' . "\n";
    }

    $db = null;
    }


catch (PDOException $e) {
    echo '<div class="row">';
    echo    '<div class="panel">';
    echo        'Connection failed: ' . $e->getMessage();
    echo    '</div>';
    echo '</div>';
}
echo '          </div>' . "\n";
echo '      </div>' . "\n";
echo '  </div>' . "\n";
      ?>
</div>
 </div>
 </div>

   <?php include 'includes/footer.php';?>
 </body>