Checking in_array value - php

The Image above is what I want to achieve, the only problem is am echoing subjects and topics where the topics go under the subjects, but I want to only echo say English and all topics under English will go under English. But what I have is every topic comes with its own Subjects. Thus from the Image Two English Language dropdowns.
Here is the code.
<?php
$sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE class = 'JHS 2'";
$result = $pdo->query($sql);
$result->setFetchMode(PDO::FETCH_ASSOC);
while($row=$result->fetch()){
if(!in_array('subject', $row['subject']){
?>
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i><?php echo $row['subject']?><i class="fa fa-angle-down rotate-icon"></i></a>
<div class="collapsible-body">
<ul class="list-unstyled">
<li><a data-id="<?php echo $row['note_id']?>" href="<?php echo $row['note_id']?>" class="waves-effect" id="getUser"><?php echo $row['topic']?></a>
</li>
</ul>
</div>
</li>
<?php
} else {
echo ""
}
}
?>

This should output the subjects and topics and allows for multiple subjects per topic. Also, the echo "" in the else is un-needed.
<?php
$sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE class = 'JHS 2' ORDER BY subject, topic";
$result = $pdo->query($sql);
$result->setFetchMode(PDO::FETCH_ASSOC);
$lastSubject = '';
while($row=$result->fetch()){
if(!in_array('subject', $row['subject']) {
if($lastSubject != $row['subject']) {
?>
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i><?php echo $row['subject']?><i class="fa fa-angle-down rotate-icon"></i></a>
<div class="collapsible-body">
<?php
}
?>
<ul class="list-unstyled">
<li><a data-id="<?php echo $row['note_id']?>" href="<?php echo $row['note_id']?>" class="waves-effect" id="getUser"><?php echo $row['topic']?></a>
</li>
</ul>
<?php
if($lastSubject != $row['subject']) {
?>
</div>
<?php
$lastSubject = $row['subject'];
}
?>
</li>
<?php
}
}
?>

Related

How to set first menu active in mega menu?

I am trying to create a hoverable mega menu, all works fine except the active one.
I checked in source codes and active class is added, but it doesn't show up the content until mouseenter on it.
see image please :
And this is when I go on it with mouse, it show up with no problem.
I gues I need to set main (FINANCE is main menu in pic) menu active, but dont know how to pass it to submenu.
Here is my full code :
$stmt_menu = $pdo->prepare("SELECT * FROM categories WHERE parentId = ? LIMIT 5");
$stmt_menu->execute([0]);
if ($stmt_menu->rowCount() > 0) {
$query = $stmt_menu->fetchAll();
foreach ($query as $row) {
if ($row['cat_type'] == 'mega') {
?>
<li class="dropdown megamenu-fw mega-li-<?php echo $row['catId']; ?>">
<?php echo $row['catName']; ?><span class="caret"></span>
<ul class="dropdown-menu megamenu-content dropdown-top" role="menu" aria-expanded="true" data-mega-ul="<?php echo $row['catId']; ?>">
<li>
<?php
$stmt_menu = $pdo->prepare("SELECT * FROM categories WHERE parentId = ? LIMIT 5");
$stmt_menu->execute([$row['catId']]);
if ($stmt_menu->rowCount() > 0) {
$subquery = $stmt_menu->fetchAll();
?>
<div class="sub-menu-left">
<ul class="nav-sub-categories">
<?php
$i = 0;
foreach ($subquery as $subq) {
$actives = '';
if($i == 0){
$actives = ' active';
}
?>
<li data-category-filter="<?php echo $subq['cat_seo_url']; ?>-<?php echo $subq['catId']; ?>" class="li-sub-category<?php echo $actives; ?>"><?php echo $subq['catName']; ?></li>
<?php
$i++;
}
?>
</ul>
</div>
<div class="sub-menu-right">
<?php
$i = 0;
foreach ($subquery as $subcats) {
$actives = '';
if($i == 0){
$actives = ' active';
}
?>
<div class="sub-menu-inner filter-<?php echo $subcats['cat_seo_url']; ?>-<?php echo $subcats['catId']; ?><?php echo $actives; ?>">
<div class="row row-menu-right">
<div class="col-sm-3 menu-post-item">
<div class="post-item-image">
<a href="is-allowance-instantly-strangers-applauded">
<img src="assets/img/img_bg_md.png" data-src="uploads/images/202203/image_380x226_624239afd7295.jpg" alt="Is allowance instantly strangers applauded" class="lazyload img-responsive img-post" width="1" height="1"/>
</a>
</div>
<h3 class="title"><?php echo $subcats['cat_seo_url']; ?> Is allowance instantly strangers applauded</h3>
<p class="post-meta">
admin
<span>Mar 28, 2022</span>
<span><i class="icon-comment"></i>2</span>
<span class="m-r-0"><i class="icon-eye"></i>894</span>
</p>
</div>
</div>
</div>
<?php
$i++;
}
?>
</div>
<?php
}
?>
</li>
</ul>
</li>
<?php
} elseif ($row['cat_type'] == 'dropdown') {
echo ' <li class="nav-item"><a class="nav-link" href="' . $row["cat_seo_url"] . '">test</a></li>';
} else {
echo ' <li class="nav-item"><a class="nav-link" href="' . $row["cat_seo_url"] . '">' . $row["catName"] . '</a></li>';
}
}
}

Amateur trying to fix dropdown menu from SQL

I'm trying to help a friend with a website. I usually don't work with PHP, jquery.
The dropdown has 4 levels. The first level has 4 points. The 4 points have their own sub-levels, different for each other.
I'm trying to find them and then make them display in a dropdown, directly from the database.
And I'm stuck at the second level, with this error
Notice: Trying to get property 'subcategorie' of non-object in .....
What I've managed to do, until now:
<?php $categorii = $wpdb->get_results("SELECT DISTINCT categorie FROM catalog_rural");
?>
<?php
$subcategorie = $wpdb->get_results("SELECT DISTINCT subcategorie FROM catalog_rural WHERE categorie = 'Afaceri'");
?>
<div class="container">
<?php
foreach($categorii as $categorie) {
?>
<ul>
<li>
<a <?php if(isset($_GET['categorie']) && $categorie->categorie==$_GET['categorie']){ echo "btn-success";}else{ echo "btn-info";};?> href="<?php site_url(); ?>catalog-rural/?categorie=<?php echo urlencode($categorie->categorie); ?>"> <i class="fa fa-caret-down"></i>
<?php echo $categorie->categorie ; ?> </a>
<ul class="">
<li> <?php echo $subcategorie->subcategorie; ?>
</li>
</ul>
</li>
</ul>
</a>
Hi You need to do a foreach loop for $subcategorieas you did for $categorii. Because you are getting an array and not a object as you got for $categorii. Here I set it around the li because it seems to be the best.
<?php
$categorii = $wpdb->get_results("SELECT DISTINCT categorie FROM catalog_rural");
?>
<?php
$subcategories = $wpdb->get_results("SELECT DISTINCT subcategorie FROM catalog_rural WHERE categorie = 'Afaceri'");
?>
<div class="container">
<?php
foreach( $categorii as $categorie ) {
?>
<ul>
<li>
<a
<?php
#### What are these? Are they classnames? ####
if( isset( $_GET['categorie'] ) && $categorie->categorie==$_GET['categorie'] ){
echo "btn-success";
}else{
echo "btn-info";
};
?> href="<?php site_url(); ?>catalog-rural/?categorie=<?php echo urlencode( $categorie->categorie ); ?>">
<i class="fa fa-caret-down"></i>
<?php echo $categorie->categorie; ?>
</a>
<ul class="">
<?php foreach( $subcategories as $subcategorie ) { ?>
<li>
<a href="<?php echo site_url(); ?>/catalog-rural/?categorie=<?php echo urlencode( $_GET['categorie'] ); ?>&subcategorie=<?php echo $subcategorie->subcategorie; ?>" class="list-group-item <?php if($subcategorie->subcategorie==$_GET['subcategorie']){ echo "active";};?> ">
<?php echo $subcategorie->subcategorie; ?>
</a>
</li>
<?php } ?>
</ul>
</li>
</ul>
Not intended as a solution to your error " Trying to get property 'subcategorie' of non-object" ~ just to illustrate the invalid markup. If you use proper code indentation you would probably have been able to spot this!
<?php
$categorii = $wpdb->get_results("SELECT DISTINCT categorie FROM catalog_rural");
?>
<?php
$subcategorie = $wpdb->get_results("SELECT DISTINCT subcategorie FROM catalog_rural WHERE categorie = 'Afaceri'");
?>
<div class="container">
<?php
foreach( $categorii as $categorie ) {
?>
<ul>
<li>
<a
<?php
#### What are these? Are they classnames? ####
if( isset( $_GET['categorie'] ) && $categorie->categorie==$_GET['categorie'] ){
echo "btn-success";
}else{
echo "btn-info";
};
?> href="<?php site_url(); ?>catalog-rural/?categorie=<?php echo urlencode( $categorie->categorie ); ?>">
<i class="fa fa-caret-down"></i>
<?php echo $categorie->categorie; ?>
</a>
<ul class="">
<!-- ### Where is the opening LI? ### -->
<a href="<?php echo site_url(); ?>/catalog-rural/?categorie=<?php echo urlencode( $_GET['categorie'] ); ?>&subcategorie=<?php echo $subcategorie->subcategorie; ?>" class="list-group-item <?php if($subcategorie->subcategorie==$_GET['subcategorie']){ echo "active";};?> ">
<?php echo $subcategorie->subcategorie; ?>
</a>
</li>
</ul>
</li>
</ul>
</a><!-- ### what is this closing? ### -->

how to change the way the url looks but keep the function

How do I change my URL's function for calling up on pages?
Here's an example: https://exaplesitename.com/index.php?id=21
What it looks like now but I want it to be something like this: https://exaplesitename.com/Entertainment
This is the navigation code:
<?php
function find_parent_categories($options=[]) {
global $db;
$Active = $options['Active'] ?? 'false';
$sql = "SELECT * FROM Parent_Categories ";
if($Active){
$sql .= "WHERE Active = true ";
}
$sql .= "ORDER BY Category_Order ASC";
$result = mysqli_query($db, $sql);
confirm_result_set($result);
return $result;
}
function find_categories($id_Category_Parent, $options=[]){
global $db;
$Active = $options['Active'] ?? 'false';
$sql = "SELECT * FROM Categories ";
$sql .= "WHERE id_Category_Parent='" . db_escape($db, $id_Category_Parent) . "' ";
if($Active){
$sql .= "AND Active = true ";
}
$sql .= "ORDER BY Category_Order ASC";
$result = mysqli_query($db, $sql);
confirm_result_set($result);
return $result;
} ?>
<nav class="navbar fixed-top navbar-expand-lg bg-white navbar-custom">
<a class="navbar-brand" href="<?php echo url_for('/index.php'); ?>">
<img src="../../images/Logo1.png" alt="Nantucket" class="img-fixed"/>
</a>
<button class="navbar-toggler custom-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent1" aria-controls="navbarSupportedContent1" aria-expanded="false" aria-label="Toggle navigation">Menu
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent1">
<?php $nav_categories = find_parent_categories(['Active' => true]); ?>
<ul class="navbar-nav mr-auto">
<?php while($nav_category = mysqli_fetch_assoc($nav_categories)) { ?>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="<?php echo url_for('index.php?id=' . h(u($nav_category['id']))); ?>" id="navbarDropdown1" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><?php echo h($nav_category['Category_Name']); ?></a>
<?php $nav_sub_categories = find_categories($nav_category[id], ['Active' => true]); ?>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown1">
<?php while($nav_sub_category = mysqli_fetch_assoc($nav_sub_categories)) { ?>
<li>
<a class="dropdown-item" href="<?php echo url_for('index.php?id=' . h(u($nav_sub_category['id']))); ?>"> <?php echo h($nav_sub_category['Category_Name']); ?></a>
</li>
<?php } //while $nav_sub_categories ?>
</ul>
<?php mysqli_free_result($nav_sub_categories); ?>
<?php mysqli_free_result($nav_categories); ?>
</li>
<?php } ?>
</ul>
</div>
</nav>
Thank you very much for any help and kindness.
Any change in url can be achieved by changing .htaccess file.
RewriteEngine On
RewriteRule https://exaplesitename.com/Entertainment https://exaplesitename.com/index.php?id=21
For more information about .htaccess please refer to:
https://www.smashingmagazine.com/2011/11/introduction-to-url-rewriting/

Nidified mysqli_fetch_assoc while

I have a problem with the following code:
$query = "SELECT * FROM movie_list WHERE id=$id";
$result_q = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result_q);
if ($row['movie_type'] == 'E' || $row['movie_type'] == 'S'){
$query_serie = "SELECT * FROM movie_list WHERE id_serie=$row[id_serie]";
$result_q_serie = mysqli_query($conn, $query_serie);
$query_serie_cont = "SELECT serie_number FROM movie_list WHERE id_serie = $row[id_serie] AND serie_number IS NOT null GROUP BY serie_number";
$result_q_serie_cont = mysqli_query($conn, $query_serie_cont);
[...]
<?php
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?></a>
<div class="collapsible-body">
<ul>
<?php
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>".$row_serie['episode_number']."</li>";
}
}
echo "</ul>";
echo "</div>";
}
?>
</li>
It works perfect the first while but the second time the while($row_serie) variable are missing. Debugging the page I see the $row_serie variable disappear after completed all the first while($row_serie_cont) but not reappearing at all when it was triggered the second time.
What I've miss in the code?
you close li after loop make it correct
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?></a>
<div class="collapsible-body">
<ul>
<?php
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>".$row_serie['episode_number']."</li>";
}
}
echo "</ul>";
echo "</div>";
echo "</li>"// inside the loop
}
?>
Seems that insert $result_q_serie = mysqli_query($conn, $query_serie);into the second while do the trick.
I don't understand why i need to repopulate result_q_serie is needed but now it works.
If someone can propose a better solution I'm hearing
<?php
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li class="no-padding">
<ul class="collapsible collapsible-accordion">
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?>
</a>
<div class="collapsible-body">
<ul>
<?php
$result_q_serie = mysqli_query($conn, $query_serie);
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>
".$row_serie['episode_number']."</li>
"; } } echo "
</ul>
"; echo "
</div>
</li>
</ul>
</li>
"; } ?>
</ul>
seems that use mysqli_data_seek($result, 0); do the trick the right way.

CSS Dropdown menu through PHP, logic defeats me

My brain doesn't seem to want to work this morning.. i have the following array:
private $nav_pages = [['0', 'Home', 'home'],
['0', 'About Us', 'about'],
['0', 'Our Services', 'services'],
['1', 'PROCURE TO PAY (P2P)', 'procure'],
['1', 'ORDER TO CASH (02C)', 'cash'],
['1', 'GENERAL ACCOUNTING', 'general'],
['2', 'Charting of Accounts', 'charting'],
['2', 'General Ledger Maintenance', 'maintenance'],
['2', 'Accounts Receivable Services', 'receivable'],
['2', 'Accounts Payable Services', 'payable'],
['2', 'Reconciliation of Bank Accounts and Credit Cards', 'reconciliation'],
['2', 'Preparing Financial Statements', 'statements'],
['2', 'Payroll Processing', 'payroll'],
['1', 'CLOSING & REPORTING', 'closing'],
['0', 'How It Works', 'how-it-works'],
['0', 'Contact Us','contact'],
['0', 'Careers', 'careers']];
This is then fed to a php page which is meant to lay out a multi-layered pure css drop down menu.. the php page code is:
<ul class="<?php echo $ul_class; ?>">
<?php $this_layer = 0;
// place array content into variables
foreach ($links as $link) {
$item_layer = $link[0];
$item_string = $link[1];
$item_link = $link[2];
if ($item_layer > $this_layer) { ?>
<ul>
<?php $this_layer++; }
elseif ($item_layer == $this_layer) { ?>
<li class="<?php echo $item_link; ?>">
<a class="<?php echo $item_link; ?>"
href="/<?php echo $item_link; ?>">
<?php echo $item_string; ?>
</a>
</li>
<?php }
elseif ($item_layer < $this_layer) { ?>
</li></ul>
<?php $this_layer--; } ?>
<?php } ?>
The output is not correct however as the above code always closes the list item containing the second layer before the second layer is ready to be closed. if that makes sense..
<ul class="pages_nav">
<li class="home">
<a class="home"
href="/home">
Home
</a>
</li>
<li class="about">
<a class="about"
href="/about">
About Us
</a>
</li>
<li class="services">
<a class="services"
href="/services">
Our Services
</a>
</li>
<ul>
<li class="cash">
<a class="cash"
href="/cash">
ORDER TO CASH (02C)
</a>
</li>
<li class="general">
<a class="general"
href="/general">
GENERAL ACCOUNTING
</a>
</li>
<ul>
<li class="maintenance">
<a class="maintenance"
href="/maintenance">
General Ledger Maintenance
</a>
</li>
<li class="receivable">
<a class="receivable"
href="/receivable">
Accounts Receivable Services
</a>
</li>
<li class="payable">
<a class="payable"
href="/payable">
Accounts Payable Services
</a>
</li>
<li class="reconciliation">
<a class="reconciliation"
href="/reconciliation">
Reconciliation of Bank Accounts and Credit Cards
</a>
</li>
<li class="statements">
<a class="statements"
href="/statements">
Preparing Financial Statements
</a>
</li>
<li class="payroll">
<a class="payroll"
href="/payroll">
Payroll Processing
</a>
</li>
</li></ul>
</li></ul>
<li class="contact">
<a class="contact"
href="/contact">
Contact Us
</a>
</li>
<li class="careers">
<a class="careers"
href="/careers">
Careers
</a>
</li>
SOLUTION:
<ul class="<?php echo $ul_class; ?>">
<?php $this_layer = 0;
// place array content into variables
foreach ($links as $link) {
$item_layer = $link[0];
$item_string = $link[1];
$item_link = $link[2];
// check if link needs to be manipulated
switch($do) {
case 'strtolower':
$item_string = strtolower($item_string);
break;
case 'strtoupper':
$item_string = strtoupper($item_string);
break;
} ?>
<?php if ($item_layer > $this_layer) { ?>
<ul>
<li class="<?php echo $item_link; ?>">
<a class="<?php echo $item_link; ?>"
href="/<?php echo $item_link; ?>">
<?php echo $item_string; ?>
</a>
<?php $this_layer++; }
elseif ($item_layer == $this_layer) { ?>
</li><li class="<?php echo $item_link; ?>">
<a class="<?php echo $item_link; ?>"
href="/<?php echo $item_link; ?>">
<?php echo $item_string; ?>
</a>
<?php }
elseif ($item_layer < $this_layer) { ?>
</li></ul></li><li class="<?php echo $item_link; ?>">
<a class="<?php echo $item_link; ?>"
href="/<?php echo $item_link; ?>">
<?php echo $item_string; ?>
</a>
<?php $this_layer--; } ?>
<?php } ?>
Never close the the li tag, close it when you add something
foreach ($links as $link) {
if ($item_layer > $this_layer) {
echo '<ul><li> ......';
this_layer++;
}elseif ($item_layer == $this_layer) {
echo '</li><li>......';
}elseif ($item_layer < $this_layer) {
echo '</li></ul><li>......';
$this_layer--;
}
}
Last you probably need to add a echo '</li></ul>' outside of the foreach to close everything
Had to rework the code a little, usually i organize the php menu structure in tree form makes it easier to parse into html menus, but here it is:
<ul class="<?php echo $ul_class; ?>">
<?php
$this_layer = 0;
$closeit = false;
// place array content into variables
foreach ($nav_pages as $link) {
$item_layer = $link[0];
$item_string = $link[1];
$item_link = $link[2];
if ($item_layer > $this_layer) {
// Changing level, dont close the previous li
?><ul><?php
$closeit = false;
$this_layer++;
}
if ($item_layer == $this_layer) {
// Same level, check if you need to close previous li
if ($closeit) {
?></li><?php
}
// Render the li
?>
<li class="<?php echo $item_link; ?>">
<a class="<?php echo $item_link; ?>"
href="/<?php echo $item_link; ?>">
<?php echo $item_string; ?>
</a>
<?php
// Close it on next loop
$closeit = true;
}
elseif ($item_layer < $this_layer) {
// Changing layer back down, close the previous li and the current level ul
?>
</li>
</ul>
<?php
$this_layer--;
}
}
// Finished loop, there should still be a li waiting to be closed
if ($closeit) {
?></li><?php
}
// Close menu
?>
</ul>
Should be working, let me know if it isnt
Suggested php menu structure:
$menu = array(
array(
"url"=>"/place.php",
"text"=>"Go to place"
),
array(
"url"=>"/place2.php", // not necessary
"text"=>"with submenu",
"children"=>array(
array(
"url"=>"/placewithinplace2.php",
"text"=>"submenu item"
)
)
)
);

Categories