473,769 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamic checkboxes from mysql table

tearing my hair out over this!

I have 2 tables:

PTable which lists all traits
pid,ptype
1,good
2,bad
3,neutral

UserP Table which records user and traits
uid,pid
1,2
1,3
9,1
9,2
9,3

For a certain user UID, how can I get php to automatically create html
checkboxes listing ALL traits from PTable and check each trait based
on the UserP table on a webpage?

Hence, if UID is 1

it should show
good - unchecked
bad - checked
neutral - checked

May 1 '07 #1
2 2739
For a certain user UID, how can I get php to automatically create html
checkboxes listing ALL traits from PTable and check each trait based
on the UserP table on a webpage?
Try a JOIN.

First, query something like:
SELECT
`u`.`uid`,
`t`.`pid`,
`t`.`ptype`
FROM
`PTable` AS `t`
INNER JOIN
`UserP` AS `u`
ON
`t`.`pid` = `u`.`pid`
WHERE
`u`.`uid` = $userId
Then iterate over the results

<?php
//...

while( @list( $uid, $tid, $tval ) = /* ... */ )
{
echo "<p>Trait: $tid<br />";

if( '1' == $tval )
echo "<input type='checkbox' id='traits[$tid]' value='1'
checked='checke d'>Good</input><br />";
else
echo "<input type='checkbox' id='traits[$tid]' value='1'>Good</
input><br />";
if( '2' == $tval )
echo "<input type='checkbox' id='traits[$tid]' value='2'
checked='checke d'>Bad</input><br />";
else
echo "<input type='checkbox' id='traits[$tid]' value='2'>Bad</
input><br />";
if( '3' == $tval )
echo "<input type='checkbox' id='traits[$tid]' value='3'
checked='checke d'>Neutral</input>";
else
echo "<input type='checkbox' id='traits[$tid]' value='3'>Neutr al</
input>";

echo '</p>';
}

//...
?>

The output would be xHTML compliant if you were to use underscores in
the middle of the trait id instead of doing it the easy way with
square brackets. You can also JOIN in another table that has the names
of the traits so you don't have to memorize what all of the trait ids
are.

-Mike PII

May 1 '07 #2
On May 1, 4:37 pm, Mike P2 <sumguyovrt...@ gmail.comwrote:
if( '1' == $tval )
echo "<input type='checkbox' id='traits[$tid]' value='1'
checked='checke d'>Good</input><br />";
else
echo "<input type='checkbox' id='traits[$tid]' value='1'>Good</
input><br />";
if( '2' == $tval )
echo "<input type='checkbox' id='traits[$tid]' value='2'
checked='checke d'>Bad</input><br />";
else
echo "<input type='checkbox' id='traits[$tid]' value='2'>Bad</
input><br />";
if( '3' == $tval )
echo "<input type='checkbox' id='traits[$tid]' value='3'
checked='checke d'>Neutral</input>";
else
echo "<input type='checkbox' id='traits[$tid]' value='3'>Neutr al</
input>";
Oops...I meant "type='radi o'".

-Mike PII

May 1 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7927
by: 'bonehead | last post by:
Okay, I've only been doing php/mysql for a few months and I admit to being a little stumped on this one. I have a form which posts updates to a mysql table. I'd like to use checkboxes to show and update boolean type values. In Access it's very simple to create a field of type Yes/No, bind a form to the table, and bind a checkbox object to the Yes/No field. I am unable to find generic examples of this in php/mysql. 1. What field type...
0
3740
by: Frank Collins | last post by:
Can anyone point me to some good examples on the web of using values from dynamically created checkboxes on forms in ASP, particularly relating to INSERTING those values into a SQL or Access database? Basically, I have a form on which I have a series of statements, with 3 checkboxes for each statement - YES, NO, MAYBE. This series of statements is being dynamically populated from a query of a table in Access, called "tblQuestions". In...
1
1624
by: middletree | last post by:
Hate to post this in a separate post, but felt that the last thread was too far down to get noticed. It is called dynamic checkboxes, and it contained some good advice for me, but it led to another question, and I wanted to go ahead and post my question here in hopes of getting an answer. The advice given to me about having a number of checkboxes which are put onto the page from the database, was to name them all the same thing, and...
8
2833
by: DylanM | last post by:
I have some checkboxes that are generated from the results of a database search. At the moment, the checkboxes are part of a table making up a form. Users are going through the form, clicking the boxes and saving to the database at the end with the 'Submit' command button. Is it possible to save the changes as the checkboxes are clicked? I suppose I'd need to write some dynamic ASP event handling at the same time as creating the checkboxes......
3
3984
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which step you're on. This all works fine, but I ran into some trouble when I started creating controls dynamically in my code-behind file. Each panel contains a table which is filled with various radio buttons, text fields and the such which are...
34
3832
by: clinttoris | last post by:
Hello Experts, I have been told to post this in the Javascript forum as I want to do this client side just before my form gets submitted. Once the user clicks the submit button a javascript function needs to run and validate all the checkboxes on my form and make sure none of them are unchecked. I suck at Javascript and my problem is 2fold. I have the following code that constructs the checkbox response.write "<input type=checkbox...
2
7053
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply. Each dynamically created row will return 3 values fee1_choice, fee1_unit and fee1_money. Note The above informaton is...
2
1430
by: Beacherboy | last post by:
Hi All, This one is a bit complex, so I'll try to be as logical explaining the issue as humanily possible. ASP/VBScript/MySQL Issue: Creating dynamic variables while there is an unknown constant Example: Somebody uses a drop down menu to select the type of fruit. Let's say they select apples. Now directly underneathe, a table of checkboxes is returned with all the possible results (chkbx1 = granny's, chkbx2 = smith, chkbx3 =...
1
3026
by: TechnoAtif | last post by:
Hi to all. I have got a form containing of checkboxes along with other items. I have simply no clue as to (i) how to make entry for those checkbox data into the mysql table . I mean : what query to put for creating field corresponding to checkboxes. Create table arc(status ---?) status is the array for checkboxes. what should be the data type and its length. (II)How to post data from the checkboxes correctly into the database on submit....
0
9590
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9424
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.