0

サブカテゴリを含むカテゴリのリストを生成できるようにするより良い PHP コードの助けを求めていますが、これを 3 つの列にまたがって印刷します。

最初にテーブル データ (デモ テーブルのみ - テーブル名と列名は正しいが):

--- TABLE NAME = walkthroughs ---

---------------------------------------------------------------
| id  | name                  | url           | cluster       |
---------------------------------------------------------------
|  1  | A Room with a View    | http://...    | Happyville    |
|  2  | An Outdoor Tower      | http://...    | Happyville    |
|  3  | An Old Cottage        | http://...    | Happyville    |
|  4  | Town Hall             | http://...    | Misty Vale    |
|  5  | Cathedral             | http://...    | Misty Vale    |
|  6  | Babbling Brook        | http://...    | Old Forest    |
|  7  | The Lonely Flower     | http://...    | Old Forest    |
|  8  | The Hollow Tree       | http://...    | Old Forest    |
|  9  | The Secret Garden     | http://...    | Old Forest    |
| 10  | The Forgotten Corale  | http://...    | Open Plains   |
| 11  | Echo Caverns          | http://...    | Mountains     |
| 12  | The Forgotten Corale  | http://...    | Mountains     |
---------------------------------------------------------------

「クラスター」カテゴリを作成し、その下に関連する名前と URL をリストする PHP コードを試してみました。面倒ですが、仕事はします!このコードの HTML 出力はすべて 1 列です。

--- HTML OUTPUT ---

<h2>Happyville</h2>
<a href="url"> A Room with a View </a>
<a href="url"> An Outdoor Tower </a>
<a href="url"> An Old Cottage </a>
<br>
<h2>Misty Vale</h2>
<a href="url"> Town Hall </a>
<a href="url"> Cathedral </a>
<br>
<h2>Old Forest</h2>
<a href="url"> Babbling Brook </a>
<a href="url"> The Lonely Flower </a>
<a href="url"> The Hollow Tree </a>
<a href="url"> The Secret Garden </a>
<br>
<h2>Open Plains</h2>
<a href="url"> Babbling Brook </a>
<br>
<h2>Mountains</h2>
<a href="url"> Echo Caverns </a>
<a href="url"> The Forgotton Corale </a>

私が助けを求めているのは、Mysql の PHP です。

  • 結果が「クラスター」フィールドによってグループ化されるように、
  • 「cluster」フィールドの一意の値が最初に出力され、その後にその値の「name」と「url」の値がすべて出力されます。
  • 結果を 3 列に分割するために使用できる PHP/Mysql カウント。

最適な HTML 出力を以下に示します。

--- HTML OUTPUT ---
<div id="col-1">
  <h2>Happyville</div>
  <a href="url"> A Room with a View </a>
  <a href="url"> An Outdoor Tower </a>
  <a href="url"> An Old Cottage </a>
  <br>
  <h2>Misty Vale</h2>
  <a href="url"> Town Hall </a>
  <a href="url"> Cathedral </a>
  <br>
<div>
<div id="col-2">
  <h2>Old Forest</h2>
  <a href="url"> Babbling Brook </a>
  <a href="url"> The Lonely Flower </a>
  <a href="url"> The Hollow Tree </a>
  <a href="url"> The Secret Garden </a>
  <br>
  <h2>Open Plains</h2>
  <a href="url"> Babbling Brook </a>
  <br>
</div>
<div id="col-3">  
  <h2>Mountains</h2>
  <a href="url"> Echo Caverns </a>
  <a href="url"> The Forgotton Corale </a>
  <br>
</div>

これは私が使用している現在の PHP です。これは厄介であることがわかっています (たとえば、配列の使用についてはよくわかりません)。結果を 3 列に分割できますが、見出しと最初の値を出力し、次に任意の値を出力します。さらなる結果は次の列に表示されます (列 3 の場合はまったく表示されません)。

この問題の実際の例は、@ http://freethedangler.com/test/walkthroughs/instances-test.phpで見つけることができます(そうです、こっけいな MMO ページです!)。

実際の例では、「cluster」フィールドの名前は「cluster_or_campaigns」であることに注意してください。ここでは、作業を簡単にするために名前を簡略化しました。

--- CURRENT PHP ---
<?php
include("../php/lotrodb.php");

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$queryc="SELECT * FROM walkthroughs ORDER BY cluster ASC";
$queryd="SELECT cluster,COUNT(DISTINCT(cluster)) FROM walkthroughs GROUP BY cluster";
$resultc=mysql_query($queryc);
$resultd=mysql_query($queryd);

$numc=mysql_numrows($resultc);
$numd=mysql_numrows($resultd); // used to count the number of unique values for the 'cluster' field

mysql_close();

$i=0; //$i = counter used to count queryc values 
$j=0; //$j = counter used to print out <h2>'cluster'</h2> heading

$x=0;         //$x = counter used to count queryd values
$y=($numd/3); //$y = 1/3rd of $numd - used to close a column and start a new column
$z=$y*2;      //$z = 2/3rds of $numd - used to close a column and start a new column

$cluster_current = null;
?>
   <div id="col-1">
<?php
while ($x < $y) {

$name=mysql_result($resultc,$i,"name");
$url=mysql_result($resultc,$i,"url");
$cluster=mysql_result($resultc,$i,"cluster");

  if ($cluster != $cluster_current) {
  $x=$x+1;
?>
</p>
<br>
<h2><?php echo $cluster; ?> Cluster</h2>
<p>
<?php
}
?>
<a href="<?php  echo $url; ?>" class="fade"><?php  echo $name; ?></a><br>
<?php
$i=$i+1;
$cluster_current=mysql_result($resultc,$j,"cluster");
$j=$j+1;
}
?>
   </div>
   <div id="col-2">
<?php
while ($x < $z) {

$name=mysql_result($resultc,$i,"name");
$url=mysql_result($resultc,$i,"url");
$cluster=mysql_result($resultc,$i,"cluster");

  if ($cluster != $cluster_current) {
  $x=$x+1;
?>
</p>
<br>
<h2><?php echo $cluster; ?> Cluster</h2>
<p>
<?php
}
?>
<a href="<?php  echo $url; ?>" class="fade"><?php  echo $name; ?></a><br>
<?php
$i=$i+1;
$cluster_current=mysql_result($resultc,$j,"cluster");
$j=$j+1;
}
?>
   </div>
   <div id="col-3">
<?php
while ($x < $numd) {

$name=mysql_result($resultc,$i,"name");
$url=mysql_result($resultc,$i,"url");
$cluster=mysql_result($resultc,$i,"cluster");

  if ($cluster != $cluster_current) {
$x=$x+1;
?>
</p>
<br>
<h2><?php echo $cluster; ?> Cluster</h2>
<p>
<?php
}
?>
<a href="<?php  echo $url; ?>" class="fade"><?php  echo $name; ?></a><br>
<?php
$i=$i+1;
$cluster_current=mysql_result($resultc,$j,"cluster");
$j=$j+1;
}
?>
</div>
4

1 に答える 1

0

私は PHP にあまり詳しくありませんが、そこに連想配列 (辞書) があることは知っています。これはあなたの問題を解決するのに十分です。

まず、次のクエリを使用します ( GROUP_CONCATおよびCONCATを使用):

SELECT cluster,
       GROUP_CONCAT(
           CONCAT('<a href="', url, '">', name, '</a>')
       ) AS block
FROM walkthroughs
GROUP BY cluster

これを取得した後、クエリ応答をディクショナリcluster => blockに配置すると、ページ上の適切な場所でクラスターとechoHTML のブロックだけを反復処理できます。

于 2013-10-21T05:50:31.333 に答える