테스트 사이트 - 개발 중인 베타 버전입니다

늦은밤에..foreach 질문드려요.. 채택완료

아기새 8년 전 조회 2,344

foreach로 메뉴명들을 뽑고있는데..


예를들면...


</p><p>$sql = select * from category where depth=1;  //1뎁스인애들불러오기</p><p>while($res = db_fetch_array($sql)) {</p><p style="margin-left: 40px;">$depth1[] =$res;</p><p>}</p><p>
</p><p>
</p><p>foreach ($depth1 as $key =>$val) {</p><p style="margin-left: 40px;">echo $val['name']; //1뎁스명애들 출력됨.</p><p style="margin-left: 40px;"> </p><p style="margin-left: 40px;"> 그다음 이어서 2뎁스애들을 불러와야하는데 여기서 어떻게해야할지를 모르겠습니다..</p><p>
</p><p>}</p><p><span style="font-size: 11pt;">
 



db category 테이블에서 1뎁스의 idx 값을 가지고 2뎁스를 찾아 불러와야하는데

$depth2 = select * from category where depth=2 and parent=$depth1['idx'];


while로는 할 수있겠는데 foreach로는 도저히 머리가 돌아가지 않네요...

제발 알려주세요ㅜㅜ




댓글을 작성하려면 로그인이 필요합니다.

답변 3개

채택된 답변
+20 포인트
8년 전

$sql = "select * from category where depth=2"; 

while($res = db_fetch_array($sql)) $depth2[$row['parent']][] = $res;


$sql = "select * from category where depth=1"; 

while($res = db_fetch_array($sql)) 

    foreach ($depth2[$res['idx']]  as $v)  echo $v;


로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

아기새
8년 전
헉..감사합니다 이거네요..

댓글을 작성하려면 로그인이 필요합니다.

플래토
8년 전

@아기새 

네, 반복문의 기능은

사용법만다르고


개념적으로 동일합니다.


루프 안에서 중첩루프 문을 만들어서 처리하는 개념을 알고 계시면

같은 패턴으로 사용하시면되요


로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

플래토
8년 전


거의다 하신거 같은데요


</p><p style="word-break: break-all; color: rgb(72, 72, 72); font-family: Arial, 돋움, Dotum; font-size: 14.004px;">
</p><p style="word-break: break-all; color: rgb(72, 72, 72); font-family: Arial, 돋움, Dotum; font-size: 14.004px;">$depth2 = <span style="color: rgb(0, 0, 0);">"select * from category where</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">depth=2 and parent=<span style="color: rgb(255, 0, 0);"><b>$val</b></span>['idx']"; // 변수를 맞는걸로 사용하시면되요</span></p><div><span style="color: rgb(255, 0, 0);">
</span></div><div><font color="#ff0000"><span style="color: rgb(0, 0, 0);">
로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

아기새
8년 전
댓글 감사합니다ㅜㅜ 이거때문에 잠을못잤어요...

그럼 foreach안에서 또 foreach를 하면될런지... 모르겠어요..

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인