0

I'm trying to modify a link array in php to add a variable at the end of a link. I thought this was a very easy thing to do, but I keep erroring out when I do it. I'm not sure if I am missing syntax or if it is not possible to do it the way I am doing it.

Here is the array without any modifications and it works fine

$links[]=array(
"url"=>'?p=worksheet', // this is one way I tried, I also added ''
'name'=>'Worksheet',   // This is where the name I want displayed goes
'order'=>999999,
);
}

The variable I am trying to add is $cust_id

This is how I am trying to add it:

$links[]=array(
"url"=>'?p=worksheet'<?php echo $cust_id ;?>, // this is one way I tried, I also added ''
'name'=>'Worksheet',   
'order'=>999999,
);
}

4 Answers 4

2

echo does output at the moment you call it. You're trying to concatenate two strings. And you also cannot embed PHP code within PHP code. Try

'url' => '?p=worksheet' . $cust_id,
                       ^^^^^^^^^^^

instead.

Sign up to request clarification or add additional context in comments.

4 Comments

sorry, should've been a , at the end, not ;.
I thought you meant that so I tried it both ways. It omits the variable altogether when I do that
Not possible. The only possibility is that $cust_id is not properly set. THis would generate '?p=worksheet42', if $cust_id was 42.
I thought maybe it was not set, I will try to test it out by just adding something in the page to see if it is set.
1

Use a concatenation operator: (of your choice):

$links = array(
"url"=>'?p=worksheet'.$cust_id, 
'name'=>'Worksheet{$cust_id}',   
'doubleQuotes'= "Make variables render $cust_id",
'finally' => $pre-tailored-variable;
);

where the last one would be have to made ahead of time:

$pre-tailored-variable = "String of some kind with value:".$value;

2 Comments

The way I am doing it, I think maybe my variable did not set correctly, is that what you mean by your answer?
The variable, or the array? You don't need the square brackets for defining an associative array --- but if $cust_id is set, then any of the above methods will suffice for adding its value into the array--
0

You can't inline functions like this in PHP...

$links[0]['url'] = '?p=worksheet' . $cust_id;

Might be what you're after.

Comments

0
$links[]=array(
 "url"=>'?p=worksheet'.$cust_id,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.