two while loop fetching data from two queries only show one result - php

I have this code that I use to fetch data from a 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 AS id_articoli,
articoli.titolo,
articoli.descrizione
FROM articoli
WHERE articoli.genere1 = ?
AND articoli.visibile = ?";
$query2 = "SELECT galleria.id AS id_galleria,
galleria.foto,
galleria.rif_id
FROM galleria
WHERE 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 I 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 I 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 it show the result but it only show one result and inside the database i have more results to show so it doesn't work as it should.. May you help me to find the error?

It seems you're reusing the same variables for both external and internal loop. For example, you're using $stmt for both of them. I'm not sure if that's why you're getting only one result (as it may depend on the amount of results you received from each of them), but I would look into that.
As a debug tip, I would remove all code relevant to the external loop and first print out the results of the external ones properly. Once you have that working, start adding the internal loop code and make sure it works. Do not reuse the same variables though.

Related

why php loop doesn't appear with all values & concatenated html?

Please check out php code given below.
<?php
$con = mysqli_connect('localhost', 'root', '', 'unityaccess');
if (mysqli_connect_errno()) {
echo "connection failed";
exit();
}
$query = "SELECT * FROM players ORDER BY id DESC";
$row_result = mysqli_query($con, $query);
while($record = mysqli_fetch_assoc($row_result)){
$card = '<div class="card">';
$card .= '<div class="card-body">';
$card .= '<div class="comment-header">';
$card .= '<h6 class="card-subtitle mb-2 comment-name">'.$record['name'].' </h6>';
$card .= '</div>';
$card .= '</div>';
$card .= '</div>';
}
?>
I want to loop all name values inside html cards. according to loop there have 7 all name values. when I echo them it will loop correctly. but when I concatenate & echo $card like follows.
<div class="row">
<div class="col-md-12 comment-section">
<h4>Comments</h4>
<?php
echo $card;
?>
</div>
</div>
Then I can only show first value of name attribute. Why I can't retrieve all the name values with card. I think code looks not bad anyway.
change
$card = '<div class="card">';
with
$card .= '<div class="card">';
*It is a good practice to initialize your variables, if you don't the PHP engine will do a type cast depending on variable usage.
You are overwriting the $card variable each time round the loop, so all you are left with after the loop is the last card.
$row_result = mysqli_query($con, $query);
$card = ''; // init the card variable, so you can use `.=` from here on
while($record = mysqli_fetch_assoc($row_result)){
//$card = '<div class="card">'; this was the offending line
$card .= '<div class="card">';
$card .= ' <div class="card-body">';
$card .= ' <div class="comment-header">';
$card .= ' <h6 class="card-subtitle mb-2 comment-name">'.$record['name'].' </h6>';
$card .= ' </div>';
$card .= ' </div>';
$card .= '</div>';
}

div is not on the code

I'm generating html code with a string this way
foreach ($busquedas as $busqueda) {
$checked = $busqueda->porDefecto ? "checked" : ' ';
$radio_html = "<input type='radio' radio-id='".$busqueda->id."' name='default' class='radio-default' value='Por defecto' checked =".$checked." > Por defecto";
$html .= "<div class='col-md-12 search-div'>";
$html .= "<div class='col-md-12'>";
$html .= "<div class='col-md-6'>".$busqueda->nom."</div>";
$html .= "<div class='col-md-6'>".$_SESSION['user_rol'] == 0?$radio_html:''."</div>";//if $radio_html is shwon the paren div col-md-6 is not shown
$html .="</div>";
$html .= "<div class='col-md-12'>";
$html .= "<div class='col-md-6'><button class='btn btn-default load_search_btn' search_id='".$busqueda->id."'>Cargar</button></div>";
$html .= "<div class='col-md-6'><button class='btn btn-default delete_search_btn' search_id='".$busqueda->id."'>Eliminar</button></div>";
$html .="</div>";
$html .="</div>";
}
When $radio_tml is shown the parent div with class col-md-6 is not on the code but if $radio_html is shown the div is shown too,I thought some tag is not closed but I can't see it
For me your code implies that when you have
$_SESSION['user_rol'] == 0
the div section after that is not closed
You should add brackets as IncredibleHat was saying or add the </div> in the if statement (and not only in the else)

foreach loop inside $output

I'm trying to run a foreach loop inside $output = ''; and later echo $output;.
I can print any other variable like this '.$row["name"].' inside $output = ''; but can do a foreach loop.
if(isset($_POST["id"]))
{
$output = '';
$query = mysqli_query($databaseLink, "SELECT * FROM test WHERE test_id = '".$_POST["id"]."'");
while($row = mysqli_fetch_array($query))
{
$output .= '
<div class="row">
<div class="input-field custom-check col s4">
<h4 class="project-label-display project-label-display-center" > Practice Vertical </h4>
'$list_id= explode(",", $row["pet_id"]);
foreach($list_id as $value) {
<br>
print $value;
<br> }'
';
}
echo $output;
}
The thing is I cant forech loop inside $output .= ' '; , it does not work.
Okay here is the thing, this code here
$list_id= explode(",", $row["pet_id"]);
foreach($list_id as $value) {
<br>
print $value;
<br>
}
runs just fine if I run it outside $output .= ' ';.
Any php code that goes inside $output .= ' ' should be wrapped inside another '. .' or ' ' else it becomes a simple text. so if i want to print a variable i have to do it like this '.$count.'. But I cant use a loop.
You didn't close your <div>s properly, nor did you assign the output to $output properly. I fixed the positions of the single apostrophes ' so they make sense.
Here is the corrected code:
if(isset($_POST["test_id"]))
{
$output = '';
$query = mysqli_query($databaseLink, "SELECT * FROM test WHERE test_id = '".$_POST["test_id"]."'");
while($row = mysqli_fetch_array($query))
{
$output .= '
<div class="row">
<div class="input-field custom-check col s4">
<h4 class="project-label-display project-label-display-center"> Practice Vertical </h4>';
$list_id= explode(",", $row["pet_id"]);
foreach($list_id as $value)
{
$output .= '<br>'.$value.'<br>';
}
$output .= '</div>
</div>';
}
echo $output;
}
A simple approach:
while($row = mysqli_fetch_array($query))
{
$output .= '
<div class="row">
<div class="input-field custom-check col s4">
<h4 class="project-label-display project-label-display-center" > Practice Vertical </h4>';
// Add every id to your output explicictly
$list_id = explode(",", $row["pet_id"]);
foreach($list_id as $value) {
$output .= '<br>' . $value . '<br>';
}
$output .= '</div>';
}
Try the following code:
<?php
if(isset($_POST["id"]))
{
$output = '';
$query = mysqli_query($databaseLink, "SELECT * FROM test WHERE test_id = '".$_POST["id"]."'");
while($row = mysqli_fetch_array($query))
{
$output .= '
<div class="row">
<div class="input-field custom-check col s4">
<h4 class="project-label-display project-label-display-center" > Practice Vertical </h4>';
$list_id= explode(",", $row["pet_id"]);
foreach($list_id as $value) {
$output = $output."<br>".$value."<br>";
}
$output .= '</div>';
}
echo $output;
}
?>

prepare different mysqli statement [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 2 years ago.
I get lost when I need to prepare multiple statements. I have problems with prepared statement and connection..
I have a function and I pass to it the connection to the database ($con variable). Inside this function i prepare more statement but i have an error, the server replied with:
mysql_error() expects parameter 1 to be resource, object given in select.php on line 1086
This is the page where i call the function and pass the connection variable:
include('include/connect.php');
echo show_volanti($con);
This is the connection file that I included above:
$con = mysqli_connect($host,$user,$password,$db);
if (!$con) {
die('Connection Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error()); }
if(!mysqli_set_charset($con, "utf8mb4")) {
printf("Error loading character set utf8: %s\n", mysqli_error($con));
exit(); }
and this is the function:
function show_volanti($data){
$con = $data; // PASSO CONNESSIONE
$con2 = ''; // SECONDA 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 ARTICOLI VOLANTE
$rows3 = array(); // PREPARO ARRAY PER GALLERIA IMMAGINI ARTICOLI
$stmt1 = ''; // PREPARO GLI STATEMENT
$stmt2 = ''; // PREPARO GLI STATEMENT
$stmt3 = ''; // PREPARO GLI STATEMENT
$g = ''; // RIFERIMENTO ASSOCIAZIONE GALLERY VIEWER
$id_articoli = array(); // IMMAGAZZINO ID ARTICOLI DA RECUPERARE NELLA GALLERIA
$con2 = mysqli_stmt_init($con); // INIZIALIZZO LA CONNESSIONE
$stmt1 = mysqli_stmt_prepare($con2,'SELECT
articoli.id
FROM articoli
WHERE articoli.genere1 = ?
AND articoli.visibile = ?')
or die(mysql_error($con2));// QUERY INSERIMENTO DATI
mysqli_stmt_bind_param($stmt1,'ii',$id,$visibile); // LEGO I PARAMETRI
mysqli_stmt_execute($stmt1); // ESEGUO LA QUERY
mysqli_stmt_bind_result($stmt1,$rows1['id']);
while(mysqli_stmt_fetch($stmt1)){
$id_articoli = $rows1['id'];
}
// CREO RIFERIMENTO PER GALLERIA NEL VIEWER
$g .= "g";
$g .= $id_articoli;
$stmt2 = mysqli_stmt_prepare($con2,'SELECT
articoli.id,
articoli.titolo,
articoli.descrizione
FROM articoli
WHERE articoli.genere1 = ?
AND articoli.visibile = ? ')
or die(mysqli_error($con2)); // QUERY INSERIMENTO DATI
mysqli_stmt_bind_param($stmt2,'ii',$id,$visibile); // LEGO I PARAMETRI
mysqli_stmt_execute($stmt2); // ESEGUO LA QUERY
mysqli_stmt_bind_result($stmt2,$rows2['id'],$rows2['titolo'],$rows2['descrizione']);
// PREPARO QUERY PER LA GALLERIA
$stmt3 = mysqli_stmt_prepare($con2,'SELECT
galleria.id,
galleria.foto
FROM galleria
WHERE galleria.rif_id = ?
AND articoli.visibile = ? ')
or die(mysqli_error($con2)); // QUERY INSERIMENTO DATI
mysqli_stmt_bind_param($stmt3,'ii',$id_articoli,$visibile); // LEGO I PARAMETRI
mysqli_stmt_execute($stmt3); // ESEGUO LA QUERY
mysqli_stmt_bind_result($stmt3,$rows3['id'],$rows3['foto']);
$html = "";
$html .= "<div class='container'>";
$html .= " <div class='row'>";
$html .= " <div class='col-sm-12'>";
$html .= " <div class='panel panel-default'>";
$html .= " <div class='panel-body'>";
while (mysqli_stmt_fetch($stmt2)){
$html .= " <div class='col'>";
$html .= " <div class='panel panel-default'>";
$html .= " <div class='panel-heading'>$rows2[titolo]</div>";
$html .= " <div class='panel-body'>";
$html .= " <div class='row'>";
$html .= " <div class='class_p'>$rows2[descrizione]</div>";
$html .= " <div> <!-- end first row -->";
$html .= " <div class='class_container clearfix'>";
while(mysqli_stmt_fetch($stmt3)){
$html .= " <div class='thumbnail col-sm-2'>";
$html .= " <div class='class_img'>";
$html .= " <a href='$rows3[foto]' data-toggle='lightbox' data-gallery='$g' >";
$html .= " <img src='$rows3[foto]' class='img-responsive' 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_close($con2); // CHIUDO CONNESSIONE
return $html;
}
This results in the error:
$stmt1 = mysqli_stmt_prepare($con2,'SELECT
articoli.id,
FROM articoli
WHERE articoli.genere1 = ?
AND articoli.visibile = ?')
or die(mysql_error($con2));// QUERY INSERIMENTO DATI
Change mysql_error to mysqli_error and your problem should be fixed.
Note: Using or die() is a bad way to handle an error.

Issue on output's return value

Currently I am using a file with css and all.I have a problem as i am using php output between many html tags. The menu consist of the hard coded pages which is included with the php require_once. However, i also want to add in some pages from my database.
The Problem: The menu only shows 1 page name from my database when i have 2. It is displaying the 2nd page's name i have made. However clicking on it, it calls up the content from the 1st page.
The page name from my database is added using php output after the <ul id="menu-toc">
The page's content from my database is added at the bottom of the "The Page" section.
update: I've changed the placing of the while loop before the to after the
Pattern of css
<div class="bb-item" id="abscess">
<div class="content">
<div class="scroller">
Content
</div>
</div>
</div>
The Page
function find_condition() {
global $connection;
$query = "SELECT * ";
$query .= "FROM pages ";
$query .= "WHERE visible = 1 ";
$query .= "AND subject_id = 2 ";
$query .= "ORDER BY position ASC";
$page_set = mysqli_query($connection, $query);
confirm_query($page_set);
return $page_set;
}
$two = find_condition();
while($page = mysqli_fetch_assoc($two)) {
$pagearray = $page['menu_name'];
?>
<ul id="menu-toc" class="menu-toc">
<li class="menu-toc-current">
ABSCESS</li>
<li>APTHOUS ULCER</li>
<li>BAD BREATH</li>
<?php
$output = "<li>";
$output .= "<a href=\"";
$output .= $pagearray;
$output .= "\">";
$output .= $pagearray;
$output .= "</a></li>";
}
echo $output;
?>
</ul>
</div>
<div class="bb-custom-wrapper">
<div id="bb-bookblock" class="bb-bookblock">
<?php require_once("abscess.php"); ?>
<?php require_once("apthousulcer.php"); ?>
<?php require_once("bad breath.php"); ?>
<?php
$two= find_condition();
while($page = mysqli_fetch_assoc($two)) {
$pagearray = $page['menu_name'];
$content = $page['content'];
$output .= "<div class=\"bb-item\" id=\"";
$output .= $pagearray;
$output .= "\">";
$output .= "<div class=\"content\">";
$output .= "<div class=\"scroller\">";
$output .= "<h2>";
$output .= $pagearray;
$output .= "</h2>";
$output .= $content;
$output .= "</div>";
$output .= "</div>";
$output .= "</div>";
}
echo $output;
?>
You're starting a new <ul> each time through the while loop. You should just do that once, before the loop. You're also reinitializing $output each time through the loop, but not printing it in the loop. So you're just printing the last assignment of output after the loop is done.
$two = find_condition();
?>
<ul id="menu-toc" class="menu-toc">
<li class="menu-toc-current">
ABSCESS</li>
<li>APTHOUS ULCER</li>
<li>BAD BREATH</li>
<?php
while($page = mysqli_fetch_assoc($two)) {
$pagearray = $page['menu_name'];
?>
<?php
$output = "<li>";
$output .= "<a href=\"";
$output .= $pagearray;
$output .= "\">";
$output .= $pagearray;
$output .= "</a></li>";
echo $output;
}
?>

Categories