Answer the question
In order to leave comments, you need to log in
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):
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>
<?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;
}
Answer the question
In order to leave comments, you need to log in
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' => 'Баринов Олег Юрьевич'],
];
$alphabet = [];
foreach ($data as $row){
$alphabet[$row['abc']][$row['id']] = $row['name'];
}
$alphabet = [
'а' => [
1 => 'Абрамов Михаил Фёдорович',
2 => 'Агутин Юрий Николаевич',
],
'б' => [
4 => 'Баринов Олег Юрьевич',
],
];
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 questionAsk a Question
731 491 924 answers to any question