Matías Matías - 4 months ago 7
SQL Question

Multiple tablets to show in a foreach

I got three tables in the SQL, showing up as an array,
ebooks | actividad | usuarios

Sometimes in 'ebooks', shows always the same result (the array is fine, I think is a problem in the foreach $key). Well, the code is below.

// 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"
}


The PHP Code:

<?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
}
?>


Show arrays (is all in the same script)

<?php
$i = 0;

foreach($timeline as $t){

print ''. $u['nombre'] .' ('. $u['usuario'] .')</strong> estuvo leyendo '. $e['titulo'] .' ('. $e['autor'] .')<br>';
print $t['tipo'];
}


$i++; ?>


And, it returns this:

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

Answer

You need to use the $key in the foreach() so that you can reference $usuarios/$epubs in the same position

<?php
    foreach($timeline as $key => $t){ 
        print ''. $usuarios[$key]['nombre'] .' ('. $usuarios[$key]['usuario'] .')</strong> estuvo leyendo '. $epubs[$key]['titulo'] .' ('. $epubs[$key]['autor'] .')<br>';
        print $t['tipo'];  
    } 
?>
Comments