[SOLVED] Newbie Question about Do...While Loops

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
cheddar
Forum Newbie
Posts: 7
Joined: Tue Nov 25, 2003 4:23 pm
Location: UK

[SOLVED] Newbie Question about Do...While Loops

Post by cheddar »

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 &#123; 
      if(!isset($atrib.$i) 
                $atrib.$i = "N"; 
                $i++; 
      else &#123; 
                $i++; 
      &#125; 
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.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

sorry, but i can't seem to make any sense at all of your loop...

what you are saying is, "if the user has not selected anything, then set the value of this box to N. Otherwise, just count $i to another number and do nothing else.".


what are you trying to do with this code? is there a point to the NULL values? the for loop seems to not be doing anything besides setting unselected boxes to N..
cheddar
Forum Newbie
Posts: 7
Joined: Tue Nov 25, 2003 4:23 pm
Location: UK

Post by cheddar »

Thanks for the reply.

This is only a test bit of code to see if the logic is correct. It's part of a bigger section of code.

What happens is that the user fills in a form and select various checkboxes, if a checkbox is checked then the value 'Y' is stored in them. I collect this data from the form and store it in a DB Table.

What I want is that if the user didnt select a checkbox then I want to store the value 'N' in the DB Table.

The variables are just examples of the values that I will get from the form that the user has submitted. All the checkboxes are named from atrib1 to atrib30, they will either have a 'Y' value if the suer has checked them or be empty because the user has not checked them.

I want a way to go through them each and check if they are empty, if they are, then simply insert the value 'N' into it. If they are not empty then they will of course all ready hold the value 'Y' so I dont have to do anything with them.

At the end I will have a DB Table with:

atrib1 atrib2 atrib3 .......... atrib30
Y Y N Y

Instead of having 30 'IF' statements to check to see if each of the variables is empty I was hoping to have some sort of loop.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

well, your reasoning is ok, but the facts are kind of off...

users are gonna mess up, so if they miss a box, and MEANT to hit Y, you are not giving them a chance and are just selecting a value for them.

unless, of course, that is what it's suposed to do..

anyways, a better approach to all of this may be to use an [php_man]array[/php_man].

arrays are very useful when you have all kinds of data that are gonna need to be stored in multiple numbers of variables. this condenses all those variables into 1 big variable, aka, an array.

you should really look into this and see if it's something that will benefit you..

a quick example :

say i have 15 words, but i don't want to assign 15 variables... just 1.

i could do this :

Code: Select all

<?php

$my_array = array('bob','henry','cow','george', 'school', 'books', 'y', 'n', 'hippy', 'lisa', 'jon', 'greg', 'mary', 'tiffany', 'boe');

//  i can then call this array like this :


// with a loop 
for($i=0; $i<16; $i++)
{
   echo $my_array[$i];
   echo '<br />';
}

//  by itself

echo $my_array[0];
echo '<br />';
echo $my_array[12];
echo '<br />';

?>
there is a lot of documentation in the php manual on [php_man]arrays[/php_man] as well as other sites.

hope that helps.
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Cheddar:

You may also want to view these threads for some more efficient ways of dealing with checkboxes and php. The first one shows how to loop through the checkboxes.

viewtopic.php?t=11148&highlight=checkbox
viewtopic.php?t=12702&highlight=checkbox
viewtopic.php?t=12417&highlight=checkbox
cheddar
Forum Newbie
Posts: 7
Joined: Tue Nov 25, 2003 4:23 pm
Location: UK

Post by cheddar »

Thanks to everyone who has replied and helped me out.

I will take a look at everything and see if I get it working.
User avatar
igoy
Forum Contributor
Posts: 203
Joined: Fri May 02, 2003 11:57 pm
Location: India
Contact:

Post by igoy »

maybe... using empty() instead of isset()... yaaawwnnn...
i'm do sleepy and lazy to view and test your code... so pardon me for making just a suggession...
User avatar
igoy
Forum Contributor
Posts: 203
Joined: Fri May 02, 2003 11:57 pm
Location: India
Contact:

Post by igoy »

okay... so the love of php is always alive... can't let me sleep...

try using this dear

Code: Select all

<?php 

	// Meet sweetest lady of PHPland, Array
	
	$atrib[1]="Y"; 
	$atrib[2]="Y"; 
	$atrib[3]="Y"; 
	$atrib[4]="Y"; 
	$atrib[5]="Y"; 
	$atrib[6]=NULL; 
	$atrib[7]=NULL; 
	$atrib[8]=NULL; 
	$atrib[9]=NULL; 
	$atrib[10]=NULL; 
	$atrib[11]=NULL; 
	
	
	$i=1; 									//sets up a counter for the loop 
	do { 									// do the Loop
		$i++; 								// increase the counter
		if (!isset($atrib[$i])) { 		// check the checkboxes...
			$atrib[$i] = "N"; 			// if condition meet, take an Action
		} 										// condition close
	} 											// loop ends
	while ($i <= 12); 					// condition for loop
	
	// aah... the sweetest result.
	echo $atrib[1]; 
	echo $atrib[2]; 
	echo $atrib[3]; 
	echo $atrib[4]; 
	echo $atrib[5]; 
	echo $atrib[6]; 
	echo $atrib[7]; 
	echo $atrib[8]; 
	echo $atrib[9]; 
	echo $atrib[10]; 
	echo $atrib[11]; 
	
	// enjoy
	
?>
cheddar
Forum Newbie
Posts: 7
Joined: Tue Nov 25, 2003 4:23 pm
Location: UK

Post by cheddar »

Thanks for all the help guys, it worked :D
Post Reply