tags:

views:

90

answers:

2

Hi...

Im have a code that querys a mysql database against a certain criteria, and returns a result. The result then, is displayed in table rows inside a while loop, using the 'mysql_fetch_array' function:

    while($row = mysql_fetch_array($qry_result)){

By the way, this is a classifieds website... Here is my problem:

After the results are displayed, I want the user to be able to click a button or link and then filter the results in 3 ways ('ALL ADS', 'ONLY PRIVATE ADS' or 'ONLY COMPANY ADS'). Is there a smart way to do this without ten pages of code?

I already have a solution, that is to add an if statement before the while loop, to check if a variable is 1, 2 or 3, and then have three while loops:

      if ($ads_shown=='1'){ while loop and show "ALL ADS"}
      else if ($ads_shown=='2'){while loop and show "PRIVATE ADS"}
      else if ($ads_shown=='3'){while loop and show "COMPANY ADS"}

but this doesn't seem like the best way if I am seeking maximum speed in this case! So, any help?

+1  A: 

You actually modify the MySQL query to filter the results for you and then run the loop as it would normally. This means one loop and you don't have to maintain 3 versions of it.

  if ($ads_shown=='1'){ $extra_query = "";}
  else if ($ads_shown=='2'){$extra_query = "WHERE `type` = 'Private'";}
  else if ($ads_shown=='3'){$extra_query = "WHERE `type` = 'Company'";}
  $qry_result="SELECT * FROM `ads` ".$extra_query." LIMIT 10";

     while($row = mysql_fetch_array($qry_result))
    {
    }

Of course you are going to need to adapt the code to suit your purposes, but it should work once you get it going. Make sure type and the values for type correctly correlate to whats in your MySQL table.

Update:

There is no real reason this cannot happen before the query but if you insist on it here goes:

     while($row = mysql_fetch_array($qry_result))
    {
        if ($row['type']==$ads_shown) //Where 'type' denotes the type of advert
        {
          //Do stuff
        }
    }

This is less efficient because all the data is being grabbed from MySQL and not just the stuff you need. But assuming there is a field in your table called 'type' that you use to store the type of advert and assuming you have set the value of $ads_shown to correspond, it should work fine.

Sam152
Yes, but I need to do this after the query, thats the problem... Because once the user searches the website, the results are displayed and also a navigation to filter the search between "ALL ADS, PRIVATE, COMPANY"...
Camran
SAM, the reason this must happen after the query is because Im using ajax to display the results in a div, and also display buttons for filtering... Now if im going to filter BEFORE the query, then I would have to add checkboxes or something for the user to select before searching... understand now?
Camran
No, you are clearly unable to program correctly therefore you flood stackoverflow with your poorly thought out questions without ever giving back to the community. You clearly have no idea what you are talking about and should have a little more humility. Where you physically filter something on a page has no correlation to the back end HTTP request that handles the data.
Sam152
I have poor questions? there is no such thing as a poor question... too bad you feel im flooding stackoverflow, you dont know what stackoverflow is all about then...
Camran
That's funny coming from a member of 8 days. And yes, there IS definitely such thing as a poor question. Do your research first and don't just look to other people to do it for you. Think about what problems you are having specifically, don't expect people to just write your code.
Sam152
get a life sam okay, I didn't ask you to answer my question, please DONT answer it, your answer is useless anyways
Camran
By posting it, you asked the entire site. And I find it pretty funny that the answer you selected pretty much said, "Go learn to program correctly".
Sam152
+1  A: 

This is based on what I believe you communicated. Maybe I am entirely wrong. Disregard my message then.

It seems you did not understand the AJAX concept if you are trying to filter MySQL results from the whole set after am user interaction.

You want to select specific ads based on what the user clicks on a website without said website to reload. You believe that you have to send the whole data table alongside the website when it is first loaded.

What you actually want to have is a Javascript that sends a HTTP request to a seperate script that creates an XML from your database which then is sent back to your site. Your still-non-reloading site then executes a JavaScript-based event that creates new content from your XML.

Short answer: Try to learn the concepts of AJAX, use this as a pointer (note: this is a PERL example, but the concept is exactly the same)

Martin Hohenberg
Thanks, finally somebody understands
Camran