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 ▿
<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();
});
});
Related
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>';
}
I need some help finding out how to show all the topics from the mysql (phpmyadmin) database on my website.
This is how it looks like now
The code for that is:
<section class="col-md-4 connectedSortable">
<!-- TABLE: LATEST ORDERS -->
<div class="box box-info">
<div class="panel panel-default">
<div class="panel-heading main-color-bg">
<h3 class="panel-title">Topics</h3>
</div>
<div class="container-fluid">
<div class="row content">
<ul class="nav nav-pills nav-stacked">
<li class="active">Home</li>
<li>Friends</li>
<li>Family</li>
<li>Photos</li>
</ul><br>
</div>
</div>
</div>
</div><!-- /.box -->
</section><!-- right col -->
I want this list to be the list of topics out of the database
How the database looks like
this is my config.php:
<?php
date_default_timezone_set('Europe/Amsterdam');
error_reporting(E_ALL & ~ E_DEPRECATED);
ini_set('display_errors','ON');
$CONFIG = array();
$CONFIG['root'] = '####';
$CONFIG['rootwebsite'] = '####';
$CONFIG['website'] = '####';
$CONFIG['dbhost'] = 'localhost';
$CONFIG['dbuser'] = '####';
$CONFIG['dbpass'] = '####';
$CONFIG['dbdatabase'] = 'tom';
?>
What I tried:
class Forum
{
private $dbh; //dbh = database handler.
public function __construct($database)
{
$this->dbh = $database;
}
/* function that gets the main forum board */
public function getForum()
{
$query = $this->dbh->prepare('SELECT * FROM `topics` ORDER BY `id` ASC');
$query->execute();
$results = $query->fetchAll();
foreach( $results as $row ){
print_r( $row );
}
}
}
Here goes from the PDO perspective:
<?php
// Connect to DB
try {
$yourdsn = 'mysql:dbname=<YOURDBNAME>;host=<YOURDBHOST>;
$data = new PDO($yourdsn, $youruser, $yourpassword);
$data->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data->exec("set names utf8");
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
Then query your table
$sql = "SELECT * FROM topics ORDER BY id DESC";
$topics = $data->prepare($sql);
$topics->execute();
$tops = $topics->fetchAll(PDO::FETCH_OBJ);
Finally loop results in HTML template
<? foreach($tops as $top):?>
<h1><?=$top->onderwerp?></h1>
<p><?=$top->omschrijving?></p>
<? endforeach;?>
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();
i have been trying to format my results which is being sorted by the first letter of the surname but having problems
i need it to echo in the following format
<section>
<div id="slider">
<div class="slider-content">
<ul>
<li id="LETTER"><a name="LETTER" class="title">LETTER</a><ul>
<li>SURNAME</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</section>
i am have tried to split it so (see code bellow) but it not rendering correctly
<html>
<head>
<title>MySQLi Read Records</title>
</head>
<body><section>
<div id="slider">
<div class="slider-content">
<ul>
<?php
//include database connection
include 'db_connect.php';
//query all records from the database
$query = " SELECT name,
surname,
mobile,
UPPER (LEFT(surname, 1)) AS letter
FROM contacts
ORDER BY surname";
//execute the query
$result = $mysqli->query( $query );
//get number of rows returned
$num_results = $result->num_rows;
//this will link us to our add.php to create new record
if( $num_results > 0){ //it means there's already a database record
//creating our table heading
//loop to show each records
while( $row = $result->fetch_assoc() ){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
if (!isset($lastLetter) || $lastLetter != $row['letter'])
{
echo '<li id="', $row['letter'], '"><a name="', $row['letter'],'" class="title">', $row['letter'],'</a><ul>';
$lastLetter = $row['letter'];
echo "bottom";
}
echo "<li><a href='#'>{$surname} - {$name}</a></li>";
}
}else{
//if database table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?> </ul>
</li>
</ul>
</div>
</div>
</section>
</body>
</html>
i need to find where and how to echo these sections so it would be like this
:- UPDATE -:
with the reply i got itested it and it goes something like this
<body>
<section>
<div id="slider">
<div class="slider-content">
<ul>
<li id="E"><a name="E" class="title">E</a>
<ul>
bottom
<li><a href='#'>egg - smash</a></li>
<li id="S"><a name="S" class="title">S</a>
<ul>
bottom
<li><a href='#'>surname</a></li>
<li><a href='#'>surname</a></li>
<li><a href='#'>surname</a></li>
<li id="Z">
<a name="Z" class="title">Z</a>
<ul>
bottom
<li><a href='#'>zoo</a></li>
<!-- </ul> BAD UL ?-->
</li>
</ul>
</div>
</div>
</section>
</body>
</html>
when it should be
<section>
<div id="slider">
<div class="slider-content">
<ul>
<li id="s"><a name="s" class="title">s</a><ul>
<li>surname</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</section>
ALWAYS indent your code, AlWAYS run tests on variables, do not take steps skipping simple testing before like looping
<body>
<section>
<div id="slider">
<div class="slider-content">
<ul>
<?php
//include database connection
include 'db_connect.php';
//query all records from the database
$query = " SELECT name,
surname,
mobile,
UPPER (LEFT(surname, 1)) AS letter
FROM contacts
ORDER BY letter ASC";
//execute the query
$result = $mysqli->query( $query );
//TEST 1st print_r($result); if not working there is an error on your sql
//get number of rows returned
$num_results = $result->num_rows;
//this will link us to our add.php to create new record
if( $num_results > 0){ //it means there's already a database records
//creating our table heading
//loop to show each records
while( $row = $result->fetch_assoc() ){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
if (!isset($lastLetter) || $lastLetter != $row['letter'])
{
echo '<li id="', $row['letter'], '"><a name="', $row['letter'],'" class="title">', $row['letter'],'</a><ul>';
$lastLetter = $row['letter'];
echo "bottom";
}
echo "<li><a href='#'>{$surname} - {$name}</a></li>";
}
}else{
//if database table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
<!-- </ul> BAD UL ?-->
</li>
</ul>
</div>
</div>
</section>
</body>
First of all, I am new to mysqli and prepare statements so please let me know if you see any error. I have this static drop down menu :
HTML code:
<ul class="menu sgray fade" id="menu">
<li>Bike
<!-- start mega menu -->
<div class="cols3">
<div class="col1">
<ol>
<li>bikes</li>
<li>wheels</li>
<li>helmets</li>
<li>components</li>
</ol>
</div>
<div class="col1">
<ol>
<li>pedals</li>
<li>GPS</li>
<li>pumps</li>
<li>bike storage</li>
</ol>
</div>
<div class="col1">
<ol>
<li>power meters</li>
<li>hydratation system</li>
<li>shoes</li>
<li>saddles</li>
</ol>
</div>
</div>
<!-- end mega menu -->
</li>
I want to make a dynamic dropdown menu. I managed to show the $categoryName and the $SubCategoryName with this function:
function showMenuCategory(){
$db = db_connect();
$query = "SELECT * FROM Category";
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->bind_result($id,$categoryName,$description,$pic,$active);
while($stmt->fetch()) {
echo'<li>'.$categoryName.'
<!-- start mega menu -->
<div class="cols3">
<div class="col1">
<ol>';
$dba = db_connect();
$Subquery = "SELECT * FROM Subcategory WHERE CategoryId = '".$id."'";
$Substmt = $dba->prepare($Subquery);
$Substmt->execute();
$Substmt->bind_result($Subid,$CatId,$SubCategoryName,$SubDescription);
while($Substmt->fetch()) {
echo'
<li>'.$SubCategoryName.'</li>';
}
echo'
</ol>
</div>
<!-- end mega menu -->
</li>';
}
}
The only problem is that it returns all the subcategories on the the same <div class="col1">:
what I would like to obtain is count the subcategories and if the result is more than 4 return the other items in the second and third column.
UPDATE***: thanks to the answer below now the menu looks like this:
thanks!
How about try this?
To explain further
What is happening is that for every subcategory fetched, I increment a counter. If that counter hits 4, it ends the <UL> and <DIV> and creates a new one which will represent the new column.
function showMenuCategory(){
$db = db_connect();
$query = "SELECT * FROM Category";
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->bind_result($id,$categoryName,$description,$pic,$active);
while($stmt->fetch()) {
echo'<li>'.$categoryName.'
<!-- start mega menu -->
<div class="cols3">
<div class="col1">
<ol>';
$dba = db_connect();
$Subquery = "SELECT * FROM Subcategory WHERE CategoryId = '".$id."'";
$Substmt = $dba->prepare($Subquery);
$Substmt->execute();
$Substmt->bind_result($Subid,$CatId,$SubCategoryName,$SubDescription);
$count = 0;
while($Substmt->fetch()) {
echo'
<li>'.$SubCategoryName.'</li>';
$count+=1;
if ($count == 4) {
$count = 0;
echo '</ol></div><div class="col1"><ol>';
}
}
echo'
</ol>
</div>
<!-- end mega menu -->
</li>';
}
}
EDIT: Misunderstood the purpose of col1. They all should be col1 and should work now. If not, leave me a comment!
Try this:
function showMenuCategory(){
$db = db_connect();
$query = "SELECT * FROM Category";
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->bind_result($id,$categoryName,$description,$pic,$active);
echo '<div class="cols3">';
while($stmt->fetch()) {
echo'<li>'.$categoryName.'
<!-- start mega menu -->
<div class="col1">
<ol>';
$dba = db_connect();
$Subquery = "SELECT * FROM Subcategory WHERE CategoryId = '".$id."'";
$Substmt = $dba->prepare($Subquery);
$Substmt->execute();
$Substmt->bind_result($Subid,$CatId,$SubCategoryName,$SubDescription);
while($Substmt->fetch()) {
echo'<li>'.$SubCategoryName.'</li>';
}
echo'</ol>';
}
echo '</div><!-- end mega menu --></li>';
}