Latest 변형 두 테이블에서 내용 가져오기 정보
Latest 변형 두 테이블에서 내용 가져오기본문
동일한 갤러리게시판에서 조회수가 높은 것을 가져오려고 latest.lib.php를 수정하였습니다.
// Union table
function latestImg($skin_dir="", $bo_table01, $bo_table02, $rows=10, $subject_len=40, $options="")
{
global $g4;
function latestImg($skin_dir="", $bo_table01, $bo_table02, $rows=10, $subject_len=40, $options="")
{
global $g4;
if ($skin_dir)
$latest_skin_path = "$g4[path]/skin/latest/$skin_dir";
else
$latest_skin_path = "$g4[path]/skin/latest/basic";
$latest_skin_path = "$g4[path]/skin/latest/$skin_dir";
else
$latest_skin_path = "$g4[path]/skin/latest/basic";
$list = array();
$sql = " select * from $g4[board_table] where bo_table = '$bo_table01' OR bo_table = '$bo_table02'";
$board = sql_fetch($sql);
$sql = " select * from $g4[board_table] where bo_table = '$bo_table01' OR bo_table = '$bo_table02'";
$board = sql_fetch($sql);
$tmp_write_table01 = $g4['write_prefix'] . $bo_table01; // 게시판 테이블 전체이름
$tmp_write_table02 = $g4['write_prefix'] . $bo_table02; // 게시판 테이블 전체이름
$sql = " SELECT * FROM $tmp_write_table01 UNION ALL SELECT * FROM $tmp_write_table02 ORDER BY $options LIMIT 0, $rows ";
//explain($sql);
$result = sql_query($sql);
$tmp_write_table02 = $g4['write_prefix'] . $bo_table02; // 게시판 테이블 전체이름
$sql = " SELECT * FROM $tmp_write_table01 UNION ALL SELECT * FROM $tmp_write_table02 ORDER BY $options LIMIT 0, $rows ";
//explain($sql);
$result = sql_query($sql);
for ($i=0; $row = sql_fetch_array($result); $i++)
$list[$i] = get_list($row, $board, $latest_skin_path, $subject_len);
ob_start();
include "$latest_skin_path/latest.skin.php";
$content = ob_get_contents();
ob_end_clean();
$list[$i] = get_list($row, $board, $latest_skin_path, $subject_len);
ob_start();
include "$latest_skin_path/latest.skin.php";
$content = ob_get_contents();
ob_end_clean();
return $content;
}
}
latest.lib.php의 하단에 추가하였고, 보여지는 부분은
<? echo latestImg("new_img", "charge", "nocharge", 5, 20, "wr_hit"); ?>
이렇게 되어 있습니다.
그런데 첫번째 "charge"의 게시물만 가져 오네요..
이게 제대로 작동하는지도.. 일단 쿼리를 보니 가져는 오는거 같은데, charge, nochage 의 2개
의 게시판 중 조회수 높은걸 가져오는지는 잘 모르겠네요..
도와주세요..
댓글 전체
mysql 버전이 union을 지원하는군요. ^^
hit가 높은 것을 먼저 나오게 하려면
$sql = " SELECT * FROM $tmp_write_table01 UNION ALL SELECT * FROM $tmp_write_table02 ORDER BY $options LIMIT 0, $rows ";
=>
$sql = " SELECT * FROM $tmp_write_table01 ORDER BY $options DESC LIMIT 0, $rows UNION ALL SELECT * FROM $tmp_write_table02 ORDER BY $options DESC LIMIT 0, $rows ";
이렇고 고치고
$list에서 wr_hit로 정열을 해야 합니다.
usort( $list, 'hitcmp');
ob_start();
그리고
function hitcmp( $a, $b) {
return intval( $b) - intval( $a); // DESC
}
// Union table
function latestImg
hit가 높은 것을 먼저 나오게 하려면
$sql = " SELECT * FROM $tmp_write_table01 UNION ALL SELECT * FROM $tmp_write_table02 ORDER BY $options LIMIT 0, $rows ";
=>
$sql = " SELECT * FROM $tmp_write_table01 ORDER BY $options DESC LIMIT 0, $rows UNION ALL SELECT * FROM $tmp_write_table02 ORDER BY $options DESC LIMIT 0, $rows ";
이렇고 고치고
$list에서 wr_hit로 정열을 해야 합니다.
usort( $list, 'hitcmp');
ob_start();
그리고
function hitcmp( $a, $b) {
return intval( $b) - intval( $a); // DESC
}
// Union table
function latestImg
답변 감사합니다.
음.. 에러문이 뜨네요...
시간이 좀 여유가 있으면 이것저것 뒤져보며 배워가며 하겠는데, 오늘 사이트 오픈이라 그러질 못하고
질문을 다시 하게 되네요..
죄송합니다.
SELECT * FROM g4_write_charge ORDER BY wr_hit DESC LIMIT 0, 5 UNION ALL SELECT * FROM g4_write_nocharge ORDER BY wr_hit DESC LIMIT 0, 5
이렇게 수정했더니
첫번째 ORDER BY 구문에서 에러가 나는것 같습니다.
MySQL 에러 메시지가 이렇게 나네요..
Wrong usage of UNION and ORDER BY
워낙 초보고 프로그래머가 아니라 잘 모르겠네요..
일단은 Union 구문에서 Order By 구문이 잘못 쓰인거 같은데...
검색으로 뒤져보니 Order By 구문을 사용하려면 해당 컬럼이 최우선에 와야한다는
얘기도 있고, 알리아스를 사용하라고 하는데 초보로서 머리가 복잡해지네요..
해결 방법이 없는지... DB는 cafe24것을 사용하고 있습니다.
답변주셔서 다시금 감사드립니다.
음.. 에러문이 뜨네요...
시간이 좀 여유가 있으면 이것저것 뒤져보며 배워가며 하겠는데, 오늘 사이트 오픈이라 그러질 못하고
질문을 다시 하게 되네요..
죄송합니다.
SELECT * FROM g4_write_charge ORDER BY wr_hit DESC LIMIT 0, 5 UNION ALL SELECT * FROM g4_write_nocharge ORDER BY wr_hit DESC LIMIT 0, 5
이렇게 수정했더니
첫번째 ORDER BY 구문에서 에러가 나는것 같습니다.
MySQL 에러 메시지가 이렇게 나네요..
Wrong usage of UNION and ORDER BY
워낙 초보고 프로그래머가 아니라 잘 모르겠네요..
일단은 Union 구문에서 Order By 구문이 잘못 쓰인거 같은데...
검색으로 뒤져보니 Order By 구문을 사용하려면 해당 컬럼이 최우선에 와야한다는
얘기도 있고, 알리아스를 사용하라고 하는데 초보로서 머리가 복잡해지네요..
해결 방법이 없는지... DB는 cafe24것을 사용하고 있습니다.
답변주셔서 다시금 감사드립니다.
첫째 select에서 order by를 빼 보고 해 보세요.
SELECT * FROM g4_write_charge UNION ...
mysql union을 거의 안 써서 문법이 가물가물하네요. ^^
SELECT * FROM g4_write_charge UNION ...
mysql union을 거의 안 써서 문법이 가물가물하네요. ^^