Jump to content

how to? selecting multipe rows displaying as checkboxes?


shane85

Recommended Posts

hey guys

 

I have a table called host_locations.

in this I have

host_id

location_name

etc etc

 

now I have a form that what im trying to do is display check boxes from the rows in this table. So for instance, say I had 4 rows in my table

Java Hut

Coffee Bean

Joes Coffee

Franks Coffe

 

what id like to do is display these items with a checkbox next to them, so that the user in the form can select which location(s) they would like to chose, and then have this info stored into a different table which will be clients. In this table, there is a row called locations which this info needs to be stored in, I guess seperated by - so that I can do an explode statement later on to get the different locations out? My main problem is I also want to display a edit page where it will show the locations with the checkboxes checked off like they submited initially, however if they wish to uncheck one and update it, it will update the db.

 

sorry for the long post..I posted this a few days ago which is where someone suggested

 

$checkboxes = array('$check[0]', '$check[1]', '$check[2]', '$check[3]');
// values for your checkboxes
foreach ($checkboxes as $key => $checkbox)
{    echo '<input type="checkbox" id="', $checkbox . $key, '" name="locations[]" value="', $checkbox, '"',
(in_array($checkbox, $check) ? ' checked="checked"' : ''), '>',
'<label for="', $checkbox . $key, '">', $checkbox, '</label>';
}

 

however this doesnt work. just displays 4 checkboxes with the names $check[0] $check[1] etc beside it

Link to comment
Share on other sites

simple as a pie:

$checkboxes = array($check[0], $check[1], $check[2], $check[3]);
// values for your checkboxes
foreach ($checkboxes as $key => $checkbox)
{    echo '<input type="checkbox" id="', $checkbox . $key, '" name="locations[]" value="', $checkbox, '"',
(in_array($checkbox, $check) ? ' checked="checked"' : ''), '>',
'<label for="', $checkbox . $key, '">', $checkbox, '</label>';
}

Link to comment
Share on other sites

hmmm

 

im using the following code

 

$checkboxes = array($check[0], $check[1], $check[2], $check[3]);
// values for checkboxes
foreach ($checkboxes as $key => $checkbox)
{    echo '<input type="checkbox" id="', $checkbox . $key, '" name=".$arrEntry[advertising_locations]." value="', $checkbox, '"',
(in_array($checkbox, $check) ? ' checked="checked"' : ''), '>',
'<label for="', $checkbox . $key, '">', $checkbox, '</label>';
}

 

and im getting the following to happen/error

 

Warning: in_array() [function.in-array]: Wrong datatype for second argument in edit_client.php on line 50

 

line 50 is

 

(in_array($checkbox, $check) ? ' checked="checked"' : ''), '>',

 

and its also displaying 4 chekboxes, with a > before them

 

Link to comment
Share on other sites

and you're all over the place in your question.  I quickly skimmed over your question(?), and this was all that stuck out:

 

I guess seperated by - so that I can do an explode statement later on to get the different locations out?

 

that statement is a big no-no.

 

keep your database normalized meaning: store items like locations in separate table (called locations?), with a relationship back to the user/owner of that location (typically in a users table).

 

why?  because if you ever want to be able to search for/by location(s), this is the easiest, most efficient method.  otherwise, if you have all your locations in one record>field, you can't really search for the locations now, can you?  try searching for: beverly-hills-new-york-chicago-baltimore-toronto

Link to comment
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.