Page 1 of 1

order of output to a screen

Posted: Tue Nov 30, 2004 2:13 pm
by wadesmart
11302004 1407 GMT-6

I have this long script. Inside a for loop Im checking to see if any of the form fields are empty. If they are then I want post the information back to the screen so they can fill in the missing information. If none of the fields is missing any information, I want to change a lot of other things on the page.

What Im struggling with is, if i have a for loop and its inside.... let me show you..

Code: Select all

<?php
for(....) {
   if( x is missing)
       repost form field to screen
    else
        just post information to screen
}

if ($empty = 0) {
  change H1 and H3
  change page title
  Add these extra items to the page
  add extra buttons to the form
}
else
{ normal h1 and h3
   normal page title
   normal form buttons
}

?>
Ok. I have this for loop inside a $form_display = ' '; ... but, if I have to check the information inside the for loop before posting info to the screen. My question then is, can I change something farther up on the page?

IS php intrepreted line by line or as a whole document?

Wade

Posted: Tue Nov 30, 2004 2:25 pm
by Christopher
It sounds like you should put the output into variables and then combine them at the end in the right order.

Posted: Tue Nov 30, 2004 2:39 pm
by pickle
Ya, so in your for loop, just add the variable to an array of missing values:

Code: Select all

for(...)
{
     if(x is missing)
     {
          $missing[] = x;
     }
}
like so.

Posted: Tue Nov 30, 2004 2:47 pm
by rehfeld

Code: Select all

<?php


$errors = '';


if (empty($_POST['name'])) {
    $errors .= 'enter a name';
}

if (empty($_POST['address'])) {
    $errors .= 'enter a address';
}


if (empty($errors)) {
    // form is complete, we generated no errors
    // proceed
} else {
     // show them the form again
    // echo $errors at them
}




// or if you want to have a error displayed next to each field, 
// you will need to serperate the errors.



$form_complete = true; // assume true for now


$error_name = '';
if (empty($_POST['name'])) {
    $error_name = 'enter a name';
    $form_complete = false;
}

$error_address = '';
if (empty($_POST['address'])) {
    $error_address = 'enter a address';
    $form_complete = false;
}


if ($form_complete) {
    // no errors
} else {
    // show form and errors
}



?>



personally i use a loop to do all of this if theres lots of fields to validate.