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>';
}
Related
I need to display datas from particular based on the results.
I have two tables movies_info => has all movies info & tv_shows_info => has TV shows info. I have search box if i enter the search it should search from both the table and display the datas but the condition in the results if it has movie_id, it should show movie details and if the result has tv_show_id it should show tv-show details.
as of now i have used basic search to fetch datas from one table and i have tried of using two but its not working.
<?php
// sql query for retrieving data from database
$sql_query = "SELECT * FROM `movies_info`";
$result = mysqli_query($connection, $sql_query);
// SQL Query for filter
if(isset($_POST['search_button']))
{
$value_to_search = $_POST['value_to_search'];
// search in all table columns
// using concat mysql function
$search_query = "SELECT * FROM `movies_info` WHERE CONCAT(`movie_name`, `movie_original_name`, `release_year`, `movie_genre`, `movie_country`, `movie_stars`, `movie_director`) LIKE '%".$value_to_search."%'";
//$search_query = "SELECT * FROM `movies_info`,`tv_shows_info` WHERE CONCAT(`movie_name`, `release_year`, `movie_genre`, `movie_country`, `tv_show_name`) LIKE '%".$value_to_search."%' GROUP BY `movie_id`";
//$search_query = "SELECT * FROM `movies_info` UNION ALL `tv_shows_info` WHERE CONCAT(`movie_name`, `release_year`, `movie_genre`, `movie_country`, `tv_show_name`) LIKE '%".$value_to_search."%'";
//$search_query2 = "SELECT * FROM `tv_shows_info` WHERE CONCAT(`tv_show_name`, `tv_show_start_year`, `tv_show_end_year`, `tv_show_genre`, `tv_show_country`) LIKE '%".$value_to_search."%'";
//$search_query .= $search_query2;
$search_result = filterTable($search_query);
}
else {
$search_query = "SELECT * FROM `movies_info`";
$search_result = filterTable($search_query);
//echo 'No Search Results Found';
}
?>
<!-- /w3l-medile-movies-grids -->
<div class="general-agileits-w3l">
<div class="w3l-medile-movies-grids">
<!-- /movie-browse-agile -->
<div class="movie-browse-agile">
<!--/browse-agile-w3ls -->
<div class="browse-agile-w3ls general-w3ls">
<div class="tittle-head">
<h4 class="latest-text">Search Results for : "<?php echo $value_to_search ?>"</h4>
<div class="container">
<div class="agileits-single-top">
<ol class="breadcrumb">
<li>Home</li>
<li class="active" style="text-transform:Capitalize;">Search Results </li>
</ol>
</div>
</div>
</div>
<div class="container">
<div class="browse-inner">
<?php
echo $search_result;
$rowcount = mysqli_num_rows($search_result);
for($i=1;$i<=$rowcount;$i++){
$row=mysqli_fetch_array($search_result);
?>
<div class="col-md-2 w3l-movie-gride-agile">
<a href="movie.php?movie_id=<?php echo $row['movie_id']; ?>" class="hvr-shutter-out-horizontal"><img src="<?php echo $row['movie_image']; ?>" title="<?php echo $row['movie_name']; ?>" alt=" " />
<div class="w3l-action-icon"><i class="fa fa-play-circle" aria-hidden="true"></i></div>
</a>
<div class="mid-1">
<div class="w3l-movie-text">
<h6><?php echo $row['movie_name']; ?></h6>
</div>
<div class="mid-2">
<p><?php echo $row['release_year']; ?></p>
<div class="block-stars">
<ul class="w3l-ratings">
<li>
<span>IMDB <i class="fa fa-star" aria-hidden="true"></i> <?php echo $row['movie_rating']; ?> </span>
</li>
</ul>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="ribben two">
<p>NEW</p>
</div>
</div> <?php } ?>
</div>
as of now i can get the values from one table.,
My exact need is it can be Movie or TV-Show but i should get the datas from both the table if it is a movie it should show some particular info and if thats a TV-show it should show someother info.
First, you need to use prepared statements which you can use even if you are using the LIKE function in MySQL. Then you need to add spaces between the column names to prevent the values from being blended together like "star warsstar wars". This will cause a movie like "star wars to be a result when a user searches for "ss" which would be inaccurate.
$search_result = array();
$search_query = "SELECT * FROM `movies_info` WHERE CONCAT(`movie_name`,' ', `movie_original_name`,' ',`release_year`,' ',`movie_genre`,' ',`movie_country`,' ',`movie_stars`,' ',`movie_director`) LIKE CONCAT('%',?,'%')";
if($stmt = $db->prepare($search_query)){
$stmt->bind_param('ii',$_SESSION['iidn_reference'],$o['parent_ref_id']);
if($stmt and $stmt->execute()){
$result = $stmt->get_result();
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$search_result[] = $row;
}
}
}
After that, you could combine these tables with MySQL UNION but I strongly recommend just searching both tables and aliasing the table names to match.
SELECT 'movie' as `type`, `movie_id` as `id`, `movie_name` as `name`...
SELECT 'show' as `type`, `show_id` as `id`, `show_name` as `name`...
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'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();
});
});
I would understand if there is a way to store the variables out of the while loop.
For example, I have a slide show that shows the images for each category present and a button to see the images gallery related to the category. The button, for graphical issues, should stay out of the CSS class that contains the while.
This is the current code:
<div class="carousel-inner"> <!-- start carousel -->
$macrocat = "SELECT * FROM tbl_category WHERE cat_parent_id = 0";
$result = dbQuery($macrocat);
while($row = dbFetchAssoc($result)) {
extract($row);
echo $cat_name;
echo $cat_description;
}
</div> <!-- end carousel -->
and the class out of the while where I have to get cat_id:
<div class="social-caption">
<a class="btn btn-large btn-primary" href="categorie.php?id=<?php echo $cat_id; ?>">Gallery</a>
</div>
How can I fix the problem?
Thanks!
Store the categories first:
<div class="carousel-inner"> <!-- start carousel -->
$macrocat = "SELECT * FROM tbl_category WHERE cat_parent_id = 0";
$cats = array[];
$result = dbQuery($macrocat);
while($row = dbFetchAssoc($result)) {
extract($row);
echo $cat_name;
echo $cat_description;
$cats[] = $cat_id;
}
</div> <!-- end carousel -->
Then later you can use them:
foreach($cats as $cat)
{
echo "<div>".$cat."</div>";
}
i want to fetch out the 4 different id's rows from the database. and display in four divs. each div contains a character base unique id. i am doing this to write same query again and again. i want to do with less query.
my code is given below.
<?php
$query = mysql_query("select * from acms_pages where id = 40");
$sql = mysql_fetch_array($query);
?>
<li id="pgAbout">
<section class="content">
<div class="one_half_last"><?php echo $sql['text']; ?></div>
</section>
</li>
<section class="content">
<?php
$query = mysql_query("select * from acms_pages where id = 41");
$sql1 = mysql_fetch_array($query);
?>
<li id="pgPortfolio">
<div class="fullwidth"><?php echo $sql1['text']; ?></div>
</section>
</li>
<?php
$query = mysql_query("select * from acms_pages where id = 42");
$sql2 = mysql_fetch_array($query);
?>
<li id="pgServices">
<section class="content">
<div class="one_half_last"><?php echo $sql2['text']; ?></div>
</section>
</li>
<?php
$query = mysql_query("select * from acms_pages where id = 43");
$sql3 = mysql_fetch_array($query);
?>
<li id="pgPrices">
<section class="content">
<div class="one_half"><?php echo $sql3['text']; ?></div>
</section>
</li>
This probably belongs on programmers.stackexchange.com, but the general approach would be to rewrite your query as a single query, with the where clause including all four conditions OR'd together.
Then you can access each row independently and render the divs.
To answer your question (I think...) you can just use OR in your query:
mysql_query("select * from acms_pages where id = 40 OR id = 41 OR id = 42");
However, you should be using the mysqli instead:
$link = mysqli_connect("localhost", "my_user", "my_password", "mydb");
if ($result = mysqli_query($link, "select * from acms_pages where id = 40 OR id = 41 OR id = 42"))
{
printf("Select returned %d rows.\n", mysqli_num_rows($result));
while ($row = mysqli_fetch_row($result)) {
printf ("%s", $row[0]); //first column value here
}
/* free result set */
mysqli_free_result($result);
}