Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Kyle Germaine
Kyle Germaine
8,174 Points

Problem calling variable within array. Please help!

I'm building an application and have run into a problem when calling a variable within an array. I'm not sure if it is semantics or if I am violating some sort of rule in PHP. The simplified question is directly below.

I'm kind of hacking together an array format with concatenation after a SQL query. I'm sure there is a more sound method of doing so and if you know it then I'm all ears.

I would really appreciate some help on this one. Thanks guys!

why doesn't this...

<?php

$the_array = 'array(13248, 14090, 14082, 14386, 13331)';

              $args = array(
                    'event_post_ids'  => $the_array,
                );

?>

equal to this...

<?php

              $args = array(
                    'event_post_ids'  => array(13248, 14090, 14082, 14386, 13331),
                );
?>

This is the entire code section below. I've checked the value of the variable after the SQL statement and have played with the concatenation in about a million different ways, but cant figure it out

// DOESN'T WORK

<nav class="cbp-spmenu cbp-spmenu-vertical2 cbp-spmenu-right" id="cbp-spmenu-s6">
     <h3 style="text-align:center;">Definitely Going</h3>
     <a id="showRight6X">Close</a>  
     <div> 
      <?php

            $my_id = get_current_user_id(); 

            $cal_id = $wpdb->get_results("SELECT meta_value FROM wp_usermeta WHERE user_id = $my_id AND meta_key = 'my_cal'");

            foreach ($cal_id as $cal_ids) { 
                     $mycal_id = $mycal_id . $cal_ids->meta_value. ', ';
                    }

            $newmycal = "array("; 
            $newmycal.= $mycal_id;
            $newmycal.= ")"; 

                $args = array(
                    'events_per_page'        => 8,
                    'show_subscribe_buttons' => false,
                    'show_calendar_button'   => false,
                    'hide_on_calendar_page'  => false,
                    'limit_by_cat'           => true,
                    'limit_by_tag'           => true,
                    'limit_by_post'          => true,
                    'event_cat_ids'          => array(),
                    'event_tag_ids'          => array(),
                    'event_post_ids'         => $newmycal,
                );

            the_widget('Ai1ec_Agenda_Widget', $args);
        ?>
    </div>
</nav>


// WORKS
<nav class="cbp-spmenu cbp-spmenu-vertical2 cbp-spmenu-right" id="cbp-spmenu-s7">
    <h3 style="text-align:center;">Maybe Going</h3>
    <a id="showRight7X">Close</a>
    <div>
     <?php
                $args2 = array(
                    'events_per_page'        => 8,
                    'show_subscribe_buttons' => false,
                    'show_calendar_button'   => false,
                    'hide_on_calendar_page'  => false,
                    'limit_by_cat'           => true,
                    'limit_by_tag'           => true,
                    'limit_by_post'           => true,
                    'event_cat_ids'          => array(),
                    'event_tag_ids'          => array(),
                    'event_post_ids'          => array(13248, 14090, 14082, 14386, 13331, 13331, 13965, 13957, 14422,),
                );

            the_widget('Ai1ec_Agenda_Widget', $args2);
        ?>
    </div>
</nav>

The $args(2) arrays are arguments to create a widget, which is part of Timely Calendar plugin for Wordpress.

There is a thread here where people have discussed this specific function.

I would really appreciate any help you can provide.

Thanks, Kyle

2 Answers

Hi Kyle,

I've never used the Timely Calendar plugin before but I'll do my best to answer your question.

Your first code example does not equal your second code example because wrapping PHP code with single quotes will cause the code to be treated a s string. There are way to convert this string to code, or evaluate it, however all these methods are very dangerous because they can allow for arbitrary code to be executed. Especially if you're getting data from user input or the database.

<?php

// This is a string not an array
$the_array = 'array(13248, 14090, 14082, 14386, 13331,)';

// This is an array
$the_array = array(13248, 14090, 14082, 14386, 13331),

?>

After reviewing your longer code example it appears you are trying to take data from the database and generate an array of IDs that can be used on the Timely Configuration. If that is indeed what you're trying to accomplish, here's how I'd tackle the problem:

<?php 

$cal_ids = $wpdb->get_results("SELECT meta_value FROM wp_usermeta WHERE user_id = $my_id AND meta_key = 'my_cal'");

foreach ($cal_ids as $id) { 
    $mycal_id[] = $id->meta_value;
}

There are lots of different way to get the data into an array but one the of the most common is the square bracket array syntax. What this tells PHP is that the variable $mycal_id is an array, and to add a new item to the array. It's very similar to the way you define a multidimensional arrays except you aren't defining a key and because you're aren't defining a key PHP will create an indexed array. Once you have the array defined you can add it to you $args multidimensional array.

You can read all about the different array creation methods in the PHP Manual for Arrays. You should also check out some of the great array functions built into PHP like array_push, array_pop, and array_map.

Hope this helps,

Ryan

Kyle Germaine
Kyle Germaine
8,174 Points

Thanks my man! I really appreciate the help.

Great solution, so much cleaner, simpler and helps build the PHP foundations I need.

This will be a core function to what I'm building, so I'm very grateful.

I obviously cant offer much in the programing arena, but if you ever need any help with finance, accounting, M&A, fundraising etc. then hit me up, I d be happy to help. kyle at devise-conjure dot com

Regards, Kyle