php
  • mysql
  • table
  • 2011-08-08 2 views 0 likes 
    0

    주문 장바구니에 대한 기본 검색 기능을 생성 중입니다.마지막 레코드 결과에 테이블 닫기 생성

    당신이 볼 수 있듯이
    $dataQuery = "SELECT * FROM `products` WHERE upper(`desc`) LIKE'%$find%'"; 
    $data = mysql_query($dataQuery) or die(mysql_error()); 
    
    $pageContent .= ' 
    <table border="1"> 
        <thead> 
         <tr> 
          <th>Stock Code</th> 
          <th>Description</th> 
          <th>Packsize</th> 
          <th>Price</th> 
          <th>In-Stock?</th> 
          <th>Quantity</th> 
         </tr> 
        </thead> 
        <tbody> 
        '; 
    //And we display the results 
    while($result = mysql_fetch_array($data)) 
    { 
        $prId = $result['id']; 
        $prRefCode = $result['refCode']; 
        $prDesc = $result['desc']; 
        $prPack = $result['pack']; 
        $prMeasure = $result['measure']; 
        $prQuantity = $result['quantity']; 
        $prDeptCode = $result['deptCode']; 
        $prTaxable = $result['taxable']; 
        $prPrice1 = $result['price1']; 
        $prPrice2 = $result['price2']; 
        $prCrdCode = $result['crdCode']; 
        $prCost1 = $result['cost1']; 
        $prCost2 = $result['cost2']; 
        $pageContent .= ' 
         <tr> 
          <td>'.$prId.'</td> 
          <td>'.$prDesc.'</td> 
          <td>'.$prPack.'x'.$prSize.' '.$prMeasure.'</td> 
          <td>R'.$prPrice1.'</td> 
        '; 
        if (empty($prQuantity)) { 
         $pageContent .= ' 
         <td>No</td> 
         '; 
         } else { 
         $pageContent .= ' 
         <td>Yes</td> 
         '; 
         } 
        $pageContent .= ' 
          <td> 
           <form action="" method="post"> 
            <div> 
             <input type="hidden" name="id" value="'.$prId.'" /> 
             <input type="submit" name="action" value="Order" /> 
            </div> 
           </form> 
          </td>    
         </tr> 
        '; 
    } 
    $pageContent .= ' 
         </tbody> 
        </table> 
    '; 
    //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
    $anymatches=mysql_num_rows($data); 
    if ($anymatches == 0) 
    { 
        $pageContent .= ' 
        <p>Sorry, but we can not find an entry to match your query</p> 
    '; 
    } 
    
    //And we remind them what they searched for 
    $pageContent .= ' 
    <p><b>Searched For:</b> '.$find.'</p> 
    '; 
    } 
    
    $pageContent .= ' 
    <br /> 
    <p>All prices are inclusive of VAT</p> 
    '; 
    

    , 이것은 where 절과 일치하는 테이블 제품의 각 행을 표시하는 테이블을 생성합니다 I는 다음과 같습니다 루프를 가지고있다.

    내가 마지막으로 수행 한 결과는 마지막 루프 결과에 $pageContent이 추가되어 효과적으로 테이블을 닫는 것입니다. 이 occurance은 다음과 같습니다

    첨가로
    $pageContent .= ' 
        </tbody> 
    </table> 
    '; 
    

    , 나는 결과를 효과적으로 테이블을 열고, 호출로 루프 전에 directely 추가 $pageContent의 첫 번째 occurance을하고 싶습니다. 이 occurance은 다음과 같습니다

    $pageContent .= ' 
    <table border="1"> 
        <thead> 
         <tr> 
          <th>Stock Code</th> 
          <th>Description</th> 
          <th>Packsize</th> 
          <th>Price</th> 
          <th>In-Stock?</th> 
          <th>Quantity</th> 
         </tr> 
        </thead> 
        <tbody> 
        '; 
    
    내가 직면하고 문제는 사용자가 유효하지 않은 문자열, 단락을 검색하는 경우 "미안하지만, 우리가 검색어와 일치하는 항목을 찾을 수 없습니다"이다

    테이블에서 출력을 얻는다 헤더에는 할당 된 행이 없습니다. 사용자가 유효하지 않은 검색 옵션을 입력하면이 단락 자체가 표 머리글없이 표시되며, 이는 유효한 검색 문자열의 출현에만 적용되어야하는 효과를 만들어 내려고합니다.

    누구나 입력 할 수 있다면 정말 고맙겠습니다!

    답변

    1

    아마도 변수를 0으로 설정 한 다음 루프 내부에서 변수 == 0인지 확인하고, 그렇다면 테이블 열기를 출력 할 수 있습니다.

    그런 다음 루프의 끝에서 변수가 결과 집합의 행 수와 같은지 확인한 다음 닫는 태그를 출력하십시오. 그런 다음 반복 할 때마다 변수를 증가시킵니다. 이 같은

    뭔가 :

    $dataQuery = "SELECT * FROM `products` WHERE upper(`desc`) LIKE'%$find%'"; 
    $data = mysql_query($dataQuery) or die(mysql_error()); 
    
    //This counts the number or results - and if there wasn't any it gives them a little message explaining that 
        $anymatches = mysql_num_rows($data); 
        if ($anymatches == 0) { 
         $pageContent .= ' 
    <p>Sorry, but we can not find an entry to match your query</p> 
    '; 
        } 
    
    $tempVar = 0; 
    //And we display the results 
    while ($result = mysql_fetch_array($data)) { 
        $prId = $result['id']; 
        $prRefCode = $result['refCode']; 
        $prDesc = $result['desc']; 
        $prPack = $result['pack']; 
        $prMeasure = $result['measure']; 
        $prQuantity = $result['quantity']; 
        $prDeptCode = $result['deptCode']; 
        $prTaxable = $result['taxable']; 
        $prPrice1 = $result['price1']; 
        $prPrice2 = $result['price2']; 
        $prCrdCode = $result['crdCode']; 
        $prCost1 = $result['cost1']; 
        $prCost2 = $result['cost2']; 
    
    
        if ($tempVar == 0) { 
         $pageContent .= ' 
    <table border="1"> 
        <thead> 
         <tr> 
          <th>Stock Code</th> 
          <th>Description</th> 
          <th>Packsize</th> 
          <th>Price</th> 
          <th>In-Stock?</th> 
          <th>Quantity</th> 
         </tr> 
        </thead> 
        <tbody> 
        '; 
        } 
        if ($tempVar == mysql_num_rows($data)) { 
         $pageContent .= ' 
        </tbody> 
    </table> 
    '; 
        } 
    
         $pageContent .= ' 
        <tr> 
         <td>' . $prId . '</td> 
         <td>' . $prDesc . '</td> 
         <td>' . $prPack . 'x' . $prSize . ' ' . $prMeasure . '</td> 
         <td>R' . $prPrice1 . '</td> 
    '; 
         if (empty($prQuantity)) { 
          $pageContent .= ' 
        <td>No</td> 
        '; 
         } else { 
          $pageContent .= ' 
        <td>Yes</td> 
        '; 
         } 
         $pageContent .= ' 
         <td> 
          <form action="" method="post"> 
           <div> 
            <input type="hidden" name="id" value="' . $prId . '" /> 
            <input type="submit" name="action" value="Order" /> 
           </div> 
          </form> 
         </td>   
        </tr> 
    '; 
         $tempVar ++; 
        } 
        $pageContent .= ' 
        </tbody> 
    </table> 
    '; 
    
    
    //And we remind them what they searched for 
        $pageContent .= ' 
    <p><b>Searched For:</b> ' . $find . '</p> 
    '; 
    } 
    
    $pageContent .= ' 
    <br /> 
    <p>All prices are inclusive of VAT</p> 
    '; 
    
    +0

    내 단어 !!! 그렇게 간단한 접근이었습니다. 너 내가 무슨 짓을했는지 모를거야! 나는 정말로 당신의 의견을 고맙다! –

    +0

    하 ... 괜찮습니다. 괜찮습니다. 나는 너를 믿는다. 나는 많은 것을 훨씬 더 간단하게해야만했던 것들을 성취하기위한 최고 접근법을 통해 미쳐 버렸다. – BumbleShrimp

    1

    몇 가지 가능한 해결 방법이 있습니다. 테이블 HTML (다른 문자로 $pageContent)을 저장하기 위해 다른 var 이름을 사용할 수 있습니다. 행 수가 0보다 큰 경우 (코드 하단에 이미이 체크가 있음) $pageContent에 연결합니다. 또 다른 가능한 방법은 mysql_num_rows 호출을 어딘가에 이동 한 다음 테이블을 생성 한 다음 if 안에 테이블 관련 코드를 래핑하는 것입니다.

    관련 문제