Jump to content

Pulling multiple categories into one table using "get_"


jonw118

Recommended Posts

Hi there,

 

I have a site that has several different pages which display products from each category on the designated page.

 

What I am looking to do is create one page that also lists ALL the products.

 

Right now, in the category specific page it has this is in the code:

$data = get_portfolio_industry('Building Products');
$offset = $_GET['offset'];
if($offset == ''){
$offset = 0;
}
function get_industries($rec_id){
$sql = "SELECT * FROM `".TBL_PORTFOLIO_INDUSTRY."` WHERE `portfolio_id` = '$rec_id'";
$data = SelectMultiRecords($sql);
return $data;
}

 

And this is the from the functions file:

function get_portfolio_industry($industry){

	$sql = "SELECT * FROM `".TBL_PORTFOLIO."` WHERE id IN (SELECT `portfolio_id` FROM `".TBL_PORTFOLIO_INDUSTRY."` WHERE `industry` = '$industry') ORDER BY `id`";
$data=SelectMultiRecords($sql);
return $data;
}

 

My hope was I would simple combine the products like this:

$data = get_portfolio_industry('Building Products,Building Services,General Products');

 

...but unfortunately it doesn't combine them. Any thoughts? Any help would be much appreciated.

 

The function is only accepting one industry, so do it multiple times and merge them:

 

$data = array_merge(
   get_portfolio_industry('Building Products'),
   get_portfolio_industry('Building Services'),
   get_portfolio_industry('General Products')
);

 

Or you can modify the get_portfolio_industry() to accept multiples.

Awesome!!! Thanks so much for the help.

 

Now, here is a little tricky question since I can now get them all in a table. In the function it calls to order them by ID. Is there anyway it could order them all alpha versus the "ORDER BY 'id'" or is that not possible because of this statement in the function (which is needed for the category specific pages):

 

   $sql = "SELECT * FROM `".TBL_PORTFOLIO."` WHERE id IN (SELECT `portfolio_id` FROM `".TBL_PORTFOLIO_INDUSTRY."` WHERE `industry` = '$industry') ORDER BY `id`";
   $data=SelectMultiRecords($sql);
   return $data;
}

 

 

Quick and dirty function fix:

 

  $sql = "SELECT * FROM `".TBL_PORTFOLIO."` WHERE id IN (SELECT `portfolio_id` FROM `".TBL_PORTFOLIO_INDUSTRY."` WHERE `industry` IN ($industry) ) ORDER BY `id`";
   $data=SelectMultiRecords($sql);
   return $data;

 

And call it like this:

 

$data = get_portfolio_industry("'Building Products','Building Services','General Products'");
//or call one like this:
$data = get_portfolio_industry("'Building Products'");

 

There are better ways, but these are easy to understand:

 

Awesome!!! Thanks so much for the help.

 

Now, here is a little tricky question since I can now get them all in a table. In the function it calls to order them by ID. Is there anyway it could order them all alpha versus the "ORDER BY 'id'" or is that not possible because of this statement in the function (which is needed for the category specific pages):

 

   $sql = "SELECT * FROM `".TBL_PORTFOLIO."` WHERE id IN (SELECT `portfolio_id` FROM `".TBL_PORTFOLIO_INDUSTRY."` WHERE `industry` = '$industry') ORDER BY `id`";
   $data=SelectMultiRecords($sql);
   return $data;
}

 

Try it.  Change ORDER BY `id` to ORDER BY `something_else`

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.