p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Finally...!


Message #1 by "Nikolai Devereaux" <yomama@u...> on Thu, 14 Feb 2002 16:03:05 -0800

Sorry for the delay:



Heya there troopers,



Krister fixed a syntactic issue, but that wasn't really a solution to the

problem.



Hermawan's radio suggestion was valid, if not for this line in the original

post:



> The answer can be 1 or more of these options which would

> be indicated by placing a check in the box.



The problem I have with the original post is that the database is what's

important here -- NOT the php code.



You can name the input variables whatever you want, it's easy enough to get

them from the target page of the form.



Let's say you have a database table with a column "Profession".  This column

only holds an id of a profession.



The profession ID's are 0 - 5 (or 1 - 6, whatever) corresponding to the 6

profession options you have.



<form action="target.php" method="get">

  <input type="checkbox" name="profession[]"> Web Developer <br>

  <input type="checkbox" name="profession[]">  <br>

  <input type="checkbox" name="profession[]">  <br>

  <input type="checkbox" name="profession[]">  <br>

  <input type="checkbox" name="profession[]">  <br>





---



Your message: (INMAIL-ID:110781)



Return-Path: <yomama@u...>

Received: from mailbox4.ucsd.edu ([132.239.1.56]) by  with SMTP (ListManager

WIN32 version 4.2); Fri, 15 Feb 2002 00:12:22 -0000

Received: from smtp.ucsd.edu (smtp-a.ucsd.edu [132.239.1.49])

	by mailbox4.ucsd.edu (8.12.1/8.12.1) with ESMTP id g1F000kf015729

	for <beginning_php@p...>; Thu, 14 Feb 2002 16:00:08 -0800 (PST)

Received: from nikolai (adsl-67-115-15-222.dsl.sndg02.pacbell.net

[67.115.15.222])

	by smtp.ucsd.edu (8.9.3/8.9.3) with SMTP id PAA09774

	for <beginning_php@p...>; Thu, 14 Feb 2002 15:59:57 -0800 (PST)

From: "Nikolai Devereaux" <yomama@u...>

To: "Beginning Php" <beginning_php@p...>

Subject: here it is

Date: Thu, 14 Feb 2002 15:59:57 -0800

Message-ID: <GPEFIKFDGDNDMFGPPCFPMELOCJAA.yomama@u...>

MIME-Version: 1.0

Content-Type: text/plain;

	charset="iso-8859-1"

Content-Transfer-Encoding: 7bit

X-Priority: 3 (Normal)

X-MSMail-Priority: Normal

X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2911.0)

Importance: Normal

X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4807.1700





Heya there troopers,



Krister fixed a syntactic issue, but that wasn't really a solution to the

problem.



Hermawan's radio suggestion was valid, if not for this line in the original

post:



> The answer can be 1 or more of these options which would

> be indicated by placing a check in the box.



The problem I have with the original post is that the database is what's

important here -- NOT the php code.



You can name the input variables whatever you want, it's easy enough to get

them from the target page of the form.



Let's say you have a database table with two columns: user_id and

profession_id.  These columns hold unsigned integers as unique id's.



The profession ID's are 0 - 5 corresponding to the 6 profession options you

have.  Let's say that the following ID's are set up for jobs:



0 Web Developer

1 Software Engineer

2 Burger Flipper

3 Executive

4 Artist

5 Pro Golfer





<form action="target.php" method="get">

  <input type="checkbox" name="profession[]"> Web Developer     <br>

  <input type="checkbox" name="profession[]"> Software Engineer <br>

  <input type="checkbox" name="profession[]"> Burger Flipper    <br>

  <input type="checkbox" name="profession[]"> Executive         <br>

  <input type="checkbox" name="profession[]"> Artist            <br>

  <input type="checkbox" name="profession[]"> Pro Golfer        <br>

  <input type="submit">

</form>





target.php would populate the profession[] array with all the checked

values, kind of like what you want, in the order that they appear in the

form.



The problem is, that only CHECKED values get sent!  all the unchecked values

are ignored.  So if you checked "Pro Golfer" and submitted the form,

$profession[0] would be "Pro Golfer".



    [profession] => Array

        (

            [0] => on

        )



This isn't good enough.



What you can do is set the array index yourself to ensure that the array

index is the same as the profession ID in your database.





<form action="checkbox.php" method="get">

  <input type="checkbox" name="profession[0]"> Web Developer     <br>

  <input type="checkbox" name="profession[1]"> Software Engineer <br>

  <input type="checkbox" name="profession[2]"> Burger Flipper    <br>

  <input type="checkbox" name="profession[3]"> Executive         <br>

  <input type="checkbox" name="profession[4]"> Artist            <br>

  <input type="checkbox" name="profession[5]"> Pro Golfer        <br>

  <input type="submit">

</form>



Now, if only "Pro Golfer" is checked, $profession looks like this:



    [profession] => Array

        (

            [5] => on

        )



This is good, because what you can do is loop through all the items in the

array and insert the KEYS into your table.  You can safely ignore the value,

since only "on" values should appear here, but if you want to be extra safe,

you can test to make sure its on:



reset($profession);

while( each($id, $value) = list($profession) )

{

  if($value == "on")

  {

    db_query("INSERT Profession " .

             "(user_id, profession_id) " .

             "VALUES ($user_id, $id)");

  }

}



Assuming you already have a $user_id variable available, this would insert

the proper item into the database.  Each profession that a user has will be

represented in a row in the database.



This means that you don't have one single key into the database -- any user

can have up to 6 professions, and any profession can be worked by any number

of users.  What you should guarantee, though, is that no row can be

duplicated -- that the combination of user_id and profession_id is unique.





I know it was long, but I hope it was helpful.



Nik



Message #2 by "Debra Scott" <hlawebservice@h...> on Fri, 15 Feb 2002 00:55:16
 I cannot thank you enough for the help.  I can finally get some rest 

because I've been obsessed with this problem all day.  I'm so glad I found 

you guys.  I'm glad you understood what I was trying to do. Your 

explanation was perfect.

Thanks to all you guys.  Each response gave me a little more insight into 

php and mysql.  

I'll be so glad when I can answer somebody's question(s)!

Thanks again,

Debra
Message #3 by "Hermawan Haryanto" <hermawan@h...> on Fri, 15 Feb 2002 08:25:38 +0700
No big deal Debra.

We are just little group of people who have no work to do and need some more

sleep, isn't Nik?. Anyway, I got something on my head, which one of you

people who work for wrox it self?



Thanks



Hermawan Haryanto

hermawan@h...

----- Original Message -----

From: "Debra Scott" <hlawebservice@h...>

To: "beginning php" <beginning_php@p...>

Sent: Friday, February 15, 2002 12:55 AM

Subject: [beginning_php] Re: Finally...!





> I cannot thank you enough for the help.  I can finally get some rest

> because I've been obsessed with this problem all day.  I'm so glad I found

> you guys.  I'm glad you understood what I was trying to do. Your

> explanation was perfect.

> Thanks to all you guys.  Each response gave me a little more insight into

> php and mysql.

> I'll be so glad when I can answer somebody's question(s)!

> Thanks again,

> Debra




$subst('Email.Unsub').

>




  Return to Index