I am curious know if and how it is possible to combine 2 values of an array together instead of overriding the other. I will show you an example:
I have a form that is mapping fields to a database from a CSV file. The problem I am running into is say for example there are 2 address fields that need to be merged into 1 address field in my database. (IE: photo below)
So my problem comes when I look at the $_POST[] array. It will show that there are 2 HOME ADDRESSES Selected and import into my database with the LAST selected home-address.
How can I merge the information into 1. I hope this gives you enough information on my problem, please let me know if you need something specific.
I am dealing with all arrays, and when I post into my database it requires an Array to loop through, as I use a reflection class. I hope this makes sense...
Any light would be appreciated on this matter.
Cheers,
I appreciate the quite comments back, the problem that I have with your responses is that I can't create my inputs to be address[] as that will be dynamic and I won't know which one will be set to address and which would perhaps be set to 'phone'... I hope this new picture helps a bit in understanding.
Some of the code (Shortened):
<select name="Home_Address_1"> // name is dynamically generated from the CSV headings
<option>...</option>
</select>
<select name="Home_Address_2"> // name is dynamically generated from the CSV headings
<option>...</option>
</select>
Example of using two posted values in a single array:
<!-- HTML -->
<input name="address[]" type="text" value="111" />
<input name="address[]" type="text" value="222" />
Notice the name attributes.
// PHP
$address = $_POST['address'][0] . ' ' . $_POST['address'][1];
echo $address; // prints "111 222"
UPDATE
Before your script loops through the $_POST array, merge the fields, like so:
$preformat = $_POST['Home_Address_1'];
$preformat .= ' ' . $_POST['Home_Address_2'];
$preformat .= ' ' . $_POST['Home_Address_3'];
$_POST['Home_Address_3'] = trim($preformat);
Then the last Home Address field contains all three.
Try array_merge()...http://php.net/manual/en/function.array-merge.php
Try array_merge with shuffle
$merged = array_merge($arr1,$arr2);
shuffle($merged);
with regards
Wazzy
Related
I am posting this because I do not really know what I could search for. Please note that I have basic knowledges and am still learning ; also, I might sometimes use wrong terms, sorry about that.
I am setting up a web application through PHP/MySQL, HTML, Bootstrap, and JQuery.
I need to post an array to my database. The problem is, this array might contain several times the same value. Indeed, the array is made out of values inside a html table, to which rows can be added to add more data at the same time thanks to JQuery and DataTable.
So far, I did not even know how to post an array, and found the following solution to be working to do so, as the following code:
<form method="post">
<input name="country[]" value="France">
<input name="country[]" value="Spain">
<input name="country[]" value="Germany">
<input type="submit">
</form>
$country = $_POST['country'];
print_r $country;
Would contain the following array result:
|country|
|-------|
|France |
|Spain |
|Germany|
Wonderful.
But what I need to do sometimes, is something like this:
<form method="post">
<input name="country[]" value="France">
<input name="country[]" value="France">
<input name="country[]" value="France">
<input type="submit">
</form>
And I would like this:
$country = $_POST['country'];
print_r $country;
To contain the array:
|country|
|-------|
|France |
|France |
|France |
But instead, the POST contains only
|country|
|-------|
|France |
Whereas Firefox Developer Tools clearly indicates me that the page sent the exact array I need to receive.
Form Data
country[]: [...]
0: France
1: France
2: France
Could you please help me with that?
Thank you very much for your time and attention.
Thank you Daan for asking me how I checked what $_POST['country']; contains.
I modified my original post to include these information. By doing so, I saw that I checked it once I changed it, as I use it again later in a foreach, to display all the rows in another array.
So I checked it the right way.
It turned out my problem was a lot more different, and is related to my use of the foreach.
Indeed, I use the country name as an index for the foreach. As it can only use unique indexes, it uses only once "France".
To scaisEdge: This is data I modified to be anonymous, and country was the easiest way to do so. Imagine products instead of countries, and this is why the user can add several times one unique name.
I am trying to populate an HTML form with values from a previous form being posted. The first form is created dynamically and is a list of t-shirt styles with 5 different sizes available.
I'm having trouble figuring out a way to populate the second form.
The HTML is this:
echo '<input name="'.$filename.'-s" type="text" size="3" value="'.$quantity.'"'>;
What I'd like to do for $quantity is something like this:
$quantity = $_POST['{$filename.$size}'];
Is it possible to use a variable with $_POST?
I think you're looking for this:
$quantity = htmlentities($_POST["$filename$size"]);
htmlentities() is necessary in case you have " or other junk in the input
$_POST[$var1.$var2] worked. Nothing came up in my initial search of previous answers, although I see several applicable questions in the related questions on this page.
This is my drop down list.
<p align="center"><select size="1" name="bo_chose" id="boID">
<option selected value="Select...">Select...</option>
<?php
while ($list_bo = mysql_fetch_array($select_brof)) {
echo "<option value=\"$list_bo[bo_name] $list_bo[bo_code]\">$list_bo[bo_code],$list_bo[bo_name]</option>"; }
?>
</select></p>
So the drop down will show first "select..."
and then will retrieve data andlist the bo_name, bo_code in <option>
It works well.
The problem is, I want to carry the value to another PHP page which will delete the
selected option in the drop down.
Of course the MySQL and PHP will complain that it does not exist ...why?
Its taking the new value $bo_chose (name of the dropdown list) as a new value
as (bo_name, bo_code) — as one value not as a split values.
So if the dropdown list is (george Mike GM)
the data will complain that there is no value called "George Mike GM"
when I want it to carry only "GM" which is the bo_code.
Can anyone help?
Just split the value with space as delimiter and pick the first value. May I know why do you
need to carry both name and id in first place.
You should just add $list_bo['bo_code'] as option value instead of both.
If I understand you correctly you have this select inside a POST form or you are sending it by ajax to the script you want to parse the value. Use a separator to merge these two variables. The separator should be a char that you are sure, it will never be in either of two variables. So, you can for example use ; for the separator and write $list_bo[bo_name].';'.$list_bo[bo_code]. In the recieving script you just explode the value by separator and you will get both values as array. If you are unsure which chars will be in both variables, you can either json_encode or serialize it before puting it into html.
I am working on an application which, in the end, will produce a .CSV file. I have already found how to turn an array or arrays into a .CSV file, but I am having trouble figuring out the preceding step.
I have a bunch of html inputs in a form that will be sent to PHP, I am hoping to have one array consisting of all the input names (input name="example") and another array that consists of the values put into those fields by the end user.
In the end I was the CSV to be a product of both arrays. The first row being things like Height, Width, Depth, and the second row being 10 mm,20 mm,9 mm, etc etc.
I am wondering if a mixture of html classes and a foreach could be used, but I'm really not sure. Any help would be great! Thanks!!
using the square bracket notation will convert these named fields into arrays, here are a few examples
<input name="array[]">
<input name="array[]">
<input name="array[]">
<input name="array[foo]">
<input name="array[bar]">
<input name="array[0][foo]">
<input name="array[0][bar]">
<input name="array[1][foo]">
<input name="array[1][bar]">
Then to get a quick preview of your data use var_dump() or print_r()
var_dump($_REQUEST['array']);
I have a page to edit user information, and I wish to show the current information and allow it for editing, to avoid overwriting existing information.
At the moment, I am fetching the data and displaying it in a text area like so:
$usernameQuery = "select username, firstname from USERS where username = '" . $con->escape_string($username) . "'";
$xblah = $con->query($usernameQuery);
while ($row = mysqli_fetch_assoc($xblah))
{
$checkUsername = $row['username'];
$checkFirstName = $row['firstname'];
}
echo "<form name=\"userForm\">
<h1>Editing information for: ".$username."</h1>
<p>
First name:
<textarea rows=\"1\" id=\"firstname\">".$checkFirstName."</textarea>
<br />
</form>"
This text area does not display correctly in firefox, due to a bug of displaying two rows when one is specified. Is there any way to do the same thing with input type=text?
Also, at present, the contents of firstname in the database is john for my testrecord, but var_dump($checkFirstName) shows just s. What must I do to get the actual contents of the field?
Is there any way to do the same thing with input type=text?
<input type="text" name="firstname" value="<?= $checkFirstName ?>" />
As for your other issue, is there another user that has a first name of 's', but also has the same username as the user with the first name of 'john'? The reason I'm saying this is that you use a while loop to fetch your data, so if there are multiple matches, you are going to be left with the last row that matched your query.
Possible ways to resolve this issue include not using a while loop (which implies that you want to fetch/process multiple rows of data) and making sure that all usernames are unique.
Other than that, I don't see why the value fetched from 'firstname' wouldn't match what is in the database.
If you use the input type=text input, anything you put in the value attribute will be shown by default.
echo '<input type="text" value="' . $checkFirstName . '">';
Of course, you'll want to make sure you do some sanitation on $checkFirstName before outputting it into that field, just in case.
As for getting the values of your field, trying var_dumping $row before your while loop, and see if you can figure out what's going wrong with that. If it doesn't show anything helpful, maybe var_dump inside your while loop with a nice < hr > in between each iteration? This should give you a full view of exactly what is being returned in its entirety from your query. Also, if var_dump is a bit too much information for you, check out:
print_r($var)
print_r documentation
Use the 'value' attribute of the input tag.
First name: <input type=\"text\" name=\"name\" value=\"$checkFirstName\"/><br />
textareas are meant to display multiline text with linebreaks. user- and first names are usually not meant to contain those, so better use the input element
<?php
echo '<input type="text" name="name" value="' . htmlentities($checkFirstName) . '">';
?>
don't forget about htmlentities or htmlspecialchars (depends on the encoding - if your encoding is unicode, htmlspecialchars should be sufficient, otherwise its htmlentities). don't use htmlentities just for form fields, but whenever you print user-provided data. otherwise someone could inject xss (cross site scripting) attacks or at least generate faulty html by providing an username like
<script type="text/javascript">execute_evil_code();</script>
as for displaying only one char instead of a full string: normally, this happens if you think you're working with an array and instead have a string. use var_dump($variable); to see the type of your variables.
also, as htw said, check if $username really is unique and you're getting the right row. run the resulting query (echo $usernameQuery;) in phpmyadmin (or whatever tool you're using). if more than one line is returned, your username's not unique (probably a bug in itself) and the row you get is nor the first, but the last one. it's strange, because 's' is not part of "john", so maybe the mysql result set is something completely different. debug at a higher level, and var_dump the whole $row.
Try put all your php code over here:
<textarea id="firstname" rows="1">
<?php
//connect to database
//Select data
if(mysql_num_rows($sql)) {
$row = mysql_fetch_row($sql);
echo nl2br($row['0']);
}
?>
</textarea>