Jump to content

show only items with values


mwalliser

Recommended Posts

I know this has to be a very simple thing to do... but I have not been able to find any code examples to make it work correctly... :-\

 

I have a form with about 60 different rows and each row has 4 columns of text fields that the user can fill out to order cash from the vault (I work at a bank).  Once submitted, the form outputs the table with the submitted values for each column and totals up the values for each row as well as for each column.  Currently, the output shows all 60 rows whether or not any values were entered or not.  I would like to ONLY show the rows for which values were entered.  I know this is able to be done... I just can't figure out how.

 

Also, the form sends an email to the person who fulfills the cash orders... I would like to do the same thing with the email and only print the rows of information that have any values.

 

 

Link to comment
Share on other sites

<?php

$regex = "/a(\d+)_(\d+)/";
foreach($_POST as $k=>$v)
{
if(!preg_match($regex, $k))
	unset($_POST[$k]);
elseif(empty($v))
	$_POST[$k] = 0;
}
$data = $_POST;
ksort($data);

$data2 = array();
foreach($data as $k=>$v)
{
list($part1, $part2) = explode("_", $k);
$data2[$part1][$part2] = $v;
}
foreach($data as $k=>$v)
if(count($data[$k]) == count(array_keys($v,0)))
	unset($data[$k]);

?>

 

Now your data is organized in a "table" (a matrix), only with the relevant values.

 

Haven't tested it tho, so I am not sure if it works.

 

Orio.

Link to comment
Share on other sites

I wasn't sure where exactly to place the code that you supplied... so I tried both at the bottom of the form page and inside the page code of the form processor.  Neither made any difference.  The output was the same as it has been.  Did I put your code in the wrong spot?

Link to comment
Share on other sites

That took some time, but I here's the whole thing. Just save it as whatever.php and run it:

 

<?php
$vaults = array("902"=>"Gilmer East","903"=>"Gilmer West","906"=>"WRBO - Lane 6","907"=>"WRBO - Lane 7","915"=>"SoPkwy Kiosk","916"=>"M Plaza East","917"=>"M Plaza West","921"=>"Chrysler","941"=>"MBO - Lane a9","942"=>"MBO - Lane 10","944"=>"HWY 72 - Lane 7","945"=>"HWY 72 - Lane 8","936"=>"North Parkway Lane 1","947"=>"NoPkwy - Lane a9","948"=>"NoPkwy - Lane 10","94a9"=>"Bradford South","950"=>"Bradford North","952"=>"6th Ave - Lane 7","953"=>"6th Ave - Lane 8","955"=>"Hobbs Rd - Lane a9","956"=>"Hobbs Rd - Lane 10","961"=>"Decatur Beltline","962"=>"Big Cove Stand Alone","963"=>"Big Cove Branch","901"=>"EdTech","904"=>"Shell - Hartselle","910"=>"Aerobee","911"=>"Boeing","912"=>"Steelcase","920"=>"Dynetics","922"=>"Sparkman Center","923"=>"Adtran East","924"=>"Adtran North","928"=>"Conoco - Hazel Green","92a9"=>"Citgo - Hwy 72 East","930"=>"Jackson County Hospital","931"=>"Chevron - Moore's Mill","932"=>"Fayetteville Citgo","933"=>"NASA","934"=>"Sur-Sav Pharmacy","935"=>"Building 5250","937"=>"DirecTV","938"=>"Huntsville Hospital","93a9"=>"Shell - Ardmore","951"=>"Joe's Pharmacy","965"=>"Redstone Lanes","966"=>"5 Points East","967"=>"5 Points West","968"=>"Winchester Rd South","970"=>"Blue Roof Cafe","971"=>"Embassy Suites","972"=>"Building 5220","973"=>"Med Mall","974"=>"Food Valu","975"=>"Building 5400","976"=>"Hwy 53 E","978"=>"Athens/Limestone Hospital");
$vault_codes = array_keys($vaults);


if(isset($_POST['submit']))
{
$data = array();
foreach($_POST as $k=>$v)
{
	if(!strpos($k,"_"))
	{
		unset($_POST[$k]);
		continue;
	}
	list($part1, $part2) = explode("_", $k);
	$part1 = substr($part1,1);
	if(!in_array($part1, $vault_codes))
	{
		unset($_POST[$k]);
		continue;
	}
	if(empty($v))
		$_POST[$k] = 0;
	$data[$part1][$part2] = $_POST[$k];
}
ksort($data);


foreach($data as $k=>$v)
{
	if(count($data[$k]) == count(array_keys($v,0)))
		unset($data[$k]);
}

echo <<<HEREDOC
<p>You placed the following order:</p>


<table width="600" border="1" cellpadding="3" cellspacing="0">
  <tr>
    <th scope="col">ATM # </th>
    <th scope="col">Site Location </th>

    <th scope="col">$50's</th>
    <th scope="col">$20's</th>
    <th scope="col">$10's</th>
    <th scope="col">$5's</th>
    <th scope="col">Total</th>
    <th scope="col">Signature</th>
  </tr>
HEREDOC;
foreach($data as $num => $vals)
{
	$sum = $vals[50]+$vals[20]+$vals[10]+$vals[5];
	echo <<<HEREDOC
  <tr>
    <td>{$num}</td>

    <td>{$vaults[$num]}</td>
    <td>{$vals[50]}</td>
    <td>{$vals[20]}</td>
    <td>{$vals[10]}</td>
    <td>{$vals[5]}</td>
    <td>{$sum}</td>
    <td><input type="text" size="30" class="clearcellborder" /></td>
  </tr>
  <tr>
HEREDOC;
}
echo "</table>";
die;	
}

?>

<form id="vault_atm" name="vault_atm" method="post" action="<?php echo basename($_SERVER['PHP_SLEF']); ?>">
  <h2>ATM Vault Shipment</h2>
<p><label>Today's Date:</label> <input name="date" type="text" /><br />
<label>Date of Shipment:</label> <input type="shipdate" name="shipdate" /><br />
<label>Ordered By:</label> <input type="orderedby" name="orderedby" /></p>
<span class="disclosures">Please enter dollar amount only, do not use symbols ($,.).</span><br />
<table width="600" border="0" cellpadding="3" cellspacing="0">
  <tr>
    <th scope="col">ATM # </th>
    <th scope="col">Site Location </th>

    <th scope="col">$50's</th>
    <th scope="col">$20's</th>
    <th scope="col">$10's</th>
    <th scope="col">$5's</th>
  </tr>
<?php
foreach ($vault_codes as $num)
{
echo <<<HEREDOC
  <tr>
    <td>{$num}</td>

    <td>{$vaults[$num]}</td>
    <td><input name="a{$num}_50" type="text" class="input_borderless" id="a{$num}_50" size="10" /></td>
    <td><input name="a{$num}_20" type="text" class="input_borderless" id="a{$num}_20" size="10" /></td>
    <td><input name="a{$num}_10" type="text" class="input_borderless" id="a{$num}_10" size="10" /></td>
    <td><input name="a{$num}_5" type="text" class="input_borderless" id="a{$num}_5" size="10" /></td>
  </tr>
  <tr>
HEREDOC;
}
?>
</table> 
<input name="submit" type="submit" value="Submit" />
<input name="reset" type="reset" value="Reset" />
</form>

 

 

Orio.

Link to comment
Share on other sites

Thanks Orio!  I told you it was a long form!  :) 

 

This does output only the rows that had information listed.  The issue now is that:

1) I need to make the Today's Date, Shipment Date, and Ordered By fields show up in the output.

2) There needs to be a "grand total" row at the bottom with totals from each column.

3) This information needs to be emailed.

4) Less important... is there a way to apply a style (with an external style sheet) to the "results" page?

 

Thanks so much!  I really appreciate your help!

 

Link to comment
Share on other sites

Man do you even know php? I mean, if you want all that to get done check out the freelancing forum. I made most of the script for you, and that took me some time. I have no problem to continue and finish the script, but really, why should I?

 

Orio.

Link to comment
Share on other sites

No, I don't know much PHP at all.  I am a designer not a programmer, and all I have learned is from books that I have tried to understand. 

I had all of that other stuff working in my original script... the script that you wrote took away functionality that was already there.

I was never asking for anyone to write the whole thing for me... I was hoping for some direction on where to find out how to do it myself.

I'll just keep looking for some information in other places to try and figure out what I need to do to the code.  Thanks for the help.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.