Jump to content

Recommended Posts

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

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

The 3, 5 is in DB1 as col_vars
In 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

[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 rows

ID | 1
ID | 3
ID | 5
ID | 8

where 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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.