two PHP loops in one html div - php

I have below code for help contents,
<div class="tab-content">
<?php
foreach($helpCategoryArray as $category)
{
?>
<div id="tab_<?php echo $category["helpcategory_id"]?>" class="tab-pane active">
<div id="accordion1" class="panel-group">
<?php
foreach($topicsArray as $topic)
{
?>
<div class="panel panel-default" id="topic_<?php echo $topic["topic_id"]?>">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1" href="#accordion1_1">
<?php echo $topic["topic_subject"]?>
</h4>
</div>
<div id="accordion1_1" class="panel-collapse collapse in">
<div class="panel-body" id="context" data-toggle="context" data-target="#context-menu">
<?php echo $topic["topic_content"]?>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
<?php
}
?>
</div>
What is my requirement is left side of page is tree of selection, when one item category is selected, related contents are loading on right side,
Now on above code have did that with id=tab_x, x will come from DB, so need loop for that, so that i can give reference to tab_x as href,
In tab div, i have help topics which are related to this items, so i need to loop all my topics here,
Problem is that my one help topic is also looping in first loop looping times,
So how to echo this help topic,
Thanks,

Finally it solved with adding If condition in first PHP loop,
if ($category["helpcategory_id"] == $topic["topic_category"])
{--code---}

Related

joomla 4 Blog category is not balanced correctly

I created a blog category from the menu. In Joomla 4 -> In Blog Layout -> Columns ، I can give a value to the column, but if we increase the value by one, the displayed contents are not balanced. And the "" did not close properly. After a general search, I realized that the contents are arranged as follows :
HTML :
<div class="blog-items">
<div class="blog-item">
<div class="content">1</div>
<div class="blog-item">
<div class="content">2</div>
</div>
<div class="blog-item">
<div class="content">3</div>
<div class="blog-item">
<div class="content">4</div>
<div class="blog-item">
<div class="content">5</div>
</div>
<div class="blog-item">
<div class="content">6</div>
</div>
<div class="blog-item">
<div class="content">7</div>
</div>
</div>
</div>
</div>
</div>
HTML The right way to be :
<div class="blog-items">
<div class="blog-item"><div class="content">1</div></div>
<div class="blog-item"><div class="content">2</div></div>
<div class="blog-item"><div class="content">3</div></div>
<div class="blog-item"><div class="content">4</div></div>
<div class="blog-item"><div class="content">5</div></div>
<div class="blog-item"><div class="content">6</div></div>
<div class="blog-item"><div class="content">7</div></div>
</div>
Note: When I disable "loadTemplate", the loop is displayed correctly and the divisions are closed correctly.
I also checked the information inside the "$this->loadTemplate('item')" but found no problem.
PHP :
<div class="blog-items">
<?php foreach ($this->intro_items as $key => &$item) : ?>
<div class="blog-item">
<?php
$this->item = & $item;
echo $this->loadTemplate('item'); //Included <div class="content">value</div>
?>
</div>
<?php endforeach; ?>
</div>
it's true,fixed.
All content had a tag before page break,
You cannot insert it inside a div. The closing tag will be missing. Close the tag before inserting Read More.
Little did I know that using before page break can be so destructive, and I've been looking for problems with PHP code for about a week now.

PHP loop depending on number of folders

I am working on a project which is a sort of FAQ made in HTML and PHP (based on Bootstrap).
Some people with limited knowledge in HTML/PHP will need to update it regularly so I decided to use the "include" feature in PHP to call some .txt files. Then, the people who will update it will just have to modify the simple .txt files, which doesn't require any coding knowledge.
The text files are stored in faq/main-category/sub-category/1/question.txt and the same with "answer.php" (both in the same folder).
The FAQ items are identified with numbers (which you can see on the screenshot below). These numbers refer to the directory ("1" in the .txt file path above) where the files are.
Screenshot of list of FAQ items
Now, here's the thing: I want to allow people who will modify it to add some new FAQ items easily. The idea is for them to only have to create a new folder (let's say "4") in the FTP server, and it would be automatically listed on the FAQ.
For this, I need that the plugin detects all items in the FTP server and list them all. I believe that is possible with a loop, however, I have no idea how...
Can anyone help with that? Any help will be greatly appreciated! :)
Here is the current code for the first FAQ item of the screenshot:
<div id="accordion" role="tablist">
<div class="card">
<div class="card-header d-flex w-100 justify-content-between" role="tab" id="headingOne">
<h5 class="mb-0">
<a data-toggle="collapse" href="#collapseOne" role="button" aria-expanded="true" aria-controls="collapseOne">
<?php include('../faqs/professionals/eligibility-conditions/1/question.txt'); ?>
</a>
</h5>
<small>1</small>
</div>
<div id="collapseOne" class="collapse" role="tabpanel" aria-labelledby="headingOne" data-parent="#accordion">
<div class="card-body">
<div><p><?php include('../faqs/professionals/eligibility-conditions/1/answer.txt'); ?></p></div>
</div>
</div>
</div>
You will see both "include" codes that call the .txt files for the question and for the answer.
Here is a screenshot of the file path
<?php
foreach (glob("../faqs/professionals/eligibility-conditions/*",GLOB_ONLYDIR) as $foldername) {
?>
<div id="accordion" role="tablist">
<div class="card">
<div class="card-header d-flex w-100 justify-content-between" role="tab" id="headingOne">
<h5 class="mb-0">
<a data-toggle="collapse" href="#collapseOne" role="button" aria-expanded="true" aria-controls="collapseOne">
<?php include($foldername.'/question.txt'); ?>
</a>
</h5>
<small>1</small>
</div>
<div id="collapseOne" class="collapse" role="tabpanel" aria-labelledby="headingOne" data-parent="#accordion">
<div class="card-body">
<div><p><?php include($foldername.'/answer.txt'); ?></p></div>
</div>
</div>
</div>
<?php
}
?>
I am not sure what is your application structure, maybe you are using MVC architecture but you can do it like below.
Create a PHP function which will return an array with questions and answers
<?php
function get_faqs(){
$folderpath = 'faqs/shared/'; //change as per your desired path
if(scandir($folderpath)){
$file_arr = scandir($folderpath);
foreach ($file_arr as $val){
if($val == '.' || $val == '..'){
continue;
} else if(scandir($folderpath.$val)){
$faq_arr = scandir($folderpath.$val);
foreach ($faq_arr as $file){
if($file == '.' || $file == '..'){
continue;
} else if($file == 'question.txt'){
$faq_final[$val]['question'] = file_get_contents($folderpath.$val.'/'.$file);
} else if($file == 'answer.txt'){
$faq_final[$val]['answer'] = file_get_contents($folderpath.$val.'/'.$file);
}
}
}
}
}
return $faq_final;
}
?>
Then iterate that array in your HTML as below
<?php
$faqs = get_faqs(); // you need to call this in controller if you have MVC application
?>
<div id="accordion" role="tablist">
<?php foreach ($faqs as $num => $data) { ?>
<div class="card">
<div class="card-header d-flex w-100 justify-content-between" role="tab" id="headingOne">
<h5 class="mb-0">
<a data-toggle="collapse" href="#collapseOne" role="button" aria-expanded="true" aria-controls="collapseOne">
<?php echo $data['question']; ?>
</a>
</h5>
<small><?php echo $num; ?></small>
</div>
<div id="collapseOne" class="collapse" role="tabpanel" aria-labelledby="headingOne" data-parent="#accordion">
<div class="card-body">
<div><p><?php echo $data['answer']; ?></p></div>
</div>
</div>
</div>
<?php } ?>
</div>

Not able to align horizontal slider in a row

I have a horizontal carousel that is working fine, here is the code for it
Now i wish to fetch the data from database and display it that corousel. Following is the code that i used
<div class='row'>
<div class='col-xs-12'>
<div class="carousel slide media-carousel" id="media">
<div class="carousel-inner">
<div class="item active">
<div class="row">
<?php foreach($student as $student): ?>
<div class="col-md-3">
<a class="thumbnail" href="#"><?php echo $student->fullname;?></a>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
<a data-slide="prev" href="#media" class="left carousel-control">‹</a>
<a data-slide="next" href="#media" class="right carousel-control">›</a>
</div>
</div>
</div>
but the view is distorted and instead of all the slides sliding in one row, the view is coming like this
That's because unless the row is active, the other .item row gets the property display:none; If you were to remove the display:none; it should work as you want.
Create the class="row" inside the loop. Also keep in mind that the screen consists of 12 units only, so if you create a row of more than 12 units it will go to next line automatically. As you have created the loop that creates "col-md-3" for more than 4 times which sums more than 12, so it moves to next line.
For reference view this
As you have not given the php code, i am giving u sample of it
The array that you are getting divide it into a group of 4 (or as many as you want) like given below
$query = $this->db->get('student');
$r = $query->result();
$s = (array_chunk($r, 4));
return $s;
And then on the code you have given, change into following
<div class="carousel-inner">
<?php foreach($s as $key => $per_student): ?>
<?php if($key=='0'): ?>
<div class="item active">
<?php else: ?>
<div class="item ">
<?php endif; ?>
<div class="row">
<?php foreach($per_student as $student): ?>
<div class="col-md-3">
<a class="thumbnail" href="#"><?php echo $student->fullname;?></a>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>

Creat individual .php?id= links for each of the products in products.php file

On my products page, there are several different product grids(created with bootstrap) in which, all of my products are displayed. What I am trying to do is to create detailed pages for each of the products the page has.
products.php:
<?php
session_start();
include('inc/config.php');
?>
<html>
<body>
some content
<div class="row">
<?php
$query=mysql_query("select*from cars");
while($data=mysql_fetch_assoc($query)){
?>
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="text-center">
<?php echo "<h2>".$data['make']." ".$data['model']." </h2>"; ?></h4>
</div>
<div class="panel-body text-center">
<p class="lead">
<?php echo '<img src="cartest/'.$data['image'].'" class="img-responsive" />'; ?></p>
</div>
<ul class="list-group list-group-flush text-center">
<li class="list-group-item"><i class="icon-ok text-danger"></i><?php echo "<h4>".$data['price']." </h4>"; ?></li>
<li class="list-group-item"><i class="icon-ok text-danger"></i>Unlimited projects</li>
<li class="list-group-item"><i class="icon-ok text-danger"></i>27/7 support</li>
</ul>
<div class="panel-footer">
<?php echo '<a class="btn btn-lg btn-block btn-default" a href="product-details.php?id='.$data['carId'].'">Details</a>';?>
</div>
</div>
</div>
<? } ?>
</div>
</body>
</html>
product-details.php:
<?php
session_start();
include('inc/config.php');
?>
<html>
<body>
some content
<div class="container-fluid">
<?php
$id = $_GET['carId'];
$query = mysql_query("SELECT * FROM cars WHERE carId='$id'") or die(mysql_error());
$data = mysql_fetch_array($query);
?>
<div>
<?php echo '<img alt="" src="cartest/'.$data['image'].'"></a>'; ?>
</div>
As you can see, I managed to create individual links for each of the products but is there a way of displaying the individual pages using something like a main template which its gonna change the content section according to the product that has been clicked? or do i have to create countless pages for each of the products?
Thanks in advance
If you want to change something in template, you need to prefer it.
for.eg. for unique background you can use something like this:
<div id="product" style="background: url($mybgurl)"></div>
In this case $mybgurl it's url for this product's image, fetched from db.
P.S: Google for MVC or template engine.

dynamic accordion with bootstrap

I'm having a hard time getting this dynamic accordion to work properly.
I'm fetching accordion title from MySQL database and trying to create a dynamic accordion based on number of rows in my database.
As of right now, I'm only able to create single tab with first title in my database.
<?php
foreach ($variable as $key);
?>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" style="margin-right:50%;" > <img src="image.jpeg" class="event-icon"> <?php echo $key['title']?>;</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body">
<p>Event content
</p>
</div>
</div>
</div>
</div>
</div>
I'm trying to create the following html structure when the foreach loop runs.
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" style="margin-right:50%;" > <img src="image.jpeg" class="event-icon"> <?php echo $key['title']?>;</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body">
<p>Event content
</p>
</div>
</div>
</div>
Any suggestion is highly appreciated.
You have to add at the end of your foreach loop, like this:
<?php endforeach; ?>
Also you have to change ; -> : in your foreach header:
foreach ($variable as $key);
to:
foreach ($variable as $key):
//^See here
Looks like you have a problem with the alternative syntax for foreach
foreach ($variable as $key);
should be
foreach ($variable as $key):
semicolon (;) should be a colon (:)
it also ends with a endforeach semicolon
endforeach;

Categories