sw9 Posted April 30, 2008 Share Posted April 30, 2008 Hi Everyone, I am trying to write my own function that can take two parameters, but the 2nd parameter needs to be able to take multiple values (so I'm assuming an array). I am not sure how to write that up when creating my function. Here is an example of what I'm thinking: <?php function video_airtime_by_category($videoID, array){ } // Then I would call the function like this? video_airtime_by_category('21059', array($category1, $category2, $category3)); ?> Can anyone help me on how to properly do this? Quote Link to comment Share on other sites More sharing options...
tdpaxton Posted April 30, 2008 Share Posted April 30, 2008 <?php function video_airtime_by_category($videoID, $temp = array()){ // $temp[0] = $category1 // $temp[1] = $category2 // $temp[2] = $category3 } // Then I would call the function like this? video_airtime_by_category('21059', array($category1, $category2, $category3)); ?> Quote Link to comment Share on other sites More sharing options...
Aeglos Posted April 30, 2008 Share Posted April 30, 2008 It's just the same as a normal argument. function video_airtime_by_category($videoID, $catArray) { //Simple loop to echo the arguments. foreach ($catArray as $key=>$value) { echo $key .' -> '. $value .'<br/>'; } echo $videoID; } video_airtime_by_category('69', array('php', 'oop', 'foo')); //valid video_airtime_by_category('69', array('php')); //valid video_airtime_by_category('69', 'php'); // not valid, can't loop through a single variable. That code assumes you pass an array, so you should check if it's a single variable also, like: function video_airtime_by_category($videoID, $categories) { if (is_array($categories) { foreach ($catArray as $key=>$value) { echo $key .' -> '. $value .'<br/>'; } } else { echo $categories; } echo $videoID; } video_airtime_by_category('69', array('php', 'oop', 'foo')); //valid video_airtime_by_category('69', array('php')); //valid video_airtime_by_category('69', 'php'); // also valid Or you can also type hint the second argument to accept ONLY arrays: function video_airtime_by_category($videoID, array $categories) { // some code. } video_airtime_by_category('69', array('php', 'oop', 'foo')); //valid video_airtime_by_category('69', array('php')); //valid video_airtime_by_category('69', 'php'); // not valid, will return an error. What you posted in as function video_airtime_by_category($videoID, $temp = array()) { } Is only giving a default empty array value to the second parameter. If you do that you can basically call the function without the $temp argument or override it, like: //All this ways would be valid video_airtime_by_category('69', array('php', 'oop', 'foo')); video_airtime_by_category('69', array('php')); video_airtime_by_category('69', 'php'); video_airtime_by_category('69'); Whether it will error out or not depends on what the function does. If you don't pass the second argument it will default to an empty array and if you pass it a string it won't be an array inside so you must account for all those cases. Quote Link to comment Share on other sites More sharing options...
sw9 Posted April 30, 2008 Author Share Posted April 30, 2008 Thank you very much for these detailed responses. I definitely want to require the second parameter, so I appreciate the extra code that was described. Upon trying to fill out my function, I realized I am not exactly sure how to treat that array parameter to fit into my mysql statement. Here is what I want to do, but I can't figure out how to parse each $terms value without making the mysql query run alongside a foreach. So if my call to the function is this: <?php print get_program_nid_from_showing_by_taxonomy( $showing_nid, '312', '222' ); ?> then I would want the WHERE clause to show up as WHERE tn.tid IN ('312', '222'), but as it is below it only takes the first value. <?php function pegevent_get_program_nid_from_showing_by_taxonomy ( $showing_nid, $terms ){ $qs = "select cs.field_program_nid from content_type_content_showing cs LEFT JOIN term_node tn ON (cs.field_program_nid = tn.nid) WHERE tn.tid IN ($terms) AND cs.nid = $showing_nid"; $qr = db_query( $qs ); $row = mysql_fetch_array( $qr ); if ($row[0]){ return $row[0]; } } ?> I appreciate any help in advance! Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 30, 2008 Share Posted April 30, 2008 function video_airtime_by_category($videoID, array $categories) { // some code. } That's probably your best bet, but it'll throw an error if you don't pass it an array, so make sure it's an array. Quote Link to comment Share on other sites More sharing options...
Aeglos Posted April 30, 2008 Share Posted April 30, 2008 So it's a mixed problem between string composition and array handling. The trick is to loop through the array and with it make a desired string to insert in the query, or ther shorter path; imploding it, or both. Let's say you type hint the argument for the sake of making it mandatory, you can do something like this: function pegevent_get_program_nid_from_showing_by_taxonomy ( $showing_nid, array $terms ) { //Initial empty string. $terms_string = ''; //Loops through the array single-quoting each element; foreach ($terms as $key => $value) { $terms[$key] = " '{$value}' "; } $terms_string = implode(', ', $terms); /* At this point the $terms_string holds a string in the form of: '301', '784', 'foo', 'xyz', '123' If you don't include the quoting loop, you can just implode the array. if so it will have the form: 301, 784, foo, xyz, 123 */ $qs = "select cs.field_program_nid from content_type_content_showing cs LEFT JOIN term_node tn ON (cs.field_program_nid = tn.nid) WHERE tn.tid IN ($terms_string) AND cs.nid = $showing_nid"; $qr = db_query( $qs ); $row = mysql_fetch_array( $qr ); if ($row[0]){ return $row[0]; } } That should do the trick. Regards. Quote Link to comment Share on other sites More sharing options...
sw9 Posted May 8, 2008 Author Share Posted May 8, 2008 Thank you very much for the response. It took me awhile to get back because I realized that the effect of the above wasn't giving me exactly what I needed. I needed to make nested sql statements for each of the terms in the array. Here is the final result: <?php function pegevent_get_program_nid_from_showing_by_taxonomy ( $showing_nid, array $terms ) { // I need to extract the first term from the array for a separate mysql statement, then we slice the array to keep the rest of the terms $numTerms = count($terms); $lessTerms = $numTerms - 1; $newTerms = array_slice($terms, 1, $lessTerms); $terms_string = ''; //Loops through the array to make a nested statement for every array element after the first one; foreach ($newTerms as $key => $value) { $newTerms[$key] = " AND tn.nid IN (SELECT nid FROM term_node WHERE tid = '{$value}')"; } $terms_string = implode(' ', $newTerms); $qs = "select distinct cs.field_program_nid from content_type_content_showing cs LEFT JOIN term_node tn ON (cs.field_program_nid = tn.nid) WHERE tn.nid IN (SELECT nid FROM term_node WHERE tid = '".$terms[0]."')".$terms_string. " AND cs.nid = $showing_nid"; // die($qs); $qr = db_query( $qs ); $row = mysql_fetch_array( $qr ); if ($row[0]){ return $row[0]; } } // End pegevent_get_program_nid_from_showing_by_taxonomy ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.