gpt4 book ai didi

javascript - 在 php 中使用不同的 mysql 在循环中循环

转载 作者:行者123 更新时间:2023-11-30 22:17:09 24 4
gpt4 key购买 nike

目标:我们需要它找到注册人员并显示访问过哪些国家/地区,例如 In this image

我们需要创建一个循环来创建具有自己类的 php 对象(艺术家)。此对象将来自数据库 (mysql)。对于每个对象,我们需要它在数据库中查找与它相关的行(国家/地区),并使它也成为一个 php objetc,具有自己的类(它自己的 CSS、HTML 设计)

我们已经用 ng-bind-html 尝试过 Angular,因此它可以读取 HTML 标签及其 CSS,但它只进行了第一个循环,而第二个循环没有出现。我们在另一个 while 中尝试了 while,但我不知道会发生什么,但它只需要第一个。

以这种方式循环的想法是在用户不想看到它们时隐藏国家/地区。如果他们点击艺术家的名字,它会显示国家。但这是另一回事了,我们认为我们将为此使用 CSS。

我将把我尝试用于 Angular 的代码放在一起,以防万一有人对 Angular.js 有想法。

非常感谢您阅读本文。最良好的祝愿和最良好的问候!

    $data;
$data['info'] = "";
$id = $_POST['id'];

if (isset($_POST['id'])){
$dsn = 'mysql:dbname=ec;host=localhost;port=3306';
$user = 'root';
$password = '';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);

try {
$connection = new PDO($dsn, $user, $password, $options); } catch (PDOException $e) {
echo 'Fallo conexión: '. $e->getMessage();
}

$sql = "SELECT ....WHERE artist.artist_id = $i"; //This is to call only artist that the user actually have access to see.

$query = $connection->prepare($sql);
$query->execute();
while($row = $query->fetch()){
$artists[] = $row;
};
if (isset($artists)){

// 1- This is the First loop, it looks for the artist. This is working, barely.

foreach($artists as $art) {
$a = $art['artist_id']
$data['info'] .= "<div class='Main'>
<div class='MainResult'>
{$art['artist_name']}
</div>
<div>

<div class='VisitedPlaces'>
<?php
$sql='SELECT country_name, country_city, country_time FROM country JOIN ...... WHERE... = $a';
$query = $connection->prepare($sql);
$query->execute();

// 2- Here's the second loop, that look for the countries related to the artist. The idea is that once it finish, look for the next artist in the first loop. This one don't works.

while($row = $query->fetch()){
$country = $row['country_name'];
$city = $row['country_city'];
$time = $row['country_time'];
echo '<div class="rslt">
<h2>'.$country.'</h2>
<span>'.$city.'</span>
<span>'.$time.'</span>
</div>';
};
?>
</div>
</div>";
}
echo JSON_encode($data);
}

最佳答案

这实际上不是答案,而是我的一些建议/忠告。

首先代码逻辑部分看不懂,有些变量不知从何而来。例如:$i$artists。下一个 - 为什么需要返回 html 代码而不是纯数据?第三,我猜对我来说是主观的,每次访问 mysql 都是一个坏主意。我的愿景是这样的:

notice: I haven't test this

<?php

$data = array();
$data['info'] = array(); // changes
$rol = $_POST['id'];

if (isset($_POST['id'])){
$dsn = 'mysql:dbname=ec;host=localhost;port=3306';
$user = 'root';
$password = '';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);

try {
$connection = new PDO($dsn, $user, $password, $options); } catch (PDOException $e) {
echo 'Fallo conexión: '. $e->getMessage();
die(); // added
}

// what data gets from database?.. idk
// it seems like companies .. what / why / where it used in this code ? unknown
// what is $i ? I guess - artist_id
$sql = "SELECT ....WHERE artist.artist_id = :artist_id";
$query = $connection->prepare($sql);
$query->bindParam(':artist_id', $i, \PDO::PARAM_INT); // suggestion
$query->execute();
$companies = $query->fetchAll(\PDO::FETCH_ASSOC);

// from where we have got this ?
if (isset($countries)){

// 1- This is the First loop, it looks for the artist. This is working, barely.

$artist_ids = array();
// from where this too ? okay, just use it
foreach($artists as $art) {
$artist = array();

// it's just a suggestion about your table scheme
$artist_id = $art['artist_id'];
$artist_ids[] = $artist_id;

$artist['name'] = $art['artist_name'];
$artist['visited_places'] = array();
$data['info'][$artist_id] = $artist;
}

// here is not an actual sql-query, I suggest that country data and artists data are separated
// and sql must be modified to get data from both table at once
$sql = 'select country_name, country_city, country_time, artist_id from country join ... where ... and artist_id in ('.implode(',',$artist_ids).')';
$query = $connection->prepare($sql);
$query->execute();
// I don't know how many rows it will be
// make this safer, get row by row
while ($row = $query->fetch(\PDO::FETCH_ASSOC)) {
$artist_id = $row['artist_id']; // according the new query
$place = array();
$place['country'] = $row['country_name'];
$place['city'] = $row['country_city'];
$place['time'] = $row['country_time'];
$data['info'][$artist_id]['visited_places'][] = $place;
}
} // if $countries
// send back pure-data, your javascript can do anything
echo JSON_encode($data);
} // if $_POST['id']

在您的前端 javascript 应用程序将收到一个 JSON 字符串之后,如下所示:{"info":{"110":{"name":"Michael Jackson","visited_places":[ {"country_name":"JP","country_city":"东京","country_time":"12-sep-1987"},{"country_name":"US","country_city":"纽约","country_time":"03-mar-1988"},{"country_name":"IT","country_city":"Rome","country_time":"23-may-1988"}]}}}

PHP 源代码:

[info] => Array
(
[110] => Array
(
[name] => Michael Jackson
[visited_places] => Array
(
[0] => Array
(
[country_name] => JP
[country_city] => Tokyo
[country_time] => 12-sep-1987
)

[1] => Array
(
[country_name] => US
[country_city] => New-York
[country_time] => 03-mar-1988
)

[2] => Array
(
[country_name] => IT
[country_city] => Rome
[country_time] => 23-may-1988
)

)

)

)

)

关于javascript - 在 php 中使用不同的 mysql 在循环中循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37901937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com