• Hi. I’m not very good with PHP but I’m learning so can someone point me in the right direction with this? I’ve been trying to get this working for weeks now.

    I have users that enter their zip code and then select a pre-determined type of business from a drop down option box during registration. This info is recorded to the wp_usermeta table in the database.

    Then when a zip code is entered in an input on a page and if there is any data in the database, that business info will be displayed. The plugin is working for this.

    What I am trying to get at is this – if no info has been recorded in the database for a selected business type option within the entered zip code, then it will display (along with the other businesses in the database) a line that reads something like “Claim this Spot”

    So after you enter a zip code it would display something like:

    Hardware?????????  Smith Hardware
    Flowers?????????    Johnson Flowers
    Construction??????Claim this Spot (This links to a registration page)

    The code I have so far is:

    function list_business_info_register_shortcodes()
    {
    add_shortcode('list-business-info', 'list_business_info_function');
    }
    add_action( 'init', 'list_business_info_register_shortcodes');
    
    function list_business_info_function()
    {
    if (isset($_POST['zip']))
    $zip = $_POST['zip'];
    $businfo = '';
    
    $result = mysql_query("SELECT user_id FROM wp_usermeta WHERE meta_key='rpr_9-digit_zip_code' AND meta_value=$zip");
    
         while ($row = mysql_fetch_array($result))
         {
         $userid = $row['user_id'];
         $bustype = mysql_query("SELECT meta_value FROM wp_usermeta WHERE meta_key='rpr_business_type' AND user_id=$userid");
    
         	while ($businesstype = mysql_fetch_array($bustype))
    	{
    	$businesstypevalue = $businesstype['meta_value'];
         	$businfo .='<tr><td>';
         	$businfo .= $businesstypevalue;
         	$businfo .='</td><td>';
         	$businfo .='Name of Business';
         	$businfo .='</td></tr>';
    	}
    
         }
    return $businfo; 
    
    }

    I’ll put in the code for “Name of Business” later.
    Thank you for your help. Any direction would be appreciated.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I think you should re-evaluate your approach slightly. Mainly because you cannot query for something that does not exist, so your code cannot easily see if there is an empty spot. You don’t want to enter place holder spots to get around this either, that would be a nightmare.

    The other thing is you may want more than one business per zip, and they should be listed together. Right now they appear in the order they appear in the table (I believe so anyway, technically it’s undefined.)

    Another consideration is how to add categories that do not currently exist. You do not want a long list of esoteric categories that all say “claim this spot”, right? I can see two levels of categories, a priority one that get’s the “claim this spot” message, and another that simply is not displayed if such a business is not in the zip.

    The ideal approach gets rather complicated, perhaps for now you might just create a static array structure that defines where each category fits in. Then the easiest approach is to loop through each category and query for businesses in that category and in the current zip. If nothing comes up and it’s a priority category, display the “claim this spot” message.

    Unfortunately, that makes for a lot of queries from one simple request. This will not scale upwards well at all. Better to do a single query with results ordered and grouped by category. Now, to detect an empty category, your static array would need to be in the same order as the query results. When the loop detects a category change, it checks to see if the next category in the results corresponds to the next in the static list. If not, we know there is an empty category and output the “claim this spot” as needed.

    Ideally, this static list should be part of the DB so that all this ordering and correlating can be done with SQL. This will scale much better, but will take some doing to get it all coded properly. It appears possible to start simple and then scale up as your needs grow.

    One last tip, consider using WordPress dedicated functions to access the database. Not only are functions such as get_user_meta() more robust, but you can achieve esoteric queries just as well with $wpdb methods as you could with mysql_*() and you gain several benefits along the way, such as result caching.

    Thread Starter DRandy

    (@drandy)

    wow, that’s quit an answer – and I think alot to learn. But thanks. I appreciate it. Maybe I should approach it a different way. Something like presenting a list of categories. If the user puts in a zipcode and doesn’t find a listing for that zip, then they’ll know what they can register with.

    Moderator bcworkz

    (@bcworkz)

    Eeek! I didn’t realize how long the answer got, one thought led to another, and it just scrolled out of the input box and out of my mind. I appreciate you taking the time to read it all. There’s no easy answer to UI design issues. I’m afraid very few people see opportunity in the lack of information, so I’m not keen on your latest thought. You are free to do what you like of course, but you did ask for “any direction” ??

    It’s looking like some sort of category listing in a DB table is going to be a good approach in order to have flexibility in constructing queries, at least for a large scale implementation. One can hack together just about anything small scale and it will work OK.

    What about at the bottom of the normal businesses in zip area list there is simply a link or button “Categories in this zip available for listing” or similar? If the categories are in the DB, you could easily query for all categories not having businesses in this zip area.

    You should be able to either adapt the current category taxonomy or create a new custom taxonomy. By using a taxonomy, you can build queries using WP_Query, which most people find more accessible than raw mySQL queries. Another WP_Query advantage is pagination is handled semi-automatically.

    Hmmm, my input box scrub bar is growing smaller, as my answer is once again getting longer. Maybe I’m just unable to edit my thoughts. I hope your finding a least some of this helpful.

    Thread Starter DRandy

    (@drandy)

    Yes. I’m finding your replies very helpful. Gives me much to think about. Thanks for your “direction” and input. I really appreciate it.

    It all makes me wonder if what I’m doing is worth my time and if I’ll ever make any money with it. I’ve been working in WordPress for about 7 years, customizing and creating what I want it to do around ideas I have but to this day, I’ve never made any money with those ideas. I’ve never had any formal training in PHP and just when I’ve struggled for months with something that should probably take a few hours coding, I get a site and concept going only to find that there is no interest.

    Oh well. Thanks for your replies.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Please Help – Trouble with new plugin – List Group Info’ is closed to new replies.