Skip navigation
MadMac01
Currently Being Moderated

How do I do this? Repeat MySQL Query

Jun 19, 2012 7:36 AM

Tags: #php #database #mysql #dreamweaver_cs5 #recordset

am trying to do something I really do not understand how to do: reuse the same MySQL query with different results.

 

I have a map that I've scripted in jQuery MapHilight, so that when you mouse over a state, it highlights, and when you click it, it opens a modal box. Now what I want to do is populate the modal box with all the stores in that state.

 

So I have a MySQL database with several tables: most importantly, "Store Descriptions" and "Locations" (Linked to "Store Descriptions" via a foreign key.) The tables are named "store_descriptions" and "store_locations" respectively. So my query says this:

 

SELECT store_descriptions.store_name, store_descriptions.store_link, store_descriptions.locat_id, store_locations.locat_location, store_locations.locat_id FROM store_descriptions, store_locations WHERE store_descriptions.locat_id = store_locations.locat_id

 

All fine and dandy. This gives me a result set where I can see all the stores and their locations. I can make a new query by adding "AND store_descriptions.locat_id = 6". (6 is, say, Louisiana.) This shows me all the stores in Louisiana, and I can use it to populate the modal box for Louisiana.

 

However, I cannot do this 50 times! It puts such a drain on my database that the page takes forever to load. Is there a simpler way to do this? I'd really appreciate even just being shown the direction to go in learning how to solve this problem.

 

P.S. I am scripting in PHP.

 
Replies
  • Currently Being Moderated
    Jun 19, 2012 10:33 AM   in reply to MadMac01

    You don't hardcode the state id in the WHERE clause. Instead, you use a either a variable or placeholder and pass the state id value in the querystring. Your PHP code then retrieves this value and uses that in the WHERE clause. To have DW code this for you, check out Master/Detail pages.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 20, 2012 11:08 AM   in reply to MadMac01

    >This isn't a form-- it is a jQuery

    >function from an imagemap.

     

    Yes sorry, I forgot the context you were using this in.

     

    >And there is a syntax error on the second line, which I could not figure out.

     

    The isset() function only works with variables, hence the error. The way it is often used in DW is to check if a querystring or posted name/value is set. If it is set, then you would assign the variable to the value passed in from the form/querystring, otherwise it is left at the default value.

     

    There are two piece that you need to complete. Neither is very difficult but I'm not a php programmer so I can't give you specific code except in small bits.

    The first is to make sure that the imagemap link contains the querystring that has the state id in it.

     

    >I appended the query above with "AND store_descriptions.locat_id = location

     

    PHP variables need to begin with the $ so the correct code would probably be "AND store_descriptions.locat_id = $location".  But I suspect you may have other problems so lets start my question regarding the link from the imagemap and work from there.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 21, 2012 2:13 PM   in reply to MadMac01

    >I did use the $ in the mysql_fetch command above. That is not the cause for error here.

    >After your suggestion above, I tried href="#louisianaTip?location=6", but again, nada.

     

    I'm confused at what you are doing here. I thought you had an image map and were opening a new page or popup with the details? Why are you using a named anchor?

     

    >Still, there has got to be a better way.

     

    Of course there is - I've already explained that. You'll need to include the complete code if you want help tweaking it. Providing only small snippets doesn't really tell us enough.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points