0
我在SQL中獲得了三個表格,顯示爲一個數組, 電子書| actividad | usuarios在一個foreach中顯示多個平板電腦
有時在'電子書',顯示總是相同的結果(數組很好,我認爲是在foreach $鍵問題)。那麼,代碼如下。
// Table actividad
array(9) {
["id"]=>
string(2) "32"
["user_id"]=>
string(1) "1"
["dest_id"]=>
string(1) "1"
["tipo"]=>
string(8) "favorito"
["quote"]=>
string(0) ""
["ip"]=>
string(3) "::1"
["ebook"]=>
string(32) "0a3ad21cbfd4f1e8900a2f0bff14010c"
["fecha"]=>
string(19) "2015-04-22 09:42:41"
["mostrar"]=>
string(1) "1"
}
array(9) {
["id"]=>
string(2) "31"
["user_id"]=>
string(1) "1"
["dest_id"]=>
string(1) "1"
["tipo"]=>
string(8) "favorito"
["quote"]=>
string(0) ""
["ip"]=>
string(3) "::1"
["ebook"]=>
string(32) "9a142cd24ec96f76a3ae36de2c2c2607"
["fecha"]=>
string(19) "2015-04-22 09:37:44"
["mostrar"]=>
string(1) "1"
}
array(9) {
["id"]=>
string(2) "30"
["user_id"]=>
string(1) "1"
["dest_id"]=>
string(1) "1"
["tipo"]=>
string(8) "favorito"
["quote"]=>
string(0) ""
["ip"]=>
string(3) "::1"
["ebook"]=>
string(32) "b11d6e522acefa1cfb960b0e88c652a7"
["fecha"]=>
string(19) "2015-04-22 09:36:49"
["mostrar"]=>
string(1) "1"
}
array(9) {
["id"]=>
string(2) "28"
["user_id"]=>
string(1) "1"
["dest_id"]=>
string(1) "1"
["tipo"]=>
string(8) "favorito"
["quote"]=>
string(0) ""
["ip"]=>
string(3) "::1"
["ebook"]=>
string(32) "85b0497972e9ad1be800ea887a4dbd46"
["fecha"]=>
string(19) "2015-04-22 09:34:31"
["mostrar"]=>
string(1) "1"
}
array(9) {
["id"]=>
string(2) "27"
["user_id"]=>
string(1) "1"
["dest_id"]=>
string(1) "1"
["tipo"]=>
string(8) "favorito"
["quote"]=>
string(0) ""
["ip"]=>
string(3) "::1"
["ebook"]=>
string(32) "3f23bf6e089135f572925bc5f03e3c43"
["fecha"]=>
string(19) "2015-04-22 08:49:49"
["mostrar"]=>
string(1) "1"
}
array(9) {
["id"]=>
string(1) "4"
["user_id"]=>
string(1) "1"
["dest_id"]=>
string(1) "1"
["tipo"]=>
string(7) "lectura"
["quote"]=>
string(0) ""
["ip"]=>
string(3) "::1"
["ebook"]=>
string(32) "b11d6e522acefa1cfb960b0e88c652a7"
["fecha"]=>
string(19) "2015-04-22 02:27:54"
["mostrar"]=>
string(1) "1"
}
array(9) {
["id"]=>
string(1) "2"
["user_id"]=>
string(1) "1"
["dest_id"]=>
string(1) "1"
["tipo"]=>
string(7) "lectura"
["quote"]=>
string(0) ""
["ip"]=>
string(3) "::1"
["ebook"]=>
string(32) "bad8abebdf9a268f78de1684a30c6f3f"
["fecha"]=>
string(19) "2015-04-21 08:23:45"
["mostrar"]=>
string(1) "1"
}
array(9) {
["id"]=>
string(1) "1"
["user_id"]=>
string(1) "1"
["dest_id"]=>
string(1) "1"
["tipo"]=>
string(7) "lectura"
["quote"]=>
string(0) ""
["ip"]=>
string(3) "::1"
["ebook"]=>
string(32) "85b0497972e9ad1be800ea887a4dbd46"
["fecha"]=>
string(19) "2015-04-21 08:21:26"
["mostrar"]=>
string(1) "1"
}
array(9) {
["id"]=>
string(1) "3"
["user_id"]=>
string(1) "1"
["dest_id"]=>
string(1) "1"
["tipo"]=>
string(7) "lectura"
["quote"]=>
string(0) ""
["ip"]=>
string(3) "::1"
["ebook"]=>
string(32) "a2e10598569e20ebd327fda6975197fb"
["fecha"]=>
string(19) "2015-04-20 08:24:07"
["mostrar"]=>
string(1) "1"
}
// Table ebooks
array(3) {
["ebook"]=>
string(37) "0a3ad21cbfd4f1e8900a2f0bff14010c.epub"
["titulo"]=>
string(24) "Doce años de esclavitud"
["autor"]=>
string(15) "Solomon Northup"
}
array(3) {
["ebook"]=>
string(37) "9a142cd24ec96f76a3ae36de2c2c2607.epub"
["titulo"]=>
string(20) "La Fortaleza Digital"
["autor"]=>
string(9) "Dan Brown"
}
array(3) {
["ebook"]=>
string(37) "b11d6e522acefa1cfb960b0e88c652a7.epub"
["titulo"]=>
string(15) "El Gran Diseño"
["autor"]=>
string(33) "Stephen Hawking; Leonard Mlodinow"
}
array(3) {
["ebook"]=>
string(37) "85b0497972e9ad1be800ea887a4dbd46.epub"
["titulo"]=>
string(42) "Un grito de amor desde el centro del mundo"
["autor"]=>
string(16) "Kyoichi Katayama"
}
array(3) {
["ebook"]=>
string(37) "3f23bf6e089135f572925bc5f03e3c43.epub"
["titulo"]=>
string(30) "Maze Runner 3 - La cura mortal"
["autor"]=>
string(13) "James Dashner"
}
array(3) {
["ebook"]=>
string(37) "b11d6e522acefa1cfb960b0e88c652a7.epub"
["titulo"]=>
string(15) "El Gran Diseño"
["autor"]=>
string(33) "Stephen Hawking; Leonard Mlodinow"
}
array(3) {
["ebook"]=>
string(37) "bad8abebdf9a268f78de1684a30c6f3f.epub"
["titulo"]=>
string(19) "El símbolo perdido"
["autor"]=>
string(9) "Dan Brown"
}
array(3) {
["ebook"]=>
string(37) "85b0497972e9ad1be800ea887a4dbd46.epub"
["titulo"]=>
string(42) "Un grito de amor desde el centro del mundo"
["autor"]=>
string(16) "Kyoichi Katayama"
}
array(3) {
["ebook"]=>
string(37) "a2e10598569e20ebd327fda6975197fb.epub"
["titulo"]=>
string(22) "Bajo la misma estrella"
["autor"]=>
string(10) "John Green"
}
// Table usuarios
array(3) {
["id"]=>
string(1) "1"
["nombre"]=>
string(4) "Mati"
["usuario"]=>
string(3) "wtf"
}
array(3) {
["id"]=>
string(1) "1"
["nombre"]=>
string(4) "Mati"
["usuario"]=>
string(3) "wtf"
}
array(3) {
["id"]=>
string(1) "1"
["nombre"]=>
string(4) "Mati"
["usuario"]=>
string(3) "wtf"
}
array(3) {
["id"]=>
string(1) "1"
["nombre"]=>
string(4) "Mati"
["usuario"]=>
string(3) "wtf"
}
array(3) {
["id"]=>
string(1) "1"
["nombre"]=>
string(4) "Mati"
["usuario"]=>
string(3) "wtf"
}
array(3) {
["id"]=>
string(1) "1"
["nombre"]=>
string(4) "Mati"
["usuario"]=>
string(3) "wtf"
}
array(3) {
["id"]=>
string(1) "1"
["nombre"]=>
string(4) "Mati"
["usuario"]=>
string(3) "wtf"
}
array(3) {
["id"]=>
string(1) "1"
["nombre"]=>
string(4) "Mati"
["usuario"]=>
string(3) "wtf"
}
array(3) {
["id"]=>
string(1) "1"
["nombre"]=>
string(4) "Mati"
["usuario"]=>
string(3) "wtf"
}
PHP代碼:
<?php
$perfiluser = htmlspecialchars($_GET['unw']);
$sql = "SELECT * FROM actividad WHERE dest_id = ".$perfiluser." ORDER BY fecha DESC";
$result = mysql_query($sql) or die(mysql_error);
while($res = mysql_fetch_assoc($result)){
$timeline[] = $res;
}
foreach($timeline as $t)
{
$sql2 = "SELECT ebook, titulo, autor FROM ebooks WHERE ebook = '".$t['ebook'].".epub'";
$result2 = mysql_query($sql2);
while($res2 = mysql_fetch_assoc($result2)){
$epubs[] = $res2;
}
}
foreach($timeline as $t)
{
$sql3 = "SELECT id, nombre, usuario FROM usuarios WHERE id = '".$t['dest_id']."'";
$result3 = mysql_query($sql3);
while($res3 = mysql_fetch_assoc($result3)){
$usuarios[] = $res3;
}
}
foreach($timeline as $t){
var_dump($t); // Listar datos de timeline en un array
}
foreach($epubs as $e){
var_dump($e); // Listar datos de la tabla ebooks en un array
}
foreach($usuarios as $u){
var_dump($u); // Listar datos de la tabla ebooks en un array
}
?>
顯示陣列(都是在同一個腳本)
<?php
$i = 0;
foreach($timeline as $t){
print ''. $u['nombre'] .' ('. $u['usuario'] .')</strong> estuvo leyendo '. $e['titulo'] .' ('. $e['autor'] .')<br>';
print $t['tipo'];
}
$i++; ?>
而且,它返回:
Mati (wtf) estuvo leyendo Bajo la misma estrella (John Green)
favoritoMati (wtf) estuvo leyendo Bajo la misma estrella (John Green)
favoritoMati (wtf) estuvo leyendo Bajo la misma estrella (John Green)
favoritoMati (wtf) estuvo leyendo Bajo la misma estrella (John Green)
favoritoMati (wtf) estuvo leyendo Bajo la misma estrella (John Green)
favoritoMati (wtf) estuvo leyendo Bajo la misma estrella (John Green)
lecturaMati (wtf) estuvo leyendo Bajo la misma estrella (John Green)
lecturaMati (wtf) estuvo leyendo Bajo la misma estrella (John Green)
lecturaMati (wtf) estuvo leyendo Bajo la misma estrella (John Green)
lectura
參考
$usuarios
/$epubs
是有你在做3個查詢,而不是1'JOIN'查詢理由嗎? – Sean是的。一些JOIN的值有所不同 –