垂直显示项目

时间:2012-12-16 16:46:20

标签: php sorting

  

可能重复:
  Sorting with a modulus

这是我的代码:

<?php
while($fetch_cat = mysql_fetch_array($rescat))
    {
        $per_cnt++;
?>
        <li style="margin-left:10px;">
            <a href="sub_cat.php?cat_id=<?php echo $fetch_cat['cat_id'];?>" 
             style="color:#431603;text-decoration:none;"><?php 
             echo stripslashes($fetch_cat['category_name']);?> 
            </a>
        </li>
<?php
    }
?>  

这是我得到的输出 - 水平字母顺序:

A B C
D E F
G H I
J

这就是我想要显示的 - 垂直字母顺序:

A E H
B F I
C G J
D 

1 个答案:

答案 0 :(得分:1)

此模式是矩阵变换。见http://www.laprbass.com/RAY_temp_balbycarr.php

<?php // RAY_temp_balbycarr.php
error_reporting(E_ALL);


// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";


// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES



// CREATING A TABLE
$sql = "CREATE TEMPORARY TABLE my_table
(
        id INT               NOT NULL AUTO_INCREMENT PRIMARY KEY,
        name     VARCHAR(24) NOT NULL DEFAULT '',
        question VARCHAR(24) NOT NULL DEFAULT '',
        answer   VARCHAR(2)  NOT NULL DEFAULT ''
)"
;
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}



// LOADING IT UP WITH TEST DATA
$sql = "INSERT INTO my_table
( name,     question,    answer )
VALUES
( 'David',  'Question1', 'A' ),
( 'John',   'Question1', 'B' ),
( 'David',  'Question2', 'B' ),
( 'Mary',   'Question1', 'C' ),
( 'John',   'Question2', 'A' ),
( 'David',  'Question3', 'B' ),
( 'Mary',   'Question2', 'A' )
"
;
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}


// GET THE COLUMN TITLES (NAMES)
$cols  = array();
$sql   = "SELECT name FROM my_table GROUP BY name ORDER BY name";
$c_res = mysql_query($sql);
while ($row = mysql_fetch_assoc($c_res))
{
    $cols[] = $row['name'];
}

// GET THE ROW TITLES (QUESTIONS)
$rows  = array();
$sql   = "SELECT question FROM my_table GROUP BY question ORDER BY question";
$r_res = mysql_query($sql);
while ($row = mysql_fetch_assoc($r_res))
{
    $rows[] = $row['question'];
}

// CREATE THE EMPTY MATRIX OF COLUMNS (NAMES) AND ROWS (QUESTIONS)
foreach ($cols as $c)
{
    foreach ($rows as $r)
    {
        $mat[$c][$r] = '-';
    }
}

// LOAD THE MATRIX WITH THE ANSWERS
$sql = "SELECT name, question, answer FROM my_table";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res))
{
    $mat[$row['name']][$row['question']] = $row['answer'];
}

// CREATE THE TABLE
$out = '<table>' . PHP_EOL;

// ADD THE COLUMN TITLES
$out .= '<tr>';
$out .= '<th></th>';
foreach ($cols as $c)
{
    $out .= '<th>' . $c . '</th>';
}
$out .= '</tr>';
$out .= PHP_EOL;

// ADD EACH ROW
foreach ($rows as $r)
{
    $out .= "<tr><td>$r</td>";
    foreach ($cols as $c)
    {
        $out .= '<td>' . $mat[$c][$r] . '</td>';
    }
    $out .= '</tr>';
    $out .= PHP_EOL;
}

// FINISH THE TABLE
$out .= '</table>';
echo $out;

表格结构如下:

// LOADING IT UP WITH TEST DATA
$sql = "INSERT INTO my_table
( name,     question,    answer )
VALUES
( 'David',  'Question1', 'A' ),
( 'John',   'Question1', 'B' ),
( 'David',  'Question2', 'B' ),
( 'Mary',   'Question1', 'C' ),
( 'John',   'Question2', 'A' ),
( 'David',  'Question3', 'B' ),
( 'Mary',   'Question2', 'A' )
"
;