php - two while loop fetching data from two queries only show one result -
i have code use fetch data database:
function show_volanti($data){ $con = $data; // passo connessione $id = 1; // 1 volante $visibile = 1; // variabile di visibilita' $rows1 = array(); // preparo array 1 per id articoli volante $rows2 = array(); // preparo array 2 per le foto volanti $id_articoli = ''; // riferimento articoli per seconda query galleria $g = ''; // riferimento associazione gallery viewer //$rif_id = ''; // rif_id se ok da cancellare $query1 = "select articoli.id id_articoli, articoli.titolo, articoli.descrizione articoli articoli.genere1 = ? , articoli.visibile = ?"; $query2 = "select galleria.id id_galleria, galleria.foto, galleria.rif_id galleria galleria.rif_id = ?"; $stmt = mysqli_stmt_init($con); // inizializzo la connessione mysqli_stmt_prepare($stmt,$query1); mysqli_stmt_bind_param($stmt,'ii',$id,$visibile); // lego parametri mysqli_stmt_execute($stmt); // eseguo la query mysqli_stmt_bind_result($stmt, $rows1['id_articoli'], $rows1['titolo'], $rows1['descrizione']); // creo riferimento per galleria nel viewer $html = ""; $html .= "<div class='container'>"; while (mysqli_stmt_fetch($stmt)){ $id_articoli = $rows1['id_articoli']; $html .= " <div class='row'>"; $html .= " <div class='col-sm-12'>"; $html .= " <div class='panel panel-default'>"; $html .= " <div class='panel-body'>"; $html .= " <div class='col'>"; $html .= " <div class='panel panel-default'>"; $html .= " <div class='panel-heading'><b>$rows1[titolo]</b></div>"; $html .= " <div class='panel-body'>"; $html .= " <div class='row'>"; $html .= " <div class='class_p'>$rows1[descrizione]</div>"; $html .= " <div> <!-- end first row -->"; $html .= " <div class='class_container clearfix'>"; mysqli_stmt_prepare($stmt,$query2); mysqli_stmt_bind_param($stmt,'i',$id_articoli); // lego parametri mysqli_stmt_execute($stmt); // eseguo la query mysqli_stmt_bind_result($stmt, $rows2['id_galleria'], $rows2['foto'], $rows2['rif_id']); while(mysqli_stmt_fetch($stmt)){ $g = ''; $g .= "g"; $g .= $rows2['rif_id']; $html .= "<div class='thumbnail col-sm-2'>"; $html .= "<div class='class_img'>"; $html .= "<a href='$rows2[foto]' data-toggle='lightbox' data-gallery='$g' >"; $html .= "<img src='$rows2[foto]' class='img-fluid'>"; $html .= "</a>"; $html .= "</div> <!-- end class_img -->"; $html .= "</div> <!-- end thumbnail col-sm-2- -->"; } $html .= "</div> <!-- end class_container -->"; $html .= "</div> <!-- end panel body -->"; $html .= "</div> <!-- end panel panel-default -->"; $html .= "</div> <!-- end col -->"; $html .= "</div> <!-- end panel-body -->"; $html .= "</div> <!-- end panel panel-default -->"; $html .= "</div> <!-- end col-sm-12 -->"; $html .= "</div> <!-- end row -->"; } mysqli_stmt_close($stmt); // chiudo lo statement mysqli_close($con); // chiudo connessione return $html; }
the code partially works because show result show 1 result , inside database have more results show doesn't work should.. may me find error?
it seems you're reusing same variables both external , internal loop. example, you're using $stmt both of them. i'm not sure if that's why you're getting 1 result (as may depend on amount of results received each of them), that.
as debug tip, remove code relevant external loop , first print out results of external ones properly. once have working, start adding internal loop code , make sure works. not reuse same variables though.
Comments
Post a Comment