load data using ajax call in bootstrap tabs from php mysql - php

i am trying to load data dynamically in bootstrap tabs using php mysql.
<ul class="nav nav-tabs " role="tablist">
<li class="active col-md-3"><a data-toggle="tab" href="#home">Notes</a></li>
<li class="col-md-3"><a data-toggle="tab" href="#menu1">Question Bank</a></li>
<li class="col-md-3"><a data-toggle="tab" href="#menu2">Summary</a></li>
<li class="col-md-3"><a data-toggle="tab" href="#menu3">Videos</a></li>
</ul>
<div class="tab-content">
<div id="home" class="tab-pane fade in active">
</div>
</div>
Now Problem is
1.make tab active when it get clicked
2.load data using ajax/jquery from db for each tab
for eg. in first tab 'home' i'm using this query
select c.title,c.description,c.content_url
from content_ref_table c inner join course_ref_table cf on c.course_id =cf.course_id
where c.title like '%eco%' and cf.courses = 'b.com'
all the data will be fetched in div's ,
what will be the solution for this problem

Please Try :
<ul class="nav nav-tabs " role="tablist">
<li class="active col-md-3"><a data-toggle="tab" onclick="GetDataFromDB('home');" href="#home">Notes</a></li>
<li class="col-md-3"><a data-toggle="tab" onclick="GetDataFromDB('menu1');" href="#menu1">Question Bank</a></li>
<li class="col-md-3"><a data-toggle="tab" onclick="GetDataFromDB('menu2');" href="#menu2">Summary</a></li>
<li class="col-md-3"><a data-toggle="tab" onclick="GetDataFromDB('menu3');" href="#menu3">Videos</a></li>
</ul>
Your responce div.
<div class="tab-content">
<div id="home" class="tab-pane fade in active">
</div>
</div>
Please write jquery for ajax request.
<script>
function GetDataFromDB(tab_type){
$.post("ajax_page.php",
{
tab_type: tab_type
},
function(data, status){
data = JSON.parse(data);
$('#home').html(data);
});
}
</script>
Create ajax_page.php file for ajax responce.
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: ".$conn->connect_error);
}
if ($_POST['tab_type'] == 'home')
{
$sql = "select c.title,c.description,c.content_url
from content_ref_table c inner join course_ref_table cf on c.course_id =cf.course_id
where c.title like '%eco%' and cf.courses = 'b.com'";
$result = $conn->query($sql);
$jsonData = [];
if ($result->num_rows > 0)
{
// output data of each row
while ($row = $result->fetch_assoc()) {
$jsonData[] = $row;
}
}
echo json_encode($jsonData);
} else if ($_POST['tab_type'] == 'menu1')
{
}
$conn->close();

Related

Writing a dynamically built DOM for entirety of session once

I'm grabbing JSON from a rest api and using the result to build a navigation menu dynamically via jQuery which works. However I worry having to repopulate the DOM every refresh or page load would most likely cause a slower user experience...
My question is, instead of getting the data, parsing and building every load. Are there any other ways of storing and implementing this so that the user only has to take the slight dip on first load? I'm thinking of possibly storing it in a cookie and grabbing that but that could be slow too.
To get the api
<?php
use yii\httpclient\Client;
$client = new Client();
$response = $client->createRequest()
->setMethod('GET')
->setUrl('API_URL')
->send();
if ($response->isOk) {
$page = $response;
}
$retJSON = $response->content;
$decode = json_decode($retJSON, true);
$page=$decode;
?>
To parse and build the data
<script async>
<?php $page = json_encode($page); ?>
var json = '<?php print $page; ?>';
$amtmenu = "";
$.each(JSON.parse(json), function(idx, obj) {
if(obj.menu_item_parent == 0){
if(obj.type != 'custom' | obj.title == 'More'){
$amtmenu += '<li class="dropdown"> '+ obj.title +'<span class="fa fa-angle-down"></span><ul class="dropdown-menu ldrop" id="' + obj.ID +'"></ul></li>';
} else {
$amtmenu += '<li>' + obj.title + '</li>';
}
}
});
$(".navbar-nav").html($amtmenu);
$.each(JSON.parse(json), function(idx, obj) {
if(obj.menu_item_parent != 0){
$itemparent = obj.menu_item_parent;
$('#' + $itemparent).append( '<li> ' + obj.title + ' </li>');
}
});
</script>
Which builds something like
<ul class="nav navbar-nav">
<li class="dropdown"> Leasing<span class="fa fa-angle-down"></span>
<ul class="dropdown-menu ldrop">
<li>Personal Car leasing</li>
<li>Business Car Leasing</li>
<li>Van Leasing</li>
</ul>
</li>
<li>Special Offers</li>
<li class="dropdown"> Services<span class="fa fa-angle-down"></span>
<ul class="dropdown-menu">
<li> Overview </li>
</ul>
</li>
<li class="dropdown"> Leasing Guide<span class="fa fa-angle-down"></span>
<ul class="dropdown-menu">
<li> The Guide </li>
</ul>
</li>
<li class="dropdown">More<span class="fa fa-angle-down"></span>
<ul class="dropdown-menu">
<li>Contact Us</li>
</ul>
</li>
</ul>
Any help or ideas are appreciated!

how to pass value from while loop to next page after clicking on menu item php

I have a a navigation bar and one of the nav items is a dropdown with sub categories. Only the subcategories are being pulled from the database by using a while loop.
When clicking on one of the dropdown items they will be redirected to dancerProfile.php.
I want dancerProfile.php to pull the menu item name from the other pages.
html
<li class="li-spacing">Home</li>
<li class="dropdown li-spacing">
<a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
<ul class="dropdown-menu">
<?php
$dancers = "SELECT `dancer_name` FROM `dancers` WHERE name = '$name'";
$dres = mysqli_query($con,$dancers);
//if($res==FALSE){
//die('there was an error running query [' . $con->error . ']');
// }
while($data=mysqli_fetch_array($dres)){
$dancerName = $data['dancer_name'];
foreach ($dancerName as $DANCER){
echo '
<li>'.$data["dancer_name"].'</li>
<li class="divider"><hr></li>
';
}
}
?>
<li>Add New</li>
</ul>
</li>
This works well as all dancers appear in the dropdown.
What I want is that when I click on dancerA the dancerProfile.php will show dancerA information. And when I click on dancerB it will show dancerB info, etc.
But it will only show the last dancer's information no matter which name I click on.
dancerProfile.php
<div class="col-sm-6 dancerInfo">
<div class="row">
<div class="col-sm-6">
<div class="dancerName"><?php echo $dancerName;?></div>
</div>
So When I click on dancerA on the nav bar in any other page, in dancerProfile.php $dancerName should print dancerA. And it should print dancerB if I clicked on dancerB from the nav bar.
but it is only printing dancerB no matter which link I click.
I am using bootstrap. Can anyone help me?
EDIT
Here's an update of what my code looks like now.
<li class="li-spacing">Home</li>
<li class="dropdown li-spacing">
<a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
<ul class="dropdown-menu">
<?php
$dancers = "SELECT `dancer_name`, `id` FROM `dancers` WHERE name = '$name'";
$dres = mysqli_query($con,$dancers);
$dancerId= 'id';
}
while($data=mysqli_fetch_array($dres)){
$dancerName = $data['dancer_name'];
echo '
<li>'.$data["dancer_name"].'</li>
<li class="divider"><hr></li>
';
}
?>
<li>Add New</li>
</ul>
</li>
And dancerProfile.php:
<?PHP
if(isset($_GET['id'])) {
$dancerId=$_GET['id'];
$dancerquery = "SELECT `dancer_name` FROM `dancers` WHERE id = " . $_GET['id'] . ";";
$dancer_res = mysqli_query($con,$dancers);
if($dancer_res){
$DANCER='dancer_name';
}
}
?>
<div class="col-sm-6 dancerInfo">
<div class="row">
<div class="col-sm-6">
<div class="dancerName"><?php echo " $DANCER ";?></div>
</div>
I also forgot to mention that this navigation is also on dancerProfile page. So all of the code I am providing is on dancerProfile page. I don't know if that matters
My database table
You need pass dancer id or which is unique in your dancer table. something like given below.
<li>'.$data["dancer_name"].'</li>
And now go to dancerProfile.php and try something like this.
if (isset($_GET['dancer_id'])) {
$dancer_id=$_GET['dancer_id'];
//your query
}
Your full code:
<li class="li-spacing">Home</li>
<li class="dropdown li-spacing">
<a class="dropdown-toggle" data-toggle="dropdown">Dancers<b class="caret"></b></a>
<ul class="dropdown-menu">
<?php
$dancers = "SELECT `dancer_name`, `id` FROM `dancers` WHERE name = '$name'";
$dres = mysqli_query($con,$dancers);
$dancerId= 'id';
}
while($data=mysqli_fetch_array($dres)){ ?>
$dancerName = $data['dancer_name'];
<li><?php echo $data['dancer_name']; ?>'</li>
<li class="divider"><hr></li>
<?php } ?>
<li>Add New</li>
</ul>
</li>
Your dancerProfile.php should be
<?PHP
if(isset($_GET['id'])) {
$dancerId=$_GET['id'];
$dancerquery = "SELECT `dancer_name` FROM `dancers` WHERE id = '$dancerId'";
$dancer_res = mysqli_query($con,$dancerquery);
$data=mysqli_fetch_array($dancer_res);
}
?>
<div class="col-sm-6 dancerInfo">
<div class="row">
<div class="col-sm-6">
<div class="dancerName"><?php echo $data['dancer_name'];?></div>
</div>
You are currently overriding the value of $DANCER with each iteration, so the last will always be the used value.
Just change your loop a bit:
while($data=mysqli_fetch_array($dres)){
echo '<li>'.$data['dancer_name'].'</li>';
}

Data from MySQL not showing while building dynamic menu in PHP

I'm building a dynamic menu using PHP and MySQL but results are not showing:
The website structure is built below:
<div id="container">
<ul class="nav">
<li>home</li>
<li>
destinations &triangledown;
<div id="subMenu">
<div class="nav-column">
Europe
</div>
<div class="nav-column" >
Africa
</div>
<div class="nav-column" >
Asia
</div>
<div class="nav-column">
Oceania
</div>
<div class="nav-column">
North America
</div>
<div class="nav-column">
South America
</div>
</div>
</li>
<li>about</li>
<li>contact</li>
</ul>
</div>
Then i have a continents DB and then another DB with countries, which connect to their respective continent.
So, in the subMenu div, i want to call each continent and then in each div cnt_submenu_, loop through a unordered list of 9 results and then show another with the same number of results and so on until all the countries with the same id reaches the end.
Here is what i'm trying to do with the PHP code below:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<div id="container">
<ul class="nav">
<li>home</li>
<li>destinations
<div id="subMenu">
<?php
$servername = "localhost:3306";
$username = "root";
$password = "";
$dbname = "site_comboios";
//Open DB connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
//check connection
if($mysqli->connect_errno) {
echo 'Error connecting to database';
exit();
}
//show sub-menu
$query = "SELECT * FROM continentes ORDER BY id";
$result = mysqli_query($mysqli, $query);
$div = 0;
while ($row = mysqli_fetch_assoc($result)) {
$país = $row['continente'];
echo "<div class='nav-column'><a href='#' class='continente' data-id='cnt_submenu'>" .($país). "</a></div> "; </div> ";
}
?>
</div>
</li>
<li>about</li>
<li>contact</li>
</ul>
</div>
<div id="cnt_submenu_" class="submenu_continente active">
<?php
//retrieve european countries from DB and open <ul> with 9 results
$conn = mysqli_connect("localhost:3306","root","","site_comboios");
$sql_countries = "SELECT * FROM countries ORDER BY id WHERE id_continentes = 1" ;
$res_countries = mysqli_query($conn, $sql_countries);
if($res_countries > 0) {
while ($row_countries = mysqli_fetch_assoc($res_countries)) {
$país = $row_countries['id_continente'];
echo "<ul><li><a href='#'>". $país ."";
}
} else if ($res_countries == 9) {
echo '</li></ul>';
}
?>
<span class="close">x</span>
</div>
Thanks for pointing me out in the right direction.
Edited I'm trying to show through 9 results onto a , close it and open another with 9 results until the end of the countries with id = 1.
Added the jquery to open each continent div below but i'm not doing it right:
//Open and close each div with country list
$('.continente').hover(function(){
menuHide('submenu_continente' . $(this).data("id") );
$('#cnt_submenu_' . $(this).data("id") ).show();
},function(){
$('#cnt_submenu_' . $(this).data("id")).mouseleave( function() {
$(this).hide();
});
});

Dynamically insert class="active" into php function that generates bootstrap menu

I have tried pretty much every solution that was available online but cannot get the class="active" to change dynamically for each menu section.
When someone clicks the top level menu item it opens up the second level menu and if someone click the on a second level menu item in that section class="active" needs to remain on the top level menu li tag.
I have the following function that generates my bootstrap menu:
function getMenu() {
global $connection;
mysqli_select_db($connection, "c9");
$query = ("SELECT testName, testId FROM testType");
$result_set = mysqli_query($connection, $query);
while ($row = mysqli_fetch_array($result_set)) {
$testId = $row['testId'];
$testName = $row['testName'];
echo '<li>'; //This is where class="active" needs to be added
echo '<span class="nav-label">'.$testName.'</span>';
echo '<ul class="nav nav-second-level collapse">';
echo '<li>Summary Report</li>';
echo '<li>Add Data</li>';
echo '</ul>';
echo '</li>';
}
}
And the menu structure is as follows:
<ul class="nav metismenu" id="side-menu">
<li class="active">
<a href="/">
<span class="nav-label">Summary</span>
<ul class="nav nav-second-level collapse in">
<li>Reports</li>
</ul>
</li>
<li><a href="#">
<span class="nav-label">Temperature</span>
<ul class="nav nav-second-level collapse">
<li>Summary Report</li>
<li>Add Data</li>
</ul>

Fetch subcategory in fetched category

What i am trying to build is next. Fetch all my menu categories and display them, then fetch all subcategories for each category special all display it also. I hope you understand what i want and saying. I got lost in code and don't now how to do this. Here is my code, hope you can help me.
Here is table structure.
subcategory table
| id | subcategory_name | id_category |
category table
| id | category |
id_category is connected with id in category table
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-left">
<?php
$pdo = connect();
$sql = "SELECT * FROM category";
$query = $pdo->prepare($sql);
$query->execute();
$row = $query->fetchAll();
foreach ($row as $rs) { ?>
<li class="dropdown"><?php echo $rs['category'] ?><span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<?php
$sql = "SELECT subcategory.subcategory_name, subcategory.id_category, category.id, category.category
FROM subcategory
INNER JOIN category
WHERE subcategory.id_category = category.category";
$query = $pdo->prepare($sql);
$query->execute();
$subcat = $query->fetchAll();
foreach ($subcat as $sub) { ?>
<li><?php echo $sub['subcategory_name'] ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>Logout</li>
</ul>
</div><!-- /.navbar-collapse -->
</div>
I think you just didn't understand the INNER JOIN sql select statement. You have to declare the relation of the second table with the first one:
SELECT column_list
FROM t1
INNER JOIN t2 ON join_condition1
WHERE where_conditions;
Join condition in where you want to put the "subcategory.id_category = category.category" part of your query. Maybe leave out the actual WHERE clause?
Hope it helps.
Look here
Edit:
WHat I was proposing pointed to rewrite the complete code to do it in one query. But your code could be simplified like this quickly using the already captured variable with the first sql query:
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-left">
<?php
$pdo = connect();
$sql = "SELECT * FROM category";
$query = $pdo->prepare($sql);
$query->execute();
$row = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($row as $rs) { ?>
<li class="dropdown"><?php echo $rs['category'] ?><span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<?php
$sql = "SELECT subcategory_name, id_category
FROM subcategory
WHERE id_category = '".$rs['id']."'";
$query = $pdo->prepare($sql);
$query->execute();
$subcat = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($subcat as $sub) { ?>
<li><?php echo $sub['subcategory_name'] ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>Logout</li>
</ul>
</div><!-- /.navbar-collapse -->
</div>

Categories