Page 1 of 3
For() Loops with If Statemements
Posted: Sun Nov 21, 2004 3:12 pm
by wadesmart
11212004 1511 GMT-6
I have a long page with lots of code and in this one section I have a for() loop that checks a bunch of variables. For each variable I use a if(isset) to see if I need to repost the informatin. Im having trouble with the fact that the information inside the for() loop is not staying inside the loop, meaning, the php parser isnt seeing the closing bracket, and I cant figure out why. I have spent several hours just counting and simplying code to make sure the correct number of bracket are present.
Wade
Posted: Sun Nov 21, 2004 3:17 pm
by rehfeld
we cant help unless you post your code
make sure to use code tags like
Posted: Sun Nov 21, 2004 4:38 pm
by wadesmart
11212004 1633 GMT_6
Code: Select all
$form_block = "
<h3>Account Information Missing</h3>
<form method = "post" action = "$_SERVER[PHP_SELF]">
";
for($j=1; $j<$_POST['Accts']; $j++){
$form_block .= "
<p>Account "$count2"</p>
<ul>
";
if(empty($_POST['name'])){
$form_block .= "
<li>Account Name: <input type = "text" name = "name" value = $name> </li>
";
}
else {
$form_block .= "
<li>Account Name: $name</li>
";
}
if(empty($_POST['number'])){
$form_block .= "
<li>Number: <input type = "text' name = "number" value = $number></li>
";
}
else {
$form_block .= "
<li>Account Number: $number</li>
";
}
if(empty($_POST['telephone'])){
$form_block .= "
<li>Telephone: <input type = "text" name = "telephone" value = $telephone></li>
";
}
else {
$form_block .= "
<li>Account Telephone: $telephone</li>
";
}
if(empty($_POST['acct_holder'])){
$form_block .= "
<li>Account Owner: <input type = "text" name = "acct_holder" value = $acct_holder></li>
";
}
else {
$form_block .= "
<li>Account Owner: $acct_holder</li>
";
}
if(empty($_POST['status'])){
$form_block .= "
<p>Account Status: <br \>
<input type = "radio" value = "Open" name = "AcctStatus" CHECKED>Open <br \>
<input type = "radio" value = "Closed" name = "AcctStatus" >Closed <br \>
<input type = "radio" value = "Open_Paid_Off" name = "AcctStatus" >Open but Paid Off <br \>
<input type = "radio" value = "Closed_Paid_Off" name = "AcctStatus" >Closed but Paid Off <br \>
<input type = "radio" value = "Open_Suspended" name = "AcctStatus" >Open but Suspended <br \>
</p>
<br />
";
}
else {
$form_block .= "
<li>Account Status: $status</li>
";
}
";
$count2++
}
Posted: Sun Nov 21, 2004 5:23 pm
by rehfeld
look at line 75
you prob get an error message pointing to that line, right?
you also forget to use a ; after count++
Posted: Sun Nov 21, 2004 6:38 pm
by wadesmart
11212004 1832 GMT-6
I have been looking at this for way too long. I should have seen that. But, no, I didnt get an error and that was part of the problem.
Code: Select all
<?php
for($i=0; $i<$_POST['num_of_accts']; $i++) {
$form_block .="
<p>Account Information: "$count"<br \>
<ul>
<li>Account Name: <input type = "text" name = "name"></li>
<li>Number:<input type = "text" name = "number"></li>
<li>Telephone: <input type = "text" name = "telephone"></li>
<li>Account Owner: <input type = "text" name = "acct_holder"></li>
</ul>
</p>
<p>Account Status: <br \>
<input type = "radio" value = "Open" name = "AcctStatus" >Open <br \>
<input type = "radio" value = "Closed" name = "AcctStatus" >Closed <br \>
<input type = "radio" value = "Open_Paid_Off" name = "AcctStatus" >Open but Paid Off <br \>
<input type = "radio" value = "Closed_Paid_Off" name = "AcctStatus" >Closed but Paid Off <br \>
<input type = "radio" value = "Open_Suspended" name = "AcctStatus" >Open but Suspended <br \>
</p>
<br />
";
$count++;
}
?>
Question. Im cycling through a set of accounts and I need to set for each one a set of radio buttons. As it sits now, only 1 out of all the accounts can be set. So, you can set account 1 to say Open but all the other acconts will be deselected. How to you seperate this?
Wade
Posted: Sun Nov 21, 2004 8:08 pm
by rehfeld
if you did not get an error, you have turned error_reporting off or changed it from its default level
you should not do this while developing. you want to see the errors, because they need to be fixed, not ignored.
its common to develop w/ error_reporting(E_ALL);
to change each entry in a db, youll need to loop through them all. youll need to post more code in order for anyone to be able to help
but im thinking your problem is your trying to use 1 form to submit a bunch of fields w/ the same name. you need to put [] after the name in the html so that php knows more than 1 field w/ the same name should be stored into an array
put this in a new php file and try it
Code: Select all
<?php
$accounts = $_POST['accounts'];
for ($i=0; $i < count($accounts); $i++) {
echo "<p>account number $i value is: ".$accounts[$i]."</p>";
}
?>
<form method="post">
<input type="text" name="accounts[]"><br>
<input type="text" name="accounts[]"><br>
<input type="text" name="accounts[]"><br>
<input type="text" name="accounts[]"><br>
<input type="submit"><br>
</form>
Posted: Mon Nov 22, 2004 11:58 am
by wadesmart
11222004 1150 GMT-6
rehfeld, you have lost me.
Im still just learning and this material and Im making strides but, Im not fully up to speed on what you just described. I get what you wrote, just not how to apply it.
So, to that end, I have supplied all the code for this one page.
A quick explaination: This page before this has links to create new accounts or new bills. This page creates new account. What Im doing, all on one page, starting at the bottom of this page and with each iteration the code moves up the page, is create the accounts and test the information provided.
The bottom part asks how many accounts you require.
The next, part 2, shows the number of accts you want. Here you fill in the information.
The next part, part 3, checks to see if you filled in all the equired information. If its not present then it reposts that one field. If it is present, it posts what you put. This section is where I have been stuck on so its not finished.
The fourth part will ask if the information is all correct before posting to the database. I could do this in part three, and I might in the end.
Code: Select all
<?php
// session information is not being used for testing at this time.
//session_start();
//if(!isset($_SESSION['auth'])) {
// header("Location:http://localhost:8080/smart%20financial/login.php");
// exit;
//}
//if(!$_SESSION['auth'] == 1){
// header("Location:http://localhost:8080/smart%20financial/login.php");
// exit;
//}
//else
//{
// Add New Accounts
// 4) Query User for Mistakes
if(isset($_POST['CheckAcc']) && isset($_POST['not'])) {
// echo back entered information with checkbox for error information
$form_block = "
<h3>New Accounts to Add</h3>
<form method = "post" action = "$_SERVER[PHP_SELF]">
";
$count4 = "1";
for($k=0; $k<$_POST['CheckAcc']; $k++) {
// variables for data
$name = $_POST["name"];
$number = $_POST["number"];
$telephone = $_POST["telephone"];
$acct_holder = $_POST["acct_holder"];
$status = $_POST["status"];
$form_block .= "
<p>Account $count4</p>
<ul>
<li>Account Name: $name </li>
<li>Account Number: $number </li>
<li>Account Telephone: $telephone</li>
<li>Account Owner: $acct_holder </li>
<li>Account Status: $status</li>
</ul>
<input type= "checkbox" name="redo[$count3]" value= "change"><br />
";
$count4++;
}
$form_block .= "
<input type = "hidden" name = "reviewed">
<input type = "hidden" name = "checked" value = "not">
<input type = "submit" name = "CheckAcc" value = "Add New Accounts">
</form>
";
}
// 3) Check Account Information for Completness
if(isset($_POST['toCheck'])) {
// variables for data
$name = $_POST['name'];
$number = $_POST['number'];
$telephone = $_POST['telephone'];
$acct_holder = $_POST['acct_holder'];
$status = $_POST['status'];
// Check each one of the lines for each account
// if any one of them is blank, reshow only that line
// post the other information for them to see.
$count2 = "1";
$form_block = "
<h3>Account Information Missing</h3>
<form method = "post" action = "$_SERVER[PHP_SELF]">
";
for($j=1; $j<$_POST['Accts']; $j++){
$form_block .= "
<p>Account "$count2"</p>
<ul>
";
if(empty($_POST['name'])){
$form_block .= "
<li>Account Name: <input type = "text" name = "name" value = $name> </li>
";
}
else {
$form_block .= "
<li>Account Name: $name</li>
";
}
if(empty($_POST['number'])){
$form_block .= "
<li>Number: <input type = "text' name = "number" value = $number></li>
";
}
else {
$form_block .= "
<li>Account Number: $number</li>
";
}
if(empty($_POST['telephone'])){
$form_block .= "
<li>Telephone: <input type = "text" name = "telephone" value = $telephone></li>
";
}
else {
$form_block .= "
<li>Account Telephone: $telephone</li>
";
}
if(empty($_POST['acct_holder'])){
$form_block .= "
<li>Account Owner: <input type = "text" name = "acct_holder" value = $acct_holder></li>
";
}
else {
$form_block .= "
<li>Account Owner: $acct_holder</li>
";
}
if(empty($_POST['status'])){
$form_block .= "
<p>Account Status: <br \>
<input type = "radio" value = "Open" name = "AcctStatus" CHECKED>Open <br \>
<input type = "radio" value = "Closed" name = "AcctStatus" >Closed <br \>
<input type = "radio" value = "Open_Paid_Off" name = "AcctStatus" >Open but Paid Off <br \>
<input type = "radio" value = "Closed_Paid_Off" name = "AcctStatus" >Closed but Paid Off <br \>
<input type = "radio" value = "Open_Suspended" name = "AcctStatus" >Open but Suspended <br \>
</p>
<br />
";
}
else {
$form_block .= "
<li>Account Status: $status</li>
";
}
$count2++;
}
}
// 2) Fill In New Account Information
if(isset($_POST['Accts'])){
// get number of account fields
// variables
$accountNum = "1";
$num_of_accts = $_POST['num_of_accts'];
$form_block = "
<h3>Add New Account</h3>
<form method = "post" action = "$_SERVER[PHP_SELF]">
";
// create loop to create required accts
for($i=0; $i< count($num_of_accts); $i++) {
$form_block .="
<p>Account Information: "$num_of_accts[$i]"<br \>
<ul>
<li>Name: <input type = "text" name = "name"></li>
<li>Number: <input type = "text" name = "number"></li>
<li>Telephone: <input type = "text" name = "telephone"></li>
<li>Owner: <input type = "text" name = "acct_holder"></li>
</ul>
</p>
<p>Account Status: <br \>
<input type = "radio" value = "Open" name = "AcctStatus" >Open <br \>
<input type = "radio" value = "Closed" name = "AcctStatus" >Closed <br \>
<input type = "radio" value = "Open_Paid_Off" name = "AcctStatus" >Open but Paid Off <br \>
<input type = "radio" value = "Closed_Paid_Off" name = "AcctStatus" >Closed but Paid Off <br \>
<input type = "radio" value = "Open_Suspended" name = "AcctStatus" >Open but Suspended <br \>
</p>
<br />
";
//$count++;
}
$form_block .="
<input type = "submit" name = "toCheck" value = "Add New Accounts">
</form>
";
}
else { // 1) Get Number of Accounts to Create
$form_block = "
<h3>Add New Accounts</h3>
<form method = "post" action = "$_SERVER[PHP_SELF]">
<p>How many accounts required: <input type = "text" name = "num_of_accts" size = "2"></p>
<input type = "submit" name = "Accts" value = "Create Accounts">
</form>
";
}
?>
<html>
<head>
<title>Smart Family Financial: How Many Accounts</title>
</head>
<body>
<h1>Account Information</h1>
<h3>Current Accounts</h3>
<p> Wade : Citibank <br /> Kathaleen : Chase Financial</p>
<br />
<?php echo "$form_block"; ?>
</body>
</html>
Posted: Mon Nov 22, 2004 12:37 pm
by rehfeld
and you problem is that your only getting the information from the last set of radio buttons, right?
its looks like its because you using the same name on each "set" of radio buttons. the last set of radio buttons in the form will override the ones before it, because they share the same name.
try the code as i posted. its not meant to be able to be plugged into your code, its meant to show you the concept of what you need to do.
to show you how to make it work w/ your code would require a lot of re writing, and would prob just fix it for you, yet you may not understand it. im trying to teach you so you can understand it.
just put it in a NEW BLANK php file and run it. play with it, try and see how/why its working.
the key thing is, instead of using <input name="accounts"
im using name="accounts[]"
the square brackets tell php that you would like the data to be put into an array, so that you can accept more than 1 form field w/ the same name. and now you will have to loop through that array to get the data.
and yes, accepting data from form fields like this can get kinda complex. thats why im trying to give you an example in its most basic form, to help you grasp the concept. personally, unless i really have to do it this way, i try to avoid it because it can get very involved.
maybe someone else can chime in and try to explain better, i often write stuff that makes sense to me, but not others.
also, this is wrong, assuming $num_of_accts contains a number value
Code: Select all
for($i=0; $i< count($num_of_accts); $i++) {
// change it to
for($i=0; $i< $num_of_accts; $i++) {
count() works on arrays, not strings or integers etc...
Posted: Mon Nov 22, 2004 6:16 pm
by wadesmart
11222004 1812 GMT-6
Ok. Im just not getting it. I played with your example and I do understand how that works but applying that to what I have - Im just making things worse.
Im looking at my code, and just tell me if Im even on the right track here, Im asking how many accounts need to be created. This is a number = 2 or 4 new accounts. For each account created I need five values to be filled in. What your saying is, by using accounts[] Im creating an array. Each account is an array with five values each. Right? What Im having trouble understanding is, when the account information fields are created, does each field - name, number, telephone, acct_holders, status - have [] after it? -> name[], number[]...... I thought maybe it was just the status field but that doesnt do anything for the radio buttons. Still only one can be selected.
Give me a hint.
Wade
Posted: Mon Nov 22, 2004 9:40 pm
by rehfeld
if a single html <form>
will contain more than 1 field w/ the same name, you need to use the [] braces after its name like i did.
if you do not, then the data of only one of them will be avail to php. you obviously want the data from all of them.
so, yes name, number etc should be
name="name[]"
name="number[]"
just look at the html source your script outputs. Can you find name="somename" more than one time in the same form? if so, you need to use name="somename[]"
then you will also need to loop through the data when you receive it
Code: Select all
<?php
$accounts = $_POST['accounts'];
$names = $_POST['name'];
$phone_numbers = $_POST['numbers'];
for ($i=0; $i < count($accounts); $i++) {
echo "<p>account number $i value is: ".$accounts[$i]."</p>";
echo "<p>Name number $i value is: ".$names[$i]."</p>";
echo "<p>Phone number $i value is: ".$phone_numbers[$i]."</p>";
}
?>
Posted: Tue Nov 23, 2004 10:34 am
by wadesmart
11232004 1029 GMT-6
Ok. I started work on this last night and just read your posting this morning. I thought I was onto something with this code but.... had a problem:
Code: Select all
<?php
<html>
<head></head>
<body>
<?php
// check for submission
if (!isset($_POST['submit'])) {
// and display form
?>
<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post">
<p>How many cars?<input type = "text" name = "number" >
<input type = "hidden" name = "step2">
<input type = "submit" name = "submit" value = "Go"><br />
</form>
<?php
}
if(isset($_POST['step2'])){
?>
<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post">
<?php
for($x=0; $x < $_POST['number']; $x++) {
?>
<p>Name: <input type = "text" name = "name[]">
<p>Engine: <input type = "text" name = "engine[]">
<p>Number of Doors: <br />
<input type = "radio" name = "doors[]" value = "two">Two
<input type = "radio" name = "doors[]" value = "three">Three
<input type = "radio" name = "doors[]" value = "four">Four
<?php
}
?>
<input type = "hidden" name = "step3">
<input type = "submit" name = "submit2" value = "Next">
</form>
<?php
}
else {
if(isset($_POST['step3'])) {
foreach ($_POST['name'] as $a) {
echo "Name:<i>$a</i><br />";
}
foreach ($_POST['engine'] as $b) {
echo "Engine:<i>$b</i><br />";
}
foreach ($_POST['doors'] as $c) {
echo "Doors: <i>$c</i><br />";
}
}
}
?>
</body>
</html>
?>
For each car you choose the number of doors but, I still can only choose one radio button in all that are printed.
Im looking at the source code from the broswer and I can see [] but, how do you - get- the information printed the first time? I mean, with my project, on the page if you select to add ten accounts then for each account you are going to have a number of radio button options. How to do seperate the radio button options from the first account so if you choose the first option on the first account you do not deselect the second option from the second account?
Im going to keep at this. Im just not seeing it yet.
Wade
Posted: Tue Nov 23, 2004 11:44 am
by patrikG
just a side-note: you don't need to manually add a timestamp to your posts wadesmart. The forum adds date and time automatically when you post on a thread.
Posted: Tue Nov 23, 2004 11:47 am
by wadesmart
Out of habit. Its on every email and everything I do
Posted: Tue Nov 23, 2004 12:14 pm
by wadesmart
11232004 1205 GMT-6
I have read somewhat vague tutorials about arrays and Im starting to get it. Have some questions though.
I understand now the [] and how by just putting that at the end of a variable makes that an array. Looking over all my code and referencing what has been said here, I get what your original posting meant.
The one thing Im still stuck on is the radio option. I have found many tutorials talking about the radio options but none about the radio options.
If I want to create three groups of items, Acct1, Acct2, Acct3, then each group needs to have its own name for its group of radio boxes =
Code: Select all
<?php
<input type = "radio" name = "acct1_status" value = " Open " > Open
....
<input type = "radio" name = "acct2_status" value = " Open " > Open
....
and so on
?>
I thought I could get tricky and do something like this:
Code: Select all
<?php
<input type = 'radio' name = $count."status" value = "open" > Open
?>
thinking that if Im counting the number of accounts to be created that by appending something to the beginning of each group would help but, that didnt work.
So... what I still have to figure out is, how to tell one group from another group.
Wade
Posted: Tue Nov 23, 2004 3:52 pm
by rehfeld
this type of stuff confused the <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> out of me at one time too. and there wasnt much help to be found(or at least i couldnt find much). thats why im trying to help you.
maybe youll grasp the concept better w/ this, its a much more visual method
Code: Select all
<?php
// i thought showing you how to use forms as arrays might be easier,
// but maybe this other method will make more sense to you.
// you dont need to use fieldset and legend, its just my style
$num_accounts = 3; // im just manually setting the num for this demo,
// $num_accounts = $_POSTї'num_accounts'];
echo '
<form method="post" action="receive_form.php">
<input type="hidden" name="num_accounts" value="'.$num_accounts.'">
';
for ($i = 0; $i < $num_accounts; $i++) {
echo '
<fieldset>
<legend>Account Number: '.$i.'</legend>
<p><input type="text" name="name-'.$i.'"> Name</p>
<p><input type="text" name="engine-'.$i.'"> Engine</p>
<p>Number of Doors: </p>
<div><input type="radio" name="doors-'.$i.'" value="two"> Two</div>
<div><input type="radio" name="doors-'.$i.'" value="three"> Three</div>
<div><input type="radio" name="doors-'.$i.'" value="four"> Four</div>
</fieldset>
';
}
echo '
<p><input type="submit" name="submit" value="Submit"></p>
</form>
';
?>
Code: Select all
<?php
/*
receive_form.php
*/
$num_accounts = $_POSTї'num_accounts'];
for ($i = 0; $i < $num_accounts; $i++) {
echo '<p>Data for account number: '.$i.'</p>';
echo '<p>'.$_POSTї'name-'.$i].'</p>';
echo '<p>'.$_POSTї'engine-'.$i].'</p>';
echo '<p>'.$_POSTї'doors-'.$i].'</p><br><br><br>';
}
echo '<pre>';
print_r($_POST);
?>