C
C
Chesterfield252021-06-07 23:18:38
PHP
Chesterfield25, 2021-06-07 23:18:38

What am I doing wrong?

I create a crud application, it should read all the data from three tables on the main page.
I made an array in index.php

<?php foreach ($result as $value) { ?>
<tr>
<td><?=$value->id ?></td>
<td><a href="<?=$value->url ?>" target="_blank"><?=$value->title ?></a></td>
<td><a href="<?=$value->url_type ?>" target="_blank"><?=$value->title_type ?></a></td>

<?php } ?>


In function.php I made a selection, or as it is correctly called, from three tables.

$get_id = $_GET['id'];
$get_type_id = $_GET['type_id'];
$get_category_id = $_GET['category_id'];
$get_title = $_GET['title'];
$get_url = $_GET['url'];
$get_time = $_GET['time'];

//Read xf_list
$sql = $pdo->prepare("SELECT * FROM xf_list");
$sql->execute();
$result = $sql->fetchAll(PDO::FETCH_OBJ);

$get_type_id = $_GET['id'];
$get_type_title = $_GET['title_type'];
$get_type_url = $_GET['url_type'];

//Read xf_type
$sql = $pdo->prepare("SELECT * FROM xf_type");
$sql->execute();
$result = $sql->fetchAll(PDO::FETCH_OBJ);

$get_category_id = $_GET['id'];
$get_category_title = $_GET['title'];

//Read xf_category
$sql = $pdo->prepare("SELECT * FROM xf_category");
$sql->execute();
$result = $sql->fetchAll(PDO::FETCH_OBJ);


But it turns out that on the data output page, I get data from the third table //Read xf_category

60be7f3825475495724233.png

I need to display all the data from the first table

$get_id = $_GET['id'];
$get_type_id = $_GET['type_id'];
$get_category_id = $_GET['category_id'];
$get_title = $_GET['title'];
$get_url = $_GET['url'];
$get_time = $_GET['time'];

//Read xf_list
$sql = $pdo->prepare("SELECT * FROM xf_list");
$sql->execute();
$result1 = $sql->fetchAll(PDO::FETCH_OBJ);


<?php foreach ($result as $value) { ?>
<tr>
<td><?=$value->id ?></td>
<td><a href="<?=$value->url ?>" target="_blank"><?=$value->title ?></a></td>
<?php } ?>
</tr>


And then look at what is equal
type_id
and output next
<td><a href="<?=$value->url_type ?>" target="_blank"><?=$value->title_type ?></a></td>


Then look at what is equal
category_id
and output next
<td><?=$value->title_category?></td>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Oleg, 2021-06-08
@402d

Forgot to learn databases.
read: third normal form.
you have three tables.
in one data. and two guides.
it is necessary to write request to three tables.
Select .... from _data_ join _sprav_ on field from the data table = reference.id
and in the reference, as I think, there is also a field with a category code, which also needs to be expanded to the name

O
Oleg Frolov, 2021-06-07
@Digiport

And this is natural, since with each subsequent request you overwrite the $result variable.
You need to get each request into your variable $result1, $result2, $result3, then combine them into one and pass it to the renderer.

L
Lev Zabudkin, 2021-06-08
@zabudkin

Just thinking out loud:
this is
$get_type_id = $_GET['id'];
should not be:
$get_type_id = $_GET['type_id'];
question?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question