foreach 문을 for 문으로 변경해야 되는데, 도움 부탁 드립니다.

foreach 문을 for 문으로 변경해야 되는데, 도움 부탁 드립니다.

QA

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'>";
  }
 
 
----
이건 제가 어설프게 변경해보았는데, 갯수별로 나열은 되는데, 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);
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++;



균이님 안녕하세요. :)
답변 감사드려요.ㅜㅜ

view.skin.php 상단에
<?
$today = "today$bo_table";
$_SESSION[$today][$wr_id] = $view[wr_id];
?>
로 세션을 만들어 오늘본상품을 불러오는 소스입니다.
상품은 잘 불러오는데, 상품 수량이 많아지면,
페이징이 필요해서, for문으로 페이징을 어떻게
구성해보려고 해요.


<?
function shop_price($today,$bo_table)
{
    global $g4;

  $price = 0;
  if(is_array($today))
  {

    foreach($today as $wr_id => $qty)
    { 
      $tmp_write_table = $g4['write_prefix'] . $bo_table;
      $sql = "select * from $tmp_write_table where wr_id=$wr_id";
      $row = sql_fetch($sql);

      $price +=$row[sel_price]*$qty;

    }
  }
  return $price;
}
  $tmp_write_table = $g4['write_prefix'] . $bo_table;

  //
  if($amount){
  $item_qt = $amount;
  }
  else{
  $item_qt = 1;
  }

  if($wr_id && $bo_table)
  {

    $wr = get_write($tmp_write_table, $wr_id);
    if (!$wr[wr_id])
        alert("존재하지 않는상품입니다.\\n\\n삭제된상품이거나 판매보류된 상품입니다.");

    if(!isset($_SESSION[$today]))
    {
      $_SESSION[$today] = array();
      $_SESSION['items'] = 0;
      $_SESSION['total_price'] = 0;

    }

    if(isset($_SESSION[$today][$wr_id])){
      $_SESSION[$today][$wr_id]++;

}
    else {
      $_SESSION[$today][$wr_id] = $item_qt;

}

    $_SESSION['total_price'] = shop_price($_SESSION[$today],$bo_table);
    $_SESSION['items']      = shop_qty($_SESSION[$today]);
  }

  if(isset($_POST['save']))
  { 
    foreach ($_SESSION[$today] as $wr_id => $qty)
    {
      if($_POST[$wr_id]=='0')
        unset($_SESSION[$today][$wr_id]);
      else
        $_SESSION[$today][$wr_id] = $_POST[$wr_id];
    }
    $_SESSION['total_price'] = shop_price($_SESSION[$today],$bo_table);
    $_SESSION['items']      = shop_qty($_SESSION[$today]);
  }

  if($del)
  {
    unset($_SESSION[$today][$del]);
  }

  if($wr_id || $del)
  {
    echo "<script language='JavaScript'>top.document.location.reload();</script>";
  }

$colspan = 4;


if($state_path =='today.php')$colspan++;



?>

답변을 작성하시기 전에 로그인 해주세요.
전체 125,873 | RSS
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1402호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT