PHP help
- Started
- Last post
- 13 Responses
- MrDaro
How do you use a variable as an array parameter?
eg.
$arraylist = user inputted text; (
$my_array = array($arraylist);If i try to use $my_array that way it wont work, however if I use:
$my_array = array(user inputted text);
it works.Any ideas?
- acescence0
implode or str_split. what is the text, comma delimited string?
- section_0140
What doesn't work exactly? Is the user inputted data an array? If it is an array, you are working with a multi-dimensional array and would to access it like:
echo $my_array[value][value];
Just from looking at the code, other than the half parenthesis at the end of the variable, there's no reason it shouldn't work.
- MrDaro0
yeah, $arraylist is a comma delimited list of numbers that the user inputs, it can be one or more numbers.
what doesn't work is is a function in which I use the variable $my_array.
$my_array = array($arraylist); does not work in my function
if I use the actual text that a use might input makes the function work. eg.
$my_array = array(2,3,9);
- acescence0
$arraylist = "user, inputted, text";
$my_array = explode(',',$arraylist);
- MrDaro0
Not quite yet.
This is the scenario:I have a form which asks the user for one or more category ID's separated by comma. Thus the user will enter 1,2,3 without quotes. The value is stored in a variable called $usercategories
If I use
<?php wp_list_categories('include='.$usercategories.'&title_li=' ); ?>
It works fine, I'll get a list of only those categories. However when I try to use the variable in my single.phpif (in_category (array($usercategories) ) { ...my loop... }
it does not recognize the variable, I try to manually enter the value just to check and it works.if (in_category (array(1,2,3) ) { ...my loop... }
if I echo the variable it outputs the list too eg 1,2,3
I just don't understand why the variable it's not working when used as a parameter for a function. Any ideas?
- section_0140
Sorry, but I don't work with wordpress, so I'm not certain how those functions work. However, as acescence suggested, use the explode function to reveal the comma seperated values stored in $usercategories
- maybe wp_list_categories has explode() built in to that functionsection_014
- acescence0
$usercategories = explode(',',$usercategories);
if (in_category ($usercategories) { ...my loop... }
- MrDaro0
Not working still
If i use acescence's code
$usercategories = explode(',',$usercategories);
if (in_category ($usercategories) { ...my loop... }and I echo $usercategories i get: Array instead of the list of numbers.
I'm guessing it has to do with the fact that the inputted text is not sorrounded by quotes. Also, what if the user enters only one number, will the explode method still work?
- acescence0
if you echo an array, you'll just get Array, so that's expected behaviour. if you want to see the contents of an array you need to do something like print_r($myArray) or var_dump($myArray)
if it's just one number, your array will contain just one element.
not sure where the error is in this case, i'd have to see all of the template code.
- section_0140
Generally if statements don't contain multiple values. Unless of course you are doing it like:
if (value1 && value2 && value3 ) {}
It seems to me that you should be looping through the $usercategories array and have your other loop nested in that loop. Such as:
foreach ($usercategoies as $value)
{
// other loop here
}Just an idea. It's hard to diagnose with a few lines. Maybe post up the whole script somewhere like snipplr
- MrDaro0
this is my entire code
<?php get_header(); ?>
<?php$cat_array = explode (',',$usercategories);
if (in_category(array($cat_array)) ) {
include(TEMPLATEPATH."/portfolio_single.php");
} else {
?><div id="main"><!-- Begin Main -->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="pagetitle"><h1><?php the_title(); ?></h1></div>
<div class="entry">
<div class="entry_content">
<?php echo $usercategories;?><?php the_content(); ?>
<div class="entry commententry">
<?php comments_template(); ?>
</div>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p><?php endif; ?>
</div><!-- END Entry Content -->
</div><!-- END Entry --></div><!-- End Main -->
<div class="clear"> </div>
<?php get_footer(); }?>
- MrDaro0
Crap, that's it! thanks acescence, I owe you one