Xiode Posted January 16, 2007 Share Posted January 16, 2007 It seems I have had the hardest time understanding arrays... and foreach.. I have been working on a project and have come to a certain spot and now I am stuck... let me explain..I have a while loop that prints out my check boxes [code]while (blah) { $checkboxes .= "<input type='checkbox' name='var_name[]' value='".$varid."' /> <span style='font-weight:bold;'>".$var."</span><br />";}[/code]Now I am a little confused on how I get this in the DB as "1, 3, 5, 8"best I have seen so far is[code]foreach($var_name as $k => $v){ $newvar .= "$v, ";}$newvar = substr($memsquad, 0, -2);[/code]and then $newvar is what goes in the DB.....Somewhere something is wrong.. I can get something in the DB.. But it is only 1 #.... Where am I going wrong... Link to comment https://forums.phpfreaks.com/topic/34453-array-problems/ Share on other sites More sharing options...
Daniel0 Posted January 16, 2007 Share Posted January 16, 2007 [code]$newvar = join(', ',$_POST['var_name']);[/code] Link to comment https://forums.phpfreaks.com/topic/34453-array-problems/#findComment-162241 Share on other sites More sharing options...
Xiode Posted January 16, 2007 Author Share Posted January 16, 2007 That works.Now that I have say 3, 5 how do I can i define each of these.. these refer to another DB. These are the IDs of the names of the options... Now I am drained of thought on how to define them... I guess I would need to split them up and then define the IDs and paste them back together.... right? Link to comment https://forums.phpfreaks.com/topic/34453-array-problems/#findComment-162266 Share on other sites More sharing options...
Xiode Posted January 16, 2007 Author Share Posted January 16, 2007 The 3, 5 is in DB1 as col_varsIn DB2 3, 5 are the IDs(col2_id) of what I want to display(col2_name)..How can I tear them apart and define them...I have an SQL JOIN for my query. and I can define a single one.[code]$sql = mysql_query("SELECT db1.col_vars, db2.table_name FROM $db_table AS db1 LEFT JOIN $db_table2 AS db2 on db2.col2_id=db1.col_vars ");[/code] Link to comment https://forums.phpfreaks.com/topic/34453-array-problems/#findComment-162297 Share on other sites More sharing options...
Xiode Posted January 16, 2007 Author Share Posted January 16, 2007 if i use $row['col2_name'] it will output the first var.. Just nothing after the comma.... Link to comment https://forums.phpfreaks.com/topic/34453-array-problems/#findComment-162336 Share on other sites More sharing options...
Barand Posted January 16, 2007 Share Posted January 16, 2007 [quote]Now I am a little confused on how I get this in the DB as "1, 3, 5, 8"[/quote]You don't.Write them to a separate table as 4 rowsID | 1ID | 3ID | 5ID | 8where ID is the unique id of the record to which these values belong Link to comment https://forums.phpfreaks.com/topic/34453-array-problems/#findComment-162362 Share on other sites More sharing options...
Xiode Posted January 16, 2007 Author Share Posted January 16, 2007 I overcame that with the help of the join()..I got them in the DB seperated with a comma.. Like I wanted.. These are the IDs of some values I want to display in another table... I just don't know how to do it using an array... I could do it with a single just fine.. trying to do this with more than one is killin me... Link to comment https://forums.phpfreaks.com/topic/34453-array-problems/#findComment-162377 Share on other sites More sharing options...
Xiode Posted January 17, 2007 Author Share Posted January 17, 2007 Anyone??!! Link to comment https://forums.phpfreaks.com/topic/34453-array-problems/#findComment-162738 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.