Bas Schreuder
Bas Schreuder

Reputation: 172

combine two "unrelated" fields in form

I am just learning PHP and MySql, and learning it by creating my own simple BLOG with CMS. I am running into a problem here though.

I have a page called edit_cat.php. This is for editing the category created earlier.

My DB table looks likes this:

 cat_id   |   cat_title   |   parent_id
 --------------------------------------
 1           Comedy               0
 2           Standup              1
 3           Music                0
 4           Film music           3

When editing the post I want the user (which will be me), to be able to select a different parent category.

<?php

  $cat_id=$_GET['id'];

  $result = $db->prepare("SELECT * FROM categories WHERE cat_id=:cat_id");      
  $result->bindParam(':cat_id', $cat_id);
  $result->execute();
  $row = $result->fetch(PDO::FETCH_ASSOC);

?>

<div class="col-md-8">

  <h2>Edit Category</h2>

  <?php 
    if(isset($succes_update)){
      echo $succes_update; 
    }
  ?>

  <form class="form-inline" action="" method="POST">

    <input type="hidden" class="form-control" name="cat_id" value="<?php echo     $cat_id; ?>">

    Category Title<br>
    <input type="text" class="form-control" name="cat_title" value="<?php echo $row['cat_title']; ?>"><br>

    Category sub Title<br>
    <textarea class="form-control" name="sub_cat_title"><?php echo $row['sub_cat_title']; ?></textarea><br>

    Parent Category<br>
    <select name="parent_id">
      <option selected><?php echo $row['parent_id'];?></option>
      <?php
        $result = $db->prepare("SELECT DISTINCT parent_id FROM categories");
        $result->execute();
        for($i=0; $row = $result->fetch(); $i++){   
          echo "<option>" . $row['parent_id'] . "</option>"; 
        }
      ?>
    </select>

    <br><br><br><br>

    <input type="submit" value="Save" class="btn btn-primary"/>
  </form>

</div>

On this piece:

echo "<option>" . $row['parent_id'] . "</option>"; 

I want the the parent_id, but also the cat_title related to the parent_id to be shown. This is so, so that it is easier to work with (title/name works better then a number).

Anyone an idea on how to do this?

Upvotes: 1

Views: 51

Answers (2)

Martyn Shutt
Martyn Shutt

Reputation: 1706

$result = $db->prepare("SELECT parent_id, cat_title FROM categories");
$result->execute();
while($row = $result->fetch()){   
echo "<option value = '".$row['parent_id']."'>" . $row['cat_title'] . "</option>"; 
}
?>

No need for DISINCT now, as your primary key cat_id will ensure every row is unique, (provided it is a primary key). You can use a while loop rather than a for here, which is much cleaner, as you don't need a counting variable.

Upvotes: 2

Pazuzu156
Pazuzu156

Reputation: 679

Update the select query to

SELECT (parent_id,cat_title) FROM categories

Then when looping thought your rows, set a value to each option

"<option value=\"$row['parent_id']\">".$row['cat_title']."</option>"

Upvotes: 1

Related Questions