[SOLVED] For() Loops with If Statemements
Moderator: General Moderators
you keep retyping the code i gave you. your switching single and double quotes around and its causing you problems.
why not just folllow the format i gave you? whats wrong with it? it worked didnt it?
take a close look at how i use single and double quotes, in everything ive posted.
seriously, cut the code down to smaller parts and work on ONE thing at time. your going to drive yourself mad if you keep trying to change a bunch of things all at once that you dont yet understand
why not just folllow the format i gave you? whats wrong with it? it worked didnt it?
take a close look at how i use single and double quotes, in everything ive posted.
seriously, cut the code down to smaller parts and work on ONE thing at time. your going to drive yourself mad if you keep trying to change a bunch of things all at once that you dont yet understand
-
wadesmart
- Forum Commoner
- Posts: 38
- Joined: Sat Oct 02, 2004 10:10 pm
- Location: US, Oklahoma
- Contact:
Ok. Worked on this more after some sleep and got it to work with this:
I got the order of all the quotes correctly placed. I had to carefully re and reread your quote order posting earlier.
The problem I have is that its only posting one loop. If you use this with say two cars, only car number two will post.
Code: Select all
<?php
<?php
if(isset($_POST['submit3'])){
for($i=0; $i< $_POST['number']; $i++) {
$form_block = '
<fieldset>
<p>Car '.$i.' is
a '.$_POST['name-'.$i].' and has
a '.$_POST['engine-'.$i].' engine
with '.$_POST['doors-'.$i].' doors.</p>
</fieldset>
';
}
}
if(isset($_POST['submit'])) {
$form_block = '
<form method = "post" action = "page3_a.php">
';
$number = $_POST['number'];
for($i = 0; $i < $_POST['number']; $i++) {
$form_block .='
<fieldset>
<p>Name: <input type = "text" name = "name-'.$i.'">
<p>Engine: <input type = "text" name = "engine-'.$i.'">
<p>Number of Doors: <br />
<input type = "radio" name = "doors-'.$i.'" value = "two">Two
<input type = "radio" name = "doors-'.$i.'" value = "three">Three
<input type = "radio" name = "doors-'.$i.'" value = "four">Four
</fieldset>
';
}
$form_block .= '
<input type = "hidden" name = "number" value = "'.$number.'">
<input type = "submit" name = "submit2" value = "Next">
';
}
else {
$form_block = '
<form method = "post" action = "page2_a.php">
<p>How many cars? <input type = "text" name = "number" >
<input type = "submit" name = "submit" value = "Go"><br />
</form>
';
}
?>
<html>
<head></head>
<body>
<?php echo "$form_block";?>
</body>
</html>
?>The problem I have is that its only posting one loop. If you use this with say two cars, only car number two will post.
use $form_block .=
your doing $form_block =
also, stop doing this:
some browsers may interpret that as a mistake,
and may correct it for you in a way that will break you code(like adding spaces into the value)
and when your posting your sample code, please make sure you are actually posting the code your currently using. i say this because theres no way possible the above code could even show 1 of the cars.
o the second form, you have
<input type="submit" name="submit2"
and then to display, you do
if(isset($_POST['submit3']))
where did submit3 come from? so try to be accurate, because people have no choice but to assume the code your posting is what your actually using.
also, its clear you have seperated the code into multiple pages, judgin by the action="page2a.php" stuff
it helps other help if you keep things as they are. either by specifying in the post that they are indeed sperate pages, or by using different blocks of code like so
to get the name of the file above the code block like i just did, do this
your doing $form_block =
also, stop doing this:
Code: Select all
<input type = "text" name = "foo" value = "value">
do like this:
<input type="text" name="foo" value="value">some browsers may interpret that as a mistake,
and may correct it for you in a way that will break you code(like adding spaces into the value)
and when your posting your sample code, please make sure you are actually posting the code your currently using. i say this because theres no way possible the above code could even show 1 of the cars.
o the second form, you have
<input type="submit" name="submit2"
and then to display, you do
if(isset($_POST['submit3']))
where did submit3 come from? so try to be accurate, because people have no choice but to assume the code your posting is what your actually using.
also, its clear you have seperated the code into multiple pages, judgin by the action="page2a.php" stuff
it helps other help if you keep things as they are. either by specifying in the post that they are indeed sperate pages, or by using different blocks of code like so
Code: Select all
// codeCode: Select all
// more codeCode: Select all
// even moreto get the name of the file above the code block like i just did, do this
Code: Select all
-
wadesmart
- Forum Commoner
- Posts: 38
- Joined: Sat Oct 02, 2004 10:10 pm
- Location: US, Oklahoma
- Contact:
Ok. I changed all the input name = value to input name=value.
I dont understand though what you mean about the $form_block.
I thought if you have a single script that you need to break into parts, you sue $form_block = ' at the beginning and then with the parts you use $form_block .= ' . If you use $form_block .= every where you get errors like this one:
and that code comes from this changed code script:
I noticed that this works fine when using three seperate pages but putting it together causes problems. Is that because of the way echo works compared to me using $form_block variable to hold code?
rehfield, I want to tell you I really appreciate you helping so much with this code. Sometimes books just dont work and tutorials and the best way is just working at it - and its with people like you who help - that people like me learn best.
Thanks.
Wade
I dont understand though what you mean about the $form_block.
I thought if you have a single script that you need to break into parts, you sue $form_block = ' at the beginning and then with the parts you use $form_block .= ' . If you use $form_block .= every where you get errors like this one:
Code: Select all
Notice: Undefined variable: form_block in C:\WebDev\Work Directory\Zend\Arrays\single\All_in_One.php on line 36Code: Select all
<?php
if(isset($_POST['submit']) || isset($_POST['submit2'])) {
if(isset($_POST['submit'])) {
$form_block .= '
<!-- <form method = "post" action = "page3_a.php"> --!>
<form method="post" action='.$_SERVER['PHP_SELF'].'>
';
$number = $_POST['number'];
for($i=0; $i < $_POST['number']; $i++) {
$form_block .='
<fieldset>
<p>Name: <input type="text" name="name-'.$i.'">
<p>Engine: <input type="text" name="engine-'.$i.'">
<p>Number of Doors: <br />
<input type="radio" name="doors-'.$i.'" value="two">Two
<input type="radio" name="doors-'.$i.'" value="three">Three
<input type="radio" name="doors-'.$i.'" value="four">Four
</fieldset>
';
}
$form_block .= '
<input type="hidden" name="number" value="'.$number.'">
<input type="submit" name="submit2" value="Next">
';
}
if(isset($_POST['submit2'])){
//var_dump($_POST);
//echo '<pre>';
//print_r($_POST);
for($i=0; $i< $_POST['number']; $i++) {
$form_block .= '
<fieldset>
<p>Car '.$i.' is a '.$_POST['name-'.$i].' and has a '.$_POST['engine-'.$i].' engine with '.$_POST['doors-'.$i].' doors.</p>
</fieldset>
';
}
}
}
else {
$form_block .= '
<!--<form method = "post" action = "page2_a.php">--!>
<form method="post" action='.$_SERVER['PHP_SELF'].'>
<p>How many cars? <input type="text" name="number" >
<input type="submit" name="submit" value="Go"><br />
</form>
';
}
?>
<html>
<head></head>
<body>
<?php echo "$form_block";?>
</body>
</html>
?>rehfield, I want to tell you I really appreciate you helping so much with this code. Sometimes books just dont work and tutorials and the best way is just working at it - and its with people like you who help - that people like me learn best.
Thanks.
Wade
-
wadesmart
- Forum Commoner
- Posts: 38
- Joined: Sat Oct 02, 2004 10:10 pm
- Location: US, Oklahoma
- Contact:
Ok. I changed the final block to this:
and it works. !!
Code: Select all
<?php
if(isset($_POST['submit2'])){
//var_dump($_POST);
//echo '<pre>';
//print_r($_POST);
$form_block = '
';
for($i=0; $i< $_POST['number']; $i++) {
echo '
<fieldset>
<p>Car '.$i.' is a '.$_POST['name-'.$i].' and has a '.$_POST['engine-'.$i].' engine with '.$_POST['doors-'.$i].' doors.</p>
</fieldset>
';
}
$form_block .='
';
}
?>wadesmart wrote:Ok. I changed all the input name = value to input name=value.
I dont understand though what you mean about the $form_block.
I thought if you have a single script that you need to break into parts, you sue $form_block = ' at the beginning and then with the parts you use $form_block .= ' . If you use $form_block .= every where you get errors like this one:Code: Select all
Notice: Undefined variable: form_block in C:\WebDev\Work Directory\Zend\Arrays\single\All_in_One.php on line 36
Wade
the reason you get the error is because
$form_block .= 'foo';
is equivalent/shorthand to doing this
$form_block = $form_block.'foo';
so if form block is not yet defined, it will throw that warning at you(but it should still work normally, its just a warning that you code is _possibly_ "sloppy")
you can avoid this by putting defining the variable before you do any .=
Code: Select all
$form_block = ''; // define it
if(isset($_POST['submit']) || isset($_POST['submit2'])) {kinda stuff. that way you define it and dont have to do it more than once.
just a question, what editor are you using to write your php? i would recomend getting one that has good syntax highlighting. it can help you spot errors as your writting the code, and in general just makes the code much more clear to you as your writing it.
and if echo worked but .= didnt, most likely what was happening is at somepoint after you did the .=
you reset the variable like $form_block = '';
-
wadesmart
- Forum Commoner
- Posts: 38
- Joined: Sat Oct 02, 2004 10:10 pm
- Location: US, Oklahoma
- Contact:
Ok. Im getting back to my project and applying everything that I have learned,
I can submit the information from the second page to the third page, and I can see it when when I post is using
var_dump($_POST);
echo '<pre>';
print_r($_POST);
but, the loop doesnt run. The html part comes up but nothing else.
Code: Select all
<?php
if(isset($_POST['accounts'])) {
// set title
$title = "Missing Form Information";
$form_block = '
<h3>Account Information Missing</h3>
<form method="post" action='.$_SERVER['PHP_SELF'].'>
';
var_dump($_POST);
echo '<pre>';
print_r($_POST);
for($j=0; $j < $_POST['accounts']; $j++) {
$form_block .= '
<fieldset>
<p>Account '.$j.'</p>
<ul>
';
if(empty($_POST['name-'.$j])){
$form_block .= '
<li>Account Name: <input type="text" name='.$_POST['name-'.$j].'> </li>
';
}
else {
$form_block .= '
<li>Account Name: '.$_POST['name-'.$j].'</li>
';
}
if(empty($_POST['number-'.$j])){
$form_block .= '
<li>Number: <input type="text" name='.$_POST['number-'.$j].'></li>
';
}
else {
$form_block .= '
<li>Account Number: '.$_POST['number-'.$j].'</li>
';
}
if(empty($_POST['telephone-'.$j])){
$form_block .= '
<li>Telephone: <input type="text" name='.$_POST['telephone-'.$j].'></li>
';
}
else {
$form_block .= '
<li>Account Telephone: '.$_POST['telephone-'.$j].'</li>
';
}
if(empty($_POST['acct_holder-'.$j])){
$form_block .= '
<li>Account Owner: <input type="text" name='.$_POST['acct_holder-'.$j].'></li>
';
}
else {
$form_block .= '
<li>Account Owner: '.$_POST['acct_holder-'.$j].'</li>
';
}
if(empty($_POST['AcctStatus-'.$i])){
$form_block .= '
<p>Account Status: <br >
<input type="radio" value="Open" name='.$_POST['AcctStatus-'.$i].' >Open <br >
<input type="radio" value="Closed" name='.$_POST['AcctStatus-'.$i].' >Closed <br >
<input type="radio" value="Open_Paid_Off" name='.$_POST['AcctStatus-'.$i].' >Open but Paid Off <br >
<input type="radio" value="Closed_Paid_Off" name='.$_POST['AcctStatus-'.$i].' >Closed and Paid Off <br >
<input type="radio" value="Open_Suspended" name='.$_POST['AcctStatus-'.$i].' >Open but Suspended <br >
</p>
<br />
';
}
else {
$form_block .= '
<li>Account Status: '.$_POST['AcctStatus-'.$i].'</li>
';
}
$form_block .='
</ul>
</fieldset>
';
}
$form_block .='
<input type="submit" name="checked" value "Next">
</form>
';
}
//2) Fill in New Account Information
if(isset($_POST['Accts'])) {
// set title
$title = "Add Account Information";
// get number of accounts
$form_block = '
<h3>Add New Account</h3>
<form method="post" action='.$_SERVER['PHP_SELF'].'>
';
// create loop to make form fields
for($i=0; $i<$_POST['num_of_accts']; $i++) {
$form_block .='
<fieldset>
<p>Account Information: '.$i.' <br />
<ul>
<li>Name:<input type="text" name="name-'.$i.'"></li>
<li>Number:<input type="text" name="number-'.$i.'"></li>
<li>Telephone:<input type="text" name="telephone-'.$i.'"></li>
<li>Owner:<input type="text" name="acct_holder-'.$i.'"></li>
</ul>
</p>
<p>Account Status:<br />
<input type="radio" value="Open" name="acctstatus-'.$i.'"> Open <br />
<input type="radio" value="Closed" name="acctstatus-'.$i.'"> Closed <br />
<input type="radio" value="Open_Paid_Off" name="acctstatus-'.$i.'"> Open but Paid Off <br />
<input type="radio" value="Closed_Paid_Off" name="acctstatus-'.$i.'"> Closed and Paid Off <br />
<input type="radio" value="Open_Suspended" name="acctstatus-'.$i.'"> Open but Suspended (non charging)<br />
</p>
</fieldset>
';
}
$form_block .='
<input type="hidden" name="toCheck" value="toCheck">
<input type="submit" name="accounts" value="Add New Accounts">
</form>
';
}
}
?>var_dump($_POST);
echo '<pre>';
print_r($_POST);
but, the loop doesnt run. The html part comes up but nothing else.