Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    92
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Hiding/Showing an input field based on a variable loop

    Hi, I have a php page where I just want to show the two input fields (first name, last name), only if the variable has a value (which I am grabbing from an array stored in a session variable)... So each time this page runs, each "potential" may or may not have a value. If it does, I want the two input fields next to it.

    So far I am doing the following, but it is not working because it's showing ALL the input fields. Can you help me possibly come up with a simpler/elegant (i.e. less code) solution to this. Thanks:

    Code:
    <?php
    session_start(); // Start Session
    
    if(isset($_SESSION['potentials'])){
    	$potentials = $_SESSION['potentials'];
    	foreach($potentials as $potential){
    		if(!empty($potential)){
    			$input_fn = '<input name="input_fn[]" type="text" />';
    			$input_ln = '<input name="input_ln[]" type="text" />';
    		} else {
    			$input_fn = "";
    			$input_ln = "";
    		}
    	}
    	$j = $potentials[0];
    	$k = $potentials[1];
    	$l = $potentials[2];
    	$m = $potentials[3];
    	$n = $potentials[4];
    	$o = $potentials[5];
    	$p = $potentials[6];
    	$q = $potentials[7];
    	unset($_SESSION['potentials']);
    	
    	$addtonames = '
    		<li>' . $j . '<span>' . $input_fn . '</span><span>' . $input_ln . '</span></li>
    		<li>' . $k . '<span>' . $input_fn . '</span><span>' . $input_ln . '</span></li>
    		<li>' . $l . '<span>' . $input_fn . '</span><span>' . $input_ln . '</span></li>
    		<li>' . $m . '<span>' . $input_fn . '</span><span>' . $input_ln . '</span></li>
    		<li>' . $n . '<span>' . $input_fn . '</span><span>' . $input_ln . '</span></li>
    		<li>' . $o . '<span>' . $input_fn . '</span><span>' . $input_ln . '</span></li>
    		<li>' . $p . '<span>' . $input_fn . '</span><span>' . $input_ln . '</span></li>
    		<li>' . $q . '<span>' . $input_fn . '</span><span>' . $input_ln . '</span></li>
    	';
    }
    ?>
    <html>
    <body>
        <li>
             <?php echo $addtonames; ?>
        </li>
    </body>
    </html>
    Last edited by Juniper747; 01-31-2012 at 01:36 AM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,466
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    What happens if you do this ... a simplified "if" statement ...

    PHP Code:

    <?php
    session_start
    (); // Start Session

    if(isset($_SESSION['potentials'])){
        
    $potentials $_SESSION['potentials'];
        foreach(
    $potentials as $potential){
            if(
    $potential){
                
    $input_fn '<input name="input_fn[]" type="text" />';
                
    $input_ln '<input name="input_ln[]" type="text" />';
            } else {
                
    $input_fn "";
                
    $input_ln "";
            }
        }
        
    $j $potentials[0];
        
    $k $potentials[1];
        
    $l $potentials[2];
        
    $m $potentials[3];
        
    $n $potentials[4];
        
    $o $potentials[5];
        
    $p $potentials[6];
        
    $q $potentials[7];
        unset(
    $_SESSION['potentials']);
        
        
    $addtonames '
            <li>' 
    $j '<span>' $input_fn '</span><span>' $input_ln '</span></li>
            <li>' 
    $k '<span>' $input_fn '</span><span>' $input_ln '</span></li>
            <li>' 
    $l '<span>' $input_fn '</span><span>' $input_ln '</span></li>
            <li>' 
    $m '<span>' $input_fn '</span><span>' $input_ln '</span></li>
            <li>' 
    $n '<span>' $input_fn '</span><span>' $input_ln '</span></li>
            <li>' 
    $o '<span>' $input_fn '</span><span>' $input_ln '</span></li>
            <li>' 
    $p '<span>' $input_fn '</span><span>' $input_ln '</span></li>
            <li>' 
    $q '<span>' $input_fn '</span><span>' $input_ln '</span></li>
        '
    ;
    }
    ?>
    <html>
    <body>
        <li>
             <?php echo $addtonames?>
        </li>
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    92
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Thanks...


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •