Welcome Guest, Not a member yet? Register   Sign In
Trying to set a checkbox here and i'm going loco
#1

[eluser]jtotheb[/eluser]
OK, my problem is this. I have got a page where someone ticks a box if they want something then they go somewhere else, then they come back and i want that checkbox to be ticked.

The info that it is ticked is shoved into the session thusly:
Code:
$this->session->set_userdata($this->input->post('web')); (FYI there is more than one checkbox)

Then in my code when they come back i have this:
Code:
foreach($extras_options->result() as $item):?> //gets matching data out of table to populate checkboxes
$checkbox_value = $item->value;
if (!empty($this->session->userdata[$checkbox_value])): //heres where it matches the session data to the db data
$checkbox_state = "1"
else:
$checkbox_state = "0"
endif;

I know the $checkbox_value is pulling the word "web" out as i echoed it to be sure, so i have to presume i'm not getting it from the session properly.

Can anyone help a poor frustrated finger bitten chap like myself?

I'd really appreciate it!
#2

[eluser]Michael Wales[/eluser]
$this->session->userdata is not an array.

Code:
if (!empty($this->session->userdata[$checkbox_value])): //heres where it matches the session data to the db data

should be:
Code:
if (!empty($this->session->userdata($checkbox_value))): //heres where it matches the session data to the db data
#3

[eluser]jtotheb[/eluser]
When i do that i get "Can't use method return value in write context in"

It seems to be that empty will only accept a variable.

Thank you for your help, i didn't even notice the square brackets round it...

Is there a better way of doing this altogether?
#4

[eluser]sandwormusmc[/eluser]
I would say avoid using empty() altogether. There's some good comments on it at PHP.Net regarding why it's not good in all cases (here).

I've been using strlen() to test for null values ... try that and see if it works. If it doesn't, assign that user_data information to a variable first, then test it. It's important to note what walesmd pointed out, that user_data() is a function, not an array. I think what empty()'s shortcomings are in this case are that it doesn't evaluate the function return value, and thus the errors you're seeing.
#5

[eluser]Michael Wales[/eluser]
Here's how I would do it - beginning to end:

Code:
// This array has a list of all your checkboxes
$extras = array('web', 'ftp', 'ssl');
foreach ($extras as $option) {
  // Check if there is a session variable for that option
  $check_status[$option] = '';
  if ($this->session->userdata($option)) {
    $check_status[$option] = ' CHECKED';
  }
}

To display form:
Code:
foreach ($extras as $option) {
  echo '<input type="checkbox" name="'.$option.'"'.$check_status[$option].' />'.$option.';
}

Just remember - when the form is submitted that boxes that are unchecked will not be sent to the form processor. I find the easiest way to handle this is to set all of the session (or database, however you are doing it) to FALSE and then go through and assign the ones that were passed to the form handler to TRUE.
#6

[eluser]jtotheb[/eluser]
Thanks walesmd, i think i have been overdoing this whole scenario.

I just read on php.net about "empty" and it certainly does look confusing in it's implementation.

Thank you very much for spelling out what i needed to do, it has given me insight into something else i have been overdoing too.

Thanks again. Very kind of you to help me out.
#7

[eluser]jtotheb[/eluser]
I still seem to be having trouble!, does anyone know how to print the entire contents of:

$this->session->userdata()

?

Thanks!
#8

[eluser]jtotheb[/eluser]
Right, ignore me, i wasn't setting my userdata properly. I was setting the array key but not the value which would be why i wasn't getting anything out!

I knew it would be something silly....




Theme © iAndrew 2016 - Forum software by © MyBB