php
  • mysql
  • table
  • 2011-08-08 10 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句に一致するテーブルの製品の各行を表示する表を生成します。私はこのようになりますループを持っています。

    私がしたいのは、最後の出現を最後のループ結果に加えた$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の中にラップする方法があります。

     関連する問題

    • 関連する問題はありません^_^