Issue on output's return value - php

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

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>';
}

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

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

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.

Conditional Joomla module position and span

I am having trouble setting up a joomla conditional module position and span.
Although I understand HTML, I have trouble with php. According to the tutorials, I have to add
<?php if ($this->countModules( 'user1' )) : ?>
<div class="user1">
<jdoc:include type="modules" name="user1" style="rounded" />
</div>
<?php endif; ?>
Although when I add something like it, I receive an error.
The code that I want to change is:
<?php
//no direct accees
defined ('_JEXEC') or die('resticted aceess');
class Helix3FeatureTitle {
private $helix3;
public function __construct($helix){
$this->helix3 = $helix;
$this->position = 'title';
}
public function renderFeature() {
$app = JFactory::getApplication();
$menuitem = $app->getMenu()->getActive(); // get the active item
if($menuitem) {
$params = $menuitem->params; // get the menu params
if($params->get('enable_page_title', 0)) {
$page_title = $menuitem->title;
$page_title_alt = $params->get('page_title_alt');
$page_subtitle = $params->get('page_subtitle');
$page_title_bg_color = $params->get('page_title_bg_color');
$page_title_bg_image = $params->get('page_title_bg_image');
$style = '';
if($page_title_bg_color) {
$style .= 'background-color: ' . $page_title_bg_color . ';';
}
if($page_title_bg_image) {
$style .= 'background-image: url(' . JURI::root(true) . '/' . $page_title_bg_image . ');';
}
if( $style ) {
$style = 'style="' . $style . '"';
}
if($page_title_alt) {
$page_title = $page_title_alt;
}
$output = '';
$output .= '<div class="sp-page-title"'. $style .'>';
$output .= '<div class="container" id="pagetitle">';
$output .= '<div class="row">';
$output .= '<div class="col-md-6 col-sm-8">';
$output .= '<h1>'. $page_title .'</h1>';
if($page_subtitle) {
$output .= '<p class="lead">'. $page_subtitle .'</p>';
}
$output .= '</div>';
$output .= '<div class="col-md-6 col-sm-4 hidden-xs">';
$output .= '<jdoc:include type="modules" name="datetime" style="none" />';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
return $output;
}
}
}
}
and what I want to do is tell the template that when the module position 'datetime' is empty, the "title" position should have 12span and be "text-center".
Otherwise if module position 'datetime' has a module, the 'title' position should have 6span and be "text-left".
I have tried different methods and I receive different errors each time. This is why I did not mention a spcecific error. For example, if I use this method:
if($this->countModules('datetime')) { $output .= '<div class="col-md-6 col-sm-4 hidden-xs">'; $output .= '<jdoc:include type="modules" name="datetime" style="none" />'; $output .= '</div>'; };
I receive the following error:
Fatal error: Call to undefined method Helix3FeatureTitle::countModules() in ...
I would really appreciate some assistance. Thank you.
The problem is that you are trying to us countModules() in a place where it isn't going to work because that method is from a different class.
From the looks of it what this method is doing is generating the Joomla template.
So what you can do is
$output .="<?php if ($this->countModules( 'user1' )) : ?>";
$output .= '<div class="col-md-6 col-sm-4 hidden-xs">';
$output .= '<jdoc:include type="modules" name="datetime" style="none" />';
$output .= '</div>';
$output .= '<?php endif; ?>';
So that it will generate the code for the condition. I haven't tested because I don't have the class you are using but it should work.
Updated to fix quoting.

wordpress ignores formatting when using template tags in functions.php

I am using the following code to create a shortcode for use in the WYSIWYG.
function consultants( $atts, $content = null ) {
$output = '';
$consultant_query = new WP_Query('post_type=consultants&showposts=10');
if ($consultant_query->have_posts()) :
$output .= '<div class="col-md-12">';
while ($consultant_query->have_posts()) : $consultant_query->the_post();
$output .= '<div class="col-xs-12 col-sm-5 col-md-4 kam-tile-own-bg"><h1>' .the_title(). '</h1> ';
if(has_post_thumbnail())
{
$output .= get_the_post_thumbnail($post->ID,'wpbs-featured-avatar');
} else {
$output .= '
<img class="kam-avatar" width="62" height="62" src="'.get_template_directory_uri().'/images/avatar#2x.jpg" class="attachment-featured_image wp-post-image" alt="" />'; }
$output .= '</div>';
endwhile;
$output .= '</div>';
endif;
wp_reset_postdata();
return $output;
}
The code works fine - HOWEVER it fails on the .the_title(). it throws this at the top of the page it has no respect for the tags or the in which it is contained.
Many thanks
instead of the_title(); use get_the_title();

Categories