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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

 

 

Link to comment
Share on other sites

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:

 

Link to comment
Share on other sites

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`

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.