0

Information

This should be a fairly simple question. On my website I have a post_categorys in my database that has a structure like below

    ID    |    Name
    1     |   Sports
    2     |   Health
    3     |   Movies
    4     |   Music

Now when I save a blog post I save the category_id such as 1 for movies in the blog_post table.

When printing out the blog post to my users I want to be able to get the blog post category name by something like $categorys[$blog_cat_id']['name']

Here is how I get all categorys for $categorys used above.

function getCategorysArray(){
    $STH = $this->database->prepare('SELECT * FROM project_categorys');
    $STH->execute();
    return $STH->fetchAll();
}

But this returns the array in this format

Array
(
[0] => Array
    (
        [id] => 1
        [0] => 1
        [name] => Sports
        [1] => Sports
    )

[1] => Array
    (
        [id] => 2
        [0] => 2
        [name] => Health
        [1] => Health
    )

[2] => Array
    (
        [id] => 3
        [0] => 3
        [name] => Movies
        [1] => Movies
    )

[3] => Array
    (
        [id] => 4
        [0] => 4
        [name] => Music
        [1] => Music
    )

Therefor I am unable to use my desired method to get the category name ($categorys[$blog_cat_id']['name'])

Question

Is there a way I can remap the array somehow so that $array[1]['name'] = Sports would be the output?

3 Answers 3

2

Try this:

function getCategorysArray(){
    $STH = $this->database->prepare('SELECT * FROM project_categorys');
    $STH->execute();
    $res = $STH->fetchAll(PDO::FETCH_ASSOC);
    $categories = array();
    foreach($res as $row){
        $categories[$row['id']] = $row;
    }
    return $categories;
}
Sign up to request clarification or add additional context in comments.

Comments

1

So, basically, you want the array indexed by the value of id?

First, you can simplify the array by calling fetchAll(PDO::FETCH_ASSOC). This means you will no longer have those unnecessary 0 and 1 indexes in your returned array.

Then the simplest solution is simply to loop over the returned array.

$STH = $this->database->prepare('SELECT * FROM project_categorys');
$STH->execute();
$results = $STH->fetchAll(PDO::FETCH_ASSOC);
$ret = array();
foreach ($results as $row) {
    $ret[$row['id']] = $row['name'];
}
return $ret;

Perhaps even better, we can remove the fetchAll call, since PDOStatement is Traversable:

$STH = $this->database->prepare('SELECT * FROM project_categorys');
$STH->execute();

$ret = array();
foreach ($STH as $row) {
    $ret[$row['id']] = $row['name'];
}
return $ret;

2 Comments

Works perfectly, thank you. One last question what do you mean by Traversable?
@Harry It means that PDOStatement implements the Traversable interface. This means you can run foreach on it.
1
public function getCategories()
{
  if (null == $this->categories) {
    $stmt = $this->database->preapre('SELECT id, name FROM project_categorys')->execute();
    $categories = $stmt->fetchAll();

    foreach($categories as $category) {
      $this->categories[$category['id']] = $category;
    }
  }
  return $this->categories;
}

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.