2015-01-10 2 views
1

음식 웹 사이트에서 작업 중이며 사용자 카트를 표시 할 때 문제가 있음을 눈치 챘는데 현재 테이블 헤더는 mySQL에서 가져온 것입니다. : 여기 php/MySQL - 항목에 대해 하나의 헤더 만 표시합니다.

enter image description here

내 현재 코드입니다 :

function cart() { 
foreach($_SESSION as $name => $value) { 
    if ($value>0){ 
     if (substr($name, 0, 5)=='cart_') { 
      $id = substr($name, 5, (strlen($name)-5)); 
      $get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id)); 
      while ($get_row = mysql_fetch_assoc($get)) { 
       $sub = $get_row['price']*$value;?> 
       <center> 
       <table class='menufinal' border=0 width=75%> 
       <th>Remove Item</th> 
       <th>Item Name</th> 
       <th>Item Price</th> 
       <th>Quantity</th> 
       <th>Line Total</th> 
       <tr> 
       <td><?echo '<a href="cart.php?delete=' .$id.'"><img src="x.png"></a><br>'?></td> 
       <td><?echo $get_row['name']?></td> 
       <td><?echo '&pound' . number_format($get_row['price'], 2);?></td> 
       <td><?echo '<a href="cart.php?remove=' .$id. '"style="text-decoration:none">- </a>' .$value. '<a href="cart.php?add=' .$id. '"style="text-decoration:none"> +</a>' ?> </td> 
       <td> <?echo '&pound ' . number_format($sub, 2);?> </td> 
       </tr> 
       <? 
      } 
     } 
     if (empty($total)) { 

      if (empty($sub)) { 
       //do nothing 
      } else { 
       $total = $sub; 
      } 
     } else { 
      $total += $sub; 
     } 
    } 
} 
if (!empty($total)){ 
    echo '<br>Total: &pound' . number_format($total, 2) . '<br>'; 
    echo '<div id="dorc"><p><a href="index.php"><img src="dishes.png" width="240" height="152"></a> <img src="spacer.png" width="200"> <a href="checkout.php"><img src="checkout.png" width="240" height="152"></a>'; 
} 
else { 
    header ('Location: index.php'); 
} 
} 

내 질문은 내가 한 번만 테이블 헤더를 표시 할 수 있으며 이유에 인쇄되는 추가 항목이 할 수있는 방법입니다 화면 맨 아래에?

+0

당신은 당신의 루프 내부에 개구부를'

',하지만 전혀 닫는 태그 - 나는 그것이 문제 야 의심하지,하지만 난 돈 그것이 도움이 될 것이라고 생각하지 않습니다. – andrewsi

+0

@andrewsi 덕분에 항목이 아래쪽에 붙어 있습니다. 덕분에 DI가 여전히 반복적 인 헤더 문제가 있습니다. – Oscar

+0

'

'태그도. – andrewsi

답변

1

테이블 및 머리글 태그를 루프 밖으로 이동하십시오.

수행과 같은 :

<table> 
<th></th> #define all table headers 

for each item: 
    <tr> 
    <td>item info</td>... 
    </tr> 

</table> 
+0

이것은 대부분 맞지만 테이블 열기 태그 및 테이블 헤더가 while 루프의 외부에 있는지 확인하십시오. "while 루프"는 끝날 때까지 반복적으로 동일한 기능을 수행합니다. OP의 경우, 그것은 테이블과 헤더를 반복해서 다시 생성 할 것입니다. – matcartmill

+0

while 루프 외부에 나열된 태그를 이동하려고 시도했지만 헤더가 여전히 복제 중입니다. ( – Oscar

0

사용이

<?php 
function cart() { 
foreach($_SESSION as $name => $value) { 
    if ($value>0){ 
     if (substr($name, 0, 5)=='cart_') { 
      $id = substr($name, 5, (strlen($name)-5)); 
      $get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));?> 
       <center> 
       <table class='menufinal' border=0 width=75%> 
       <th>Remove Item</th> 
       <th>Item Name</th> 
       <th>Item Price</th> 
       <th>Quantity</th> 
       <th>Line Total</th> 
      <?php while ($get_row = mysql_fetch_assoc($get)) { 
       $sub = $get_row['price']*$value;?> 
       <tr> 
       <td><?echo '<a href="cart.php?delete=' .$id.'"><img src="x.png"></a><br>'?></td> 
       <td><?echo $get_row['name']?></td> 
       <td><?echo '&pound' . number_format($get_row['price'], 2);?></td> 
       <td><?echo '<a href="cart.php?remove=' .$id. '"style="text-decoration:none">- </a>' .$value. '<a href="cart.php?add=' .$id. '"style="text-decoration:none"> +</a>' ?> </td> 
       <td> <?echo '&pound ' . number_format($sub, 2);?> </td> 
       </tr> 
       <? 
      } 
     } 
     if (empty($total)) { 

      if (empty($sub)) { 
       //do nothing 
      } else { 
       $total = $sub; 
      } 
     } else { 
      $total += $sub; 
     } 
    } 
} 
if (!empty($total)){ 
    echo '<br>Total: &pound' . number_format($total, 2) . '<br>'; 
    echo '<div id="dorc"><p><a href="index.php"><img src="dishes.png" width="240" height="152"></a> <img src="spacer.png" width="200"> <a href="checkout.php"><img src="checkout.png" width="240" height="152"></a>'; 
} 
else { 
    header ('Location: index.php'); 
} 
} 
+0

답장을 보내 주셔서 감사하지만 불행히도 제목이 여전히 중복됩니다. (너무 실망합니다.)이 문제가 발생하는 이유를 알고 있습니까? – Oscar

+0

시도해보십시오. 테이블 시작 태그 및 기타 'function cart() {' –

관련 문제

'태그도 루프 바깥에 있어야합니다. 또한 '