|
-
Jun 11th, 2008, 09:22 AM
#1
Thread Starter
Lively Member
extracting key & value of $_POST[] array
I have a peculiar requirement. I have a form that contains 39 groups of radio buttons with each group containing three radio buttons. When user makes choices, the results are sent via the array $_POST. This is an associative array that has the radio button name as the key and radio button value as the value.
Using the following code, I checked the keys and their values.
PHP Code:
foreach ($_POST as $fn=>$fv){
echo "field name is $fn and has value $fv</br>";
}
Is it possible to save the key as a string in a variable? Coming straight to the point, I want to write a table with 20 rows and two columns on the fly and write the key and its value.
----------------------------------
| apples: 20 | tomatoes : 35 |
-----------------------------------
| brinjal: 23 | pumpkin: 20 |
-----------------------------------
where apples is the key and 20 is the value of the first element of $_POST[] array. How do I do that, kindly excuse my ignorance.
-
Jun 11th, 2008, 10:20 AM
#2
Re: extracting key & value of $_POST[] array
PHP Code:
<table> <thead> <tr> <th>Field</th> <th>Value</th> </tr> </thead> <tbody> <?php foreach ($_POST as $fn => $v): ?> <tr> <td><?php echo htmlentities($fn); ?></td> <td><?php echo htmlentities($fv); ?></td> </tr> <?php endforeach; ?> </tbody> </table>
Okay, it's not 2x20, but I'm not quite sure whether that even makes sense.
-
Jun 11th, 2008, 12:35 PM
#3
Thread Starter
Lively Member
Re: extracting key & value of $_POST[] array
Dear Penagate,
You are amazing, you made it look so simple. I am from health sciences with no background in computing, hence I took 5 hours to come to a solution. Compared to your code, it sure looks crude, but I achieved the results.
HTML Code:
if (isset($_POST["subm"])) {
echo ('<div align="center"><center><table border="1" cellpadding="2" style="border-collapse: collapse" bordercolor="#111111" width="40%" id="AutoNumber4" class="tbl4"><tr><td width="100%" colspan="2"><b>Following are the results you chose:</b></td></tr>');
$keys = array_keys($_POST);
for ($j=0; $j<=45;$j+=2){
$key1 = $keys[$j];
$val1 = getres($_POST[$key1]);
$j+=1;
$key2 = $keys[$j];
$val2 = getres($_POST[$key2]);
echo ('<tr><td width="50%">'.$key1.' :'.$val1.'</td><td width="50%">'.$key2.' :'.$val2.'</td></tr>');
$j-=1;
}
echo('<td width="50%"> </td></tr></table>');
The getres function was to convert the values into words and font color.
Believe me, my first attempt at scripting this code this was 90% bigger. I was not aware of htmlentities($fv); at all. I thank you once again for being around and helping me out.
By the way, the generated html output is a very long line, how do I shorten them at scripting.
-
Jun 11th, 2008, 08:48 PM
#4
Re: extracting key & value of $_POST[] array
You're welcome, and well done. If you have no background in computing at all, writing code can be quite challenging. I'm glad you came up with a solution, even if it wasn't ideal, rather than just asking for pre-rolled code as many do.
You can write a line break using the escape sequences for newline and carriage return characters.
Ensure that they are in double quotes or the escape sequences (backslash followed by key character) will not be converted into the correct characters. Use single-quoted strings for everything else.
It's generally regarded as bad practice to output the HTML using echo or print commands. It's slower, and harder to read and scan for errors. Instead, just go in and out of the script blocks as I did in my example. This works because PHP is designed to be embedded in HTML, not the other way round.
Always validate any input that comes from the client, such as POST variables. The htmlentities function is sufficient for preventing any nasty characters from breaking HTML. However, you should also validate each item specifically before doing other things with them such as inserting into a database.
Last edited by penagate; Jun 11th, 2008 at 08:52 PM.
-
Jun 12th, 2008, 07:54 AM
#5
Thread Starter
Lively Member
Re: extracting key & value of $_POST[] array
Thanks penagate,
Your suggestion would be implemented! You made a significant point, that is to insert php code inside html tags instead of getting php to write html tags. An excellent point for a beginner like me.
Coming to my own issue....
I have a master table containing fixed data, which should not change. In simple, the table actually contains name and standard values for a range of tests (>40). When user inputs his test results via a form, I have to perform few calculations on the table and output the nearest one that matches the table. I have already accomplished it, but I feel I have used "non-standard" methods. With your guidance, maybe I can optimize the code.
Let me describe the situation; the table has data similar to this
name test1 test2 test3 test4....
-------------------------------------
abc 100 230 11 0.77 standard results (% positive) for abc
------------------------------------
def 12 .88 2.3008 8.991
--------------------------------------
The user is presented with three options in the form; positive, negative & not performed, each of which option returns 1, 0, or 3.
If a test is positive, its value in the table is replaced after dividing by 100. For e.g, if the user declared test1 as positive the value of column test1 would be 100/100. i.e 1 for the first row, 12/100 .i.e 0.12. All the rows on column test1 would be updated similarly.
If user declares test2 as negative, all the values would be replaced by (1-test2)/100. Eg., 1-(230/100) i.e 1.3 (actually there are no negative values)
If the user declares test not performed, all the rows for that test is replaced by 1.
Once all the results are updated, calculation is performed like this. Values present in all test columns in each row are multiplied and result stored in prod column. Next step is to add the prod of each row and calculate the percentage. (I have already posted this part before, but couldn't understand the reply posts).
Since master table contain standard data, I copied the table & its contents and updated every field using sql statement row by row. Then I used sql statement select (test1*test2*...) .. to get product and used the resultant array to update prod column row by row. After deriving the sum of prod using select sum(prod)..., i updatet the percent column row by row. Three names corresponding to highest percent was displayed back to the user.
Your result:
test fgh: 75% //based on the user input, there is 75% probability of the test being fgh
test ggh: 22%
test tig: 11%
I have not locked the table any time during run. I don't know how the server will respond if someone else makes the same query.
The method I adopted must be "of stone age" and an absolute crap by any programming standards. If I showed you my codes, you might banish me from this forum. However crude, I wrote the code in four days since I took up learning PHP on my own. You are invited to see it running on my website at www.microrao.com/ident.php It is still in development and not available for public use. Once I optimize the code and address security issues, if any, I will give access to public.
I don't want ready made code, because I want to learn by experimenting. I prefer making mistakes and then be told where I went wrong and how else could I have done it. Please give me hints and direction on how to structure the coding.
Last edited by sridharao; Jun 13th, 2008 at 09:58 AM.
-
Jun 15th, 2008, 09:22 AM
#6
Re: extracting key & value of $_POST[] array
I see what you're doing, but I'd need to see the code in order to offer any suggestions on improving it.
(Sorry for the tardy response. I had subscribed to this thread, but must have managed to dismiss it somehow.)
-
Jun 15th, 2008, 04:24 PM
#7
Thread Starter
Lively Member
Re: extracting key & value of $_POST[] array
I figured it all out myself by working on it for 8 hours. My code now has been reduced by 75%. I am not working on the table at all. Maybe slower, but I get to perform them all on arrays and variables. Please visit www.microrao.com/ident.htm and see the code working.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|