Page 1 of 1

Possible Array{SOLVED}

Posted: Fri May 14, 2010 9:16 pm
by nite4000
I have a form that has total of 16 boxes. 4 across. now on the first row of 4 its named plan1,min1,max1,percent1, row 2 its plan2,min2,max2,percent2 and so on

Here is my code I have for the form.

Code: Select all

<table cellspacing=0 cellpadding=2 border=0 width=100%>
<tr>
                  <td colspan=3><b>Specify the Rates:</b></td>
</tr><tr>
 <td align=center>#</td>
 <td align=center>Name</td>
 <td align=center>Min Amount</td>
 <td align=center>Max Amount</td>
 <td align=center>Percent</td>
</tr>
<tr>
 <td>1. </td>
 <td><input name="plan1" type=text class=textbox id="plan1" value="Plan 1" size=10></td>
 <td><input name="min1" type=text class=textbox id="min1" style="text-align:right" value="0.00" size=10></td>
 <td><input name="max1" type=text class=textbox id="max1" style="text-align:right" value="100.00" size=10></td>
 <td><input name="perc1" type=text class=textbox id="perc1" style="text-align:right" value="2.20" size=10></td>
</tr>
<tr>
 <td>2. </td>
 <td><input name="plan2" type=text class=textbox id="plan2" value="Plan 2" size=10></td>
 <td><input name="min2" type=text class=textbox id="min2" style="text-align:right" value="0.00" size=10></td>
 <td><input name="max2" type=text class=textbox id="max2" style="text-align:right" value="100.00" size=10></td>
 <td><input name="perc2" type=text class=textbox id="perc2" style="text-align:right" value="2.20" size=10></td>
</tr>
<tr>
 <td>3. </td>
  <td><input name="plan3" type=text class=textbox id="plan3" value="Plan 3" size=10></td>
 <td><input name="min3" type=text class=textbox id="min3" style="text-align:right" value="0.00" size=10></td>
 <td><input name="max3" type=text class=textbox id="max3" style="text-align:right" value="100.00" size=10></td>
 <td><input name="perc3" type=text class=textbox id="perc3" style="text-align:right" value="2.20" size=10></td>
</tr>
<tr>
 <td>4. </td>
 <td><input name="plan4" type=text class=textbox id="plan4" value="Plan 4" size=10></td>
 <td><input name="min4" type=text class=textbox id="min4" style="text-align:right" value="0.00" size=10></td>
 <td><input name="max4" type=text class=textbox id="max4" style="text-align:right" value="100.00" size=10></td>
 <td><input name="perc4" type=text class=textbox id="perc4" style="text-align:right" value="2.20" size=10></td>
</tr>
</table>
as you see i have all the names the same with numbers.

What I want to do is for each row I fll out have it insert into a table if i fill out the first 2 rows then it would insert 2 records. I am thinking its an aray thing but AM not sure.

Any help someone can give would be nice.

Thanks

Re: Possible Array

Posted: Fri May 14, 2010 9:35 pm
by califdon
So this is a data entry form, and each row represents a record, and the user might enter anywhere from 1 to 4 records? I don't see any reason to use an array. You have 16 named input elements, some of which may be blank, right? So just use the first 4 to insert a record, check to see if the next row starts with a blank, and if so, you're done! If not, use the next 4 elements to insert another record, check to see if the next row starts with a blank, and if so, you're done. And so on.

Re: Possible Array

Posted: Sat May 15, 2010 2:11 pm
by nite4000
I was able to do what you said. and check for them being empty and so on but now I gotta see how I can update them when they are displayed.

I am echoing them from the db in a table so should I just put the table and have it echo the info or what.


Not sure you will understand what I mean but if not its ok

Re: Possible Array

Posted: Sat May 15, 2010 2:48 pm
by califdon
You're right, I don't understand your question. If you were able to follow my directions and insert the new records in the table, then that constitutes the updating. You can always display the records in any manner that you please, independent of how or whether you have updated the table.

Re: Possible Array

Posted: Sat May 15, 2010 4:31 pm
by nite4000
I am working on the last part of it now I do appriciate your help.

Thanks
Don

Re: Possible Array

Posted: Sat May 15, 2010 9:02 pm
by nite4000
ok The last thing I am having trouble with is this.

i messed up last time

I have 16 boxes as before and 4 across and the first 4 is named plan1 next 4 is plan 2 and plan 3 and plan 4

Now I want to create a new record into the database for each one but the chkbox of the form next to the first box must me check and then boxes filled out and button pushed to create the new record.

I also want to remove the record if I was to unchk the box and push the button.

I hope you can help.

Re: Possible Array

Posted: Sun May 16, 2010 1:23 pm
by califdon
I don't understand what the checkbox is for.

Re: Possible Array

Posted: Sun May 16, 2010 3:10 pm
by nite4000
Hey to help you out i created a example of what mine looks like without css and all that. i have setup a test db for you to use and you can go here http://www.supportticketpro.com/PHP/test_plans.php


i hope you can help. it would be a blessing if someone could help me get this done.

Re: Possible Array

Posted: Sun May 16, 2010 8:06 pm
by califdon
There's no need to have any checkboxes, that only complicates both the programming and the use of the form. In my first response, I tried to explain how you can do this. In your action script that receives the data, you will have 16 variables. Your PHP logic should test the first 4 to insure that they have been filled in, if they have, insert one record; then go to the next 4, check to see if they have been filled in, if they have, insert another record; then go to the next 4 ... get the idea?

This situation of having an uncertain number of data input elements is often handled in the first form with Javascript. Only one (or the minimum number, whatever that is) row of inputs would be displayed initially, plus a button that says "Add another record." The button would call a Javascript function to add a new line of <input> elements below the previous ones, assigning them names with suffixes just like you are doing now. Your action PHP script would test for the existence of the additional rows using isset(). This entails a little more Javascript, but can be designed to work very smoothly for the user.

Re: Possible Array

Posted: Mon May 17, 2010 6:18 am
by nite4000
so let me see if i understand you totally.

on the add form have just 4 boxes then a button that says "Add New Plan" that then would add 4 new boxes and so on then saving would save them to database.

Then on the edit I could have same thing but show all the plans and have option to add more OR to remove them with chking a box. and saving would update the info.


AM I correct?

Re: Possible Array

Posted: Mon May 17, 2010 8:45 am
by nite4000
ok i am starting to get it set like you suggest however I am having a small problem.
Here is my php code

Code: Select all

<?php
	require_once("db.php");
		$dbc = db_connect() or die(mysql_error());

	require_once('initialize.inc.php');


if(isset($_POST['go'])) {
extract( $_POST );

foreach ($_POST['plan'] as $row=>$name) 
{ 
$plan = mysql_real_escape_string($_POST['plan'][$row]); 
$min_d = mysql_real_escape_string($_POST['min'][$row]); 
$max_d = mysql_real_escape_string($_POST['max'][$row]); 
$rate = mysql_real_escape_string($_POST['percent'][$row]); 
$q=mysql_query("INSERT into plans(id,plan_name,min_deposit,max_deposit,percent)VALUES(null,'$plan','$min_d','$max_d','$rate')")or die(mysql_error());

}
}
?>
Here is my javascript code

Code: Select all

<script language="javascript">
row_no=0;
function addRow(tbl,row){
row_no++;
if (row_no<=5){
if (row_no>=10){
var textbox  = '<input type="text" size = "20"  maxlength= "20" name= plan[]>';}
if (row_no<10){
var textbox  = '<input type="text" size = "20"  maxlength= "20" name= plan[]>';}
var textbox2 = '<input type="text" size = "20" maxlength= "20" name= min[]>';
var textbox3 = '<input type="text" size = "20" maxlength= "20" name= max[]>';
var textbox4 = '<input type="text" size = "20" maxlength= "20" name= percent[]>';
var tbl = document.getElementById(tbl);
var rowIndex = document.getElementById(row).value;
var newRow = tbl.insertRow(row_no);
var newCell = newRow.insertCell(0);
newCell.innerHTML = textbox;
var newCell = newRow.insertCell(1);
newCell.innerHTML = textbox2;
var newCell = newRow.insertCell(2);
newCell.innerHTML = textbox3;
var newCell = newRow.insertCell(3);
newCell.innerHTML = textbox4;

}
if (row_no>5){
alert ("Too Many Items. Limit of 5.");
}
}
</script>
Here is my HTML form

Code: Select all

<form name="invoice" method="post" action="pack2.php">
<table width="607" border="0" cellspacing="0" cellpadding="2" id="table1">
<th width="150">Plan Name </th>
<th width="151">Min Deposit </th>
<th width="147">Max Deposit </th>
<th width="152">Percent</th>

<tr id="row1">
</tr>
</table><input type="button" name="add" value="Add Plan" onClick="addRow('table1','row1')">
<br>
<br>
<input type="submit" name="go" value="Save"></form>
What I need is to save the info. If i add 5 rows and then click save I need each one to save as a record. but not sure how to do it with it having [] on the textbox names i am getting array in the table.

Please help

Re: Possible Array

Posted: Mon May 17, 2010 12:05 pm
by nite4000
anyone?

Re: Possible Array

Posted: Mon May 17, 2010 1:34 pm
by califdon
Your PHP code looks like it should work, but I'm totally lost in your Javascript code. If you want to implement this approach (which I described as common, but I've never tried to write the code for it, myself), you'll have to work out how to add new rows to your HTML table. I'm not sure, at the moment, the best way to do that. I've often used the InnerHtml property of CSS elements, but that seems like it would be awkward in this situation. I would search Google for something like "javascript expanding table rows" or something.