Hey guys I'm trying to see where my pairs of keys stop, I have arrays built like this
EDIT People are getting really confused so I'm using a real array instead of an example
array (
'key' => '',
'po' => '',
'label' => '',
'report_key' => '',
'shipper' => '',
'status' => '',
'location' => '',
'inspector' => '',
'commodity' => '',
'brand' => '',
'case_count' => '',
'variety' => '',
'style' => '',
'grower_lot' => '',
'pack_date' => '',
// grouping 4 items
'berry_size1' => '',
'berry_size2' => '',
'berry_size3' => '',
'berry_size4' => '',
// grouping 3 items
'bunch_color1' => '',
'bunch_color2' => '',
'bunch_color3' => '',
// grouping 2 items
'color1' => '',
'color2' => '',
// grouping 3 items
'stem1' => '',
'stem2' => '',
'stem3' => '',
// grouping 2 items
'shatter1' => '',
'shatter2' => '',
// grouping 2 items
'splits1' => '',
'splits2' => '',
// grouping 2 items
'wet_sticky1' => '',
'wet_sticky2' => '',
'overall_quality' => '',
// grouping 2 items
'sugar_brix1' => '',
'sugar_brix2' => '',
'rating' => '',
'comments' => '',
)
I came up with some stupid way that really doesn't work to try and sort things out, its extremely backwards, honestly I'm pretty embarrassed by my attempt.
foreach($obj as $key=>$val) {
if(strpos( preg_replace('/[^a-z]/i', '', $key),
preg_replace('/[^a-z]/i', '', $all_keys[$key+$b+1])
) !== false) { echo "<p>$key</p>"; // items 1-3 will show
} elseif(strpos(preg_replace('/[^a-z]/i', '', $key),
preg_replace('/[^a-z]/i', '', $all_keys[$key+$b-1])
) !== false) { echo "<p>$key</p>"; // show last item
} else {
$in.='<aside class="left">';
$in .= "<label for='$key'>". ucwords(strtolower(str_replace('_',' ',$key))) ."</label><br/>";
$in .= ($key=='key') ? "<input type='text' value='". $objLastId ."' id='$key' class='disabled' disabled='disabled'>" : "<input type='text' value='' name='$key' id='$key'>";
$in.='</aside>';
$b++;
}
}
Anyway what I'm really trying to achieve is something like this, could someone steer me in the right direction please?
<style>
.row2 input {width: 50px !important;}
.row3 input {width: 27px !important;}
.row4 input {width: 15px !important;}
</style>
// stem was a 2 item group, so should have the row4 class
// and should have the second item appended by a
// all be inside the same grouping, like below ...
<aside class="left row2">
<label for="color1">Color</label>
<br/><input type="text" value="" name="color1" id="color1">
<input type="text" value="" name="color2" id="color2">
</aside>
// stem was a 3 item group, so should have the row4 class
// and should have items 2-3 appended by a all be inside
// the same grouping, like below ...
<aside class="left row3">
<label for="stem1">Stem</label>
<br><input type="text" id="stem1" name="stem1" value="">
<input type="text" id="stem2" name="stem2" value="">
<input type="text" id="stem3" name="stem3" value="">
</aside>
// berry_size was a 4 item group, so should have the row4 class
// and should have items 2-4 appended by a all be inside
// the same grouping, like below ...
<aside class="left row4">
<label for="berry_size1">Berry Size</label>
<br/><input type="text" id="berry_size1" name="berry_size1" value="">
<input type="text" id="berry_size2" name="berry_size2" value="">
<input type="text" id="berry_size3" name="berry_size3" value="">
<input type="text" id="berry_size4" name="berry_size4" value="">
</aside>
... or ...
// this is a single, so no extra class and ....
<aside class="left">
<label for="other_item">Other Item</label>
<br/><input type="text" id="other_item" name="other_item" value="">
</aside>
What I see this really boiling down to is reading the next array keys name (I stripped the name and used the integer in my version), atleast I think that's the right way to do it?
$arr = array(
'other_item' => 'value',
// this one ranges 1-3
'first_name1' => 'value',
'first_name2' => 'value',
'first_name3' => 'value',
// this one ranges 1-4
'next_name1' => 'value',
'next_name2' => 'value',
'next_name3' => 'value',
'next_name4' => 'value',
'other_item' => 'value',
// this one ranges 1-4
'last_name1' => 'value',
'last_name2' => 'value',
'last_name3' => 'value',
'last_name4' => 'value',
'other_item' => 'value'
);
$newarr = array();
foreach($arr as $key=>$value)
{
if (preg_match('#^([^\d]+)#', $key, $matches)===1)
$newarr[$matches[1]][] = $value;
}
print_r($newarr);
Output:
Array
(
[other_item] => Array
(
[0] => value
)
[first_name] => Array
(
[0] => value
[1] => value
[2] => value
)
[next_name] => Array
(
[0] => value
[1] => value
[2] => value
[3] => value
)
[last_name] => Array
(
[0] => value
[1] => value
[2] => value
[3] => value
)
)
And do whatever you want to do with it. Like that code (just an example, not a very nice one)
foreach($newarr as $name => $block)
{
$cnt = count($block);
echo '<aside class="left'.($cnt>1?' row' . $cnt:'').'">
<label for="' . $name . ($cnt>1?'1':''). '">' .
ucwords(str_replace('_', ' ', $name)) . '</label>
<br/>';
foreach($block as $key=>$element)
{
echo ($key>0?' ':'') . '<input type="text" value="" name="' . $name .
($cnt>1?($key+1):'') . '" id="' . $name .
($cnt>1?($key+1):'') . '">' . "\n";
}
echo '</aside>' . "\n";
}
It gives:
<aside class="left">
<label for="other_item">Other Item</label>
<br/><input type="text" value="" name="other_item" id="other_item">
</aside>
<aside class="left row3">
<label for="first_name1">First Name</label>
<br/><input type="text" value="" name="first_name1" id="first_name1">
<input type="text" value="" name="first_name2" id="first_name2">
<input type="text" value="" name="first_name3" id="first_name3">
</aside>
<aside class="left row4">
<label for="next_name1">Next Name</label>
<br/><input type="text" value="" name="next_name1" id="next_name1">
<input type="text" value="" name="next_name2" id="next_name2">
<input type="text" value="" name="next_name3" id="next_name3">
<input type="text" value="" name="next_name4" id="next_name4">
</aside>
<aside class="left row4">
<label for="last_name1">Last Name</label>
<br/><input type="text" value="" name="last_name1" id="last_name1">
<input type="text" value="" name="last_name2" id="last_name2">
<input type="text" value="" name="last_name3" id="last_name3">
<input type="text" value="" name="last_name4" id="last_name4">
</aside>
As far as I can tell you just want to modify your output based on your 'key type'. I am thinking something like this:
foreach($obj as $key => $val)
{
$parts = explode('_', $key);
switch($parts[0])
{
case 'first':
// Do something here
break;
case 'other':
// Do something here
break;
case 'next':
// Do something here
break;
case 'last':
// Do something here
break;
}
}
I think I might be missing something however, as you appear to be checking 'next' elements...
Related
I am Inserting a multiple form input data in to database using Codeigniter.
I have this post input array:
Array
(
[subject_id] => Array
(
[0] => 1
[1] => 1
)
[question] => Array
(
[0] => test
[1] => test2
)
[option1] => Array
(
[0] => test
[1] => test2
) )
I don't get that how do i convert this array to insert How to insert this array using Insert batch.
$this->db->insert_batch('mytable', $data);
This is the form code which i use for posting the data:
<form method="post">
<input type="text" name="subject_id[]" >
<input type="text" name="question[]" >
<input type="text" name="record[]" >
// Down side Part is appended when user want to add more question
<input type="text" name="subject_id[]" >
<input type="text" name="question[]" >
<input type="text" name="record[]" >
<input type="submit" name="submit" >
</form>
Below is the Array format which i want.
$data = array(
array(
'subject_id' => 'My title' ,
'question' => 'My Name' ,
'option1' => 'My date'
),
array(
'subject_id' => 'Another title' ,
'question' => 'Another Name' ,
'option1' => 'Another date'
)
);
<?php
$i = 0;
foreach($subject_id as $key=>$val)
{
$data[$i]['subject_id'] = $val;
$data[$i]['question'] = $question[$key];
$data[$i]['option1'] = $record[$key];
$i++;
}
$this->db->insert_batch('mytable', $data);
?>
Try like below:Assume $records is an array that you want to insert.
foreach ($records as $record)
{
for ($i=0; $i < count($record); $i++)
{
$data[$i]['subject_id'] = $record['subject_id'][$i];
$data[$i]['question'] = $record['question'][$i]
$data[$i]['option1'] = $record['option1'][$i];
}
}
Then
$this->db->insert_batch('mytable', $data);
I'm a new with PHP Array and have a form that input multiple parent-child data and save into an array. HTML will be something like this:
<ul>
<li><input type="text" name="group[0][name]" placeholder="Group name">
<ul>
<li>
<p>Member #1</p>
<input type="text" name="group[0][member][0][name]" placeholder="Name">
<input type="text" name="group[0][member][0][age]" placeholder="Age">
</li>
<li>
<p>Member #2</p>
<input type="text" name="group[0][member][1][name]" placeholder="Name">
<input type="text" name="group[0][member][1][age]" placeholder="Age">
</li>
</ul>
</li>
<li><input type="text" name="group[1][name]" placeholder="Group name">
<ul>
<li>
<p>Member #1</p>
<input type="text" name="group[1][member][0][name]" placeholder="Name">
<input type="text" name="group[1][member][0][age]" placeholder="Age">
</li>
</ul>
</li>
</ul>
PHP code:
$output = array();
$i = 0;
foreach ( $_POST['group'] as $group ) {
$members = array();
$m = 0;
foreach ( $_POST['group'][$i]['member'] as $name ) {
$members[$i][] = array(
'name' => $name,
'age' => $_POST['group'][$i]['member'][$m]
);
$m++;
}
$output[] = array(
'group_name' => $_POST['group'][$i]['name'],
'members' => $members[$i]
);
$i++;
}
var_dump( $output );
And I got this result:
array (size=2)
0 =>
array (size=2)
'group_name' => string 'Group 1' (length=7)
'members' =>
array (size=2)
0 =>
array (size=2)
...
1 =>
array (size=2)
...
1 =>
array (size=2)
'group_name' => string 'Group 2' (length=7)
'members' =>
array (size=1)
0 =>
array (size=2)
...
Can't get the member names and ages to be submitted into array. Can somebody help me? And sorry if I didn't explain this correctly. Thanks!
You have to process down the heirarchy, using the new arrays created by the foreach loop is also easier to understand than going back to the master array like you woudl have to in a for loop
$output = [];
foreach ( $_POST['group'] as $group ) {
$mem = []; // init the members each time you start a new group
foreach ( $group['member'] as $member) {
$mem[] = ['name' => $member['name'], 'age' => $member['age']];
}
$output[] = [ 'group_name' => $group, 'members' => $mem ];
}
I've a custom system that provides adding new information in custom columns.
I've two tables in my database. One for my value data and the second for the columns (data fieds).
Above you see a image from my form that is build by custom data fields with data per field.
Only in my for each i get for every data a extra loop so that is why i got from every data field two. How can I fix this?
<?php foreach ($list as $key => $value) { ?>
<?php foreach ($dataList as $data) { ?>
<div class="row clearfix">
<div class="col-xl-12">
<div class="form-group form-group-default">
<label><?php echo ucfirst($key); ?></label>
<?php if($key == $data['name']) { ?>
<input type="text" class="form-control" name="value_<?php echo trim($data['name']); ?>" value="<?php echo trim($data['value']); ?>" required>
<?php }else{ ?>
<input type="text" class="form-control" name="value_<?php echo trim($data['name']); ?>" placeholder="<?php echo ucfirst($key); ?>">
<?php } ?>
</div>
</div>
</div>
<?php } ?>
<?php } ?>
$list you'll find a list of data fields and in $dataList you'll find records of data.
Database structure:
data:
['id','hash','field_id','value']
Example of data:
['id' => 1, 'hash' => 123, 'field_id' => 1, 'value' => 'food']
Fields:
['id','name']
Example of fields:
['id' => 1, 'name' => 'firstname']
Below you'll find the foreach variables:
$data = processing('read', 'data JOIN fields ON data.field_id = fields.id', ['data.value,data.uid,fields.name,data.id,fields.category_id'], 'uid = "' . trim($_GET['datalist']) . '"', true);
$dataItem = processing('read', 'data JOIN fields ON data.field_id = fields.id', ['fields.category_id'], 'uid = "' . trim($_GET['datalist']) . '"');
$fieldList = processing('read', 'fields', ['name'], 'category_id = "'. trim($dataItem['category_id']) . '"',true);
$list = [];
foreach($fieldList as $key => $value) {
$list[$value['name']] = $value['name'];
}
Update:
I've update my own question because the other answers didn't resolved my problem. The only problem on my answer is, that when I update it doesnt look at the id of the item but at the name, so i can't save two items with the same name.
<?php foreach ($dataItems as $key => $value) { ?>
<div class="row clearfix">
<div class="col-xl-12">
<div class="form-group form-group-default">
<label><?php echo ucfirst($key); ?></label>
<input type="text" placeholder="<?php echo ucfirst($key); ?>" class="form-control" name="<?php echo trim($key); ?>" value="<?php echo trim($value); ?>">
</div>
</div>
</div>
<?php } ?>
method:
public function getDataListItems(int $category, array $list) {
global $dbh;
$query = 'SELECT data.value, data.uid, fields.name FROM data JOIN fields ON data.field_id = fields.id WHERE fields.category_id = "' . trim($category) . '" ORDER BY uid';
$sql = $dbh->prepare($query);
$sql->execute();
$values = $sql->fetchAll(PDO::FETCH_ASSOC);
foreach ($values as $row) {
if (!isset($items[$row['uid']])) {
$items[$row['uid']] = array_fill_keys($list, ''); // if it needs to dynamically generated
$items[$row['uid']]['uid'] = $row['uid'];
}
$items[$row['uid']][$row['name']] = $row['value'];
}
return $items;
}
Return:
array (
'7d1f4f8e906245f' =>
array (
'Voornaam' => 'Bettina',
'Achternaam' => 'Les',
'Initialen' => 'pop',
'uid' => '7d1f4f8e906245f',
),
'7d1f4f8e906245g' =>
array (
'Voornaam' => 'Simone',
'Achternaam' => '',
'Initialen' => '',
'uid' => '7d1f4f8e906245g',
),
'7d1f4f8e906245l' =>
array (
'Voornaam' => 'test',
'Achternaam' => 'Kül',
'Initialen' => 'lol',
'uid' => '7d1f4f8e906245l',
),
'7d1f4f8e906245s' =>
array (
'Voornaam' => 'Joshua',
'Achternaam' => 'Mas',
'Initialen' => '',
'uid' => '7d1f4f8e906245s',
),
'gGcYEJdRYJ1vqcn' =>
array (
'Voornaam' => '',
'Achternaam' => 'Hello',
'Initialen' => '',
'uid' => 'gGcYEJdRYJ1vqcn',
),
)
I am Inserting a multiple form input data in to database using Codeigniter.
I have this post input array:
Array
(
[subject_id] => Array
(
[0] => 1
[1] => 1
)
[question] => Array
(
[0] => test
[1] => test2
)
[option1] => Array
(
[0] => test
[1] => test2
) )
I don't get that how do i convert this array to insert How to insert this array using Insert batch.
$this->db->insert_batch('mytable', $data);
This is the form code which i use for posting the data:
<form method="post">
<input type="text" name="subject_id[]" >
<input type="text" name="question[]" >
<input type="text" name="record[]" >
// Down side Part is appended when user want to add more question
<input type="text" name="subject_id[]" >
<input type="text" name="question[]" >
<input type="text" name="record[]" >
<input type="submit" name="submit" >
</form>
Below is the Array format which i want.
$data = array(
array(
'subject_id' => 'My title' ,
'question' => 'My Name' ,
'option1' => 'My date'
),
array(
'subject_id' => 'Another title' ,
'question' => 'Another Name' ,
'option1' => 'Another date'
)
);
<?php
$i = 0;
foreach($subject_id as $key=>$val)
{
$data[$i]['subject_id'] = $val;
$data[$i]['question'] = $question[$key];
$data[$i]['option1'] = $record[$key];
$i++;
}
$this->db->insert_batch('mytable', $data);
?>
Try like below:Assume $records is an array that you want to insert.
foreach ($records as $record)
{
for ($i=0; $i < count($record); $i++)
{
$data[$i]['subject_id'] = $record['subject_id'][$i];
$data[$i]['question'] = $record['question'][$i]
$data[$i]['option1'] = $record['option1'][$i];
}
}
Then
$this->db->insert_batch('mytable', $data);
i am working on textbox in which value is display after selecting value from the combo box .i need to get that value that appear in textbox for further computation in the same page.
here is the coding
<?php
$options = array(
'0' => array(
'title' => ' ',
'value1' => '',
'value2' => '',
),
'1' => array(
'title' => 'Islamabad',
'value1' => '31.41',
'value2' => '73.11',
),
'2' => array(
'title' => 'Lahore',
'value1' => '31.56',
'value2' => '74.35',
),
'3' => array(
'title' => 'Kharachi',
'value1' => '24.86',
'value2' => '67.01',
),
'4' => array(
'title' => 'Faisalâbâd',
'value1' => '31.41',
'value2' => '73.11',
),
'5' => array(
'title' => 'Gujrânwâla',
'value1' => '32.16',
'value2' => '74.18',
),
);
if (isset($_GET['option']) && isset($options[$_GET['option']])) {
echo json_encode($options[$_GET['option']]);
exit;
}
?>
<form>
<?php echo "SELECT " ?>
<select name="combo" id="combo">
<?php
foreach($options as $key_value => $option)
{
printf('<option value="%s">%s</option>', $key_value, $option['title']);
}
?>
</select>
<input type="text" name="hidden" id="textboxB" value="" />
<input type="text" name="hidden" id="textboxC" value="" />
<script type="text/javascript" src="jquery.min1.js"></script>
<script type="text/javascript">
$(function(){
$('#combo').change(function(){
$.getJSON("?", {
option : $(this).val()
}, function (data) {
$('#textboxB').val(data.value1);
$('#textboxD').val(data.value1);
$('#textboxE').val(data.value1);
$('#textboxC').val(data.value2);
});
});
});
</script>
i need to get the value of textboxB and textboxC in separate variable so that i can use theses value for further computation on the same page.
i want to use theses value in if -else statement like
if(textboxD==3.15)
{
$a=$b......
}
my requirement is to get these value in separate variable.
people plz help me in ths
You need to set your input names to arrays, such as:
<input type="text" name="hidden[]" id="textboxA" value="123" />
<input type="text" name="hidden[]" id="textboxB" value="654" />
<input type="text" name="hidden[]" id="textboxC" value="abc" />
<input type="text" name="hidden[]" id="textboxD" value="zyg" />
Remember, ID's are only useful for client-side. ID's never get posted, only the NAME and VALUE.
Now, in PHP, you can access these values via:
<?php
$hidden = !empty($_POST['hidden']) ? $_POST['hidden'] : false;
if($hidden) {
foreach($hidden as $key => $value) {
echo "$key => $value\n"; // 0 => 123, 1 => 654, etc.
}
// Directly access single item:
echo $hidden[3]; // zyg
}
?>