작성자별 게시물 수 구하기
본문
<?php
$sql = "select * from g5_write_day order by wr_name ASC";
$result = sql_query($sql);
$name_list = array();
for($i=0;$row=sql_fetch_array($result);$i++){
$name_list[] = $row['wr_name'];
}
$unique_name = array_unique($name_list);
?>
<select class="form-select required" onchange="window.open(value,'_self');">
<option>작성자</option>
<option value="<?php echo G5_URL ?>/day">전체</option>
<?php
for ($i=0; $i<count($unique_name); $i++) {
$name_arr = $unique_name[$i];
?>
<option value="<?php echo G5_BBS_URL ?>/board.php?bo_table=day&sop=and&sfl=wr_name%2C1&stx=<?php echo $name_arr ?>"<?php echo ($stx == $name_arr) ? " selected" : "";?>><?php echo $name_arr; ?></option>
<?php
}
?>
</select>
이렇게 셀렉트 박스로 특정 게시판 작성자 리스트를 불러와서 작성자별 게시물 검색을 바로 하게 만들었는데 작성자별 게시물 수를 닉네임 옆에 표시하려면 데이터를 다시 불러와야 할까요?
!-->답변 2
<?php
$sql = "
select wr_name, count(*) as cnt
from g5_write_day
where wr_is_comment = 0
group by wr_name
order by wr_name ASC
";
$result = sql_query($sql);
?>
<select class="form-select required" onchange="window.open(value,'_self');">
<option>작성자</option>
<option value="<?php echo G5_URL ?>/day">전체</option>
<?php for ($i = 0; $row = sql_fetch_array($result); $i++) { ?>
<option value="<?php echo G5_BBS_URL ?>/board.php?bo_table=day&sop=and&sfl=wr_name%2C1&stx=<?php echo $row['wr_name'] ?>" <?php echo ($stx == $row['wr_name']) ? "selected" : "" ?>><?php echo $row['wr_name'] ?><?php echo '(' . $row['cnt'] . ')' ?></option>
<?php } ?>
</select>
<?php
$sql = "select * from g5_write_day order by wr_name ASC";
$result = sql_query($sql);
$name_list = array();
for($i=0;$row=sql_fetch_array($result);$i++){
$name_list[] = $row['wr_name'];
}
// $unique_name = array_unique($name_list);
$unique_name = array_count_values($name_list);
?>
<select class="form-select required" onchange="window.open(value,'_self');">
<option>작성자</option>
<option value="<?php echo G5_URL ?>/day">전체</option>
<?php
// for ($i=0; $i<count($unique_name); $i++) {
// $name_arr = $unique_name[$i];
foreach ($unique_name as $name_arr => $name_cnt) {
?>
<option value="<?php echo G5_BBS_URL ?>/board.php?bo_table=day&sop=and&sfl=wr_name%2C1&stx=<?php echo $name_arr ?>"<?php echo ($stx == $name_arr) ? " selected" : "";?>><?php echo $name_arr . ' (' . $name_cnt . ')'; ?></option>
<?php
}
?>
</select>
답변을 작성하시기 전에 로그인 해주세요.