[재질문!] 변수 홀수 / 짝수 호출방법
본문
안녕하세요.. php와 그누보드를 열심히 공부한지 1개월 된 유저입니다
다름이 아니라, 최신글을 홀수, 짝수를 나눠서 불러오게 하고 있습니다(슬라이드를 2줄로 이용하기 위함)
그래서 이렇게 코드를 작성했습니다
<?php for ($n=1; $n<count($list); $n+=2) { ?>
<?php for ($i=0; $i<count($list); $i+=2) { ?>
<div class="slider2 sbox">
<a href="<?php echo $list[$i]['href'] ?>">
<?php
$thumb = get_list_thumbnail($bo_table, $list[$i]['wr_id'], $imgmaxwidth, $imgmaxheight);
if($thumb['src']) {
$img_content = '<a rel="gallery" class="pirobox_gall" href="'.$thumb['src'].'" title="'.$list[$i]['subject'].'" width="'.$imgmaxwidth.'" height="'.$imgmaxheight.'"><img src="'.$thumb['src'].'" alt="'.$thumb['alt'].'" width="'.$imgminwidth.'" height="'.$imgminheight.'"></a>';
} else {
$img_content = '<span style="width:'.$board['bo_gallery_width'].'px;height:'.$board['bo_gallery_height'].'px">no image</span>';
}
echo $img_content;
?>
</a>
<div class="gy_cotent">
<strong><?php echo cut_str($list[$i]['subject'], 150, "..") ?></strong>
</div>
<a href="<?php echo $list[$n]['href'] ?>">
<?php
$thumb = get_list_thumbnail($bo_table, $list[$n]['wr_id'], $imgmaxwidth, $imgmaxheight);
if($thumb['src']) {
$img_content = '<a rel="gallery" class="pirobox_gall" href="'.$thumb['src'].'" title="'.$list[$n]['subject'].'" width="'.$imgmaxwidth.'" height="'.$imgmaxheight.'"><img src="'.$thumb['src'].'" alt="'.$thumb['alt'].'" width="'.$imgminwidth.'" height="'.$imgminheight.'"></a>';
} else {
$img_content = '<span style="width:'.$board['bo_gallery_width'].'px;height:'.$board['bo_gallery_height'].'px">no image</span>';
}
echo $img_content;
?>
</a>
<div class="gy_cotent">
<strong><?php echo cut_str($list[$n]['subject'], 150, "..") ?></strong>
</div>
</div>
<?php } ?>
<?php } ?>
근데 출력이 첫째줄 [i 라인]은 잘 됩니다.
0,2,4,6,8
근데 [n]라인이 이상합니다
1,1,1,3,3,3,5,5,5,7,7,7,9,9,9... 이렇게요
n라인도 잘 표시되게 할 수 있을까요?
!-->
답변 1
for문 안에 for문을 넣어서 생긴 문제입니다. 밖으로 빼주면 됩니다.
<?php for ($n=1; $n<count($list); $n+=2) { ?>
<div class="slider2 sbox">
<a href="<?php echo $list[$i]['href'] ?>">
<?php
$thumb = get_list_thumbnail($bo_table, $list[$i]['wr_id'], $imgmaxwidth, $imgmaxheight);
if($thumb['src']) {
$img_content = '<a rel="gallery" class="pirobox_gall" href="'.$thumb['src'].'" title="'.$list[$i]['subject'].'" width="'.$imgmaxwidth.'" height="'.$imgmaxheight.'"><img src="'.$thumb['src'].'" alt="'.$thumb['alt'].'" width="'.$imgminwidth.'" height="'.$imgminheight.'"></a>';
} else {
$img_content = '<span style="width:'.$board['bo_gallery_width'].'px;height:'.$board['bo_gallery_height'].'px">no image</span>';
}
echo $img_content;
?>
</a>
<div class="gy_cotent">
<strong><?php echo cut_str($list[$i]['subject'], 150, "..") ?></strong>
</div>
<a href="<?php echo $list[$n]['href'] ?>">
<?php
$thumb = get_list_thumbnail($bo_table, $list[$n]['wr_id'], $imgmaxwidth, $imgmaxheight);
if($thumb['src']) {
$img_content = '<a rel="gallery" class="pirobox_gall" href="'.$thumb['src'].'" title="'.$list[$n]['subject'].'" width="'.$imgmaxwidth.'" height="'.$imgmaxheight.'"><img src="'.$thumb['src'].'" alt="'.$thumb['alt'].'" width="'.$imgminwidth.'" height="'.$imgminheight.'"></a>';
} else {
$img_content = '<span style="width:'.$board['bo_gallery_width'].'px;height:'.$board['bo_gallery_height'].'px">no image</span>';
}
echo $img_content;
?>
</a>
<div class="gy_cotent">
<strong><?php echo cut_str($list[$n]['subject'], 150, "..") ?></strong>
</div>
</div>
<?php } ?>
<?php for ($i=0; $i<count($list); $i+=2) { ?>
<div class="slider2 sbox">
<a href="<?php echo $list[$i]['href'] ?>">
<?php
$thumb = get_list_thumbnail($bo_table, $list[$i]['wr_id'], $imgmaxwidth, $imgmaxheight);
if($thumb['src']) {
$img_content = '<a rel="gallery" class="pirobox_gall" href="'.$thumb['src'].'" title="'.$list[$i]['subject'].'" width="'.$imgmaxwidth.'" height="'.$imgmaxheight.'"><img src="'.$thumb['src'].'" alt="'.$thumb['alt'].'" width="'.$imgminwidth.'" height="'.$imgminheight.'"></a>';
} else {
$img_content = '<span style="width:'.$board['bo_gallery_width'].'px;height:'.$board['bo_gallery_height'].'px">no image</span>';
}
echo $img_content;
?>
</a>
<div class="gy_cotent">
<strong><?php echo cut_str($list[$i]['subject'], 150, "..") ?></strong>
</div>
<a href="<?php echo $list[$n]['href'] ?>">
<?php
$thumb = get_list_thumbnail($bo_table, $list[$n]['wr_id'], $imgmaxwidth, $imgmaxheight);
if($thumb['src']) {
$img_content = '<a rel="gallery" class="pirobox_gall" href="'.$thumb['src'].'" title="'.$list[$n]['subject'].'" width="'.$imgmaxwidth.'" height="'.$imgmaxheight.'"><img src="'.$thumb['src'].'" alt="'.$thumb['alt'].'" width="'.$imgminwidth.'" height="'.$imgminheight.'"></a>';
} else {
$img_content = '<span style="width:'.$board['bo_gallery_width'].'px;height:'.$board['bo_gallery_height'].'px">no image</span>';
}
echo $img_content;
?>
</a>
<div class="gy_cotent">
<strong><?php echo cut_str($list[$n]['subject'], 150, "..") ?></strong>
</div>
</div>
<?php } ?>
답변을 작성하시기 전에 로그인 해주세요.