p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: radio buttons + mysql


Message #1 on Mon, 20 May 2002 17:10:47
Hi,



I am writing a script that will read-in the values from mysql
db and automatically select correct redio button from radio button
group.

I have 4 values in my db. C, M, A and E as usertype.

<input type="radio" name="usertype" value="E">

When user wants to edit information in the database a form is displayed
together with 'usertype option' containing 4 radion buttons (group).

I want usertype to be indicated in autom. selected radio button (CHECKED), 
as default.

Any suggestions?


Message #2 on Mon, 20 May 2002 12:18:30 -0400
check out this thread:
http://p2p.wrox.com/archive/beginning_php/2002-05/1.asp

I did the same thing with select boxes.  Nik helped a great deal here.


-- 

 


Message #3 on Mon, 20 May 2002 18:46:19
Hi,


I explicitly need solution based on radio buttons.

action:

- user indicated his usertype from the radiogroup. data is saved in db.
- user gets info from db. his selection is checked in the radio button 
group.


Message #4 on Mon, 20 May 2002 11:23:16 -0700
> I explicitly need solution based on radio buttons.

So what?  Maybe it's because I haven't had my morning coffee yet, but would
it really be too hard to take the checkbox code and change it to generate
radio buttons instead??

I try to offer as much help as I can to anyone who asks, but you've already
been handed a link which gives you almost all the code you need!


Anyway, since I might be overreacting, I'll give you some help.

write two functions.

get_types() // returns an array of the different user types.
create_radio()  // generates and a series of radio buttons

get_types will probably get the set of user types from the database or maybe
it's hand coded, who knows.  Doesn't matter, really, as long as the only
access to the list of types is through this function, you can change it
however you want whenever you want (as long as the format of the return
array stays the same) and the rest of your site will be none the wiser.


create_radio accepts a name (to name the inputs), an array of options for
the different values, and (optionally) a default value to select.  Note that
the default value is only selected if there's not already a variable with
the same name as the one you're passing in set.


This is pulled directly from the archive link:

// create a dropdown combo box and preselect a default option
  function create_select($name, $options, $default = "")
  {
    $tags = "<SELECT name=\"$name\">\n";

    // http://www.php.net/foreach
    foreach($options as $value => $text)
    {
      $tags .= "  <OPTION value=\"$value\"";

      if($value == $default)
      {
        $tags .= ' SELECTED';
      }

      $tags .= ">$text</OPTION>\n";
    }

    $tags .= "</SELECT>\n";

    return $tags;
  }


Let's rename "select" to "radio", change SELECT/OPTION tags to INPUT
type="radio", and move the name from the SELECT tag to each of the INPUT
tags.  Oh yeah, we need to change "SELECTED" to "CHECKED"

  function create_radio($name, $options, $default = "")
  {
    // http://www.php.net/foreach
    foreach($options as $value => $text)
    {
      $tags .= "  <INPUT type=\"radio\" name=\"$name\" value=\"$value\"";

      if($value == $default)
      {
        $tags .= ' CHECKED';
      }

      $tags .= ">$text</INPUT>\n";
    }

    return $tags;
  }


At this point, you have all the code you need.  Just to be extra nice,
though, I'll add this:

  function create_radio($name, $options, $default = "")
  {
    global $$name;
    if(isset($$name) && ($$name != ''))
    {
      $default = $$name;
    }

    // http://www.php.net/foreach
    foreach($options as $value => $text)
    {
      $tags .= "  <INPUT type=\"radio\" name=\"$name\" value=\"$value\"";

      if($value == $default)
      {
        $tags .= ' CHECKED';
      }

      $tags .= ">$text</INPUT>\n";
    }

    return $tags;
  }


That minor change will allow your radio button to be preselected with the
existing value of $name.


Just because I'm super super nice, I'll put it all together for you.

Copy this into a PHP file and run it.  twice.

<?php

// This should really be defined in some form utilities inc file
  function create_radio($name, $options, $default = "")
  {
    global $$name;
    if(isset($$name) && ($$name != ''))
    {
      $default = $$name;
    }

    // http://www.php.net/foreach
    foreach($options as $value => $text)
    {
      $tags .= "  <INPUT type=\"radio\" name=\"$name\" value=\"$value\"";

      if($value == $default)
      {
        $tags .= ' CHECKED';
      }

      $tags .= ">$text</INPUT>\n";
    }

    return $tags;
  }

// this should be defined in some db interface include file
function get_types()
{
  return array('Beer', 'Gin', 'Rum', 'Scotch', 'Vodka', 'Whiskey');
}


// this should be the actual start of the script:
$types = get_types();


echo "<FORM method=\"GET\" action=\"$PHP_SELF\">\n";

echo create_radio('drink', $types);

echo "  <INPUT type=\"submit\" value=\"Pick yer poison!\">\n";
echo "</FORM>\n"

?>


I think you should be able to handle inserting the value of $drink into the
database, right?

take care,

nik

Message #5 on Wed, 22 May 2002 00:07:04
Heh, 


Thanx Nik.
Appreciate your help, And time you spent on this code.

I'll make it as OO as possible and make sure to use
it all the way long.
Message #6 on Tue, 21 May 2002 16:07:16 -0700
No problem.

It's been my experience that if I spend some time coming up with some cool
utility functions that replace the need to type redundant code, the first
part of the project takes a little longer but the rest speeds up as you add
to and continually update/tweak the utilities.

Best of luck!

nik


  Return to Index