Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Newbie Problem with a 'while do' loop

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Newbie Problem with a 'while do' loop

Postby cheddar » Tue Nov 25, 2003 2:03 pm

Overview of Problem
--------------------------
I have caught some information from a form with checkboxes and placed this into variables. The problem is that if the user doesnt click on a checkbox then the value passed will be nothing/null/zip/nadda, I want that value to be 'N' instead.

I have 30 of these checkboxes, I want all the variables that hold the information on these checkboxes to have a 'Y' or 'N'. The 'Y' is no problem as that value is passed when I access the $POST array. The 'N' value is what I am struggling with though.

I have come up with this code below to try and fix my problem. Instead of having 30 'If' statements I want to use a loop of some kind, but it's not working. I am a complete novice with PHP so i'm not sure if my logic is wrong or if it's something else.


Code: Select all
<?php

$atrib1=Y;
$atrib2=Y;
$atrib3=Y;
$atrib4=Y;
$atrib5=Y;
$atrib6=NULL;
$atrib7=NULL;
$atrib8=NULL;
$atrib9=NULL;
$atrib10=NULL;
$atrib11=NULL;

$i=1; //sets up a counter for the loop
do {
      if(!isset($atrib.$i)
                $atrib.$i = "N";
                $i++;
      else {
                $i++;
      }
while ($i<12);

echo $atrib1;
echo $atrib2;
echo $atrib3;
echo $atrib4;
echo $atrib5;
echo $atrib6;
echo $atrib7;
echo $atrib8;
echo $atrib9;
echo $atrib10;
echo $atrib11;

?>




I think the problem lies with ' if(!isset($atrib.$i)', basically what I am trying to do here is say if atrib1 is empty then put a 'N' in as the value for it. Of course each time the loop goes through 'i' will be incremented so the next time we get, if atrib2 is empty then put a 'N' in as the value for it.

Can anyone help me out here?

Thanks.
cheddar
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Nov 25, 2003 1:56 pm

Postby determined » Tue Nov 25, 2003 3:45 pm

There are a few problems that I see:
First there is not an ending "}" on the for loop and the do..while loop.

Second there is a ")" missing after isset(), remember to close all brackets, even if it means there will be a ")" right after a ")". You can see this in my example below.

Third you are trying to get $atrib1 out of $atrib.$i and It is not working. Unfortunately I cant seem to get it to work either, so I changed the way your variables are set up; rather than each value having its own variable, I put the values into an array, that can be called like this $array[1], $array[2], etc.

I have made some changes and tested it to make sure it works. This is what I got:
Code: Select all
<?php

for ($i=1;$i<7;$i++) {
   $atrib[$i] = "Y";
}
$i=1;
//sets up a counter for the loop
   for ($i=1;$i<12;$i++) {
      if (!isset($atrib[$i])) {
      $atrib[$i] = "N";
      } else {
         $i++;
      }
   }

for ($i=1;$i<12;$i++) {
   print $i . ': ' . $atrib[$i] . '<br>';
}

?>


I hope that takes care of the problem. I will be looking around for how to get add a variable to another variables name. If anyone else could clear this up I would be very appreciative.
User avatar
determined
New php-forum User
New php-forum User
 
Posts: 23
Joined: Sun Sep 28, 2003 12:19 pm
Location: Table Arizona

Postby yK » Tue Nov 25, 2003 5:38 pm

User avatar
yK
New php-forum User
New php-forum User
 
Posts: 28
Joined: Tue Aug 19, 2003 1:27 pm

Postby cheddar » Tue Nov 25, 2003 11:04 pm

Thanks to everyone who helped, I think I have a better idea about this now.

I'll try everything out and see what happens.
cheddar
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Nov 25, 2003 1:56 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google Feedfetcher and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron