How to get '?' and '=' in a url? - php

I'm new to Drupal and to web development. I'm very surprised I have not been able to find the answer to this with 2 hours of looking.
I'm using Drupal 7 and putting the below php into a tpl.php file.
The ? is printing as %3F and the = is printing as %3D.
<?php print l("link name", "relationship/".$user->uid."/request/1?destination=user/".$user->uid); ?>
What I need is: http://example.com/relationship/42/request/1?destination=user/42.

You're looking to add a query string to the URL, which means you need to pass the query string parameters as part of the $options argument to the l function.
In particular you need to pass the 'query' option inherited from the url function
l('link name', "relationship/{$user->uid}/request/1", array(
'query' => array(
'destination' => "user/{$user->uid}"
)
);

Related

How to return a variable operator like $option[];

Obviously, I can't do this, but is there some way to achieve what I am trying to? I only found can not do's online, but no potential workarounds.
Here is what I am trying to do.
Currently I get the following error... "Cannot use [] for reading"
For my theme, I have a framework and the fields from that framework are built using an array that I create.
It looks something like this (minus the 300+ lines of code that I actually use)...
$options[] =
array(
'title' => 'This Field Tab Title',
'name' => 'this-field-tab-slug',
'fields' =>
array(
// ----------------------------------------------------------------------
// This Field Option Name
// ----------------------------------------------------------------------
array(
'type' => 'this_field_type',
'id' => 'this_field_types_id',
),
// ----------------------------------------------------------------------
// This Field Option Name
// ----------------------------------------------------------------------
array(
'type' => 'this_field_type',
'id' => 'this_field_types_id',
),
// ----------------------------------------------------------------------
// This Field Option Name
// ----------------------------------------------------------------------
array(
'type' => 'this_field_type',
'id' => 'this_field_types_id',
),
),
);
I am running a grouped field type, so my output has many options/fields within this grouped field/area which can then be added again and again as many times as the user needs. Then I am repeating that whole process/code again but for other taxonomies of the user's site.
So for example, the whole process above applies to post types, categories, tags, archived, etc. etc. So instead of having thousands of lines of repetitive codes, I'm trying to create my own function and pass the variables to that function.
But for the function, I find I can't return $options[];
Here is a screenshot of what I mean by the grouped field that can be added as many times as the user needs.
And here's an example of the function I am trying to create!
public static function layout_settings_config($title_name = '', $title_slug = '', $title_id = '', $query = '') {
$title_name = 'Post Type';
$title_slug = 'post-type';
$title_id = 'post_type';
$query = 'post_types';
$options[] =
array(
// all the config array codes in here...
);
return $options ??? $options[]; doesn't work.
}
Is this possible to achieve what I am trying to a different way? I'm still a little new to creating my own functions and OOP, but nothing I find online for this specific issue with a workaround.
Thanks!
$options[] is not object but it is an operation like function.
You should return $options instead.
and, by the way, when you say $options[] = something. it actually insert something inside an array called $option. so effectively you have to access your options like this.
$option[0]->title.
So I suggest Instead of making it complex like this. simply say
$option = something.

Parse WHERE php

I using https://github.com/ParsePlatform/parse-php-sdk.
I want to send notify 2 other devices.
But it's only send notify for device token is 'abcdef'.
How to send notify 2 other devices?
Thanks all,
$query = ParseInstallation::query();
$query->equalTo('deviceToken', 'xxxxx');
$query->equalTo('deviceToken', 'abcdef');
$data = [
'data' => ['alert' => 'Hello, this is a test'],
'where' => $query,
];
ParsePush::send(
$data
);
I believe your dual equalTo queries are overwriting each other. From what I understand, equalTo matches a single value. You should be able to see this behavior by inspecting a vardump of your $query after each equalTo call.
You should instead use containedIn (http://parseplatform.github.io/parse-php-sdk/classes/Parse.ParseQuery.html#method_containedIn) and pass in an array of values that you'd like the query to match. E.g., $query->containedIn('deviceToken', ['abcdef', 'xxxxx']).
Note: I don't have a parse PHP project handy so the above code isn't tested, but the general logic should work.

How to parse function name with arguments in PHP?

I am building a module for my own PHP framework, so my question is very specific and special. It's difficult to explain my question so I will go ahead and show it on code below.
I have a little piece of PHP in a $code variable, it looks like this:
$code = "___echo(TODAY_IS, date('j.n.Y', time()), time());";
What I need is to parse this $code variable and I want to get this result:
$result = array(
'function_name' => "___echo",
'arguments' => array(
0 => "TODAY_IS",
1 => "date('j.n.Y', time())",
2 => "time()"
)
);
I am thinking and I have tried using some regex, but neither worked sufficiently well. I also tried using Tokenizer, however I wasn't successful either.
Thanks for any hints or help in advance.
Here is a shot using PHP-Parser. It's likely going to be more useful than tokenizer or some freaky regex.
Example:
$code = "___echo(TODAY_IS, date('j.n.Y', time()), time());";
$parser = new PhpParser\Parser(new PhpParser\Lexer);
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
$statements = $parser->parse("<?php $code");
$result['function_name'] = $statements[0]->name->toString();
foreach ($statements[0]->args as $arg) {
$result['arguments'][] = $prettyPrinter->prettyPrint(array($arg));
}
var_export($result);
Output:
array (
'function_name' => '___echo',
'arguments' =>
array (
0 => 'TODAY_IS',
1 => 'date(\'j.n.Y\', time())',
2 => 'time()',
),
)
token_get_all() function is what you need here:
token_get_all("<?php ___echo(TODAY_IS, date('j.n.Y', time()), time());")
This returns a list of tokens parsed from the given string. See the tokens documentation for recognizing the items of the list.
In my opinion, tokenizer-based solution should be preferred over any regular expressions based on whatever is written in the PHP manual regarding syntax.

Return Different PHP Array Values Based on A User ID-Related Constant

I am working with an array of tokens for an HTML template. Two of them ('{SYS_MENU}' and '{SUB_MENU}') are used to generate control buttons for the web application. Right now the buttons show up on the login page before the user's credential's are validated, and I need to change the code so that the buttons are hidden until after users login and reach the main menu. When someone types the http: address into their browser and arrives at the login page the system starts a session for them in the MySQL sessions table with USER_ID = 0. After they login the USER_ID changes to whatever number was assigned to them at initial registration (Example: USER_ID = 54), and after they logout at the end of the session back to 0. Tying this constant to the buttons seems like the best solution and I have found it to work in the past under similar circumstances.
Here is the original array:
$template_vars = array(
'{LANG_DIR}' => $lang_text_dir,
'{TITLE}' => theme_page_title($section),
'{CHARSET}' => $charset,
'{META}' => $meta,
'{GAL_NAME}' => $CONFIG['gallery_name'],
'{GAL_DESCRIPTION}' => $CONFIG['gallery_description'],
'{SYS_MENU}' => theme_main_menu('sys_menu'),
'{SUB_MENU}' => theme_main_menu('sub_menu'),
'{ADMIN_MENU}' => theme_admin_mode_menu(),
'{CUSTOM_HEADER}' => $custom_header,
'{JAVASCRIPT}' => theme_javascript_head(),
'{MESSAGE_BLOCK}' => theme_display_message_block(),
);
The first thing I did was to work with the references directly in the HTML template. I saw an example on w3schools that made it look like you could just type a PHP script into HTML and have it resolve. That didn't do anything except echo a bunch of text randomly into the page. I then found another citation that said you had to activate the PHP with an .HTACCESS entry before it would work directly in HTML. But that didn't close the deal either.
I know that changing '{SYS_MENU}' and '{SUB_MENU}' values in the array to => "", produces the results that I want (I.E. make the menu buttons disappear). So my next thought was I'll create an IF statement that returns two versions of the array based on circumstances, something like:
if(USER_ID != 0)
{
return $template_vars = //FIRST VERSION OF ARRAY WITH FULL VALUES//
}
else
{
return $template_vars = //SECOND VERSION OF ARRAY WITH ONLY => ""//
}
But all that did was cause the application load to terminate at a white screen with no error feedback.
My most recent attempt came from something I read here on Stack Overflow. I know that you cannot put IF statements into an array. But the article at this link described a workaround:
If statement within an array declaration ...is that possible?
So I rewrote the array as follows:
template_vars = array(
'{LANG_DIR}' => $lang_text_dir,
'{TITLE}' => theme_page_title($section),
'{CHARSET}' => $charset,
'{META}' => $meta,
'{GAL_NAME}' => $CONFIG['gallery_name'],
'{GAL_DESCRIPTION}' => $CONFIG['gallery_description'],
'{SYS_MENU}' => ('USER_ID != 0' ? theme_main_menu('sys_menu') : ""),
'{SUB_MENU}' => ('USER_ID != 0' ? theme_main_menu('sub_menu') : ""),
'{ADMIN_MENU}' => theme_admin_mode_menu(),
'{CUSTOM_HEADER}' => $custom_header,
'{JAVASCRIPT}' => theme_javascript_head(),
'{MESSAGE_BLOCK}' => theme_display_message_block(),
);
But that seems to have no effect at all. The application doesn't crash but the buttons are static whether you are logged in or logged out.
My question is: What am I missing? I can see that this is possible. But I've been trying things for a day and a half and just seem to be dancing around the solution. Your thoughts would be greatly appreciated.
The problem here is that you are calling return. With a global include file like this there is not context to return to so the application terminates. What you want to do is just assign the variables.
if(USER_ID != 0)
{
$template_vars = //FIRST VERSION OF ARRAY WITH FULL VALUES//
}
else
{
$template_vars = //SECOND VERSION OF ARRAY WITH ONLY => ""//
}

ZF2 Zend\Db Insert/Update Using Mysql Expression (Zend\Db\Sql\Expression?)

Is there any way to include MySQL expressions like NOW() in the current build of ZF2 (2.0.0beta4) through Zend\Db and/or TableGateway insert()/update() statements?
Here is a related post on the mailing list, though it hasn't been answered: http://zend-framework-community.634137.n4.nabble.com/Zend-Db-Expr-and-Transactions-in-ZF2-Db-td4472944.html
It appears that ZF1 used something like:
$data = array(
'update_time' => new \Zend\Db\Expr("NOW()")
);
And after looking through Zend\Db\Sql\Expression I tried:
$data = array(
'update_time' => new \Zend\Db\Sql\Expression("NOW()"),
);
But get the following error:
Catchable fatal error: Object of class Zend\Db\Sql\Expression could not be converted to string in /var/www/vendor/ZF2/library/Zend/Db/Adapter/Driver/Pdo/Statement.php on line 256
As recommended here: http://zend-framework-community.634137.n4.nabble.com/ZF2-beta3-Zend-Db-Sql-Insert-new-Expression-now-td4536197.html I'm currently just setting the date with PHP code, but I'd rather use the MySQL server as the single source of truth for date/times.
$data = array(
'update_time' => date('Y-m-d H:i:s'),
);
Thanks, I appreciate any input!
Zend_Db_Sql_Expression works for me now.
$data = array(
'update_time' => new \Zend\Db\Sql\Expression("NOW()"),
);
I am not sure if they fixed this in the latest updates, but it was a know bug and even if they said it was fixed, for me it still didn't work after the fix.
It works for me too. Maybe, your problem was like mine. The string error conversion was because the parameter that I passed to method was elements of array and not the array. Check this and have sure that you are passing $object->method($array).
You can try:
\Zend\Db\Sql\Predicate\Literal();
//or
\Zend\Db\Sql\Literal();
------------------------------------
$data = array(
'update_time' => new \Zend\Db\Sql\Literal("NOW()"),
);
https://framework.zend.com/manual/2.2/en/modules/zend.db.sql.html#literal-literal

Categories