merge array into content - php

I need to create a function that will merge an array into a template that I have created please see below :
My array:
$view_array = array();
$view_array['start.form'] = '<form method="POST" >';
$view_array['end.form'] = '</form>';
$view_array['name.input'] = '<label>Name : </label><input type="text" name="frm_name" />';
$view_array['name.input'] = '<label>E-mail: </label><input type="text" name="frm_email" />';
$view_array['submit.form'] = '<input type="submit" value="Submit" />';
My template:
$content = "
~error.messages~
~start.form~
~name.input~
~email.input~
~submit.from~
~end.form~";

foreach($view_array AS $key=>$value)
{
$content = str_replace('~'.$key.'~',$value,$content);
}
echo $content;

Related

Generate multiple Form Inputs php

How do i get the values when submitted
I am generating the input via a loop based on the users selection but don't know how to retrieve the input values via post method
here is a sample of what i have
// string is based on database values it can be anything which i can't tell
Example code
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach($exp as $value){
print '<input type="text" name="'.$value.'[]" value="" />
}
You don't have to use name array (name="blabla[]")
$string = 'math,english,biology';
$exp = explode(',', $string);
if ($_POST) {
foreach ($exp as $name) {
if (isset($_POST[$name])) {
echo 'input ' . $name . ' is ' . $_POST[$name] . '<br>';
}
}
exit();
}
echo '<form method="post">';
foreach($exp as $value){
print '<input type="text" name="'.$value.'" value="" />';
}
echo '<button type="submit">Submit</button></form>';
Enter a, b, c to each input and submit. Here is the result:
input math is a
input english is b
input biology is c
Put the value in value="", name the field and make it an array [].
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach ($exp as $value) {
echo '<input type="text" name="fieldName[]" value="<?= htmlentities($value) ?>" />
}
Then it will be accessible in *$_POST['fieldName'] as an array.
*presuming you are using method="POST" on the form
If math,english,biology are form keys, then do:
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach ($exp as $key) {
echo '<input type="text" name="fieldName[<?= htmlentities($key) ?>]" value=""/>
}
or
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach ($exp as $key) {
echo '<input type="text" name="<?= htmlentities($key) ?>" value=""/>
}

Array values retrieving not working

I've an input field where I paste an url and I want to fetch the details from an url, After fetching the result now I'm unable to assign these values to an input filed. You can see the link here
<form action="" method="POST">
<input type="text" name="ref_url" placeholder="Paste the URL" value="https://www.lovely-cards.com/wedding-cards/1836-ethnica01.html">
<input type="submit" value="Get Details">
</form>
<input type="text" name="height" value="<?php echo $tags[0]['height']; ?>">
PHP
<?php
include('simple_html_dom.php');
if (!EMPTY(isset($_POST['ref_url']))) {
$url = $_POST['ref_url'];
$html = file_get_html($url);
$row = $html->find('tr');
unset($row[0]);
unset($row[1]);
foreach($row as $element) {
$tag_name = $element->find('td',0)->plaintext;
$tag_value = $element->find('td',1)->plaintext;
if (!EMPTY($tag_name) && !EMPTY($tag_value)) {
$tags[] = array($tag_name =>$tag_value);
}
}
echo '<pre>';
print_r($tags);
echo '</pre>';
}
?>
Its 'Height' not 'height' which you are trying to set to

Group Arrays By Another Array?

I'm trying to sort one array by another array. Both these arrays get their content from a form.
Here's my form code:
<form method="post" action="">
<div class="groupcontainer">
<br/><label>Group One:</label><br/>
<input type="text" name="groupname[]" value="groupone" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="variable[]" value="variableone" />
<input type="text" name="variable[]" value="variabletwo" />
</div>
<br/>
<div class="groupcontainer">
<br/><label>Group Two:</label><br/>
<input type="text" name="groupname[]" value="grouptwo" /><br/>
<br/><label>Variable Group Two:</label><br/>
<input type="text" name="variable[]" value="variablethree" />
<input type="text" name="variable[]" value="variablefour" />
</div>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
Here's the PHP code:
<?php
if (!$_POST['submit'] == "") {
foreach($_POST['groupname'] as $groupname) {
$groupnum = 1;
foreach($_POST['variable'] as $variable) {
print "$".$groupname.$groupnum." = '".$variable."';<br/>";
$groupnum++;
}
print "$".$groupname." = array(";
for ($arrnum = 1; $arrnum <= count($_POST['variable']); $arrnum++) {
print "$".$groupname.$arrnum.", ";
}
print ");<br/><br/>";
}
}
?>
This is the result I get when I submit the form:
$groupone1 = '$variableone';
$groupone2 = '$variabletwo';
$groupone3 = '$variablethree';
$groupone4 = '$variablefour';
$groupone = array($groupone1, $groupone2, $groupone3, $groupone4, )
$grouptwo1 = '$variableone';
$grouptwo2 = '$variabletwo';
$grouptwo3 = '$variablethree';
$grouptwo4 = '$variablefour';
$grouptwo = array($grouptwo1, $grouptwo2, $grouptwo3, $grouptwo4, )
This is the result that I actually want:
$groupone1 = '$variableone';
$groupone2 = '$variabletwo';
$groupone = array($groupone1, $groupone2)
$grouptwo1 = '$variablethree';
$grouptwo2 = '$variablefour';
$grouptwo = array($grouptwo1, $grouptwo2)
The whole thing needs to be dynamic since I want to add as many groups and variables as I want.
I've been searching for an answer for days and already asked two people who didn't know an answer. Maybe you guys can help. Thanks!
Update:
Just to clarify a few points:
So basically I want to be able to add as many input forms as I want (I use jQuery for that) to create as many groups and variables as I want, for example like this:
$groupwuteva1 = 'hello';
$groupwuteva2 = 'bye':
$randomname1 = 'green';
$randomname2 = 'blue';
$randomname3 = 'red';
$blabla1 = 'abc';
$blabla2 = 'xyz';
$blabla3 = '123';
$blabla4 = 'bla';
Whatever I use as groupname will be used in array one, e.g. I call a group "Colors" and the variables I put into the form for that group are "blue", "red" and "green". Then I would get this code:
$colors1 = 'green';
$colors2 = 'blue';
$colors3 = 'red';
I hope this clairfies some questions. And thanks a ton for all responses so far!
You can take the group name as a container to store all associated variable values in it. And later, use variable variables and implode() function to process your html form.
HTML
<form method="post" action="">
<div class="groupcontainer">
<br/><label>Groupe One:</label><br/>
<input type="text" name="groupname[]" value="groupone" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="groupone[]" value="variableone" />
<input type="text" name="groupone[]" value="variabletwo" />
</div>
<br/>
<div class="groupcontainer">
<br/><label>Groupe Two:</label><br/>
<input type="text" name="groupname[]" value="grouptwo" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="grouptwo[]" value="variablethree" />
<input type="text" name="grouptwo[]" value="variablefour" />
</div>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
PHP
if(isset($_POST['submit'])){
foreach($_POST['groupname'] as $value){
$arr = array();
$i = 1;
foreach($_POST[$value] as $v){
$var = $value . $i;
$$var = $v;
echo $var . " = " . $$var . "<br />";
$arr[] = $$var;
++$i;
}
$output = $value . " = array(" . implode(",", $arr) . ")";
echo $output . "<br /><br />";
}
}
Output:
groupone1 = variableone
groupone2 = variabletwo
groupone = array(variableone,variabletwo)
grouptwo1 = variablethree
grouptwo2 = variablefour
grouptwo = array(variablethree,variablefour)
try this form:
<form method="post" action="">
<?php foreach(array('groupone', 'grouptwo') as $num):?>
<div class="groupcontainer">
<br/><label>Groupe <?php echo $num;?>:</label><br/>
<input type="text" name="groupname[]" value="<?php echo $num;?>" /><br/>
<br/><label>Variable Group <?php echo $num;?>:</label><br/>
<input type="text" name="variable[<?php echo $num;?>][]" value="<?php echo uniqid();?>" />
<input type="text" name="variable[<?php echo $num;?>][]" value="<?php echo uniqid();?>" />
</div>
<br/>
<?php endforeach;?>
<input type="submit" name="submit" value="Submit" />
</form>
and code:
if ($_POST) {
foreach($_POST['groupname'] as $groupname) {
$$groupname = array();
foreach($_POST['variable'][$groupname] as $variable) {
${$groupname}[] = $variable;
}
}
var_dump($groupone);
var_dump($grouptwo);
}

$_POST and code manipulation

I want to grab the value of a field using $_POST, manipulate it, then pass the value back to the same page to the same field before the PHP code manipulates it.
If I put the PHP code after the field, it manipulates the code, reloads the page but doesn't put the manipulated code back into the field.
if (!isset($input)) {
$input = '';
}
echo '<form id="testform" method="post" action="">';
echo '<input type="text" name="inputText" value="' . $input . '">';
echo '<button type="submit" name="button"> Button </button>';
echo '</form>';
$input = $_POST['inputText'];
if(isset($_POST['inputText'])) {
$input = $input . ' manipulated';
}
echo $input; //test
If I put the PHP code before the field, it can't find the field to manipulate the value...
if (!isset($input)) {
$input = '';
}
$input = $_POST['inputText'];
if(isset($_POST['inputText'])) {
$input = $input . ' manipulated';
}
echo $input; //test
echo '<form id="testform" method="post" action="">';
echo '<input type="text" name="inputText" value="' . $input . '">';
echo '<button type="submit" name="button"> Button </button>';
echo '</form>';
Obviously the first approach is more correct, but how do I pass the $input variable to the field before the rest of my PHP manipulation code executes?
I tried $_POST['inputText'] = $input as a desperate attempt but nothing..
Well, from what I've understood in your explanation, you want to change the input value to something else and show it in he same field. If that's correct, you may want to do this:
<form id="testform" method="post" action="">
<input type="text" name="inputText" value="<?php echo ( isset($_POST['inputText']) ) ? sprintf( '%s manipulated', $_POST['inputText'] ) : ''; ?>">
<button type="submit"> Send </button>
</form>
Let me know if that's what you wanted. Regards !
Try
$input = isset($_POST['inputText']) ?$_POST['inputText'] :'';
in the begining instead of
if (!isset($input)) {
$input = '';
}

Wordpress + PHP: Using array variable with in_array inside another function

I am using jcart http://conceptlogic.com/jcart/ to create some cart functionality in wordpress.
I am trying to access a jcart function within another function in my functions.php file.
foreach($jcart->get_contents() as $item) {
$psitems[] = $item['id'];
}
This code works fine on functions.php itself or on any template page-- it creates the $psitems array so I can check if that array contains certain values.
Now, within the following function:
function gallery_shortcode_fancybox($attr) { ...
I have the following:
foreach($jcart->get_contents() as $item) {
$psitems[] = $item['id'];
}
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_image($id, 'full', false, false) : wp_get_attachment_image($id, 'full', true, false);
$output .= "<div class='property'>";
$output .= "$link";
$output .= '
<div class="property-details">
<div class="property-details-inner">
<form method="post" action="" class="jcart">
<fieldset>
<input type="hidden" name="jcartToken" value="' . $_SESSION['jcartToken'] . '" />
<input type="hidden" name="my-item-id" value="' . $id . '" />
<input type="hidden" name="my-item-name" value="Photo #' . $id . '" />
<input type="hidden" name="my-item-url" value="' . wp_get_attachment_url( $id ) . '" />
<input type="hidden" name="my-item-qty" value="1" size="3" />';
if(in_array($id,$psitems)) {
$output .= '<span class="action terminal pull-sheet">Image in Pullsheet</span>'
} else {
$output .= '<input type="submit" name="my-add-button" value="Add to Pull Sheet" class="action terminal pull-sheet" />';
}
$output .= '</fieldset>
</form>
</div> <!-- property-details-inner -->
</div>';
$output .= "</div>";
}
return $output;
I get the following error:
Call to a member function get_contents() on a non-object
If I try placing the original foreach($jcart->get_contents() as $item) { ... code outside of the function, I get the following error:
Warning: in_array() expects parameter 2 to be array, null given in...
How can I access this array inside the function and avoid these errors?
Thanks!
Regarding the first error, it means that you have to initialize $jcart.
As for the warning, try this:
$array = $jcart->get_contents();
foreach ($array as $item) ...

Categories