I tried to make an accordeon menu, but when I click on the menu it does not expand. I follow an example that it works well. Maybe I make a mistake somewhere I think
Note : I have just a category and one sub category, not more
Main Category
sub Category One
sub Category two
2nd Main Category
sub Category One
sub Category two
Below my php code :
<div class="accordion" id="accordionExample">
<?php
$i = 0;
if (is_array($categories)) {
foreach($categories as $racine) {
$i++;
?>
<?php
if($categories[0]) {
?>
<div class="accordion-item">
<h2 class="accordion-header" id="heading<?php echo $i; ?>">
<?php
if ($i == 1 || $i == 16) {
$button = '<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse'. $i .'" aria-expanded="true" aria-controls="collapse'. $i .'">';
} else {
$button = '<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse' . $i . '" aria-expanded="false" aria-controls="collapse' . $i . '">';
}
?>
<?php echo $button; ?>
<?php echo $racine['text']; ?>
</button>
</h2>
</div>
<?php
}
?>
<?php
}
}
?>
</div>
the html code. I think my problem could be inside the loop ?
<div class="accordion" id="accordionExample">
<div class="accordion-item">
<h2 class="accordion-header" id="heading1">
<button class="accordion-button show" type="button" data-bs-toggle="collapse" data-bs-target="#collapse1" aria-expanded="true" aria-controls="collapse1">Apps</button>
</h2>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="heading2">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse2" aria-expanded="false" aria-controls="collapse2"> Apps Catalog</button>
</h2>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="heading3">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse3" aria-expanded="false" aria-controls="collapse3"> Apps Communication</button>
</h2>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="heading4">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse4" aria-expanded="false" aria-controls="collapse4"> Apps Configuration</button>
</h2>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="heading5">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse5" aria-expanded="false" aria-controls="collapse5"> Apps Customers</button>
</h2>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="heading16">
<button class="accordion-button show" type="button" data-bs-toggle="collapse" data-bs-target="#collapse16" aria-expanded="true" aria-controls="collapse16">Hooks</a> </button>
</h2>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="heading17">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse17" aria-expanded="false" aria-controls="collapse17"> Modules Hooks Admin - Dashboard </button>
</h2>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="heading18">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse18" aria-expanded="false" aria-controls="collapse18"> Modules Hooks Import</button>
</h2>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="heading21">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse21" aria-expanded="false" aria-controls="collapse21"> Modules Hooks Cart</button>
</h2>
</div>
</div>
the function
public function getLabelTree(int|string $parent_id = '0', string $spacing = '', array|string $exclude = '', array|string $category_tree_array = '', bool $include_itself = false): array
{
if (!\is_array($category_tree_array)) {
$category_tree_array = [];
}
if ((\count($category_tree_array) < 1) && ($exclude != '0')) {
$category_tree_array[] = [
'id' => '0',
'text' => $this->upgrade->getDef('text_top')
];
}
if ($include_itself) {
$Qcategory = $this->upgrade->get('marketplace_categories', 'categories_name', ['id' => (int)$parent_id]);
$category_tree_array[] = [
'id' => $parent_id,
'text' => $Qcategory->value('categories_name')
];
}
$Qcategories = $this->upgrade->db->prepare('select categories_id,
categories_name,
parent_id
from :table_marketplace_categories
where parent_id = :parent_id
order by sort_order, categories_name
');
$Qcategories->bindInt(':parent_id', $parent_id);
$Qcategories->execute();
while ($Qcategories->fetch()) {
if ($exclude != $Qcategories->valueInt('categories_id'))
$category_tree_array[] = [
'id' => $Qcategories->valueInt('categories_id'),
'text' => $spacing . $Qcategories->value('categories_name')
];
$category_tree_array = $this->getLabelTree($Qcategories->valueInt('categories_id'), $spacing . ' ', $exclude, $category_tree_array);
}
return $category_tree_array;
}
As I said in my comments, for Bootstrap (V5.3) accordions to work you need an accordion-body.
Try the code below. This will print the "Main Category" in the accordion-header and each "Sub Category" as a list-item in the accordion-body. You can change/add HTML to your needs.
<?php
echo '<div class="accordion" id="accordionExample">';
$i = 0;
foreach ($categories as $racine) {
$i++;
// if Main Category
if ($i == 1 || $i == 16) {
if($i != 1) {
echo '</ul></div></div></div>';
}
echo '<div class="accordion-item"><h2 class="accordion-header" id="heading-' . $i .'"><button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse-' . $i .'" aria-expanded="false" aria-controls="collapse-' . $i .'">' . $racine['text'] . '</button></h2>';
echo '<div id="collapse-'. $i .'" class="accordion-collapse collapse" aria-labelledby="heading-'. $i .'" data-bs-parent="#accordionExample"><div class="accordion-body"><ul>';
}
// if Sub Category
else {
echo '<li>' . $racine['text'] . '</li>';
}
}
echo '</ul></div></div></div>';
echo '</div>';
?>
Note that the line echo '</ul></div></div></div>' is used twice because we need close each accordion-item before a new accordion-item is printed, but also after the foreach() loop. Because your $category_tree_array array is one dimensional, there is no way to avoid this duplicate line.
A better way would be to make $category_tree_array a multidimensional array (inside your getLabelTree() function) and then use an inner foreach() to loop through the subcategories of each main category. Then you can avoid that duplicate line as well as the if ($i == 1 || $i == 16) { } statement altogether.
Let me know if you need any help with that. In any case, the code above should do the trick.
Good luck.
Related
I have a PHP file that prints some divs and button elements, each one has the same class and they have jQuery events attached.
<?php $i=1; foreach ($questions as $question): ?>
<div class="question" id="<?php echo $i ?>">
<div class="id">
<h3 class="id-header">Domanda #<?php echo $i ?></h3>
<button id="minacce-button" class="btn btn-danger btn-rounded"> Minacce Associate <i class="fas fa-info-circle"></i></button>
</div>
<div class="testo">
<p><?php echo $question['testo'] ?></p>
</div>
<div class="valutazione">
<div class="btn-group shadow-0" role="group">
<button type="button" class="btn btn-danger compliance" id="<?php echo $question['id']; ?>" value="Non Compliance" data-color="dark">
Non Compliance
</button>
<button type="button" class="btn btn-warning compliance" id="<?php echo $question['id']; ?>" value="Da Verificare" data-color="dark">
Da Verificare
</button>
<button type="button" class="btn btn-success compliance" id="<?php echo $question['id']; ?>" value="Compliance" data-color="dark">
Compliance
</button>
<input type="hidden" name="id_question[]" id="q<?php echo $question['id']; ?>" value="">
<input type="hidden" name="value_question[]" id="v<?php echo $question['id']; ?>" value="1">
</div>
</div>
<div class="container-1">
<div class="spiegazione">
<p class="spiegazione-header">Spiegazione <i class="fas fa-info-circle"></i></p>
<p class="spiegazione-text">
<?php if($question['spiegazione'] != '') echo $question['spiegazione']; else echo('Non ci sono ancora spiegazioni disponibili!') ?>
</p>
</div>
<div class="contromisura">
<p class="contromisura-header">Contromisura <i class="fas fa-check-circle"></i></p>
<p class="contromisura-text">
<?php if($question['contromisura'] != '') echo $question['contromisura']; else echo('Non ci sono ancora contromisure disponibili!') ?>
</p>
</div>
<div class="sanzioni">
<p class="sanzioni-header">Sanzioni <i class="far fa-frown"></i></p>
<p class="sanzioni-text">
<?php
$db = pdo_connect_mysql();
$sanzioni = select("SELECT * FROM sanzioni_domande WHERE id_domanda ".$question['id']." ORDER BY tipo", $db);
if($sanzioni) {
foreach ($sanzioni as $sanzione) {
$output = "- ".$sanzione['nome']." (";
$sanzione['tipo'] == 1 ? $output .= "sanzione penale!" : $output .= "sanzione amministrativa!";
$output .= "): ".$sanzione['descrizione'];
echo $output;
} // fine foreach
} else {
echo('Non ci sono ancora sanzioni disponibili!');
} // fine if
?>
</p>
</div>
</div>
</div>
<hr>
<?php $i++; endforeach; unset($questions); ?>
Here i have another php page that after making an ajax adds more divs with the same class as the other, they all have jQuery event click attached as the previous page but the click does not work
$output = '';
//print_r($questions);
$i=$_POST['last']+1;
//echo $i."\n";
foreach ($questions as $question) {
//echo $i;
$output.= '<div class="question" id="'.$i.'">
<div class="id">
<h3 class="id-header">Domanda #'.$i.'</h3>
<button id="minacce-button" class="btn btn-danger btn-rounded"> Minacce Associate <i class="fas fa-info-circle"></i></button>
</div>
<div class="testo">
<p>'.$question['testo'].'</p>
</div>
<div class="valutazione">
<div class="btn-group shadow-0" role="group">
<button type="button" class="btn btn-danger compliance" id="'.$question['id'].'" value="Non Compliance" data-color="dark">
Non Compliance
</button>
<button type="button" class="btn btn-warning compliance" id="'.$question['id'].'" value="Da Verificare" data-color="dark">
Da Verificare
</button>
<button type="button" class="btn btn-success compliance" id="'.$question['id'].'" value="Compliance" data-color="dark">
Compliance
</button>
<input type="hidden" name="value_question" id="v'.$question['id'].'" value="1">
</div>
</div>
<div class="container-1">
<div class="spiegazione">
<p class="spiegazione-header">Spiegazione <i class="fas fa-info-circle"></i></p>
<p class="spiegazione-text">
'; if($question['spiegazione'] != '') $output .= $question['spiegazione']; else $output .= 'Informazioni su questa domanda non disponibili!';
$output .= '</p>
</div>
<div class="contromisura">
<p class="contromisura-header">Contromisura <i class="fas fa-check-circle"></i></p>
<p class="contromisura-text">';
if($question['contromisura'] != '') $output .= $question['contromisura']; else $output .='Non ci sono ancora contromisure disponibili!';
$output .= '</p>
</div>
</div>
</div>
<hr>';
$i++;
} //unset($questions);
echo $output;
Here it is my jQuery code:
$(".compliance").on("click", function(event) {
var id_domanda = $(this).attr("id");
var valutazione = -1;
//alert("Testo bottone cliccato:-" + $(this).val() + "-");
switch ($(this).val()) {
case 'Non Compliance':
valutazione = 0;
break;
case 'Da Verificare':
valutazione = 1;
break;
case 'Compliance':
valutazione = 2;
break;
default:
valutazione = -1;
}
// Assegnazione valutazione a input valutazione domanda
$("#v"+id_domanda).val(valutazione);
$("#q"+id_domanda).val(id_domanda);
alert("Valutazione assegnata: " + valutazione);
alert("Riepilogo:\nID Domanda: " + $("#q"+id_domanda).val() + "\nValutazione: " + $("#v"+id_domanda).val() );})
$(".compliance").on("click",
Only works on content when the DOM is loaded.
If you load content trough AJAX and want JQuery events to work on then you need to change it to:
$(document).on('click', '.compliance', function(event) {})
That way the click event will work also on ajax loaded content
Here is the situation. I have an accordion, setup and the problem is that multiple accordions are opening and closing the same time.
Here is the code:
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<?php $x = 0; ?>
<?php foreach ($testFaqs as $faqs) { ?>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="heading-<?php echo $x; ?>">
<h4 class="panel-title">
<a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse-<?php echo $x; ?>" aria-expanded="true" aria-controls="collapse-<?php echo $x; ?>">
<?php echo $faqs['name']; ?>
</a>
</h4>
</div>
<div id="collapse-<?php echo $x; ?>" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="heading-<?php echo $x; ?>">
<div class="panel-body">
<?php if ($faqs['videoUrl'] != '' ) { ?>
<iframe align="center" class="youtube" src="https://www.youtube.com/embed/<?php echo $faqs['videoUrl']?>"> </iframe>
<?php } ?>
<?php echo htmlspecialchars_decode(($faqs['answer'])); ?>
</div>
</div>
</div>
<?php $x++; } ?>
</div>
Any help would be appreciated.
Thank you,
Kevin Davis
You can use latest Bootstrap 4 Accordion. More about it here https://getbootstrap.com/docs/4.3/components/collapse/#accordion-example
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>
<div class="accordion" id="accordionExample">
<div class="card">
<div class="card-header" id="heading-1">
<h2>
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapse-1" aria-expanded="true" aria-controls="collapse-1">
111
</button>
</h2>
</div>
<div id="collapse-1" class="collapse show" aria-labelledby="heading-1" data-parent="#accordionExample">
<div class="card-body">
111
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="heading-2">
<h2>
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapse-2" aria-expanded="false" aria-controls="collapse-2">
222
</button>
</h2>
</div>
<div id="collapse-2" class="collapse" aria-labelledby="heading-2" data-parent="#accordionExample">
<div class="card-body">
222
</div>
</div>
</div>
<div class="card">
<div class="card-header" id="heading-3">
<h2>
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapse-3" aria-expanded="false" aria-controls="collapse-3">
333
</button>
</h2>
</div>
<div id="collapse-3" class="collapse" aria-labelledby="heading-3" data-parent="#accordionExample">
<div class="card-body">
333
</div>
</div>
</div>
</div>
So your code will be
<div class="accordion" id="accordionExample">
<?php $x = 0; ?>
<?php foreach ($testFaqs as $faqs) { ?>
<div class="card">
<div class="card-header" id="heading-<?php echo $x; ?>">
<h2>
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapse-<?php echo $x; ?>" aria-expanded="true" aria-controls="collapse-<?php echo $x; ?>">
<?php echo $faqs['name']; ?>
</button>
</h2>
</div>
<div id="collapse-<?php echo $x; ?>" class="collapse<?php if ($x == 0) { ?> show<?php } ?>" aria-labelledby="heading-<?php echo $x; ?>" data-parent="#accordionExample">
<div class="card-body">
<?php if ($faqs['videoUrl'] != '' ) { ?>
<iframe align="center" class="youtube" src="https://www.youtube.com/embed/<?php echo $faqs['videoUrl']?>"></iframe>
<?php } ?>
<?php echo htmlspecialchars_decode(($faqs['answer'])); ?>
</div>
</div>
</div>
<?php $x++; } ?>
</div>
Added this class="collapse<?php if ($x == 0) { ?> show<?php } ?>" for the first box to be opened.
If you have more then one accordion on one page - don't forget to change id="accordionExample" and data-parent="#accordionExample", maybe by adding them an increment for counting accordions.
I need to create a grouped year / month list
2020
January
February
2019
May
June
My Table (name: reports)
enter image description here
Query
SELECT YEAR(report_date) as y, MONTH(report_date) as m, report_company_id
FROM reports
WHERE YEAR(report_date) = YEAR(report_date) AND report_company_id="11"
GROUP BY y, m ORDER BY y DESC, m DESC
I need help with a script displaying the structure of the list
<div class="tab-pane fade show" id="tab-<?php echo $row['company_id']; ?>" role="tabpanel" aria-labelledby="v-pills-home-tab">
<div class="accordion" id="accordionExample">
<div class="card">
<?php
$count_month = $db -> query
(
'SELECT YEAR(report_date) as y, MONTH(report_date) as m, report_company_id
FROM reports
WHERE YEAR(report_date) = YEAR(report_date) AND report_company_id="'.$company_id.'"
GROUP BY y, m
ORDER BY y DESC, m DESC'
);
$last_year = 0;
?>
<div data-role="main" class="ui-content">
<?php
foreach ($count_month as $key) { ?>
<div class="card-header" id="headingOne">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne-<?php echo $key['y']; ?>" aria-expanded="true" aria-controls="collapseOne"><?php echo $key['y']; ?></button>
</div>
<div id="collapseOne-<?php echo $key['y']; ?>" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
<div class="card-body">
<?php echo $monthName = date('F', mktime(0, 0, 0, $key['m'], 10)); // March ?>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
My result. year is repeated twice
enter image description here
Method 1:
If you want 2nd level aggregation in visual then you need to convert your array to another associative array using year as key
$count_month_map = Array();
foreach ($count_month as $value) {
if(array_key_exists($value['y'], $count_month_map)) {
array_push($count_month_map[ $value['y'] ], $value);
} else {
$count_month_map[ $value['y'] ] = Array( $value );
}
}
$last_year = 0;
?>
<div data-role="main" class="ui-content">
<?php
foreach ($count_month_map as $key => $value) { ?>
<div class="card-header" id="headingOne">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne-<?php echo $key; ?>" aria-expanded="true" aria-controls="collapseOne"><?php echo $key; ?></button>
</div>
<?php
foreach ($value as $key1) {
?>
<div id="collapseOne-<?php echo $key1['y']; ?>" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
<div class="card-body">
<?php echo $monthName = date('F', mktime(0, 0, 0, $key1['m'], 10)); // March ?>
</div>
</div>
<?php } ?>
<?php
}
?>
</div>
Here $count_month_map is another associative array formed from $count_month
Then inside the main loop you need to have a second loop to loop though the array of array
Method 2:
You can store the year in your loop in a dummy variable say $curr, at the end bracket you assign it to another dummy variable $prev
then conditionally you suppress the button
$curr = '';
$prev = '';
?>
<div data-role="main" class="ui-content">
<?php
foreach ($count_month as $key) {
$curr = $key['y'];
if($prev != $curr) {
?>
<div class="card-header" id="headingOne">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne-<?php echo $key['y']; ?>" aria-expanded="true" aria-controls="collapseOne"><?php echo $key['y']; ?></button>
</div>
<?php
}
?>
<div id="collapseOne-<?php echo $key['y']; ?>" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
<div class="card-body">
<?php echo $monthName = date('F', mktime(0, 0, 0, $key['m'], 10)); // March ?>
</div>
</div>
<?php
$prev = $curr;
}
?>
</div>
Note: Method 2 only applicable for year column sorted, which you are already doing
Basically I have this code where I want to extract some data from a database and display it on a pop-up window. The problem is when I click on the eye icon ( labelled More ) I want it to display the correspondent id of that table row but I can't seem to figure out how to do that.
For more references I set up a temporary website to better see the problem : http://twgtest-org.stackstaging.com/bau50/bau50_extract.php
You can see that when I click on more the displayed ID is actually all the IDs from the database instead of it being only the ID of that row
<?php
//load database connection
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$query_string = "SELECT * FROM bau50";
$query = $pdo->prepare($query_string);
$query->execute();
?>
<body>
<div class="container">
<ul class="text-align">
<?php
if (!$query->rowCount() == 0) {
while ($show_all = $query->fetch()) {
echo '<tr><th scope="row">';
echo $show_all['id_Bau50'];
echo '</th><td>';
echo $show_all['name'];
echo '</td><td>';
echo $show_all['produkt'];
echo '</td><td>';
echo $show_all['preis']," €";
echo '</th><td>';
echo $show_all['Ergebniss'];
echo '</td>
<td><p data-placement="top" data-toggle="tooltip" title="more"><button class="btn btn-primary btn-xs" data-title="more" data-toggle="modal" data-target="#more" ><span class="fa fa-eye"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-secondary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="fa fa-pencil"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="fa fa-trash"></span></button></p></td></tr>';
}
echo '</tbody></table>';
}
?></ul>
</div>
<?php
$query_view = "SELECT * FROM bau50";
$query2= $pdo->prepare($query_view);
$query2->execute();
echo'<div class="modal fade" id="more" tabindex="-1" role="dialog" aria-labelledby="more" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="fa fa-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">More</h4>
</div>
<div class="modal-body">
<div>';
while ($show= $query2->fetch()) {
$id= $show ['id_Bau50'];
echo $id;
}
echo '</div>
<div class="modal-footer ">
<button type="button" class="btn btn-secondary btn-lg" style="width: 100%;" data-toggle="modal" data-target="#edit"><span class="fa fa-pencil"></span> Update</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
</div>';
?>
This is the very simply solution: add a new modal for each row identified by its id for example for id "4" you will open the modal with id "modal4" and so on:
<?php
//load database connection
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$query_string = "SELECT * FROM bau50";
$query = $pdo->prepare($query_string);
$query->execute();
?>
<body>
<div class="container">
<ul class="text-align">
<?php
if (!$query->rowCount() == 0) {
while ($show_all = $query->fetch()) {
echo '<tr><th scope="row">';
echo $show_all['id_Bau50'];
echo '</th><td>';
echo $show_all['name'];
echo '</td><td>';
echo $show_all['produkt'];
echo '</td><td>';
echo $show_all['preis']," €";
echo '</th><td>';
echo $show_all['Ergebniss'];
echo '</td>
<td><p data-placement="top" data-toggle="tooltip" title="more"><button class="btn btn-primary btn-xs" data-title="more" data-toggle="modal" data-target="#more'.$show_all['id_Bau50'].'" ><span class="fa fa-eye"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-secondary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="fa fa-pencil"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="fa fa-trash"></span></button></p></td></tr>';
}
echo '</tbody></table>';
}
?></ul>
</div>
<?php
$query_view = "SELECT * FROM bau50";
$query2= $pdo->prepare($query_view);
$query2->execute();
while ($show= $query2->fetch()) {
$id= $show ['id_Bau50'];
echo'<div class="modal fade" id="more'.$id.'" tabindex="-1" role="dialog" aria-labelledby="more" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="fa fa-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">More</h4>
</div>
<div class="modal-body">
<div>';
echo $id;
echo '</div>
<div class="modal-footer ">
<button type="button" class="btn btn-secondary btn-lg" style="width: 100%;" data-toggle="modal" data-target="#edit"><span class="fa fa-pencil"></span> Update</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
</div>';
}
?>
This is ok for not so many rows or if you don't want to write some javascript.
A nicer solution would be to use one single modal with all content inside and show/hide the selected one, for this you need to use some js code and don't use the standard bootstrap attribute-driven methods.
In this example I'm using jquery and js code (se at the bottom).
<?php
//load database connection
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
$query_string = "SELECT * FROM bau50";
$query = $pdo->prepare($query_string);
$query->execute();
?>
<body>
<div class="container">
<ul class="text-align">
<?php
if (!$query->rowCount() == 0) {
while ($show_all = $query->fetch()) {
echo '<tr><th scope="row">';
echo $show_all['id_Bau50'];
echo '</th><td>';
echo $show_all['name'];
echo '</td><td>';
echo $show_all['produkt'];
echo '</td><td>';
echo $show_all['preis']," €";
echo '</th><td>';
echo $show_all['Ergebniss'];
echo '</td>
<td><p data-placement="top" data-toggle="tooltip" title="more"><button class="btn btn-primary btn-xs" data-moreid="'.$show_all['id_Bau50'].'" ><span class="fa fa-eye"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-secondary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="fa fa-pencil"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="fa fa-trash"></span></button></p></td></tr>';
}
echo '</tbody></table>';
}
?></ul>
</div>
<div class="modal fade" id="more" tabindex="-1" role="dialog" aria-labelledby="more" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="fa fa-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">More</h4>
</div>
<div class="modal-body">
<?php
$query_view = "SELECT * FROM bau50";
$query2= $pdo->prepare($query_view);
$query2->execute();
while ($show= $query2->fetch()) {
$id= $show ['id_Bau50'];
echo '<div id="more'.$id.'">'.$id.'</div>';
}
?>
</div>
<div class="modal-footer ">
<button type="button" class="btn btn-secondary btn-lg" style="width: 100%;" data-toggle="modal" data-target="#edit"><span class="fa fa-pencil"></span> Update</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- YOU NEED JQUERY FOR THIS: -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script type="text/javascript">
$(function(){
$("button[data-moreid]").click(function(){
$("#more .modal-body > div").hide(); //hide all more div
var id=$(this).attr("data-moreid"); //get id from pushed button
$("#more .modal-body #more"+id).show(); //show only pushed id
$("#more").modal("show"); //show modal
});
});
</script>
I want to display more results in another column via foreach from the database but I can't get it to work.
<div class="row">
<div class="col-md-4">
<?php
if (is_array($results))
{
foreach ($results as $data) { ?>
<!-- User item starts -->
<div class="user-item">
<!-- User action buttons -->
<div class="user-btns">
<i class="fa fa-pencil"></i>
<i class="fa fa-times"></i>
</div>
<!-- Image -->
<img src="<?php echo $data->avatar ?>" alt="" class="img-responsive">
<!-- User details -->
<div class="u-details">
<h5><i class="fa fa-user"></i><?php echo $data->first_name . ' ' . $data->last_name ?></h5>
<h5><i class="fa fa-envelope"></i> <?php echo $data->email ?></h5>
<h5><i class="fa fa-user-md"></i> <?php echo $data->username ?></h5>
</div>
<div class="clearfix"></div>
</div>
<!-- User item ends -->
<?php } } ?>
</div>
<div class="col-md-4">
<?php
if (is_array($results))
{
foreach ($results as $data) { ?>
<!-- User item starts -->
<div class="user-item">
<!-- User action buttons -->
<div class="user-btns">
<i class="fa fa-pencil"></i>
<i class="fa fa-times"></i>
</div>
<!-- Image -->
<img src="<?php echo $data->avatar ?>" alt="" class="img-responsive">
<!-- User details -->
<div class="u-details">
<h5><i class="fa fa-user"></i><?php echo $data->first_name . ' ' . $data->last_name ?></h5>
<h5><i class="fa fa-envelope"></i> <?php echo $data->email ?></h5>
<h5><i class="fa fa-user-md"></i> <?php echo $data->username ?></h5>
</div>
<div class="clearfix"></div>
</div>
<!-- User item ends -->
<?php } } ?>
</div>
</div>
As you can see, it's two different columns. I want to do foreach and show one set of information in one column and the other set in another. However, the code below returns the same value in both columns.
how can I do this? Thanks.
You are iterating through each item of results in both loops, so their results are the same. Why not try something like this
<?php
$cols = 3;
?>
<div class="row">
<?php
if (is_array($results))
{
for($i = 0 ; $i < $cols ; $i++)
{
echo "
<div class='col-md-".(12/$cols)."'>";
for ($j = 0 ; $j < (count($results) / $cols) + 1 ; $j++)
{
if ($i * $cols + $j < count($results)) // make sure it won't give errors for array out of range....
{
echo "
<div class='user-item'>
<div class='user-btns'>
<a href='#' class='btn btn-green btn-xs'><i class='fa fa-pencil'></i></a>
<a href='#' class='btn btn-red btn-xs'><i class='fa fa-times'></i></a>
</div>
<img src='".$results[$i* $cols + $j]->avatar."' alt=' class='img-responsive'/>
<div class='u-details'>
<h5><i class='fa fa-user'></i>".$results[$i* $cols + $j]->first_name . ' ' . $results[$i* $cols + $j]->last_name."</h5>
<h5><i class='fa fa-envelope'></i>".$results[$i* $cols + $j]->email."</h5>
<h5><i class='fa fa-user-md'></i>".$results[$i * $cols + $j]->username."</h5>
</div>
<div class='clearfix'></div>";
}
}
echo "
</div>";
}
}
?>
</div>