T
T
tizis2015-01-26 09:39:11
PHP
tizis, 2015-01-26 09:39:11

How can a tagging system be implemented?

In general, I'll probably start from afar.
There is a site with lists of works, initially books were written simply in html on the page.
Now the data is being transferred to the database with the output of the list on the page in the form of a table.
I myself am at the level of html / css and superficially js / php, thanks to Google I was able to implement data storage + output to a table.
Actually the question - is it possible to implement a tag system with their output in a separate column in the table? I myself imagine it like this: tags are stored in one database table, books in another + there is a table where the book id and tag id are indicated, but I don’t know how to implement this.
(example of code for outputting data from db to table:

<?php 
    // определяем начальные данные
    $db_host = 'localhost';
    $db_name = 'mydatabase';
    $db_username = 'admin';
    $db_password = 'admin09876';
    $db_table_to_show = 'Contacts';

    // соединяемся с сервером базы данных
    $connect_to_db = mysql_connect($db_host, $db_username, $db_password)
    or die("Could not connect: " . mysql_error());

    // подключаемся к базе данных
    mysql_select_db($db_name, $connect_to_db)
    or die("Could not select DB: " . mysql_error());

    // выбираем все значения из таблицы "Contacts"
    $qr_result = mysql_query("select * from " . $db_table_to_show)
    or die(mysql_error());

    // выводим на страницу сайта заголовки HTML-таблицы
    echo '<table border="1">';
  echo '<thead>';
  echo '<tr>';
  echo '<th>Имя</th>';
  echo '<th>Телефон</th>';
  echo '<th>E-Mail</th>';
  echo '</tr>';
  echo '</thead>';
  echo '<tbody>';
  
   // выводим в HTML-таблицу все данные клиентов из таблицы MySQL 
  while($data = mysql_fetch_array($qr_result)){ 
    echo '<tr>';
    echo '<td>' . $data['Name'] . '</td>';
    echo '<td>' . $data['Phone'] . '</td>';
    echo '<td>' . $data['eMail'] . '</td>';
    echo '</tr>';
  }
  
    echo '</tbody>';
  echo '</table>';

    // закрываем соединение с сервером  базы данных
    mysql_close($connect_to_db);
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Santiago K, 2015-01-26
@allexp

Everything is correct. Create a table tag (id, name) and tag_to_book (tag_id, book_id) The
query to get book tags would be something like this:

SELECT tag.* FROM tag_to_book AS tb
JOIN tag ON tag.id = tb.tag_id
WHERE tb.book_id = $book_id

D
Dmitry Entelis, 2015-01-26
@DmitriyEntelis

I myself imagine it like this: tags are stored in one database table, books in another + there is a table where the book id and tag id are indicated, but I don’t know how to implement this.
You are absolutely correct. Enough to start, then, depending on the tasks, there may be different implementation options.
can. google "left join", "group by", "group_concat"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question