1

PHP と MySQL で書かれた新しいオンライン ストアがあります。

<div class="content-area">
<div class="page-heading">
<h1>Store</h1>
</div>
<p style="padding-top: 5px;"><strong>You are here:</strong> <a href="<?php echo $cls->root(); ?>/">Home</a> &raquo; Store</p>
<table border="0" cellpadding="0" cellspacing="0" width="500">
<?php
$categories=mysql_query("SELECT * FROM categories WHERE parent='0' ORDER by owner ASC, title ASC");
while($categoriesRow=mysql_fetch_array($categories)) {
$categoriesSub=mysql_query("SELECT * FROM categories WHERE parent='$categoriesRow[id]'");
?>
<tr>
<td valign="top">
<div class="product_list">
<div class="image_product">
<img alt="<?php echo $categoriesRow['title']; ?>" src="<?php echo $cls->truska(true); ?>/theme_section_image.gif" border="0" style="vertical-align: middle;" />
</div>
<div>
<h3 class="product"><a href="<?php echo $cls->root(); ?>/category/<?php echo $categoriesRow['permalink']; ?>/" target="_self"><?php echo $categoriesRow['title']; ?></a> <?php if(mysql_num_rows($categoriesSub) > 0) { ?>(<?php while($categoriesSubRow=mysql_fetch_array($categoriesSub)) {  }?>)<?php } ?></h3>
</div>
</div>
</td>
</tr>
<tr>
<td class="dotted_line_blue" colspan="1">
<img src="<?php echo $cls->truska(true); ?>/theme_shim.gif" height="1" width="1" alt=" " />
</td>
</tr>
<?php
}
?>
</table>
</div>

2 番目の While ループがある場所では、最後の結果が何であるかを調べる必要があるため、while ループからコンマを省略できます。

「レゴ (レゴ シティ、レゴ スターウォーズ)」と表示されますが、「レゴ (レゴ シティ、レゴ スターウォーズ)」と表示させたいです。

現在の結果が最後の場合、どうすれば取得できますか?

4

4 に答える 4

2

結果の配列を作成し、それを内破するだけです。これにより、カウントが自動的に処理されます。

$comma_separated = implode(",", $array);
于 2012-07-10T16:49:28.903 に答える
2

最初の結果の場合はコンマを追加せず、次のすべての結果の前に追加してください。

于 2012-07-10T16:47:36.633 に答える
2

これは、別の方向からアプローチすることで修正できます。

最後の結果を除く各結果の後にカンマを追加する代わりに、最初の結果を除くすべての結果の前にカンマを追加してみてください。

ループの外側で呼び出される変数を設定し、$firstそれを 1 に設定します。ループの内側:

if ($first == 0) {
   echo ",";
} else {
    $first = 0;
}
于 2012-07-10T16:47:50.550 に答える
1

プレーンなmysqlアクセスを使用するべきではありません。PDOを見てください。

あなたの質問に答えて、このようなことを試してください:

$items = array();
while ($row = mysql_fetch_assoc($result)) {
  $items[] = $row['foo'];
}
echo implode(', ', $items);
于 2012-07-10T16:51:03.403 に答える