Radio Buttons with PHP
I was wondering if I can manipulate a webpage content using radio buttons and php. I have a page of peoples names and e-mail addresses. I would like to sort them into three groups, lets say the blue group, yellow group, and green group. I want the user to be able to choose how to sort them. So, I will have a form like this one.
Green
Blue
Yellow
I would like to have php sort the people depending on what group they choose. I do not know where to start with this, so if you could please help, it would be appreciated.
Keep in mind that I would like it to stay on the same page (contacts.php) and I have text and images above the form that must not change. Thanks!
Suzanne posted this at 00:36 — 16th June 2004.
She has: 5,507 posts
Joined: Feb 2000
assuming mysql db -- lookup table with peopleid and groupscolour
display lookup.peopleid, lookup.groupscolour, people, et cetera
sort by lookup.groupscolour
Or have groupscolour as a field in the table of peopleids
nick.88 posted this at 02:53 — 16th June 2004.
They have: 10 posts
Joined: Jun 2004
Thanks, but this is not a mysql database. Would doing it in a mysql database table be easier? I am new at php, and I do not know how to set up mysql. Is there a way of doing it from list of people on a html page. The list would just be names with 's seperating them. Thanks for your response.
Nick
andy206uk posted this at 10:10 — 16th June 2004.
He has: 1,758 posts
Joined: Jul 2002
Only if the values were stored in an array as well. Although you could always write the data to a delimited text file instead. (a flat file database).
Andy
nick.88 posted this at 21:13 — 16th June 2004.
They have: 10 posts
Joined: Jun 2004
Thanks Andy,
As I mentioned, I am very new to php. How would I call the text file and what would some of the script look like. I am sorry, but I only know the basic commands of php, if possible, an explanation of the script would be great.
Would the flat file database look something like this?
FirstName LastName Color
Thanks for all your help!
Tudor.b posted this at 20:40 — 17th June 2004.
They have: 35 posts
Joined: Jun 2004
I can`t understand what you actually want! Let`s say the user chose green. Do you still want the blue and yellow to appear on the page?
Suzanne posted this at 22:07 — 17th June 2004.
She has: 5,507 posts
Joined: Feb 2000
A flat file database would look like this:
Jane|Smith|Green
Julie|Smith|Green
Joseph|Smith|Blue
Javier|Smith|Yellow
Jesus|Smith|Green
The problem is in editing this sort of file you have a lot more work to do -- you need to read in the whole file, break it apart into the array, edit/sort the array, then over-write/display the results.
If you're really new to php, try looking up the array (esp multi-dimensional arrays) information at php.net and practice. With some sample code that you've created, we can help you where you need it. At the moment, you're asking us to do it entirely for you. While some may, in fact, choose to do this, in general you'll get a faster response and a BETTER response if you have specific questions and specific things to work with.
nick.88 posted this at 18:55 — 18th June 2004.
They have: 10 posts
Joined: Jun 2004
Here is some of the code:
<?php
$groups = array();
$filearray = file('peoplebase.txt');
foreach ($filearray as $line) {
list($name,$group) = explode("\t",$line); # each line in db is name<tab>group
$groups[$group][] = $name;
}
?>
This does not work... All it does is give me the name of the person who is last on the database. I have tried varrious modifications, but it does not seem to work.
Nick
Suzanne posted this at 19:56 — 18th June 2004.
She has: 5,507 posts
Joined: Feb 2000
<?php
// set the variable $groups
$groups = array();
// get the flat file with the information in it
$filearray = file('peoplebase.txt');
// while reading through the file, for each line, do this
foreach ($filearray as $line) {
// for each line, enter it into the $name and $group arrays
list($name,$group) = explode(\"\t\",$line); # each line in db is name<tab>group
// print the list
echo<<<CODE
<pre>$name is in $group</pre>
CODE;
}
?>
What do you want to do otherwise -- sort on group? Then you'll have to look into the [url="http://www.php.net/array]array[/url] functions.
ShawnK posted this at 21:54 — 18th June 2004.
He has: 19 posts
Joined: Jun 2004
Scratch that, i'm done!
<?php
<html>
<head>
<style>
body{font-family:verdana;size:2;color:black}
table{border:1 solid black}
td{border:0;cellpadding:0;cellspacing:0}
tr{border:0;cellpadding:0;cellspacing:0}
th{border:0;cellpadding:0;cellspacing:0}
a{font-family:verdana;size:2;color:black;text-decoration:none}
a:hover{font-family:verdana;size:2;color:black;text-decoration:underline}
</style>
</head>
<body>
$group1 = \"red\";
$group2 = \"blue\";
$group3 = \"green\";
function choose(){
global $group1;
global $group2;
global $group3;
echo
\"<form method='post' action=\\"$PHP_SELF\\">\",
\"<input type='radio' name='groups' value=\\"$group1\\" />$group1<br />\",
\"<input type='radio' name='groups' value=\\"$group2\\" />$group2<br />\",
\"<input type='radio' name='groups' value=\\"$group3\\" />$group3<br />\",
\"<input type='hidden' name='passed' value='true'>\",
\"<input type='submit'>\",
\"</form>\";
}
if(!isset($_POST['passed']))
{
choose();
}
elseif(isset($_POST['passed']))
{
if(isset($_POST['groups']))
{
echo \"<table border='1' cellpadding='0' callspacing='0'><tr bgcolor='#cccccc' align='left'><th width='125'>First</th><th width='125'>Last</th><th width='300'>Email</th></tr>\";
$file_name = $_POST['groups'].\".txt\";
$file_handle = fopen(\"$file_name\", \"r\");
$data = fread($file_handle, filesize($file_name));
fclose($file_handle);
$persons = explode(\"\n\",$data);
foreach($persons as $person){
$info = explode(\"|\",$person);
echo \"<tr><td>\".$info['0'].\"</td><td>\".$info['1'].\"</td><td><a href='mailto:\".$info['2'].\"'>\".$info['2'].\"</a></td></tr>\";
}
echo \"</table>\";
}
elseif(!isset($_POST['groups']))
{
unset($passed);
choose();
}
}
else
{
die(\"There has been a critical error, please report this to the webmaster!\");
}
</body>
</html>
?>
I made it really user-friendly. All you do is fill in whatever you want your groups to be called. Then create a text file that matches the title.
example:
Group = FatChicks
Filename = fatchicks.txt
The text file should look like this:
shawn|kurtz|[email protected]
joe|bob|[email protected]
shawn|kurtz|[email protected]
joe|bob|[email protected]
shawn|kurtz|[email protected]
joe|bob|[email protected]
If you need any customzations just tell me! I already tested it on my local server. It works fine.
I hope you like!
Oh ya, I made it so that the email address shows up as a link. I can also make you a page that adds/edits the names
If you need any PHP programming don't hesitate to shoot me an email or PM me here at Webmaster-Forums!
[EMAIL] [PM]
ShawnK posted this at 22:00 — 18th June 2004.
He has: 19 posts
Joined: Jun 2004
Oh, if it cannot find your file it will show like a whole page of errors. So make it look better just add this:
<?php
error_reporting(0);
?>
right after:
<?php
?>
So it should look like:
<?php
error_reporting(0);
?>
But it shouldn't show errors if you make the files!
If you need any PHP programming don't hesitate to shoot me an email or PM me here at Webmaster-Forums!
[EMAIL] [PM]
nick.88 posted this at 00:37 — 19th June 2004.
They have: 10 posts
Joined: Jun 2004
Thanks Shawn!
That is amazing! That is exactly what I wanted. At the moment, I am just customizing it alittle bit but I might have a few more questions... Thanks so much.
Nick
ShawnK posted this at 14:37 — 19th June 2004.
He has: 19 posts
Joined: Jun 2004
No problem!
nick.88 posted this at 16:14 — 19th June 2004.
They have: 10 posts
Joined: Jun 2004
Ok, it didn't take me long to come up with a question , but I was wondering if there would be a way to display all three files, if the user does not submit the form.... For example: The user loads the page and it will display some text at the top (thats not hard...) and it will display the names in the files, all in one go.
I was thinking it would be something in the else tag, but am not 2 sure.
Oh yea, if possible, is there a way, without me doing it in the flat file databases, that I can sort the names in alphabetical order? THis is not a must have, because I can do that myself... I was just curious.
Thanks so much,
Nick
Suzanne posted this at 02:36 — 20th June 2004.
She has: 5,507 posts
Joined: Feb 2000
Not to be snarky, but this is why I suggested you learn more about what you're trying to do instead of having someone else do the work for you. It's empowering, and you develop a skill.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.