-2

Simple question...

wanna create something like this:

$args = array(
  'post_type' => 'posts',
  'limit' => 5,
  'cat' => 'news'
);

Then, a function that can actually get these 'keys'... like:

function get_posts($args) {

    $post_type = $args['post_type'];
    $limit = $args['limit'];
    $cat = $args['cat'];

    $sql = mysql_query("SELECT * FROM posts WHERE posttype='$post_type'  .... ");

    // other code...
}

how can i?

Sorry for bad english and thanks in advance.

Simon Arnold
  • 15,849
  • 7
  • 67
  • 85
wikarus
  • 71
  • 1
  • 1
  • 8

1 Answers1

1

Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

However, it seems that you have already answered yourself!

You should call the function this way:

$args = array(
  'post_type' => 'posts',
  'limit' => 5,
  'cat' => 'news'
);

function get_posts($args)
{

    $post_type = $args['post_type'];
    $limit = $args['limit'];
    $cat = $args['cat'];

    $sql = mysql_query("SELECT * FROM posts WHERE posttype='$post_type' ..........");
    // ... other code
}

get_posts($args);

If you get some errors, please update your question with error log.

Zoe
  • 27,060
  • 21
  • 118
  • 148
jacoz
  • 3,508
  • 5
  • 26
  • 42
  • The error was giving cause i have an error in my code... the function was wrote in wrong way.... – wikarus Dec 07 '12 at 13:33
  • Yeah, I get it! Post error message, please (and maybe the full code of `get_posts()` function). – jacoz Dec 07 '12 at 13:34