order of output to a screen

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wadesmart
Forum Commoner
Posts: 38
Joined: Sat Oct 02, 2004 10:10 pm
Location: US, Oklahoma
Contact:

order of output to a screen

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

It sounds like you should put the output into variables and then combine them at the end in the right order.
(#10850)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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.
Post Reply