1

I'm working on an e-commerce solution for one of my clients that will not involve any type of checkout functionality; but instead, the user will have the choice to submit a form that requests a quote. The user will also have the option to click on a product display, and then view a more detailed description about that product.

Here is a sample page that shows these options for each product:
http://dev.welcome2solutions.com/magnetfactory/store/products/category/awareness/

Although it's not active at the moment, I created a custom page for when the user clicks "VIEW PRODUCT DETAILS" - it will show a simple image along with some detailed info about the product. I need the custom page to somehow populate the product details of the product that was selected by the user.

What is the best way to call the product details on the product details page so that it is auto-populated? Can I do this without having to "GET" the information through a form submission and posting it into the new page?

5
  • 2
    what do you have against just passing the product id in the url ?
    – user557846
    Mar 25, 2012 at 20:08
  • I don't have any problems at all doing that, although each product url is dynamically created through the MarketPress Lite plugin. So maybe a more specific question could be "How do I create that product url ID dynamically?" In the plugin, they define the link as a variable up front. For example, $link = <?php get_permalink ?> or something similar.
    – Rob Myrick
    Mar 25, 2012 at 20:10
  • could I use php inside the url reference somehow to call that url dynamically?
    – Rob Myrick
    Mar 25, 2012 at 20:13
  • You might want to read the documentation of the software, you are using. They solved this in their demo, so there is a way: wp-ecommerce.org Also read en.wikipedia.org/wiki/Representational_state_transfer for a generally better aproach for creating URLs.
    – Basti
    Mar 25, 2012 at 20:13
  • with out knowing the specific software, only you can answer that.
    – user557846
    Mar 25, 2012 at 20:20

1 Answer 1

2

I need the custom page to somehow populate the product details of the product that was selected by the user

It's perfectly acceptable to build the URL to the product with the associated productId, so that you can call $_GET['id'] on the detail page. Once you have localized the productId on the detail page, you can issue a query to your products database to pull in the associated product data, via the productId. Obviously a better solution than attempting to pass in all product data via the url.

Example:

// Results page
//  You can build links similar to the following
//  This assumes you have a product object from which you can get id and name
<a href="/product/detail/<?php echo $product->getId() ?>"><?php echo $product->getName() ?></a>

// Detail page

// Localize id
$productId = $_REQUEST['id']; // I prefer to use $_REQUEST vs $_GET

// Create our database object (dbo)
$dsn = 'mysql:dbname=mydbname;host=127.0.0.1';
$user = 'myusername';
$pass = 'mypass';

try {
    $dbo = new PDO($dsn, $user, $pass);
} catch (Exception $e) {
    echo 'Unable to connect: ' . $e->getMessage();
}

// Construct your read query
$sql = "SELECT * FROM my_product_table WHERE id = :productId";

// Prepare query (avoids nasty things like sql injection by properly escaping 
//  passed in parameters (:productId)
$stmt = $dbo->prepare($sql);

// Bind your local variable to the sql statement
$stmt->bindValue(':productId', $productId, PDO::PARAM_INT);

// Execute query
$stmt->execute();

// Localize query results (PDO::FETCH_OBJ will turn $product into a stdObject)
$product = $stmt->fetch(PDO::FETCH_OBJECT);

// Now you have access to the product data as it is stored in your database
var_dump($product->name);
var_dump($product->category); 
....

I suggest you read up on PHP PDO (from which the above is based) for your database access. There are 100 ways to accomplish the same objective, but I think the above example should be enough to get you started in the right direction.

9
  • Mike, that sounds acceptable. So can you be more specific with an example? I understand the idea, but have no idea how to even get started. I don't need an exact answer, but any simple example you may think of. Also, couldn't I simple call the product url dynamically by inserting a PHP reference inside the url? Is it more complex than that?
    – Rob Myrick
    Mar 25, 2012 at 20:29
  • Added a basic example of how to access said data which resides in a database. Mar 25, 2012 at 20:45
  • Mike, that's great. One question....my products are stored as posts inside wordpress. I don't know what the object ID is? Is it the post id, or is it the title? Will those work for your example?
    – Rob Myrick
    Mar 25, 2012 at 21:07
  • Are you using an ecomm plugin for wordpress? Mar 25, 2012 at 21:25
  • yes I am. Here's where I'm at so far. In the plugin file I'm able to add ?post_id to the end of the url, but it's not calling the id at all. I'm probably doing this all wrong. You can see the url if you hover over the image on my product layout here dev.welcome2solutions.com/magnetfactory/store/products/category/….
    – Rob Myrick
    Mar 25, 2012 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.