Jump to content

Checking array values


Xtremer360

Recommended Posts

I have the following code that works however I want to add to it. I have another array of values that I want to see if the news_article_category_id exists inside the array and if it is then make the checked value of the checkbox set to checked. How would that be accomplished?

 

 

<?php 
                foreach ($news_article_categories as $news_article_category)
                {
                    
                    $data = array('name' => 'news_article_categories', 'id' => $news_article_category->news_article_category_id);
                    echo form_label(form_checkbox($data).$news_article_category->news_article_category_name);
                }
                ?>

Link to comment
Share on other sites

<?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);
                }
                ?>

 

I ran a print_r on both arrays and this is the result.

 

Array
(
    [0] => stdClass Object
        (
            [news_article_category_id] => 1
            [news_article_category_name] => Headlines
            [news_article_posting_permission_name] => Editors and Admins and Webmasters Only
        )

    [1] => stdClass Object
        (
            [news_article_category_id] => 3
            [news_article_category_name] => Columns
            [news_article_posting_permission_name] => Editors and Admins and Webmasters Only
        )

    [2] => stdClass Object
        (
            [news_article_category_id] => 4
            [news_article_category_name] => Main News
            [news_article_posting_permission_name] => Editors and Admins and Webmasters Only
        )

    [3] => stdClass Object
        (
            [news_article_category_id] => 2
            [news_article_category_name] => Rumors
            [news_article_posting_permission_name] => All Users
        )

)

Array
(
    [0] => stdClass Object
        (
            [news_article_id] => 1
            [news_article_category_id] => 1
        )

)

 

I'm trying to figure out why none of the checkboxes are being checked.

Link to comment
Share on other sites

in_array doesn't work with arrays of objects.  You have arrays of objects.  You need to know if one incomplete object is inside a list of other more-complete objects.  You need to write a function to check that.  You may hit a snag when you realize that one array's objects have category_id and the other array's objects have category_name.

Link to comment
Share on other sites

Still having an issue with this. 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.

 

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"
  }
}

s

Link to comment
Share on other sites

For the third time now:  in_array won't work here.  These are arrays of objects.

 

Your var-dump output keeps changing, so I'm going to leave you to figure this out on your own.

 

You have a problem:  You have two arrays of objects.  You wish for the objects in array1 to do something special when their attribute matches the attribute of any object in array2.  Break down that problem.  Write a function which accepts 2 inputs (a news_article_post_tag object, and the news article index post tags data array) and returns TRUE if the news_article_post_tag object matches any of the items in the index post tags array.  Then, use it when you print these items to check them when appropriate.

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.