0

この方法で、アイテムのコンテンツをカテゴリ別に Web ページに印刷しようとしています。

カテゴリー1:
アイテム1 アイテム2 アイテム3 アイテム4
アイテム5...

カテゴリー
2 アイテム1 アイテム2 ...

これは私のPHPコードです:

$cat="";
$maxcols = 3;
$i = 0;

while ($row = $result->fetch_assoc()) {

    if ($i == $maxcols) {
        $i = 0;
        echo "</tr><tr>"; 
    }

    if($row['name']!=$cat)
    { 
        echo "<table>
        <tr><td collspan='3'>".$row['name']."</td></tr>
        <tr>";
    }

    echo "<td>".$row['title']."</td>";
    $cat=$row['name'];
    $i++;

}

while ($i <= $maxcols) {
    echo "<td>&nbsp;</td>";
    $i++;
    echo "</table>";
}

私が得ているものは次のとおりです。

<table>
<tr><td collspan='3'>Archivers</td></tr>
<tr><td>7-Zip</td><td>IZArc</td><td>dfssdfsdf sdfsdf</td></tr><tr><td>fgdgdfgd</td><td>sdfsdfsdfsdf dsfsdfsd</td><table>
<tr><td collspan='3'>Benchmark</td></tr>
<tr><td>Fresh Diagnose</td><td>&nbsp;</td></table>

私が取得したいものは次のとおりです。

<table>
<tr><td collspan='3'>Archivers</td></tr>
<tr><td>7-Zip</td><td>IZArc</td><td>dfssdfsdf sdfsdf</td></tr>
<tr><td>fgdgdfgd</td><td>sdfsdfsdfsdf dsfsdfsd</td>**<td>&nbsp;</td></tr>
</table>**
<table>
<tr><td collspan='3'>Benchmark</td></tr>
<tr><td>Fresh Diagnose</td><td>&nbsp;</td><td>&nbsp;</td>
</table>
4

2 に答える 2

0

どうもありがとう、Sumurai8!あなたのアイデアはうまくいきます。これが私のphpコードです:

<?php
include_once('include/db.inc.php');

$sql="SELECT m.name, GROUP_CONCAT(s.title order by s.title SEPARATOR '|' ) as titles FROM menu m, software s where m.id=s.category
GROUP BY m.name
order by m.name";
$result = $mysqli->query($sql);
$cat="";

$maxcols = 3;
//$i = 0;

echo "
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
    <head>
    <title>content</title>
         <meta content='text/html; charset=utf-8' http-equiv='content-type'>
         <meta content='Software Details' name='description'>
<link href='styles/fontstyle.css' rel='stylesheet' type='text/css'>  
<link href='styles/style.css' rel='stylesheet' type='text/css'>  
</head>

<body class='body'>";


while( $row = $result->fetch_assoc() ) {
  //The seperator needs to be something that isn't in the values it seperates
  //This creates an array with the titles for this category
  $titles = explode( '|', $row['titles'] );

  echo "
<table>
<tr><td colspan='" .$maxcols. "'>" .$row['name']. "</td></tr> \n";

  //$offset + $i is the position in the array of titles
  $offset = 0;

  //This loop ensures each row is properly opened and closed
  while( $offset < count( $titles ) ) {

    echo "<tr>";
    //This will ensure each row has $maxcols td's in it
    for( $i = 0; $i < $maxcols; $i++ ) {
      if( isset( $titles[ $offset + $i ] ) ) {
        echo "<td>" .$titles[ $offset + $i ]. "</td>";
      } else {
        echo "<td>&nbsp;</td>";
      }
    }
    $offset += $maxcols;
    echo "</tr> \n";
  }

  echo "</table><br> \n";
}

これは、私が望む方法での出力です。

<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
    <head>
    <title>content</title>
         <meta content='text/html; charset=utf-8' http-equiv='content-type'>
         <meta content='Software Details' name='description'>
<link href='styles/fontstyle.css' rel='stylesheet' type='text/css'>  
<link href='styles/style.css' rel='stylesheet' type='text/css'>  
</head>

<body class='body'>
<table>
<tr><td colspan='3'>Archivers</td></tr> 
<tr><td>7-Zip</td><td>IZArc</td><td>dfssdfsdf sdfsdf</td></tr> 
<tr><td>fgdgdfgd</td><td>sdfsdfsdfsdf dsfsdf</td><td>&nbsp;</td></tr> 
</table><br> 

<table>
<tr><td colspan='3'>Benchmark</td></tr> 
<tr><td>Fresh Diagnose</td><td>&nbsp;</td><td>&nbsp;</td></tr> 
</table><br> 



</body>
</html>
于 2013-07-01T13:24:43.187 に答える