Last active
August 18, 2017 12:39
-
-
Save javedbaloch4/b71fc203c391516c1874683b5851b673 to your computer and use it in GitHub Desktop.
A great use of php Implode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* By Muhammad Javed Baloch | |
** You can process data via $_POST or $_GET | |
** You could add secuirty functionalties suchas Escaping | |
*/ | |
$data = array( | |
'name' => 'Javed', | |
'email' => '[email protected]', | |
'message' => 'Hello Javed, How are you?', | |
'telephone' => '0340 24176--', | |
'postCode' => 'xxxx', | |
'cnic' => 'xxxx-xxxx' | |
); | |
$sql_fields = '`'. implode('`, `', array_keys($data)). '`'; | |
$sql_values = '\''.implode('\', \'', $data). '\''; | |
$query = "INSERT INTO `users` ($sql_fields) VALUES ($sql_values)"; | |
echo $query; | |
?> |
Right good idea, Thank you for contribution.
It has been updated.
This can be interesting too.
/**
* Wrap array items into html elements
*
* @param array $items
* @param string $tag
* @return string
*/
function wrap($items, $tag = 'li')
{
return sprintf('<%1$s>%2$s</%1$s>', $tag, implode("</$tag> <$tag>", $items));
}
$items = ['One', 'Two', 'Three'];
echo wrap($items);
// output
// <li>One</li> <li>Two</li> <li>Three</li>
echo wrap($items, 'div');
// output
//<div>One</div> <div>Two</div> <div>Three</div>
Yeah, Fabulous i like that.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The variables are not being used elsewhere why don't you inline them?