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);
Related
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 somewhat new to PHP but have created almost everything I need, except one thing which has led me here. I am creating a simple (for you probably but not for me) site for a friend so they can keep track of when they receive rent payments, input new tenants, and check current balances. I had everything working perfect...I thought. When checking the balance, I tested with only one word input to read the text file and output the proper info. But, what I found is if the first and last name are stored with a space between then, I am not getting a match. I need code to read the inputted tenant name in its entirety with the space between. I haven't found anything I could really comprehend and have been searching for several nights. Here is my full code for searching and retrieving results.
The text file being searched is like this:
John Doe,123 Main St,400.00,01/01/2016,
Jane Doe,124 Main St,300.00,01/01/2016,
John Doe,123 Main St,,01/03/2016,200.00
<?php
$lines = file('data.txt'); //here's the filename
?>
<center>
<table id="historytable" width="640" border="1">
<caption>Billing & Payment History
</caption>
<tbody>
<tr>
<td width="40">Tenant</td>
<td width="40">Address</td>
<td width="40">Date</td>
<td width="40">Rent</td>
<td width="40">Payment</td>
</tr>
<?php
$search = $_POST["name"]; //assigning a string to each piece of input data
// Store true when the text is found
$balance = 0; //assign initial value to balance
$renttotal = 0; //assign initial value to renttotals
$received = 0; //assign initial value to received
$found = false;
foreach ($lines as $line) { //this is the loop to read the txt file
{
if(strpos($line, $search) !== false)
{
$found = true;
list($a,$b,$c,$d,$e) = explode(',', $line); //this assigns a variable name to each data item separated by a comma
$renttotal+= $c; //each time it loops, it gathers the value of c adding it to itself same for the two lines below
$received+= $e;
$balance = $renttotal - $received; //subtracts the final value of renttotal and received assigning the difference to balance
$line_array = explode(",", $line); //breaks each piece of data apart to be placed in a table }
echo "<tr>
<td width=40>$a</td>
<td width=40>$b</td>
<td width=40>$c</td>
<td width=40>$d</td>
<td width=40>$e</td>
</tr>";
}}}
?>
</tbody>
</table>
</center>
<BR />
<BR />
<center>
<p>TOTAL RENTS CHARGED $<?php echo "$renttotal"?><br />
TOTAL PAYMENTS RECEIVED $<?php echo "$received"?><br />
BALANCE DUE $<?php echo "$balance"?></p>
Thank you in advance for any help.
Simply do a urldecode() like so:
$search = urldecode($_POST["name"]);
And you should be fine afterwards.
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
I am doing a project in which as per number getting by GET method, I display dynamic number of HTML Textbox for storing Multiple values. I am giving each textbox unique name+id in ascending manner starting from 1(Like textbox1,textbox2). Now I want that when I click on submit button, it should fire an insert statement which insert all textbox values at once. I know I can do by array, but my question is that how to get all textbox's value in an array and How to perform insert statement?
I have done following code:
Here is PHP Code for submit button:
$schedules = array();
if(isset($_POST['submit']))
{
for($d=1; $d<=$_GET['totalDay'] ;$d++)
{
array_push($schedules,$_POST['txtSchedule'.'$d']);
}
print_r($schedules);
}
Here is the html code:
<form method="post">
<table>
<tr>
<td>Day</td>
<td>Schedule</td>
</tr>
<?php
if(isset($_GET['tour_code']) and ($_GET['totalDay']!=1))
{
$tour_code = $_GET['tour_code'];
$total = $_GET['totalDay'];
$i=0;
do
{
$i=$i+1;
?>
<tr>
<td><?php echo $i;?></td>
<td>
<input name="txtSchedule<?php echo $i;?>" type="text" size="30"/>
</td>
</tr>
<?php
$start = date('Y-m-j',strtotime($start.'+1 days'));
}while($i!=$total);
}
?>
</table>
<input type="submit" name="submit" value="Add Tour Details" />
But I am getting an empty array.
Note: $total is coming through URLString's $GET method.
Below is the output of HTML:
Simplest thing first. You have an error, you can't use
array_push($schedules,$_POST['txtSchedule'.'$d']);
You must use DOUBLE QUOTES on the $d (single quotes won't evaluate d, it will literally read "txtSchedule$d" with a dollar sign, and not actually 0, 1,..., n)
array_push($schedules,$_POST['txtSchedule'."$d"]);
//or no quotes at all
array_push($schedules,$_POST['txtSchedule'.$d]);
(that may sovlve your problems)
But now let's get to how to make an array available to the $_POST object in the processing page via form naming conventions
You're not using array syntax, but you are oh-so close. In PHP, whatever is submitted needs to be of an expected format, and iterating txtSchedule0, txtSchedule1, ...txtScheduleN is not an Array(), but $_POST[] is an array that contains each (given what you've named your input fields, which is missing 1 small thing - square brackets).
What you need to do is be naming your inputs as an array is the array name followed by square brackets (arrayName[]), here is how you create an input array of the name txtSchedule (that way when you print_r($_POST['txtSchedule']) you get an Array())
<input name="txtSchedule[<?php echo $i;?>]" type="text" size="30"/>
I had the same issue when I started in PHP, you were forgetting the square brackets around [<?php echo $i;?>]
Just make sure that if you want to do an iteration over an array of inputs:
for($i=0; $i < count($_POST['txtSchedule']); $i++){
echo "They entered " . $_POST['txtSchedule'][$i] . " in the $i" . "th position";
}
... you have used the <input name="arrayName[$i]"> sytax, or even more simply <input name="arrayName[]"> for it to auto-magically generate an array on submit in the order the inputs were in the HTML page. The naming convention is so important, and since you have it wrong (you used arrayName0, arrayName1, ... arrayNameN instead of arrayName[0], arrayName[1], ... arrayName[n]), it will never be available to you as an array.
if i understand your question correctly you are trying to retrive user input from each textbox and save it in an array?
if so I would use jquery to select all textboxes and loop through them and retrive the value
If you are looking purely at the SQL syntax, then you can just append extra records to insert at the end of your query by providing more value sets:
INSERT INTO myTable (fieldName1, fieldName2) values ("Value1A", "Value1B"), ("Value2A", "Value2B")
If you looking at the PHP logic, then my first suggestion is to use the http POST method instead of GET. Then start with processing the $_POST fields:
$data= array();
foreach($_POST as $key => $value) {
if (preg_match('/^TextBox\d+$/', $key)) {
$data[] = $mysqli->real_escape_string($value);
}
}
The construct the SQL query based on the available data
if (count($data) > 0) {
$sql = 'INSERT INTO `myTable` VALUES("' . implode('"),("', $data).'")';
// log query
// execute query
// process query results
// redirect user to a thankyou page
header('Location: thankyou.php');
}
Note that the code assumes that you have a mysqli connection instance available at $mysqli
Not sure if this is what you are looking for but should give you at least a start..
String []ar=request.getParameterValues("name");
String cmd=request.getParameter("cmd");
if(cmd==null) cmd="";
if(cmd.equals("Submit")){
for(int i=0;i<ar.length;i++) {
insert logic;
<form method="post" action="page3.jsp">
<br/><input type="text" name="name"/>
<br/><input type="text" name="name"/>
<br/><input type="text" name="name"/>
<br/> <input type="submit" value="Submit" name="cmd"/>
</form>
Orignal post http://www.daniweb.com/web-development/jsp/threads/197777/insert-dynamic-textbox-value-in-database