Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

thommenl

PHP + SQL - Replace category ID with a name

Recommended Posts

I have script

<?php

require('includes/application_top.php');

# Output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');

# Create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

try {
# SQL for products with products extra images
$exportSQL = "SELECT c.categories_id, c.parent_id AS pi, cd.categories_name AS cn
FROM categories c
LEFT JOIN categories_description cd
ON c.categories_id = cd.categories_id
WHERE cd.language_id = 2";

$exportQuery = tep_db_query($exportSQL);

if (tep_db_num_rows($exportQuery) > 1) {
	while ($exportList = tep_db_fetch_array($exportQuery)) {
		// print_r($exportList).PHP_EOL;
		// var_dump($exportList);
		fputcsv($output, $exportList, ";");
	}
}
} catch (PDOException $e) {
    print "Error: " . $e->getMessage();
    die();
}

Now I need to add the function of replacing the parent's ID with the name of the category. I have tried many things but I am not familiar with osCommerce.

Please help.

 

Share this post


Link to post
Share on other sites

I'm not sure I understand what you are trying to do but $exportList will just be an array of each category with the two ID's and the name. If you want to change one of those you will need to edit the array before saving it. Something like

$exportList['pi'] = $exportList['cn'];
fputcsv($output, $exportList, ";");

 


Support Links:

For Hire: Contact me for anything you need help with for your shop: upgrading, hosting, repairs, code written, etc.

Get the latest versions of my addons

Recommended SEO Addons

Share this post


Link to post
Share on other sites

I had

while ($exportList = tep_db_fetch_array($exportQuery)) {
		while ($exportList1 = tep_db_fetch_array($exportQuery1)) {
			if ($exportList['pi'] == $exportList1['categories_id']) {
				$exportList['pi'] = $exportList1['cn'];
				// break;
			}
		}
		// print_r($exportList).PHP_EOL;
		// var_dump($exportList);
		fputcsv($output, $exportList, ";");
	}

but the code only worked for the first cycle of the outer loop.

Edited by thommenl

Share this post


Link to post
Share on other sites

So you are trying to get all of the categories? If so, use the tep_get_categories function. You can duplicate it and change what details it returns.


Support Links:

For Hire: Contact me for anything you need help with for your shop: upgrading, hosting, repairs, code written, etc.

Get the latest versions of my addons

Recommended SEO Addons

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×