[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

attaching categories to a product

Posted on 2012-08-27
7
Medium Priority
?
513 Views
Last Modified: 2012-09-06
http://support.google.com/merchants/bin/answer.py?hl=en&answer=160081

want to create a form that adds categories to products

product 1 could be
electronics

product 2
electronics > audio > audio components




how could I design this process

not sure if google merchant has an api

my best idea is
form1 inserting categories to categories table
form2 updating products table setting categories column
0
Comment
Question by:rgb192
  • 3
  • 3
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38340677
You could have a products table with a categories column, but that might be better served with three tables.  Products would be a table, Categories would be a table and Products_Categories would be a junction table establishing a relationship between the other tables.  Products_Categories Would have two columns with the keys of a row in Products and a row in Categories.  In my experience, this third table makes the queries easier to get right.

At the top level of this design, you probably want to decide how many layers of hierarchy you need.  I'm not sure if Google builds that in or if you have to make it up.  I think Amazon builds it in, and you choose where your products fit in their giant online department store.
0
 

Author Comment

by:rgb192
ID: 38340819
>>
Products_Categories Would have two columns with the keys of a row in Products and a row in Categories.

could you give example



and how could I do levels
a product may only have one level
electronics
or it could have 3
electronics > audio > audio components

or maybe 5
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 38340902
I only have one example of the junction table (written for another question here at EE).
http://www.laprbass.com/RAY_temp_redscrapbook.php

On the question of how many levels, etc., that is beyond anything we can answer in a written Q/A dialog over the internet.  It's more like a study in data base design and administration, probably at the level of a college 300 course.  You might want to consult a DBA for some guidance.

Here's the junction table example.

<?php // RAY_temp_redscrapbook.php
error_reporting(E_ALL);
echo "<pre>";

// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES


// CREATE AND LOAD THE students
$sql = "CREATE TEMPORARY TABLE students (
        s_id INT            NOT NULL AUTO_INCREMENT,
        student VARCHAR(24) NOT NULL DEFAULT '',
        PRIMARY KEY(s_id)  )";
$res = mysql_query($sql) or die( "FAIL: $sql <br/>" . mysql_error() );

// ESCAPING A DATA FIELD FOR USE IN MYSQL QUERIES
// MAN PAGE: http://php.net/manual/en/function.mysql-real-escape-string.php
// CREATE AN ARRAY OF NAMES
$names[] = mysql_real_escape_string("Williams");
$names[] = mysql_real_escape_string("Paseur");
$names[] = mysql_real_escape_string("O'Reilly");

// MAKING AN INSERT QUERY, USING THE ESCAPED STRINGS AND TESTING THE RESULTS
foreach ($names as $name)
{
    $sql = "INSERT INTO students ( student ) VALUES ( '$name' )";
    $res = mysql_query($sql) or die( "FAIL: $sql <br/>" . mysql_error() );
}
// IF WE GET THIS FAR, THE INSERT QUERIES ALL SUCCEEDED


// CREATE AND LOAD THE modules
$sql = "CREATE TEMPORARY TABLE modules (
        m_id INT           NOT NULL AUTO_INCREMENT,
        module VARCHAR(24) NOT NULL DEFAULT '',
        PRIMARY KEY(m_id)  )";
$res = mysql_query($sql);
unset($names);
$names[] = mysql_real_escape_string("HTML");
$names[] = mysql_real_escape_string("PHP");
$names[] = mysql_real_escape_string("MySQL");

// MAKING AN INSERT QUERY, USING THE ESCAPED STRINGS AND TESTING THE RESULTS
foreach ($names as $name)
{
    $sql = "INSERT INTO modules ( module ) VALUES ( '$name' )";
    $res = mysql_query($sql) or die( "FAIL: $sql <br/>" . mysql_error() );
}
// IF WE GET THIS FAR, THE INSERT QUERIES ALL SUCCEEDED


// CREATE AND LOAD THE JUNCTION TABLE
$sql = "CREATE TEMPORARY TABLE modules_and_students (
        m_id INT         NOT NULL DEFAULT 0,
        s_id INT         NOT NULL DEFAULT 0 )";
$res = mysql_query($sql) or die( "FAIL: $sql <br/>" . mysql_error() );

// WILLIAMS + MySQL
mysql_query( "INSERT INTO modules_and_students (s_id, m_id) VALUES ( 1, 3)" ) or die(mysql_error());

// PASEUR + PHP + MySQL
mysql_query( "INSERT INTO modules_and_students (s_id, m_id) VALUES ( 2, 2)" ) or die(mysql_error());
mysql_query( "INSERT INTO modules_and_students (s_id, m_id) VALUES ( 2, 3)" ) or die(mysql_error());


// SHOW THE TABLES
$sql = "SELECT * FROM students";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res)) { print_r($row); }
$sql = "SELECT * FROM modules";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res)) { print_r($row); }
$sql = "SELECT * FROM modules_and_students";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res)) { print_r($row); }


// PASEUR IS s_id=2 WHAT MODULES HAS PASEUR TAKEN?
$sql
=
"
SELECT
student, module
FROM students, modules, modules_and_students
WHERE students.s_id = 2
AND modules_and_students.s_id = students.s_id
AND modules_and_students.m_id = modules.m_id
"
;
$res = mysql_query($sql) or die( "FAIL: $sql <br/>" . mysql_error() );
while ($row = mysql_fetch_assoc($res)) { var_dump($row); }


// MySQL IS m_id=3 WHAT STUDENTS HAVE TAKEN MySQL?
$sql
=
"
SELECT
student, module
FROM students, modules, modules_and_students
WHERE modules.m_id = 3
AND modules_and_students.s_id = students.s_id
AND modules_and_students.m_id = modules.m_id
"
;
$res = mysql_query($sql) or die( "FAIL: $sql <br/>" . mysql_error() );
while ($row = mysql_fetch_assoc($res)) { var_dump($row); }

Open in new window

HTH, ~Ray
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 1000 total points
ID: 38341034
@Ray - the Google Merchant categories are a fixed set of choices. There's not much in the way of flexibility there. :)

The choices can be seen here:
http://support.google.com/merchants/bin/answer.py?hl=en&answer=1705911

@rgb192 - there is an API for Google Merchant. It's the Google Shopper API, actually, but it can be used to do what you're talking about.

The list of available attributes for a product:
https://developers.google.com/shopping-content/getting-started/requirements-products#attributes_xml

...includes the "google product category" attribute for a given product. The value looks like this:

<scp:google_product_category> Clothing &amp; Accessories &gt; Clothing &gt; Outerwear &gt; Sweaters </scp:google_product_category>

Open in new window


Now, there's a lot of work to use the APIs. You have to get a key for the API and login with their OAuth system first. Here's the basic script for just getting a valid session:

http://code.google.com/p/google-api-php-client/

Here is the documentation section on how to insert, update, and delete products using the API:

https://developers.google.com/shopping-content/getting-started/usingapi-products#insertupdatedelete

So yeah, you can do this, but unless you have thousands of products to update all the time, it's probably better to just manage the products via the data feed and upload that once in a while:

http://support.google.com/merchants/bin/answer.py?hl=en&answer=188478
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38341072
@Jonathan: Would you consider writing an article for EE on the Google Merchant?
0
 

Author Comment

by:rgb192
ID: 38344037
0
 

Author Closing Comment

by:rgb192
ID: 38375056
thanks

I should learn the api
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month19 days, 21 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question