1

I'm currently coding a blog to get experience with php.

In the edit.php I want to give the category of a post if a post has one. This is the edit.php:

    <div class="categories-heading">
    <?php if (!empty($entry->c_Id)): ?>
      <div class="category-heading">
        <h3>Category: <?php echo e($categoryFromId); ?></h3>
      </div>
      <div class="category-heading">
        <h3>change category</h3>
      </div>
    </div>
    <form method="post">
      <div class="categories-post-edit">
        <?php foreach($categories as $cat): ?>
          <div class="category-post-edit">
            <input type="radio" name="category" value="<?php echo e($cat->id); ?>">
              <label> <?php echo e($cat->category); ?></label>
            </input>
          </div>
        <?php endforeach; ?>
      </div>
    </form>
  <?php else: ?>
    <div class="category-heading">
      <h3>choose category</h3>
    </div>
  </div>
  <form method="post">
    <div class="categories-post-edit">
      <?php foreach($categories as $cat): ?>
        <div class="category-post-edit">
          <input type="radio" name="category" value="<?php echo e($cat->id); ?>">
            <label> <?php echo e($cat->category); ?></label>
          </input>
        </div>
      <?php endforeach; ?>

This is the part of the function edit() in the PostsAdminController.php which is relevant for this:

if(!empty($_POST['title']) AND !empty($_POST['subheading']) AND 
!empty($_POST['content'])
AND !empty($_POST['category'])) {
  $entry->title = $_POST['title']; 
  $entry->subheading = $_POST['subheading'];
  $entry->content = $_POST['content'];
  $entry->c_Id = $_POST['category'];

  $this->postsRepository->update($entry);

  $savedSuccess = true;
 }
 $categoryFId = $this->categoryRepository->oneCategory($entry->c_Id); 

 $categoryFromId = $categoryFId['category'];
 var_dump($categoryFromId);

 $this->render("post/admin/edit", [
  'entry' => $entry,
  'savedSuccess' => $savedSuccess,
  'categories' => $categories,
  'categoryFromId' => $categoryFromId
 ]);
}

And this is the function oneCategory in the CategoryRepository.php that interacts with the database.

public function oneCategory($id)
{
 $table = $this->getTableName();
 $model = $this->getModelName();

 $stmt = $this->pdo->prepare("SELECT `category` FROM `$table` 
 WHERE id = :id"); 

 $stmt->setFetchMode(PDO::FETCH_CLASS, $model);
 $oneCategory = $stmt->fetch(PDO::FETCH_CLASS);

 return $oneCategory;
}

oneCategory() gives out an array no matter if I put SELECT * or SELECT category in the query, that's why I put $categoryFId['category'] into a variable after.

It works, but I think there must be a more easy or quicker way (like special functions or so), I just didn't manage to find what I search for

5
  • It's unclear what exactly you want. If you want oneCategory() to return a single element of an array, just do return $oneCategory['category']; That being said, are you sure that you're getting an array and not an object? Commented Dec 4, 2018 at 13:45
  • 1
    Unclear for me too, but my guess is the same with Patrick. You want your function to only return the string value not an array? if so take a look here stackoverflow.com/questions/11928136/… Commented Dec 4, 2018 at 13:48
  • What I want is that $categoryFromId is a string like for e.g. ("Horror"). So the way I get the value that's at 'category' is okay I assume? Commented Dec 4, 2018 at 13:50
  • 1
    Yes, in your case your function returns the query result as array and you save only the "category" into a variable and then use it. That is one way of doing it, but you could also make the function to return that value instead of array. Commented Dec 4, 2018 at 13:54
  • Alright, thank you :) Commented Dec 4, 2018 at 13:58

0

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.