I've created the following function to print HTML tags in PHP.
function div($attr = [], $child = []) {
$div = "<div";
if (is_array($attr)) {
foreach ($attr as $key => $value) {
$div .= " " . $key . '="' . $value . '"';
}
}
$div .= ">";
if (is_array($child)) {
foreach ($child as $value) {
$div .= $value;
}
}
$div .= "</div>";
return $div;
}
echo div(["class" => "container"], [
div(["class" => "title"], ["Lorem Ipsum"]])
]);
Now, I use this function with multiple tags; div, table, tr, td, etc.
For each tag, I declare the function over and over again with little modification in the body. This seems redundant. I want to create a main function which will return the actual function. For example,
$div = construct("div");
$tr = construct("div");
Of course, PHP is different than JS. In JS, this would work:
function construct(tagName) {
var elm = tagName;
return function(value) {
console.log(elm + ": " + value);
}
}
var div = construct("div");
var tr = construct("tr");
div("test"); // div: test
tr("test"); // tr: test
How should I proceed?
Similar actually as in js:
Just use the first argument of the function to pass the element type as a string.
function construct($elem, $attr = [], $child = []) {
$out = "<".$elem;
if (is_array($attr)) {
foreach ($attr as $key => $value) {
$out .= " " . $key . '="' . $value . '"';
}
}
$out .= ">";
if (is_array($child)) {
foreach ($child as $value) {
$out .= $value;
}
}
$out .= "</".$elem.">";
return $out;
}
echo construct("div",["class" => "container"], [
div(["class" => "title"], ["Lorem Ipsum"]])
]);
You could pass the type of tag through a parameter of the function.
function ConstrucElement($tag, $attr, $body)
{
$output = '<' . $tag;
....
$output .= '</' . $tag . '>';
}
This is how I would solve the problem at hand:
function element($elementName, $attr = [], $child = []) {
$element = "<" . $elementName;
...
return $element;
}
function div($attr = [], $child = []) {
return element("div", $attr, $child);
}
This has the additional benefit of enabling element specific validations etc.: (pseudocode)
function a($attr = [], $child = []) {
if(attr contains no href) return ERROR;
return element("div", $attr, $child);
}
Doing some more digging, I figured out a way to make it work the JS way. The use of use in the examples in PHP: Closure - Manual did the job.
function construct($tag_name = "") {
return function($attr = [], $child = []) use ($tag_name) {
$element = "<$tag_name";
if (is_array($attr)) {
foreach ($attr as $key => $value) {
$element .= " " . $key . '="' . $value . '"';
}
}
$element .= ">";
if (is_string($child)) {
$element .= $child;
}
if (is_array($child)) {
foreach ($child as $value) {
$element .= $value;
}
}
$element .= "</$tag_name>";
return $element;
};
}
$h2 = construct("h2");
$span = construct("span");
$div = construct("div");
$table = construct("table");
$tr = construct("tr");
$td = construct("td");
Using a generic function to print the tags seems like a more practical approach, but not that desirable, because the code gets harder to read. Compare the following blocks.
while ($row = $albums->fetch_assoc()) {
echo $div(["class" => "album"], [
$h2([], [$row["album"] . " (" . $row["year"] . ")"]),
$div(["class" => "album-cover"], [
img(["src" => "covers/" . $row["cover"]]),
]),
$div(["class" => "songs"], [
$table([], [
loop_songs($row["album"]),
]),
]),
]);
}
while ($row = $albums->fetch_assoc()) {
echo construct("div", ["class" => "album"], [
construct("h2", [], [$row["album"] . " (" . $row["year"] . ")"]),
construct("div", ["class" => "album-cover"], [
construct("img", ["src" => "covers/" . $row["cover"]]),
]),
construct("div", ["class" => "songs"], [
construct("table", [], [
loop_songs($row["album"]),
]),
]),
]);
}
First one seems more readable to me.
Here's another approach using __callStatic:
So now you can build your HTML with these:
Build::div(...)
Build::span(...)
Build::table(...)
Like this:
<?php
class Build {
public static function __callStatic($name, $arguments) {
$attr = $arguments[0];
$child = $arguments[1];
$div = "<" . $name;
if (is_array($attr))
foreach ($attr as $key => $value)
$div .= " " . $key . '="' . $value . '"';
$div .= ">";
if (is_array($child))
foreach ($child as $value)
$div .= $value;
$div .= "</" . $name . ">";
return $div;
}
}
echo Build::div(["class" => "container"], [
Build::span(["class" => "title"], ["Lorem Ipsum"])
]);
Output:
<div class="container"><span class="title">Lorem Ipsum</span></div>
Related
I have created a form,I read from a INI file and show its values in the form.
Now I want to edit that file.
So I show values in text box and let the user edit the file.
Now I am using INI Library to write my files, but my code changes the structure of INI file, also it remove the comments.
Is it possible that when I write to the file only those value is changed and other things like comments and Section remain as it is ?
This is my code:
$CFG_file = $this->data['file_data'] = parse_ini_file("CFG/fms.cfg" ,true);
if (isset($_POST['submit']))
{
//var_dump($_POST);
$data_file = $this->parameter_m->array_from_post(array('VAL1','VAL2','InMedia','VAL','VAL','VAL5','VAL3'));
$this->load->helper('file');
$file = "CFG/fms.cfg";
$this->load->library('ini');
$ini = new INI($file);
$ini->write($file, $data_file );
}
And the library code that writes file :
function write($file = NULL, $file_content = array(), $sections = FALSE) {
$this->file_content = (!empty($file_content)) ? $file_content : $this->file_content;
$this->file = ($file) ? $file : $this->file;
$this->sections = $sections;
$content = NULL;
if ($this->sections) {
foreach ($this->file_content as $section => $file_content) {
$content .= '[' . $section . ']' . PHP_EOL;
foreach ($file_content as $key => $val) {
if (is_array($val)) {
foreach ($val as $v) {
$content .= $key . '[]=' . (is_numeric($v) ? $v : $v ) . PHP_EOL;
}
} elseif (empty($val)) {
$content .= $key . '=' . PHP_EOL;
} else {
$content .= $key . '=' . (is_numeric($val) ? $val : $val ) . PHP_EOL;
}
}
$content .= PHP_EOL;
}
} else {
foreach ($this->file_content as $key => $val) {
if (is_array($val)) {
foreach ($val as $v) {
$content .= $key . '[] = ' . (is_numeric($v) ? $v : '"' . $v . '"') . PHP_EOL;
}
} elseif (empty($val)) {
$content .= $key . ' = ' . PHP_EOL;
} else {
$content .= $key . ' = ' . (is_numeric($val) ? $val : '"' . $val . '"') . PHP_EOL;
}
}
}
return (($handle = fopen($this->file, 'w+')) && fwrite($handle, trim($content)) && fclose($handle)) ? TRUE : FALSE;
}
And this is my file :
[]
VAL1= 0.0.0.0
VAL2= 5070
;media settings
;ON/OFF/AUTO
VAL3= OFF
VAL4= 8000
VAL5= 4000
VAL6= 360
;Transport settings
;ON/OFF
VAL7= OFF
When i execute my code this becomes :
VAL1= "0.0.0.0"
VAL2 = 5071
VAL3 =
VAL4 = 8000
VAL5 = 4000
VAL6 = 360
VAL7 = "ON"
I open a .ini, then write some add some values to its parameters and then i saved it back.
My writing the content works perfectly fine, but when i open the file the previous content of that file is cleared.[Maybe due to my write mode].
What change should i do in my code to let user see what the previous content was when he edits a file.
file_get_contents($path.$filename);
/*Get All The datas from that file*/
$this->data['params'] = $this->parameter_m->get();
/*Getting the parameters to display on view*/
$this->data['parameters'] = parse_ini_file($path.$filename,true);
while (current($this->data['parameters']) )
{
$param_set = current($this->data['parameters']);
$param_type = key($this->data['parameters']);
foreach ($param_set as $key =>$value)
{
$this->data['parameters'][$param_type][$key] = $this->input->post($key);
}
next($this->data['parameters']);
}
$this->load->helper('file');
$this->load->library('ini');
$file = $path.$filename;
$ini = new INI($file);
$ini->read($file);
$ini->write($file, $this->data['parameters']);
My Write Function
function write($file = NULL, $data_file = array(), $sections = TRUE) {
$this->data_file = (!empty($data_file)) ? $data_file : $this->data_file;
$this->file = ($file) ? $file : $this->file;
$this->sections = $sections;
$content = NULL;
if ($this->sections) {
foreach ($this->data_file as $section => $data_file) {
$content .= '[' . $section . ']' . PHP_EOL;
foreach ($data_file as $key => $val) {
if (is_array($val)) {
foreach ($val as $v) {
$content .= $key . '[] = ' . (is_numeric($v) ? $v : $v ) . PHP_EOL;
}
} elseif (empty($val)) {
$content .= $key . ' = ' . PHP_EOL;
} else {
$content .= $key . ' = ' . (is_numeric($val) ? $val : $val ) . PHP_EOL;
}
}
$content .= PHP_EOL;
}
} else {
foreach ($this->data_file as $key => $val) {
if (is_array($val)) {
foreach ($val as $v) {
$content .= $key . '[] = ' . (is_numeric($v) ? $v : '"' . $v . '"') . PHP_EOL;
}
} elseif (empty($val)) {
$content .= $key . ' = ' . PHP_EOL;
} else {
$content .= $key . ' = ' . (is_numeric($val) ? $val : '"' . $val . '"') . PHP_EOL;
}
}
}
return (($handle = fopen($this->file, 'w+')) && fwrite($handle, trim($content)) && fclose($handle)) ? TRUE : FALSE;
}
I what to put a span element for $term['nodes']
I have tried to put after bracket and between but nothing works for me
if (isset($term['nodes'])) {
$term['name'] = $term['name'] . ' (' . $term['nodes'] . ')';
}
here is the all functin
function bootstrap_taxonomy_menu_block($variables) {
$tree = $variables['items'];
$config = $variables['config'];
$num_items = count($tree);
$i = 0;
$output = '<ul class="nav nav-pills nav-stacked">';
foreach ($tree as $tid => $term) {
$i++;
// Add classes.
$attributes = array();
if ($i == 1) {
$attributes['class'][] = '';
}
if ($i == $num_items) {
$attributes['class'][] = '';
}
if ($term['active_trail'] == '1') {
$attributes['class'][] = 'active-trail';
}
if ($term['active_trail'] == '2') {
$attributes['class'][] = 'active';
}
// Alter link text if we have to display the nodes attached.
if (isset($term['nodes']))
{
$term['name'] = $term['name'] . ' (<span>' . $term['nodes'] . '</span>)';
}
// Set alias option to true so we don't have to query for the alias every
// time, as this is cached anyway.
$output .= '<li' . drupal_attributes($attributes) . '>' . l($term['name'], $term['path'], $options = array('alias' => TRUE));
if (!empty($term['children'])) {
$output .= theme('taxonomy_menu_block__' . $config['delta'], (array('items' => $term['children'], 'config' => $config)));
}
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
i what this for the bootstrap cdn class , i have move the function on template.php , of drupal theme , but the span element is in plain text in browser
Try this:
if (isset($term['nodes']))
{
$term['name'] = $term['name'] . ' (<span>' . $term['nodes'] . '</span>)';
echo $term['name']; // To see the output
}
$resultUpdate = Nemesis::select("*", $table, "id = '{$id}'");
if (!$resultUpdate) {
self::show_error(QUERY_ERROR);
} elseif ($resultUpdate->num_rows > 0) {
$out .= '<div class="form-desc">' . $formDesc . '</div>';
} else {
self::show_error(QUERY_EMPTY);
}
$array = array_values($array);
print_r($array);
$out .= '<form action="' . $_SERVER['PHP_SELF'] . '?id=' . $id . '&table=' . $table . '" method="post" class="form-horizontal" ' . $formAppend . '>';
while ($row = $resultUpdate->fetch_assoc()) {
foreach ($row as $fieldname => $value) {
if (in_array($fieldname, $array)) {
$out .= generateInputField($fieldname, $value);
}
}
foreach ($row as $fieldname => $value) {
if (in_array($fieldname, $array)) {
$out .= generateTextarea($fieldname, $value, $cke);
}
}
foreach ($row as $fieldname => $value) {
if (in_array($fieldname, $array)) {
$out .= generateImgField($fieldname, $value);
}
}
}
$arr = array("last_modified"=>"input", "published"=>"input", "content"=>"textarea");
echo $automate->createArrayForm('projects', 'update', 'Some form desc', '178514825', $arr, true);
Right now all fields are outputting in every foreach when only inputs should output in the generateInputField section for example. I know this is because I need to check if the fieldtype (input, textarea) key matches with one of the values marked as input or textarea for values of the $fieldname. But I am not sure how.
I am pretty sure I have to filter the array so only values with input go into a separate array like arrayInput in which I can use as the second argument in in_array.
If I understand you and your code correctly...
This code
while ($row = $resultUpdate->fetch_assoc()) {
foreach ($row as $fieldname => $value) {
if (in_array($fieldname, $array)) {
will always return true for all your rows thats why you are getting your current output.
Instead you should be doing this:
while ($row = $resultUpdate->fetch_assoc()) {
foreach ($row as $fieldname => $value) {
if ($fieldname == 'input') {
$out .= generateInputField($fieldname, $value);
} elseif($fieldname == 'textarea') {
$out .= generateTextarea($fieldname, $value, $cke);
} elseif ($fieldname == 'img') {
$out .= generateImgField($fieldname, $value);
}
else{ $out = $out;}
}
}
With alot of help from stackers...
/**
* Create Form With Array
*
* Creates a form based on an array. If $do == update, we match fieldnames with values
*
* #param string $table name of database table
* #param string $do whether the form is an insert or update
* #param string $formDesc form description to be echoed
* #param array $array associative array of type (keys), and fieldnames (values)
* #param bool $markFields whether or not to add headers for inputs, textareas .etc during insert
* #param bool $formBrackets whether or not to prepend and append form brackets
* #return $out html form
*
*/
public function createArrayForm($table, $do, $formDesc = '', $id, $array, $markFields = false, $formBrackets = true) {
if (!isset($table) && !isset($do)) {
self::show_error('One or more parameters are missing in ' . __FUNCTION__);
} elseif ($table == 'update' && !isset($id)) {
self::show_error('For this form to be built, and ID must be set. Missing parameter `ID` in ' . __FUNCTION__);
}
if (!is_array($array)) {
self::show_error('For this form to be built, an array must be given. Missing parameter `array` in ' . __FUNCTION__);
}
$result = array();
// create two dimensional array to preserve keys that
// otherwise would be lost with array_flip
foreach($array as $k => $v) {
if (array_key_exists($v, $result)) {
$result[$v][] = $k;
} else {
$result[$v] = array($k);
}
}
// make sure we do not have any duplicates
$result = super_unique($result);
// we just need the array_values for matching with in_array
// however we do not want to run array_values on null
// so we check to see if the $result is a valid array first
// if not, we just output a blank array so in_array doesn't complain
$arrayInput = is_array($result['input']) ? array_values($result['input']) : array();
$arrayTextarea = is_array($result['textarea']) ? array_values($result['textarea']) : array();
$arrayImages = is_array($result['images']) ? array_values($result['images']) : array();
$out = $formBrackets == true ? '<form action="' . $_SERVER['PHP_SELF'] . '?id=' . $id . '&table=' . $table . '" method="post" class="form-horizontal" ' . $formAppend . '>' : NULL;
if($do == 'insert') {
$out .= isset($formDesc) ? '<div class="form-desc">' . $formDesc . '</div>' : NULL;
$out .= $markFields && in_array('input', $array) ? '<h3>Input Fields</h3>' : NULL;
foreach ($arrayInput as $fieldname) {
$out .= generateInputField($fieldname);
}
$out .= $markFields && in_array('textarea', $array) ? '<h3>Content Fields</h3>' : NULL;
foreach ($arrayTextarea as $fieldname) {
$out .= generateTextarea($fieldname, $cke);
}
$out .= $markFields && in_array('image', $array) ? '<h3>Images Fields</h3>' : NULL;
foreach ($arrayImages as $fieldname) {
$out .= generateImgField($fieldname);
}
} elseif ($do == 'update') {
$resultUpdate = Nemesis::select("*", $table, "id = '{$id}'");
if (!$resultUpdate) {
self::show_error(QUERY_ERROR);
} elseif ($resultUpdate->num_rows > 0) {
$out .= isset($formDesc) ? '<div class="form-desc">' . $formDesc . '</div>' : NULL;
} else {
self::show_error(QUERY_EMPTY);
}
while ($row = $resultUpdate->fetch_assoc()) {
foreach ($row as $fieldname => $value) {
if (in_array($fieldname, $arrayInput)) {
$out .= generateInputField($fieldname, $value);
}
}
foreach ($row as $fieldname => $value) {
if (in_array($fieldname, $arrayTextarea)) {
$out .= generateTextarea($fieldname, $value, $cke);
}
}
foreach ($row as $fieldname => $value) {
if (in_array($fieldname, $arrayImages)) {
$out .= generateImgField($fieldname, $value);
}
}
}
} else {
self::show_error('Missing array or `do` argument in function ' . __FUNCTION__);
}
$out .= form_hidden('user_data', '1');
$out .= form_hidden('id', $do == 'update' ? $id : self::generateID());
$out .= $formBrackets == true ? form_close() : NULL;
return $out;
}
Usage:
$arr = array("last_modified"=>"input", "published"=>"input", "project_content"=>"textarea", "project_content"=>"textarea");
echo $automate->createArrayForm('projects', 'insert', 'Some form desc', '123', $arr, true);
echo $automate->createArrayForm('projects', 'update', 'Some form desc', '178514825', $arr, true);
I'm trying to take a multidimensional array and convert it into HTML form fields, like this:
<input type="hidden" name="c_record[contact][0][name]" value="First Last">
<input type="hidden" name="c_record[contact][0][date_submitted][date]" value="2010-01-01">
<input type="hidden" name="c_record[contact][0][date_submitted][hour]" value="10">
<input type="hidden" name="c_record[contact][0][date_submitted][min]" value="08">
<input type="hidden" name="c_record[contact][0][date_submitted][sec]" value="16">
<input type="hidden" name="c_record[contact][0][ip_address]" value="192.168.1.1">
Here is what I have so far:
$fields = array(
'c_record' => array(
'contact' => array(
0 => array(
'name' => 'First Last',
'date_submitted' => array(
'date' => '2010-01-01',
'hour' => '10',
'min' => '08',
'sec' => '16',
),
'ip_address' => '192.168.1.1',
),
),
),
);
$form_html = array_to_fields($fields);
function array_to_fields($fields, $prefix = '') {
$form_html = '';
foreach ($fields as $name => $value) {
if ( ! is_array($value)) {
if ( ! empty($prefix)) {
$name = $prefix . '[' . $name . ']';
}
// generate the hidden field
$form_html .= Form::hidden($name, $value) . EOL;
} else {
if ( ! empty($prefix)) {
$prefix .= '[' . $name . ']';
} else {
$prefix = $name;
}
$form_html .= array_to_fields($value, $prefix);
}
}
return $form_html;
}
This works fine until ip_address, which results in:
<input type="hidden" name="c_record[contact][0][date_submitted][ip_address]" value="192.168.1.1">
And any additional fields after ip_address keep having the previous field names added to them.
How can I make this work?
You could manhandle http_build_query into service for this purpose, since it naturally creates the scrunched-together keys you're looking for. If you wait until the end to urldecode your keys and values, it's easy to explode the output, since any = or & in a key or value will be safely encoded.
function array_to_fields($array) {
$html = '';
$entries = explode('&', http_build_query($array));
foreach ($entries as $entry) {
list($key, $value) = explode('=', $entry);
$html .= Form::hidden(urldecode($key), urldecode($value)) . EOL;
}
return $html;
}
You are updating $prefix in the current scope before you pass it into the recursive function. You don't want to do that. For multi-valued arrays it means that in the next iteration of the current function the prefix is going to contain the name of the previoust array. You can work around this by passing an updated prefix to the recursive function independent of $prefix.
function array_to_fields($fields, $prefix = '') {
$form_html = '';
foreach ($fields as $name => $value) {
if ( ! is_array($value)) {
if ( ! empty($prefix)) {
$name = $prefix . '[' . $name . ']';
}
// generate the hidden field
$form_html .= Form::hidden($name, $value) . EOL;
} else {
if ( ! empty($prefix)) {
$subprefix = $prefix . '[' . $name . ']';
} else {
$subprefix = $name;
}
$form_html .= array_to_fields($value, $subprefix);
}
}
return $form_html;
}
Try that.
As a hack and temporary solution, I did the following, although it's damn ugly and not flexible:
foreach ($fields as $name1 => $value1) {
if ( ! is_array($value1)) {
$form_html .= Form::hidden($name1, $value1) . EOL;
} else {
foreach ($value1 as $name2 => $value2) {
if ( ! is_array($value2)) {
$form_html .= Form::hidden($name1 . '[' . $name2 . ']', $value2) . EOL;
} else {
foreach ($value2 as $name3 => $value3) {
if ( ! is_array($value3)) {
$form_html .= Form::hidden($name1 . '[' . $name2 . '][' . $name3 . ']', $value3) . EOL;
} else {
foreach ($value3 as $name4 => $value4) {
if ( ! is_array($value4)) {
$form_html .= Form::hidden($name1 . '[' . $name2 . '][' . $name3 . '][' . $name4 . ']', $value4) . EOL;
} else {
foreach ($value4 as $name5 => $value5) {
if ( ! is_array($value5)) {
$form_html .= Form::hidden($name1 . '[' . $name2 . '][' . $name3 . '][' . $name4 . '][' . $name5 . ']', $value5) . EOL;
} else {
foreach ($value5 as $name6 => $value6) {
if ( ! is_array($value6)) {
$form_html .= Form::hidden($name1 . '[' . $name2 . '][' . $name3 . '][' . $name4 . '][' . $name5 . '][' . $name6 . ']', $value6) . EOL;
} else {
throw new Kohana_Exception('There are no levels than are supported by array_to_fields . Ending entire loop');
}
}
}
}
}
}
}
}
}
}
}
}