/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Substitute in a variable

I’m a PHP amateur, so please bear with me. ?

[code=php]<?php
if (get_category(‘1’)->category_count == 1) {
$blah = “‘numberposts=3&cat=-1′”;
}
elseif (get_category(‘1’)->category_count > 1) {
$blah = “‘numberposts=2&cat=-17”;
}
else {
$blah = “‘numberposts=4&cat=-1′”;
}
global $post;
$myposts = get_posts($blah);
foreach($myposts as $post) :
setup_postdata($post);
?>

———-stuff to display each post———-

<?php endforeach; ?>[/code]

Some may recognise this as coming from a WordPress template, but I think this is primarily a PHP-related question. Essentially what I’m trying to do is to modify this line depending on the result of the if/elseif/else statement:

[COLOR=”DarkRed”]$myposts = get_posts(‘numberposts=[X]&cat=-1’)[/COLOR]

So if the first condition holds (category count = 1), then I want to have:

[COLOR=”DarkRed”]$myposts = get_posts(‘numberposts=3&cat=-1’)[/COLOR]

And so on. Based on testing, the above block of code isn’t working: the $blah isn’t being substituted into the $myposts line. How would I go about making this work? This may well be some embarrassing mistake, so apologies in advance if it is. ?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 06.2010 — Try only using one set of quotes:
[code=php]
$blah = 'numberposts=3&cat=-1';
[/code]

Otherwise you're passing the inner set of quotes as part of the literal string value to the functions that uses $blah.
Copy linkTweet thisAlerts:
@Cyril_WashbrookauthorAug 06.2010 — Ah, that solves it - thanks! As predicted, I have indeed committed an embarrassing mistake. ?
×

Success!

Help @Cyril_Washbrook spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...