Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 12-19-2008, 06:17 AM Multiple checkboxes
Andy Pugh's Avatar
Extreme Talker

Posts: 203
Name: Andy
Location: N.Ireland
Trades: 0
Ok, I'm a little stumped with something, I have a playlist script that I wish to use on a number of radio stations, I'm building it so I can share particular playlist songs with other stations for simplicity, in order to do this though, I need to find a way to insert the values from 4 different checkboxes into one row, is that actually possible?

So I have my form..
PHP Code:
Citybeat:<input type="checkbox" name="station" value="citybeat" ><br />
The Bay:<input type="checkbox" name="station" value="thebay" ><br />
Lakeland:<input type="checkbox" name="station" value="lakeland"><br />
Midlands:<input type="checkbox" name="station" value="midlands"><br /> 
One of the suggestions made online was to do the following, no go though..

PHP Code:
Citybeat:<input type="checkbox" name=[]station value="citybeat" ><br />
The Bay:<input type="checkbox" name=[]station value="thebay" ><br />
Lakeland:<input type="checkbox" name=[]station value="lakeland"><br />
Midlands:<input type="checkbox" name=[]station value="midlands"><br /> 
MySQL query is as follows:

PHP Code:
$query "INSERT INTO playlist (artistname, songname, postdate, synopsis, video, featured, list, station, image) VALUES ('$artistname','$songname','$postdate','$synopsis','$video','$featured','$list','$station','$rand_name.$file_ext')";
mysql_query($query) or die(mysql_error()); 
The overall goal is that I can use a url option such as showplaylist.php?station=citybeat

this will then pull from the database using the 'contains' variable..

PHP Code:
$sql="SELECT * FROM playlist WHERE station CONTAINS '$station' ORDER BY id DESC";
$result=mysql_query($sql); 
I hope this makes sense, to be honest, going by my searching so far, I'm not even 100% sure it can be done..

Hope you can help!
Thanks,
Andy
__________________

Please login or register to view this content. Registration is FREE
Andy Pugh is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-19-2008, 06:21 AM Re: Multiple checkboxes
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
If you use the syntax...

Code:
<input type="checkbox" id="station[]" value="whatever" />
Then you will get an array of the checked values (i think...) with the name 'station' and then you can loop through them and add your database rows as you need...
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 12-19-2008, 06:32 AM Re: Multiple checkboxes
Andy Pugh's Avatar
Extreme Talker

Posts: 203
Name: Andy
Location: N.Ireland
Trades: 0
Hi, thanks for your response, that's getting a little further, however, I'm getting an undefined error inserted into my database, as follows

PHP Code:
<br />
<
b>Notice</b>:  Undefined variablestation in <b>/var/www/cnradiogroup.co.uk/pets/httpdocs/panel/addsong.php</bon line <b>237</b><br /> 
However I've defined all variables before that command...

PHP Code:
$artistname $_POST['artistname'];
$songname $_POST['songname'];
$postdate $_POST['postdate'];
$synopsis $_POST['synopsis'];
$video $_POST['video'];
$featured $_POST['featured'];
$list $_POST['list'];
$station $_POST['station']; 
veryyy strange!! argh! Thanks so far
Andy
__________________

Please login or register to view this content. Registration is FREE
Andy Pugh is offline
Reply With Quote
View Public Profile
 
Old 12-19-2008, 08:10 AM Re: Multiple checkboxes
Andy Pugh's Avatar
Extreme Talker

Posts: 203
Name: Andy
Location: N.Ireland
Trades: 0
Hi Guys,
I got it to work, it's a little buggy however...

For the form, I used the following...
HTML Code:
Citybeat : <input name="station[]" id="station[]" type="checkbox" value="Citybeat">

The Bay : <input name="station[]" id="station[]" type="checkbox" value="thebay">
I pulled it all together using the serialize function

PHP Code:
$station serialize($_POST['station']); 
and then of course the insert in the database, now, the text that's being insert is as follows...

HTML Code:
a:2:{i:0;s:8:"Citybeat";i:1;s:6:"thebay";}
this will and does work anyway using
PHP Code:
$sql="SELECT * FROM playlist WHERE station LIKE '%$station%' ORDER BY id DESC"
How would I output a cleaner version of the serialized text before committing it to the database?
Any ideas?
Thanks,
Andy
__________________

Please login or register to view this content. Registration is FREE

Last edited by Andy Pugh; 12-19-2008 at 08:26 AM..
Andy Pugh is offline
Reply With Quote
View Public Profile
 
Old 12-19-2008, 08:34 AM Re: Multiple checkboxes
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
why dont you just concatenate the array values and then you would lose all the serialising nonsense. Something like...

PHP Code:

$concatenatedStations 
"";
foreach( 
$station as $val )
{
      
$concatenatedStations .= $val

You might want to put in some spacing characters or something but ill leave you to put that in!
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 12-19-2008, 08:55 AM Re: Multiple checkboxes
Andy Pugh's Avatar
Extreme Talker

Posts: 203
Name: Andy
Location: N.Ireland
Trades: 0
You're a legend, I got a variation of that working, thanks for your help, much appreciated.

Andy
__________________

Please login or register to view this content. Registration is FREE
Andy Pugh is offline
Reply With Quote
View Public Profile
 
Old 12-22-2008, 04:35 AM Re: Multiple checkboxes
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
OMG, why concatenate in THAT way?
PHP Code:
$joined implode(','$stations); 
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 12-22-2008, 04:39 AM Re: Multiple checkboxes
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
In fact you should use database normalization which include table structure like the following:
songs: id, title, artist, duration, etc
stations: id, name, etc
song_to_station: song_id, station_id, etc
This will allow you to store the data in most correct way and manipulate numeric identifiers instead of strings.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to Multiple checkboxes
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.21648 seconds with 12 queries