Which of these 2 PHP & MySQL methods for displaying credits from a table is more logical to use?

48 Views Asked by At

I'm working on a site that uses PHP & MySQL to display credits. I've made 2 versions of the same thing and I'm having a hard time deciding which of them is the best one to use. One uses 2 tables, the other uses just 1. I have things set up where it lists the name of the position and then places the people below it. I can't decide if it's easier to look at the credits_position table and then grab what is needed from credits_people or if I should use just the credits_people to hold everything. Which version would be easier to keep updated?

SQL

CREATE TABLE `credits__topic` (
 `topic_id` INT(11) NOT NULL AUTO_INCREMENT,
 `category_id` INT(11) DEFAULT NULL,
 `name` VARCHAR(48) DEFAULT NULL,
 `category` VARCHAR(48) DEFAULT NULL COMMENT 'Job name',
 PRIMARY KEY (`topic_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 26;
CREATE TABLE `credits__category` (
 `category_id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(48) DEFAULT NULL COMMENT 'Job name',
 PRIMARY KEY (`category_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 6;
INSERT INTO `credits_people` (`topic_id`, `category_id`, `name`, `category`)
VALUES 
(1, 1, 'Matthew Campbell', 'Creator'),
(2, 2, 'Godzilla', 'Assistant'),
(3, 1, 'Billy Bob', 'Creator'),
(4, 4, 'Martha Stewart', 'Tester'),
(5, 2, 'Mothra', 'Designer'),
(6, 2, 'Rodan', 'Contributing'),
(7, 2, 'King Ghidorah', 'Designer'),
(8, 3, 'Mechagodzilla', 'Assistant');
INSERT INTO `credits_position` (`category_id`, `name`)
VALUES
(1, 'Creator'),
(2, 'Designer'),
(3, 'Assistant'),
(4, 'Tester'),
(5, 'Contributing');

PHP

<?php
 $connection = mysqli_connect ("localhost", "root", "root", "main");

 // Version 1:
 $data = mysqli_query ($connection, "SELECT `mc`.`name` AS 'category_name', `mt`.`name` AS 'topic_name', `mt`.`category` AS 'topic_category' FROM `credits_position` AS `mc` INNER JOIN `credits_people` AS `mt` USING (`category_id`);");
 // Version 2:
 $data = mysqli_query ($connection, "SELECT `name` AS 'category_name', `category` AS 'topic_category' FROM `credits_people`;");

 // Works with both versions
 $responses = array ();
 while ($row = mysqli_fetch_array ($data)) {
  array_push ($responses, $row);
 }
 // This loads the 1st category name.
 $category = $responses [0] ["category_name"];
 echo "<p>" . $category . "</p>\n";
 echo "<ol>\n";
 foreach ($responses as $response) {
  // This loads when it finds that the category is different from the previous loop.
  if ($category !== $response ["category_name"]) {
   $category = $response ["category_name"];
   echo "</ol>\n";
   echo "<p>" . $response ["category_name"] . "</p>\n";
   echo "<ol>\n";
  }
  echo "<li>" . $response ["topic_name"] . "</li>\n";
 }
 echo "</ol>";

 mysqli_close ($connection);
?>
1

There are 1 best solutions below

0
nbk On

On big databases you want to save space, to have it for the relevant data.

So a normalized Tables, where you only have category_id(and of course many more such tables) you save a lot of space.

But i doubt that you will have millions of categories so you should start small and if the need arises you can increase it.