Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6229353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:33:06+00:00 2026-05-24T09:33:06+00:00

I am creating a basic search function for an order cart. I have a

  • 0

I am creating a basic search function for an order cart. I have a loop that looks like this :

$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>
';

As you can see, this produces a table that displays each row of the table products that matches the where clause.

What I would like to do is have the last occurance of $pageContent added to the very last loop result, so effectively it closes the table. This occurance would be:

$pageContent .= '
    </tbody>
</table>
';

As an addition, I would like to have the first occurance of $pageContent added directely before the loop as the results are called, effectively opening the table. This occurance would be:

$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>
    ';

The problem that I am facing is that if a user searches an invalid string, the paragraph “Sorry, but we can not find an entry to match your query” gets output under the table header, which has no rows assigned to it. I am trying to produce an effect where if the user enters an invalid search option, then this paragraph will be displayed by itself, without table header, which should only apply to the occurance of a valid search string.

If anyone has some input on this, I would really appreciate it!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:33:08+00:00Added an answer on May 24, 2026 at 9:33 am

    Perhaps you can set a variable to 0, then inside the loop, check if the variable == 0, if so, then output your table opening.

    Then, at the end of the loop, also check if the variable is equal to the number of rows in the resultset, and if so, output your closing tags.
    Then, increment the variable every iteration.

    something like this:

    $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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating an offline search-engine like application that's also like a dictionary, and I
Using jQuery I'm creating a basic 'tooltip' animation so that the tooltip will appear
My current class is planning on creating a basic networked game, but we have
The basic setup is classic - you're creating a Windows Forms application that connects
Creating Traversals for Binary Search Tree with Recursion. void inOrder(void (*inOrderPtr)(T&)) { if(this->left !=
I'm creating some basic work assistance utilities using Ruby. I've hit a problem that
What options exist for creating a scalable, full text search with results that need
I'm creating a basic database application in WPF, and I have started using the
I'm creating a basic Struts2, Maven webApp and getting this error when I deploy
I am creating a really basic program, that has a method to fill an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.