Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-31-2012, 01:32 AM   PM User | #1
Juniper747
New Coder

 
Join Date: Apr 2011
Posts: 92
Thanks: 26
Thanked 0 Times in 0 Posts
Juniper747 is an unknown quantity at this point
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..
Juniper747 is offline   Reply With Quote
Old 01-31-2012, 01:57 AM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,055
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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>
mlseim is offline   Reply With Quote
Old 01-31-2012, 05:41 AM   PM User | #3
Juniper747
New Coder

 
Join Date: Apr 2011
Posts: 92
Thanks: 26
Thanked 0 Times in 0 Posts
Juniper747 is an unknown quantity at this point
Thanks...
Juniper747 is offline   Reply With Quote
Reply

Bookmarks

Tags
foreach, hide, input

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:20 AM.


Advertisement
Log in to turn off these ads.