Link to home
Start Free TrialLog in
Avatar of techbots
techbotsFlag for United States of America

asked on

Using a variable in "category_and"

If I use...

query_posts( array( 'category__and' => array( 10,55,56 )));

Open in new window


...I get the expected results.

However I need to use a variable instead:

$catarray = 'array( 10,55,56 )' 
query_posts( array( 'category__and' => $catarray));

Open in new window


When I do that,I don't get any results.

My category IDs (which will populate $catarray) come in from a form and I want only the post that fit ALL of the categories.

Can anyone please help?!
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

The code seems to be correct. How to process query results? That is: what's the line 3?
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sure! I've been blind.
Avatar of techbots

ASKER

Dave, thank you for heading me in the right direction!

I pulled the quotations marks out and though that works with hardcoded values, I still have an issue with building the array as integers rather than a string. Since I have to add the commas when I pull in the category IDs, I am not sure how to do this.

This is what I am using:

foreach ($NV_archivecat as $catlist)
{ 
	$cats = $cats.','.
	(int) $catlist;
}

$catstrimmed = lTrim($cats,',');
$catarray = array($catstimmed);

Open in new window


And that doesn't work.

How do I keep this as integers separated by commas, rather than a string?
First, you have to use rTrim to delete the last comma.
Second, usually you can omit the typecasting because Php is smart enough to know when it's dealing with a string or a number.
Thirs, you can rewrite your loop this more concise way:

foreach ($NV_archivecat as $catlist)
{ 
	$.cats = $catlist.',';
}

Open in new window


Maybe it didn't work because of the last comma left there...
I was actually building it this way:

				if( !empty($NV_archivecat) )
				{ 
					// Get category ID Array
					$cats = '';
					
					foreach ($NV_archivecat as $catlist)
					{ 
					$cats = $cats.','.$catlist;
					}
				}
				
				$catstrimmed = lTrim($cats,',');
				$catarray = array($catstimmed);

Open in new window


So there is not extra comma.

I do an echo of $catstrimed & get 10,55,56 (as expected)
if I do an echo of $catsarray though, I just get Array

Not sure what that means, but I don't get any results - so the problem must lie somewhere between the two.
To print an array you can't use echo, you must use print_r or var_dump.
If you want a more readable output do this

echo "<pre>";
var_dump($catsarray);
echo "</pre>";

Open in new window

Thanks! Ok - this is interesting! I think we are finally seeing the problem. I did the var_dump and got this:

array(1) {
  [0]=>
  string(5) "55,56"
}

prinr_r got me this:

Array
(
    [0] => 55,56
)

So, am I reading this right that my array only has one item - the string containing 55,56 -not two items 55 & 56? If so, no wonder it isn't working properly. Of course, I still don't know how to build the array as a variable.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Holy smokes, Batman, that worked!!!

I can't tell you how happy I am! I would have had to hand code over 300 pages and now I can just pull in the categories from a form. Thank you, thank you, thank you!
Marco, you were a little quicker than I was.  Here's my working version... which look an awful lot like Marco's.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Array Cat</title>
</head>
<body>
<h1>Array Cat</h1>
<?php 
$NV_archivecat = array( '12','23','34');
$catsarray = array();
foreach ($NV_archivecat as $catlist)
{ 
	$catsarray[] = (int) $catlist;
}
echo "<pre>";
var_dump($catsarray);
$query = array( 'category__and' => $catsarray);
echo "\r\n";
var_dump($query);
echo "</pre>";

 ?>
</body>
</html>

Open in new window

Thanks also to Dave Baldwin for pointing out that problem initially was the array was a string, but the majority of the points go to Marco for helping me find a working solution.
@Dave, lol, I wrote less than you!

@techbots: thank you for points. I'm glad to have helped you to avoid such a boring task!
Batman? Mmmhh; where is my Lamborghini?
Marco, I often try to write self-contained examples as much to check my own assumptions as anything else.  But I am unlikely to write more than a page unless I already did it for another project.
I was only joking, Dave. What I meant was that my quickness was lazyness. It was not a criticism or a way to say you made an error: the ciritic was to me self! :-)
Guys like you are the reason I pay for an ongoing subscription to Expert Exchange even though I might only have a question once every few months. I love that I can always count on getting really helpful, timely, friendly solutions!
Thank you, @techbots, glad to help.