0

I'm still learning php so please take it easy on me. This might sound a silly question for you guys.

Right so. I have categories lets say Blogs, eCommerce, Portfolios etc. Files according too. Blog.php etc.

I also have functions.php where all my functions are.

MYSQL database is where I store information from them files. Website information, descriptions etc.

I want to have 1 function that queries data from a website just about 1 category so then I can display it in Blogs.php, eCommerce.php etc. My function to query data from mysql looks like this.

This is an example:

function querying_category($category){
    $db = DB::getInstance();
    $all = $db->query('SELECT * FROM website WHERE category = {$category} ORDER BY id DESC');

    if($all->count()){
       foreach($all->results() as $website){
              $web_data[] = $website;
       }
    }
    return $web_data;
}

and then let's say in my blog.php would go like:

$category = 'blog';
$website = querying_category($category);

Could you please tell me what am I doing wrong? I want to declare a variable in my blog.php or ecommerce.php etc without rewriting the following query:

'SELECT * FROM website WHERE category = {$category} ORDER BY id DESC

Can I achieve it with passing in $category as an argument in querying_category() but declaring $category in my blog.php or ecommerce.php?

14
  • 1
    Sidenote: Shouldn't that be return $website;? I stand at being wrong ;-) Commented Mar 27, 2014 at 19:23
  • Yes sorry. I was writting really quick made a mistake :) Commented Mar 27, 2014 at 19:25
  • 1
    Are you receiving an error? If I had to guess you need to wrap {$category} in single quotes in your query but without an error message i'm not sure. Commented Mar 27, 2014 at 19:29
  • @Fred-ii- answer the question. :) Your edit worked Commented Mar 27, 2014 at 20:13
  • 1
    About return $website;? @webAwwards Commented Mar 27, 2014 at 20:16

1 Answer 1

1

It seems as if the editing I made to the question fixed the OP's problem.

There were a few lines of code that were not properly indented along with a few spelling mistakes.

As per the OP's request, this answer has been given in order to close the question.

However, this line return $web_data; should have been return $website;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.