Welcome Guest, Not a member yet? Register   Sign In
Working with arrays and checkboxes
#1

[eluser]xtremer360[/eluser]
If you notice in the bottom var dump you notice how both of the objects have a news article id of 1 which is the article your viewing which is right. Now the tags associated with it is tag id 1 and tag id of 2. So if you look above those are all the post tags in the database.
I need it to take those two post tag ids of 1 and 2 and scroll through the top object and have the post tag with an id of 1 and 2 be checked so according to this the checkbox of Kid Wonder and Oriel should both be checked. I tried the in_array but couldn't find a working solution.

Code:
news_article_post_tags

array(3) {
  [0]=>
  object(stdClass)#33 (2) {
    ["news_article_post_tag_id"]=>
    string(1) "1"
    ["news_article_post_tag_name"]=>
    string(10) "Kid Wonder"
  }
  [1]=>
  object(stdClass)#34 (2) {
    ["news_article_post_tag_id"]=>
    string(1) "2"
    ["news_article_post_tag_name"]=>
    string(5) "Oriel"
  }
  [2]=>
  object(stdClass)#35 (2) {
    ["news_article_post_tag_id"]=>
    string(1) "3"
    ["news_article_post_tag_name"]=>
    string(11) "Ryu Satoshi"
  }
}

news article index post tags data

array(2) {
  [0]=>
  object(stdClass)#37 (2) {
    ["news_article_id"]=>
    string(1) "1"
    ["news_articles_post_tag_id"]=>
    string(1) "1"
  }
  [1]=>
  object(stdClass)#38 (2) {
    ["news_article_id"]=>
    string(1) "1"
    ["news_articles_post_tag_id"]=>
    string(1) "2"
  }
}

Code:
<?php
foreach ($news_article_post_tags as $news_article_post_tag)
{
    if (in_array($news_article_post_tag->news_article_post_tag_id, $news_articles_index_post_tags_data))
    {
        $checked_value = 'checked';
    }
    else
    {
        $checked_value = '';
    }
        $data = array('name' => 'news_article_post_tags', 'id' => $news_article_post_tag->news_article_post_tag_id, 'checked' => $checked_value);
        echo form_label(form_checkbox($data).$news_article_post_tag->news_article_post_tag_name);
    }
?>
#2

[eluser]CroNiX[/eluser]
Look at how you are creating form_checkbox() and then compare it to the user guide. You're setting your checkboxes wrong.

From the user guide for form_checkbox():
Code:
$data = array(
    'name'        => 'newsletter',
    'id'          => 'newsletter',
    'value'       => 'accept',
    'checked'     => TRUE,
    'style'       => 'margin:10px',
    );

echo form_checkbox($data);

// Would produce:

<input type="checkbox" name="newsletter" id="newsletter" value="accept" checked="checked" />
#3

[eluser]CroNiX[/eluser]
Also, your form_label() appears to be wrong. (look at where you are concatenating using a . where a , should be used to separate the first and 2nd parameters)
#4

[eluser]xtremer360[/eluser]
Now it won't even even the label. Plus this doesn't even help in my original issue.

Code:
<?php
                foreach ($news_article_post_tags as $news_article_post_tag)
                {
                    $data = array('name' => 'news_article_post_tags', 'id' => $news_article_post_tag->news_article_post_tag_id, 'checked' => TRUE);
                    echo form_label(form_checkbox($data), $news_article_post_tag->news_article_post_tag_name);
                }
                ?>
#5

[eluser]CroNiX[/eluser]
Sorry, just pointing out mistakes as I see them. I'll leave you to it now...
#6

[eluser]xtremer360[/eluser]
That's why I came here. I do thank you for pointing out the mistake but since I fixed an issue like you suggested it did create another issue with not echoing the label.




Theme © iAndrew 2016 - Forum software by © MyBB