I need help for a little problem with my custom metabox in Wordpress.
I have a table with a group of 3 values per row. Number of row is undefined, because users can add or remove one of them.
I need to save each row (so 3 values in 3 inputs) in a specific array.
For this source code, i removed voluntarily all the system which is add a row, delete a row etc... I also remove all the query/variable tests.
I think this problem isn't difficult to solve, but i don't see any solution..
Thanks!
function my_meta($post){
$get_values = get_post_meta($post->ID,'_MY_META_ROW',false);
echo '<table>';
foreach($get_values as $v => $MY_META_ROW){
echo '<tr>
<td><input type="text" name="MY_INPUT_1" value="'.$MY_META_ROW['INPUT_1'].'" /></td>
<td><input type="text" name="MY_INPUT_2" value="'.$MY_META_ROW['INPUT_2']'" /></td>
<td><input type="text" name="MY_INPUT_3" value="'.$MY_META_ROW['INPUT_3'].'" /></td>
</tr>';
}
echo '</table>';
}
add_action('save_post','save_my_inputs');
function save_my_inputs($post_id){
foreach($_POST['??????????'] as $value){
$array_row = array (
"MY_INPUT_1" => $POST['MY_INPUT_1'];
"MY_INPUT_2" => $POST['MY_INPUT_2'];
"MY_INPUT_3" => $POST['MY_INPUT_3'];
)
add_post_meta($post_id, '_MY_META_ROW', $value);
}
}
Related
I've got an HTML form that allows a user to submit details about one or more children. Each child can have one or more items.
My HTML form structure looks like this:
<input type="text" name="child[1][name]">
<input type="text" name="child[1][dob]">
I then have a table display for the items (as there are 2 fields per item) where the user can add/remove rows via Javascript. Example:
<table>
<tr>
<td><input type="text" name="child[1][item][name]"></td>
<td><input type="text" name="child[1][item][value]"></td>
</tr>
</table>
When I access this data, I'm inputting the child data into a table, then I need to access the items per child separately, so they can be stored in an 'items' table (with the child ID which I'll grab from the database insert).
In my PHP, I'm looking to achieve something like:
foreach($_POST['child'] as $child) {
$child_name = $child['name'];
$child_dob = $child['dob'];
// insert child data to children table
foreach($_POST['item'] as $item) {
$item_name = $item['name'];
$item_value = $item['value'];
// insert item data to items table
}
}
My problem is that without the items, the child array looks fine when I print_r($_POST['child']). However when I include the items as per my HTML above, the array only outputs the last item added (whether there is one or more). I'm not sure if I'm correctly specifying the array for items in the input tags, or how I should then access in the PHP.
If anyone has any suggestions about where my syntax may be wrong, or if perhaps I'm approaching this in the wrong way, that would be much appreciated.
Figured this out.
As suspected, the naming format was wrong for the item input fields, meaning the sub array for items wasn't being posted correctly.
Updated to:
<input type="text" name="child[1][item][1][name]">
<input type="text" name="child[1][item][1][value]">
I have been reading endless posts today on php arrays, and multidimensional arrays ... I'm a tad confused :)
I have a mySql table that has rows that consist of the following columns:
name, email, choice1, choice2, choice3, choice4, choice5, choice6, choice7, choice8, choice9, choice10
I'm populating an array as follows:
while($row = mysql_fetch_assoc($result)){
$allResults[$index] = $row;
$index++;
}
I know I can access the data by $allResults[0][name]; that all is fine.
However here is what I'm trying to do.
One of the rows will include a piece of data: IPO3_1 (it would be in one of the choice columns)
I need to cycle through all of the rows in the array, find this data and then pull out the name column. Here is very rough code of my ultimate goal (although incomplete as I cannot get my head around this)
<TR>
<?php
if (in_array("IPO3_1", $allResults[0])||in_array("IPO3_1", $allResults[1])) {
?>
<TD WIDTH="30" ALIGN="CENTER"><input type="checkbox" name="sp1" value="IPO3_1" DISABLED></TD>
<?php
} else {
?>
<TD WIDTH="30" ALIGN="CENTER"><input type="checkbox" name="sp1" value="IPO3_1" ></TD>
<?php
}
?>
<TD WIDTH="210" ALIGN="LEFT"> IPO3 - 1st</TD>
<TD WIDTH="40" ALIGN="CENTER">$120</TD>
<TD WIDTH="270" ALIGN="CENTER"><?php echo $allResults[0][name]; ?></TD>
</TR>
Basically when I find the value "IPO3_1" in any row, I need to disable a checkbox , and also add the name into the table
There could be as many as 34 rows in my mySql table maximum. THere are 34 values similiar to "IPO3_1" but are all unique text strings.
Any thoughts to get me pointed in the right direction ?
I've written as undestood the task. But I'm not shure in my undestanding :)
// $row will be false if string will be not found anywhere
// else it will be index of row, in wwhich first occurence found
$row = false;
foreach($allResults as $key=>$value) {
// remove non-'choice' fields
unset($value['name']);
unset($value['email']);
// Lookup the string in other field
if(in_array('IPO3_1', $value, true))
// if found break the loop
{ $row = $key; break; }
}
I am trying to make a marksheet in php, mysql using CodeIgnitor. I have used XAMPP and created a database.to store each record.The data is being retrieved from the database, The new record inserted edited successfully
But the problem is in retrieving the sum. It is not giving me the sum answer means total marks. I am pasting the code here, please help me or correct me If I am wrong, as I am new to CodeIgnitor.
I have also tried the function array_sum(), But it also not giving me any answer. And It is not giving me any error about the code or any query. Either query about sum in marksheet is wrong or the answer can't be fetched by view.
Model/marksheet.php file
function tm() {
$data = array(
'math' => $_POST['math'],
'eng' => $_POST['eng'],
'bio' => $_POST['bio'],
'total_marks' => $_POST['total_marks']);
$this->db->insert('marks', $data);
// $mark = array_sum($data);
$mark= $_POST['math'] + $_POST['eng'] + $_POST['bio'];
return $mark;
}
Controller/welcome.php file
public function sum() {
$data['page_title'] = "New report";
$data['msg']="";
if(isset($_POST['saveit'])) {
$this->Marksheet->tm();
//$data['marks_obt'] = $this->Marksheet->$mark;
$data['msg']="report added";
}
$this->load->view('header',$data);
$this->load->view('welcome_create', $data);
}
view/welcome_create.php file
I want to be my total marks in text box.
<table>
<tr>
<td>MO</td>
<td><input type="text" value="<?php if (isset($mark)) echo $mark ?>"/> </td>
</tr>
<tr>
<td><input type="submit" name="saveit" id="saveit" value="Save"/></td>
<td><input type="button" name="cancel" id="cancel" value="Cancel"/></td>
</tr>
</table>
you need to use $this->input->post('math'); instead of $_POST['math']
var_dump() incoming values if they are string you need to convert them to integer . you can not add these values like: "4" (string) + 5 (integer)
if incoming data is string you need to convert them to integer.
$math = "23";
$math = intval($math);
I'm designing a management interface where admins can modify members of a website all at once (bulk edit). We already have a single edit, but they want to be able to edit all the users at once and I wanted to do it so there is a single "Submit" button. The PHP code should then cycle through each record, look for changes and update as necessary. I know how to do everything but cycle through the records. I've tried creating an array, counting the posts records, I just haven't worked with arrays or mutli-dimensional arrays enough. Here's my existing code on the POST page:
<?php $i = 0;
while($member_list = mysql_fetch_array($getmembers)){
?>
<tr style="vertical-align:top;">
<td><input type="text" name="userid[<?=$i?>]" readonly value="<?php echo $id; ?>" style="width:40px;"></td>
<td><input type="text" name="username[<?=$i?>]" value="<?php echo $member_list['mem_username']; ?>" onchange='changes=true;' style="width:90px;"></td>
<td><input type="text" name="firstname[<?=$i?>]" value="<?php echo $member_list['first_name']; ?>" onchange='changes=true;' style="width:90px;"></td>
<td><input type="text" name="lastname[<?=$i?>]" value="<?php echo $member_list['last_name']; ?>" onchange='changes=true;' style="width:90px;"></td>
<td><input type="text" name="email[<?=$i?>]" value="<?php echo $member_list['email']; ?>" onchange='changes=true;'></td>
<td><textarea name="notes[<?=$i?>]" cols="20" rows="2" onchange='changes=true;'><?php echo $member_list['admin_notes']; ?></textarea></td>
</tr>
<?php $i = $i + 1; } ?>
And it posts to:
$user_records = array(count($_POST['userid'])); // create the array
foreach ($user_records as $value) { // go through array
$user_records[] = array( // Go through the array
$username => $_POST['username'.$i],
$firstname => $_POST['firstname'.$i],
$lastname => $_POST['lastname'.$i],
$email => $_POST['email'.$i],
$notes => $_POST['notes'.$i],
);
echo $username . ' | ' . $firstname; //this line is for testing to display only
// Then posting comparison occurs and is updates as needed
}
So, in a nutshell - when I run the above example, I get 5 pipes (the amount of seperators for the 6 fields) but don't get any data and it doesn't cycle through any of the other records. I know I'm missing a few things, just can't put my head in gear to see it. Any suggestions are greatly appreciated.
NOTE: obviously I'll prevent SQL injection and clean up things in the final version. I'm just trying to figure out the logic here.
You never defined $i inside your processing loop, so you're doing accessing unknown/undefined array keys.
Since you've defined explicit keys in your html via the userid[$i] stuff, your loop should be:
foreach($_POST['userid'] as $key => $userid) {
$username = $_POST["username"][$key];
$firstname = $_POST["firstname"][$key];
etc...
Note how each of your username/firstname/etc... have themselves become arrays. It's not username42, it's username[42] to access that particular field.
I have this table:
foreach( //conditionals ){
<td id="changeStatus">
<input type="text" name="rcStatus" value=""/>
</td>
}
The <td>s are blank, and when I click on each one, the bgcolor of the cell changes, and also the value of rcStatus.
I do using this code:
<script>
$('#table #changeStatus').click(
function(){
var cell = $(this);
state = cell.data('state') || 'first';
switch(state){
case 'first':
cell.addClass('red');
cell.data('state', 'second');
this.getElementsByTagName('input')[0].value = "missed";
break;
// other cases here
}
});
</script>
My problem now is that I need to store the value of rcStatus in my database.
What happens is that I am only able to store the most recently set rcStatus.
I'm using PHP.
foreach( //conditionals ){
mysql_query("INSERT INTO `details`(ID, Name, Status) VALUES('NULL','$_POST[name]','$_POST[rcStatus]');");
}
How can I call each individual variable using $_POST even though I'm using the same name/id?
You could append a number to the name of the input field to identify each input field, see also Multiple inputs with same name through POST in php.
you can use this logic.
$i = 1;
foreach( //conditionals ){
<td id="changeStatus">
<input type="text" name="rcStatus<?php echo $i; ?>" value=""/>
</td>
$i++;
}
Change your <tr> to:
foreach( //conditionals ){
<td id="changeStatus">
// giving [] to a name attribute makes it an input array, like #Smutje mentioned
<input type="text" name="rcStatus[]" value=""/>
</td>
}
And in your PHP:
foreach($_POST[rcStatus] as $key=>$val){
mysql_query("INSERT INTO `details`(ID, Name, Status) VALUES('NULL','$_POST[name]','$val');");
}
A few notes:
Please migrate to mysqli_ functions as mysql_ is
deprecated
You are open to SQL Injection . For the time being, till you migrate, you can use mysql_real_escape_string to escape $_POST values