foreach($_POST as $key => $value)
{
if($key != "submit")
{
$$key = mysql_escape_string($value);
if($$key != "" || $$key != NULL)
{
$filledin .= $key . ": " . $$key . "<br />"; ##### Builds variable for testing purposes, see below for echo
$fcount += 1;
}
if($$key == "" || $$key == NULL)
{
$empty .= $key . ": " . $$key . "<br />"; ##### Builds variable for testing purposes, see below for echo
$ecount += 1;
}
}
}
My code above doesn't quite represent what I want to achieve, but I hope combined with description will explain what I'm hoping to achieve.
The page where this code resides on is basically parsing a form.
The form has just under 700 variables, all of which (aside from the submit button, which I have dealt with in the code above) are in the following format:
xx_yyyy_zzz
xx is either 1 or two characters (k, d, h, r1, r2, b1, b2, b3, b4, b5, s, g, o)
yyyy is a shorthand name for an item, for example oven, or sfreezer
zzz is either qty or cmt (quantity or comment)
Two examples:
k_oven_qty
s_bookcase_cmt
Basically, variables come in pairs, there will be a qty and a cmt for each item, so using the above example, we have:
k_oven_qty
k_oven_cmt
s_bookcase_qty
s_bookcase_cmt
What I would like to do is to work on two items at a time, the qty and cmt for each item. This i because I want to add to an output string along the following lines: (pseudocode)
if(not empty)
{
split input into parts by _
if(first part == k)
{
$room = kitchen
$kcount += 1
if(third part == 'qty')
{
$ktext .= second part ':' qty // qty here is the value of the qty variable
}
else // we assume if third part isnt qty it is cmt
{
if(third part not empty)
{
$ktext .= '(' cmt ')<br>' // cmt here is the value of the cmt variable
}
else
{
$ktext .= '<br>' // if cmt value empty just newline
}
}
}
elseif(first part == b1)
{
repeat above
}
}
The output I'm aiming for might look something like this:
[U]Room 1[/U]
Item 1: 5 (Comment)
Item 2: 1
Item 3: 3
Item 4: 1 (Comment)
Item 5: 2
I hope this makes sense, if you have any questions please ask away. I realise it is very hard to 'get around' in your head, and hopefully my pseudocode will make it easier to do this.
My question to you is, would the logic behind it work, and is it more feasible to do it a different way?
Also, if the variables are in a guaranteed order of qty, cmt, qty, cmt will the output always appear as expected?