Results 1 to 10 of 13

Hybrid View

  1. #1
    Join Date
    Jan 2008
    Posts
    67
    Plugin Contributions
    0

    Default DB query question

    I would like to query the db with an onclick using JQUERY/AJAX, then return the results without a page refresh. The JQUERY part is not where the problem lies.

    I'm a newb when it comes to PHP. I've been looking over the product info main_template_vars.php file to see if I can figure out how to query the db. I'm not having any luck so far, there is a lot of stuff in there I probably don't need that is throwing me off.

    Here's what I need to happen, user clicks button, button onclick submits Product ID to AJAX script. AJAX script then calls the file product_info_query.php(this would be where the DB query lives). The php file would then query the DB and return results(Product name, Product Image, Product description, product price) for that particular product ID. What's the trick to coding this query into a php file for the zen cart DB?

  2. #2
    Join Date
    May 2010
    Location
    WA State
    Posts
    1,678
    Plugin Contributions
    3

    Default Re: DB query question

    Maybe I'm missing something, but doesn't Zen Cart already do this?

  3. #3
    Join Date
    Jan 2008
    Posts
    67
    Plugin Contributions
    0

    Default Re: DB query question

    No it doesn't, unless I've missed something, I'm building a custom interface that will create itself somewhat dynamically. The only thing left is the DB query. The plan is to return product data without a page refresh so you never leave the page.

  4. #4
    Join Date
    Jul 2005
    Location
    Upstate NY
    Posts
    22,010
    Plugin Contributions
    25

    Default Re: DB query question

    So you want to have a bunch of buttons that contain only product id info, and load the one product clicked? You can do that, I suppose, but Zen Cart loads all of the product basic info, not just the id, automatically for the product listing page. Unless you have a huge number of them, or they aren't actually defined as to where the button points, you could apply your javascript to showing only the called-for set of already-loaded products.

  5. #5
    Join Date
    Jan 2008
    Posts
    67
    Plugin Contributions
    0

    Default Re: DB query question

    I only have a few products so I created my own index page. Right now it is hard coded, but I want to make it easier to update by making it dynamic. For me the easiest way is via Javascript because I understand it. PHP not so much. I've spent a couple of days analyzing the code. I have done a basic proof of concept with a standard db query:
    Code:
    $db = mysql_select_db(DB) or  die('Could not connect to database !<br />Please contact the site\'s administrator.');
    
    $query = mysql_query(" SELECT * FROM products_description WHERE products_id='".$_POST['value']."' ");
    With a few other bits for formatting it returned basic results. However what I want to do is rework a copy of the main_template_vars.php file used with the product info page so it will execute on a $_POST. This would be easy enough if I could actually figure out how zen cart is querying the DB. It looks as though Zen cart would normally execute this page as it loaded the header for the product info page using the $_GET command with the product ID

    Code:
    $sql = "select count(*) as total
              from " . TABLE_PRODUCTS . " p, " .
                       TABLE_PRODUCTS_DESCRIPTION . " pd
              where    p.products_status = '1'
              and      p.products_id = '" . (int)$_GET['products_id'] . "'
              and      pd.products_id = p.products_id
              and      pd.language_id = '" . (int)$_SESSION['languages_id'] . "'";
    
    
      $res = $db->Execute($sql);
    I understand this section for the most part, but where are the $res and $db variable being defined in zen cart, where is the mysql connect, select and query that define those variables? There seem to be multiple files needed the execute upon loading the product info page that I must be missing in order to get this script running. I need to figure out if there is a way to execute it as a stand alone file when the $_GET is replaced by a $POST variable?

  6. #6
    Join Date
    Jan 2004
    Posts
    66,446
    Plugin Contributions
    81

    Default Re: DB query question

    Apparently your unique requirements are demanding that you write your own custom PHP-based ajax handler.
    The $db object is built by calling application_top.php as part of normal bootstrapping. Anything you do outside of that means you'll need to write your own complete framework instead of using the Zen Cart core code framework which spawns from index.php and application_top. If you skip calling application_top then you also bypass all the security protections and other safeties built into the system.
    There are numerous existing addons which have built their own ajax handlers. You might consider one of those instead of writing your own from the ground up.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

 

 

Similar Threads

  1. Rewards Points - Global Query/Point Addition Question
    By Youngblood in forum General Questions
    Replies: 0
    Last Post: 26 Jul 2011, 04:27 PM
  2. Query Question
    By fotofx in forum General Questions
    Replies: 0
    Last Post: 5 Oct 2010, 02:51 AM
  3. Basic query question with MoveNext
    By delia in forum Contribution-Writing Guidelines
    Replies: 2
    Last Post: 12 Jan 2010, 05:20 PM
  4. also purchased query question
    By ryanf in forum General Questions
    Replies: 14
    Last Post: 15 Aug 2009, 07:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg