RSS Forums RSS
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 4121 | Replies: 9 | Thread Tools  Display Modes
Reply
Join Date: Jun 2004
Posts: 17
Reputation: Sideshow Bob is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Sideshow Bob's Avatar
Sideshow Bob Sideshow Bob is offline Offline
Newbie Poster

Fun with Forms

  #1  
Jun 8th, 2004
Hi, I'm creating an administration area for a little web-app and I've come across a problem that I've never encountered before.

The form that I'm working on is used for the maintenance of a table that has several properties. One of these properties can be selected with checkboxes, and then recorded in the correct column in a comma-delimited list. The problem that I'm having is constructing/alterring the list - for some reason I can't for the life of me figure out how to do it.

Oh yeah, the fun part is that the properties that are in the checkbox are created on the fly from another table, which is why i didn't decide to create a column for each option and flag it.

Here's the snippet of code that creates the checkbox area:
[php]
<?php
$sql2 = "SELECT * FROM tbl_action";
$result = mysql_query($sql2);

while($act_row = mysql_fetch_array($result)) {
echo "<input type=checkbox value=\"".$act_row['action_id']."\"> ";
if(strstr($myrow['spy_action'], $act_row['action_id']))
echo "checked>";
echo ucwords($act_row['action_name'])."<br>";
}
?>
[/php]

Ok, 2 tables - spy and action. All columns from spy are called spy_name, and from action are act_name.

So far I've considered using Javascript, making the code create a new column every time a new action is created, and various other techniques that have been thrown out as soon as I think of them.

This is the last part of the admin area, and once it's complete I'll be a very happy boy.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2002
Location: Long Island, NY
Posts: 1,134
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Rep Power: 12
Solved Threads: 4
Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Fun with Forms

  #2  
Jun 10th, 2004
I don't think I understand you correctly. All you want to do is check multiple checkboxes, put those checked values in a comma delimited string, and insert it in some table?
_.:: my websites ::._
blog @ www.samaru.net * engi No Jutsu @ www.narutorp.net * portfolio @ shinylight.com
deviantART: inscissor
Reply With Quote  
Join Date: Jun 2004
Posts: 17
Reputation: Sideshow Bob is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Sideshow Bob's Avatar
Sideshow Bob Sideshow Bob is offline Offline
Newbie Poster

Re: Fun with Forms

  #3  
Jun 10th, 2004
Hi inscissor, thanks for the reply. Let me see if I can clarify myself.

I want to be able to check/uncheck multiple checkboxes, and put those values in a comma delimited string, and then stick it in a column, that's right.

The part that's throwing me for a loop though is that the checkboxes are created from the database (the options come from another table) in the first place.

I'm starting to think I'm overthinking this whole problem, but still haven't figured out how to do it.
Reply With Quote  
Join Date: Feb 2002
Location: Long Island, NY
Posts: 1,134
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Rep Power: 12
Solved Threads: 4
Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Fun with Forms

  #4  
Jun 11th, 2004
Well, your problem seems to stem from how you're creating the checkboxes in the loop. First of all, here:

Originally Posted by SEO Bob

[php]
echo "<input type=checkbox value=\"".$act_row['action_id']."\"> ";
[/php]

you don't create a name for the checkbox, so how are you going to retrieve it? The way it's usually done is you give the checkbox a name with open and closed bracks, like this: values[]

So when you pass this over, it'll pass it over as an array. So this is all you have to do:

Originally Posted by SEO Bob

[php]

<?php
$sql2 = "SELECT * FROM tbl_action";
$result = mysql_query($sql2);

while($act_row = mysql_fetch_array($result)) {
echo "<input type=checkbox name="values[]" value=\"".$act_row['action_id']."\"> ";
if(strstr($myrow['spy_action'], $act_row['action_id']))
echo "checked>";
echo ucwords($act_row['action_name'])."<br>";
}
?>

[/php]

So when you submit this, it'll submit an array of the values of only the ones that got checked. So you loop through this array, in this case being values[]. Also, when you're retrieving this form variable, retrieve it as $_POST['values'] and not $_POST['values[]']. I'm sure you can do the rest. Anything else, let me know.
_.:: my websites ::._
blog @ www.samaru.net * engi No Jutsu @ www.narutorp.net * portfolio @ shinylight.com
deviantART: inscissor
Reply With Quote  
Join Date: Jun 2004
Posts: 17
Reputation: Sideshow Bob is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Sideshow Bob's Avatar
Sideshow Bob Sideshow Bob is offline Offline
Newbie Poster

Re: Fun with Forms

  #5  
Jun 11th, 2004
Thanks Inscissor,

To be honest, I don't do much work with forms, which is why I wasn't sure how to handle this. I really appreciate the help, though.
Reply With Quote  
Join Date: Feb 2002
Location: Long Island, NY
Posts: 1,134
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Rep Power: 12
Solved Threads: 4
Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Fun with Forms

  #6  
Jun 11th, 2004
Originally Posted by SEO Bob
Thanks Inscissor,

To be honest, I don't do much work with forms, which is why I wasn't sure how to handle this. I really appreciate the help, though.
Were you able to figure it out? I do this all the time, so it's no sweat. I'll give you code if you want.
_.:: my websites ::._
blog @ www.samaru.net * engi No Jutsu @ www.narutorp.net * portfolio @ shinylight.com
deviantART: inscissor
Reply With Quote  
Join Date: Jun 2004
Posts: 17
Reputation: Sideshow Bob is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Sideshow Bob's Avatar
Sideshow Bob Sideshow Bob is offline Offline
Newbie Poster

Re: Fun with Forms

  #7  
Jun 11th, 2004
Given that the data is stored in an array, what type of structure should I use for storing it?
Reply With Quote  
Join Date: Feb 2002
Location: Long Island, NY
Posts: 1,134
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Rep Power: 12
Solved Threads: 4
Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Fun with Forms

  #8  
Jun 11th, 2004
Originally Posted by SEO Bob
Given that the data is stored in an array, what type of structure should I use for storing it?
Didn't you say you wanted it in a comma delimited string? So your structure would be a string. All you would need to do is loop through the array, and as you're looping, append\concatenate the adjacent strings in the array. Fortunately, you could also use the implode() function, so you'll have something like this:

[php]
$values = $_POST['value']; // getting value array from form variable
$delimited_values = implode(",", $values);
echo $delimited_values;
[/php]
_.:: my websites ::._
blog @ www.samaru.net * engi No Jutsu @ www.narutorp.net * portfolio @ shinylight.com
deviantART: inscissor
Reply With Quote  
Join Date: Jun 2004
Posts: 17
Reputation: Sideshow Bob is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Sideshow Bob's Avatar
Sideshow Bob Sideshow Bob is offline Offline
Newbie Poster

Re: Fun with Forms

  #9  
Jun 11th, 2004
It actually doesn't matter if they're stored in a comma-delimited string or not (but yes, i did say that that's how i wanted it).

My only requirement is that the data can be retrieved and written to easily. I've been looking at serializing the array and putting it into the column, but if you have another recommendation...
Reply With Quote  
Join Date: Feb 2002
Location: Long Island, NY
Posts: 1,134
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Rep Power: 12
Solved Threads: 4
Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Fun with Forms

  #10  
Jun 13th, 2004
That sounds fine. Maintaining it as an array gives you a lot of flexivity. You can manipulate it easily with the PHP array functions.
_.:: my websites ::._
blog @ www.samaru.net * engi No Jutsu @ www.narutorp.net * portfolio @ shinylight.com
deviantART: inscissor
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 5:38 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC