从2个表循环内的Php mySQL循环

时间:2017-04-27 16:47:08

标签: php mysql loops while-loop

所以,我有2个表,我想尝试在循环中进行迭代循环...

表一是足球比赛列表 表2列出了这些频道。

表1中有每个游戏的自动增量ID&然后表2有一个与此相关的列......

见表1的例子

	
id | title                 | highlight |  date       |  time	 	|	logo	|	background
1  | Sevilla vs Celta Vigo | No        |  2017-04-28 |  18:00:00	|	La Liga	|	"background": "url"

和表2中的

	
id | name
1  | Sports Pick 2
1  | Sports Pick 2 HD
1  | Sports Pick UHD

现在我想要做的是显示表1的循环,但是对于每个值,还要循环表2中ID匹配的通道

这是我到目前为止编写的代码

<?php

	echo "<pre>{
	\"fixtures\": [<pre>";


    //connect to mysql db
    $con = mysql_connect("localhost","DATABASEUSERHERE","DATABASEPASSWORDHERE") or die('Could not connect: ' . mysql_error());
    //connect to the employee database
    mysql_select_db("DATABASENAMEHERE", $con);

	$sql = "SELECT * FROM fixture_list";
	$result = mysql_query($sql);

	while ($row = mysql_fetch_array($result)) {
	    echo '{
			"title": "'.$row[title].'",
			"highlight": "'.$row[highlight].'",
			"date": "'.$row[date].'",
			"time": "'.$row[time].'",
			"logo": "'.$row[logo].'",'.$row[background].'
			';
		echo '"channels": [
				{ "name": "Sports Pick 2" },
				{ "name": "Sky Sports 2" }
			]
		},';
	}
	
	echo "
	]
}";


?>

所以这很好用,但我只有第一个循环在这里运行,我正在努力看看我是如何或在哪里弹出第二个循环以及我如何做一个foreach或类似的东西来获得相关的第二个循环中的值......

以下是我要查找的所需输出的副本

{
	"fixtures": [
{
			"title": "Sevilla vs Celta Vigo",
			"highlight": "No",
			"date": "2017-04-28",
			"time": "18:00:00",
			"logo": "La Liga",
			"background": "URL",
			"channels": [
				{ "name": "Sports Pick 2" },
				{ "name": "Sports Pick 2 HD" },
				{ "name": "Sports Pick UHD" }
			]
		},
	]
}

我期待着对此有一些指导,看看这是如何解决的......感谢期待!

2 个答案:

答案 0 :(得分:1)

我会在两个查询中选择两个表并在PHP中进行分配。

因为我不知道mysql_*函数 - 这是一个mysqli解决方案:

$con = new mysqli('localhost', 'DATABASEUSERHERE', 'DATABASEPASSWORDHERE', 'DATABASENAMEHERE');

$data = [];
$result = $con->query("SELECT * FROM fixture_list");
while($row = $result->fetch_assoc()) {
    $id = $row['id'];
    unset($row['id']);
    $row['channels'] = [];
    $data[$id] = $row;
}

$result = $con->query("SELECT * FROM channels");
while($row = $result->fetch_assoc()) {
    $data[$row['id']]['channels'][] = ['name' => $row['name']];
}

您现在可以循环访问$data数组并打印值。或者只使用json_encode

echo json_encode($data);

echo json_encode($data, JSON_PRETTY_PRINT);

答案 1 :(得分:1)

使用mysqli,因为msql已被弃用

<?php

echo "<pre>{
    \"fixtures\": [<pre>";


//connect to mysql db
$con = mysqli_connect("localhost","root","000000");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    die();
}
//connect to the employee database
mysqli_select_db($con, 'ng_app');

$sql = "SELECT * FROM fixture_list";
$result = mysqli_query($con, $sql);
$json = '';
while ($row = mysqli_fetch_array($result)) {
    $json .= '{
            "title": "'.$row['title'].'",
            "highlight": "'.$row['highlight'].'",
            "date": "'.$row['date'].'",
            "time": "'.$row['time'].'",
            "logo": "'.$row['logo'].'",
            "background" : "' . $row['background'] . '",
            ';
    $json .= '"channels": [
                ' . getChannels($row['id'], $con) . '
            ]
        },';
}
echo rtrim($json, ',');
echo "
    ]
}";


function getChannels($fixture_id, $con) {
    $sql = "SELECT * FROM `channels` WHERE `id` = $fixture_id";
    $result = mysqli_query($con, $sql);
    $data = '';
    while ($row = mysqli_fetch_array($result)) {
        $data .= '{"name": "' . $row['name'] .'"},';
    }
    return rtrim($data, ',');
}

?>

<强>输出

{
"fixtures": [{
  "title": "Sevilla vs Celta Vigo",
  "highlight": "No",
  "date": "2017-04-28 00:00:00",
  "time": "18:00:00",
  "logo": "La Liga",
  "background": "URL",
  "channels": [{
    "name": "Sports Pick 2"
  }, {
    "name": "Sports Pick 2 HD"
  }, {
    "name": "Sports Pick UHD"
   }]
 }]
}