foreach 문을 for 문으로 변경해야 되는데, 도움 부탁 드립니다.
본문
안녕하세요. :)
for 문의 $i가 필요해서,
아래 foreach 문을 for 문으로 변경해야 되는데, 잘 안되네요. ;
고수님들 도움 부탁 드립니다.
foreach ($_SESSION[$today] as $wr_id => $qty)
{
$tmp_write_table = $g4['write_prefix'] . $bo_table;
$sql = "select * from $tmp_write_table where wr_id='$wr_id'";
$list = sql_fetch($sql);
$t = 0; // 첫번째 이미지
$row = sql_fetch(" select bf_file from $g4[board_file_table] where bo_table = '$bo_table' and wr_id = '$wr_id' and bf_no = '$t' ");
if($row[bf_file]){
$i_width = "120";
$dst = "$g4[path]/data/file/$bo_table/thumbs/" . $row[bf_file];
$dst_img = "<img src='{$dst}' width='93' height='60' border='0'>";
}
{
$tmp_write_table = $g4['write_prefix'] . $bo_table;
$sql = "select * from $tmp_write_table where wr_id='$wr_id'";
$list = sql_fetch($sql);
$t = 0; // 첫번째 이미지
$row = sql_fetch(" select bf_file from $g4[board_file_table] where bo_table = '$bo_table' and wr_id = '$wr_id' and bf_no = '$t' ");
if($row[bf_file]){
$i_width = "120";
$dst = "$g4[path]/data/file/$bo_table/thumbs/" . $row[bf_file];
$dst_img = "<img src='{$dst}' width='93' height='60' border='0'>";
}
----
이건 제가 어설프게 변경해보았는데, 갯수별로 나열은 되는데, Array[0]Array[1] 이렇게만 떠요.ㅜ
어떻게 수정해야 되나요? 어렵네요. 에고~
for( $i = 0 ; $i < count($_SESSION[$today]) ; $i++ ) {
$tmp_write_table = $g4['write_prefix'] . $bo_table;
$sql = "select * from $tmp_write_table where wr_id='$wr_id'";
$list = sql_fetch($sql);
$sql = "select * from $tmp_write_table where wr_id='$wr_id'";
$list = sql_fetch($sql);
echo "$_SESSION[$today][$i]";}
답변 2
for( $i = 0 ; $i < count($_SESSION[$today]) ; $i++ ) 이렇게 사용할 수 없습니다
for문은 0,1,2,3..이렇게 loop를 도는 것인데
wr_id=1, 3, 4 이렇게 된다면 $i 값과 wr_id 값이 일치하지않게 되겠죠
예를들어 아래와 같이하면 $aa[1] 밖에 출력되지않습니다( count($aa) =3 )
$aa[1]=100; $aa[3]=300; $aa[4]=400;
for($i=0; $i<count($aa); $i++) echo $aa[$i].'<br>';
아래처럼 만들어서 for문을 사용하면 됩니다
foreach ($_SESSION[$today] as $wr_id => $qty) $todaylist[$wr_id][]=$qty;
for($i=0; $i<count( $todaylist[$wr_id]); $i++) echo $todaylist[$wr_id][$i];
읽어오는 $_SESSION[$today] 값을 기록할 때 어떤 형식으로 하는지, $today는 어떤 값인지 설명이 있어야겠네요
for 문의 $i가 필요해서 <---- 이것은 아래처럼 그냥 foreach에서 해도 되겠습니다
$i=-1;
foreach ($_SESSION[$today] as $wr_id => $qty) { $i++;
답변을 작성하시기 전에 로그인 해주세요.