M
M
Morozilnikkk2018-05-29 16:20:59
PHP
Morozilnikkk, 2018-05-29 16:20:59

Pulling data from MySql?

I welcome everyone!
Please help.
I'm not experienced (in working with the database even more so), so please don't scold me for stupid questions and don't spit on shit code.
I will try to describe my problem in as much detail as possible.
The site has a list of people. The full name of each person goes as a link from a new line, wrapped in an li tag.
The list is divided into groups according to the letters of the alphabet (so far I have only written 2 letters). An example in the photo below (full name in the photo is filled in via html):
5b0d4d3939eda737158940.png
It is planned to do a search by full name, so I entered the full name in the MySQL database. Connected the database to the site, everything works.
The list structure is:

<div id="1">
  <h2>
    А
  </h2>
  <div id="2">
    <li>
      <a href="#">
        ФИО человека 1
      </a>
    </li>
    <li>
      <a href="#">
        ФИО человека 2
      </a>
    </li>
  </div>

  <h2>
    Б
  </h2>
  <div id="2">
    <li>
      <a href="#">
        ФИО человека 1
      </a>
    </li>
    <li>
      <a href="#">
        ФИО человека 2
      </a>
    </li>
  </div>
</div>

I would like the following:
1) Full name from the database to be substituted for "Name of person 1", "Name of person 2" and so on.
2) Names were substituted alphabetically. That is, in the block with the letter A, surnames were substituted for A, from B, surnames for B, and so on.
How I connected the database and the list of full names:
<?php
  
$dbhost = "localhost";
$dbname = "Drivers_list";
$username = "root";
$password = "root";

$db = new PDO("mysql:host=$dbhost; dbname=$dbname", $username, $password);

function get_drivers_names() {
  global $db;
  $driver_names = $db->query("SELECT * FROM driver_names");
  return $driver_names;
}

How I tried to bring the full name from the database to the list on the site:
5b0d548d07d8a456140318.png
Of course, this is wrong and it turns out like this:
5b0d525452747050130201.png
But how to do it right? Not to manually drive in by id)
I repeat,
I would like that:
1) the full name from the database was substituted for "name of person 1", "name of person 2" and so on.
2) Names were substituted alphabetically. That is, in the block with the letter A, surnames were substituted for A, from B, surnames for B, and so on.
Here is the example of the database itself:
5b0d52d3136c4762647088.png
It seems that I described it clearly. I hope at least someone will read my stupid question to the end and, perhaps, help solve this problem. Thanks to all

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2018-05-29
@Morozilnikkk

The request should be something like this:
As a result, you will get an array like this:

$data = [
  ['abc' => 'а', 'id' => 1, 'name' => 'Абрамов Михаил Фёдорович'],
  ['abc' => 'а', 'id' => 2, 'name' => 'Агутин Юрий Николаевич'],
  ['abc' => 'б', 'id' => 4, 'name' => 'Баринов Олег Юрьевич'],
];

Next, we go through the entire array and add by initial letters:
$alphabet = [];
foreach ($data as $row){
  $alphabet[$row['abc']][$row['id']] = $row['name'];
}

It turns out something like this:
$alphabet = [
  'а' => [
    1 => 'Абрамов Михаил Фёдорович',
    2 => 'Агутин Юрий Николаевич',
  ],
  'б' => [
    4 => 'Баринов Олег Юрьевич',
  ],
];

Further, I think you have already guessed:
foreach($alphabet as $alpha => $rows){
  echo '<h2>'.$alpha.'</h2>';
  echo '<ul>';
  foreach ($rows as $id => $name){
    echo '<li><a href="/person?id='.$id.'">'.$name.'</a></li>';
  }
  echo '</ul>';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question