...

View Full Version : Hiding/Showing an input field based on a variable loop



Juniper747
01-31-2012, 01:32 AM
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:



<?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>

mlseim
01-31-2012, 01:57 AM
What happens if you do this ... a simplified "if" statement ...




<?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>

Juniper747
01-31-2012, 05:41 AM
Thanks...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum