I got a simple if else statement where I show a list of pdf files, when the id is empty (aka there is no pdf file) I want to show: 'No available downloads'. But it shows that text no matter what, even if there are pdf files present.
My code:
<div class="widget broucher">
<h4>DOWNLOADS</h4>
<ul>
<?
//pdf bestanden
$pdf = "SELECT * FROM `snm_attachments` WHERE parent_id = '".$conn->real_escape_string($contentcr[0]['id'])."'";
$pdfcon = $conn->query($pdf);
$pdfcr = array();
while ($pdfcr[] = $pdfcon->fetch_array());
foreach($pdfcr as $pdf){
if($pdf['id'] != ''){
$downloads .= '<li><i class="fa fa-file-pdf-o"></i>'.$pdf['filename'].'</li>';
}else{
$downloads .= '<li>No available downloads</li>';
}
}
echo $downloads;
?>
</ul>
</div>
Why is it always showing, even when there is no id present for a row?
Use this code:
<?
//pdf bestanden
$pdf = "SELECT * FROM `snm_attachments` WHERE parent_id = '".$conn->real_escape_string($contentcr[0]['id'])."'";
$pdfcon = $conn->query($pdf);
// initialize downloads variable:
$downloads = '';
// see how while statement disappears (in your code you have the fetch_array attached to the first element of the array):
$pdfcr = $pdfcon->fetch_array();
foreach($pdfcr as $pdf){
if($pdf['id'] != ''){
$downloads .= '<li><i class="fa fa-file-pdf-o"></i>'.$pdf['filename'].'</li>';
}else{
$downloads .= '<li>No available downloads</li>';
}
}
echo $downloads;
?>
Note than the No available downloads message will be rendered in all iterations of the data array when id is different than empty. To show the message when there are no records use this code:
if(count($pdfcr) > 0) {
foreach($pdfcr as $pdf){
if($pdf['id'] != ''){
$downloads .= '<li><i class="fa fa-file-pdf-o"></i>'.$pdf['filename'].'</li>';
}
}
}
Related
I have created one shortcode to return a tab content and it is working fine
if I am using one time in a page.But if want one more on this same page it is not working. I have tried in several way but its not giving fruits.
Here is the code
if($tab_box == 'tab_box_1' && $tabs_lay == 'awavc-tabs-pos-left') {
$add_class = rand(99,9999);
$q = rand(99,99999);
$html .= '
<div class="awavc-tabs awavc-tabs-'.$add_class.' '.$tabs_lay.' '.$tab_style.' awavc-tabs-response-to-icons '.$el_class.' ">';
$i = 1;
foreach($tab_contents as $tab_content){
$tab_lable = 'Lable';
$tab_icon = '';
if(!empty($tab_content['tab_lbl'])){$tab_lable = $tab_content['tab_lbl'];}
if(!empty($tab_content['icon'])){$tab_icon = $tab_content['icon'];}
$html .= ' <input type="radio" name="awavc-tabs" checked id="awavc-tab-'.$i.'" class="awavc-tab-content-'.$i.'">
<label for="awavc-tab-'.$i.'"><span><span style="font-size:'.$lable_size.'px;color:'.$lable_clr.';"><i class="'.$tab_icon.'" style="font-size:'.$lable_size.'px;color:'.$icon_clr.';"></i>'.$tab_lable.'</span></span></label>';
$i++;
}
$html .= '
<ul>';
$i = 1;
foreach($tab_contents as $tab_content){
$tab_title = 'Title';
$content = '';
if(!empty($tab_content['title'])){$tab_title = $tab_content['title'];}
if(!empty($tab_content['content'])){$content = $tab_content['content'];}
$html .= '<li class="awavc-tab-content-'.$i.'">
<div class="typography">';
if(!empty($tab_title)){ $html .= '<h1 style="font-size:'.$ttl_size.'px;color:'.$ttl_clr.';">'.$tab_title.'</h1>';}
$html .= '
<p style="font-size:'.$content_size.'px;color:'.$content_clr.';font-style:'.$content_style.';">'.$content.'</p>
</div>
</li>';
$i++;
}
I have tried something like $i.$add_class but...
If you want to use the shortcode multiple times on the same page do it like so:
add_shortcode("fmg_shortcode", "fmg_callback");
function fmg_callback($args) {
ob_start();
$html = "";
//your code here
echo $html;
return ob_get_clean();
}
I got a script that shows products according to their id. In the echoed data there is a link which redirects the user to the same page but with a posted id, according to that id I delete a product (id) from my session which is an array. This works perfectly except for the first array value. What could be causing that?
How I have my code at the moment:
//Link to quote page and send product id with it
echo '<p><a class="offertelink" href="offerte.php?product='.$productcr[0]['id'].'">Request a quote</a></p>';
On my quote page:
ob_start();
include 'includes/header.php';
if(!isset($_SESSION['product'])){
$_SESSION['product'] = array();
}
// Check if product is set before putting it in an array
if(isset($_GET['product']) && !in_array($_GET['product'], $_SESSION['product'])){
$_SESSION['product'][] = $_GET['product'];
}
// Implode array to use ids in query
$prods = implode(",", $_SESSION['product']);
Query:
if(count($_SESSION['product']) != 0){
// Get all products with id in session
$offerte = "SELECT * FROM `web_content` WHERE `id` in (".$conn->real_escape_string($prods).") AND state = 1";
$offertecon = $conn->query($offerte);
$offertecr = array();
while ($offertecr[] = $offertecon->fetch_array());
}
And finally below my query:
// Check if id is send, and delete from session accordingly
if(isset($_GET['id'])){
$productID = $_GET['id'];
if( $index = array_search($productID, $_SESSION['product']) ) {
$_SESSION['product'][$index] = null;
unset($_SESSION['product'][$index]);
header('Location: http://www.web.nl/_extern/web/offerte.php');
}
}
if(count($_SESSION['product']) == 0){
echo 'No products added';
}else{
foreach($offertecr as $product){
$offerte_imgs = $product['images']; // Get image parameters of the article
$offertepic = json_decode($offerte_imgs); // Split the parameters apart
if($offertepic->{'image_intro'} != ''){
$image = 'cms/'.$offertepic->{'image_intro'};
}else{
$image = 'http://www.web.nl/_extern/web/cms/images/Producten/Untitled-7.jpg';
}
if($product['id'] != ''){
if (strlen($product['introtext']) > 100){
$shortcat = substr($product['introtext'], 0, 100) . '...';
}else{
$shortcat = $product['introtext'];
}
$deleteLink = "offerte.php?id=".$product['id'];
$offerteoverzicht .= '
<div class="row productofferte">
<a href="producten/'.$product['alias'].'.html">
<div class="col-md-6 offerteimg">
<img style="border:1px solid #ddd;" src="'.$image.'">
</div>
</a>
<div class="desc">
<a style="color:#2d4160;" href="producten/'.$product['alias'].'.html"><p style="font-weight:bold;">'.$product['title'].' </a>
<a href="'.$deleteLink.'">
<i class="fa fa-times" aria-hidden="true"></i>
</a>
</p>
<p>'.$shortcat.'</p>
</div>
</div>';
}
}
}
echo $offerteoverzicht;
I've read a bit on my problem and figured it has something to do with shifting the array if I'm correct. If I print my session all the arrays are deleted from it, except the first one. Doesn't matter what id it is, when I change the first product then that product cannot be deleted from the session.
Your test on finding an element in an array with array_search should compare to false. Doing ! will return false when the element found is at index 0:
Change:
if( $index = array_search($productID, $_SESSION['product']) ) {
to:
if( ($index = array_search($productID, $_SESSION['product'])) !== false ) {
In working on a dynamic menu w/ CRUD I have been having some trouble with the finishing touches. It works, but there are some extra tags getting inserted where there shouldn't be and can't figure out how to clean it up before I share it with the world. I used this as a starting point, then changed it to work with an accordion menu (http://www.phpro.org/tutorials/Simple-Mysql-PHP-Menu.html).
Below is the data from the table (I changed the names on the first 2 fields to get it to fit in the SO format from menu_item_id to id, and from menu_parent_id to pid).
id pid menu_item_name menu_url sortorder status
1 0 Settings 0 ACTIVE
2 5 Grid Demo grid.php ACTIVE
3 5 setGridOptions gridoptions.php ACTIVE
4 1 Menu Items adminmenu.php 1 ACTIVE
5 0 Grid Settings 100 ACTIVE
6 1 General Settings settings.php 100 ACTIVE
Here is the PHP that connects to the mysql database and creates the hierarchical tree array to make it work:
include 'db.php';
$sql = "SELECT * FROM menu_items WHERE status = \"ACTIVE\" ORDER BY sortorder, menu_item_name";
$query = $db->query($sql);
while($data = $query->fetch(PDO::FETCH_ASSOC))
// loop over the results
{
// Assign by reference
$thisref = &$refs[ $data['menu_item_id'] ];
// add the the menu parent
$thisref['menu_item_id'] = $data['menu_item_id'];
$thisref['menu_parent_id'] = $data['menu_parent_id'];
$thisref['menu_item_name'] = $data['menu_item_name'];
$thisref['menu_url'] = $data['menu_url'];
// if there is no parent id
if ($data['menu_parent_id'] == 0)
{
$list[ $data['menu_item_id'] ] = &$thisref;
}
else
{
$refs[ $data['menu_parent_id'] ]['children'][ $data['menu_item_id'] ] = &$thisref;
}
}
function create_list( $arr )
{
$html = "";
foreach ($arr as $key=>$v)
{
if ($v['menu_parent_id'] == '0')
{
$html .= '<a class="menuitem submenuheader" href="'. $v['menu_url'] .'">'.$v['menu_item_name']."</a>\n";
$html .= "<div class=\"submenu\">\n<ul>\n";
$html .= "<li>" . create_list($v['children']) . "</li>";
$html .= "</ul>\n";
}
else{
$html .= '<li><a id="' . $v['menu_item_id'] . '">'.$v['menu_item_name']."</a></li>\n";
}
}
$html .= "</div>\n";
return $html;
}
echo "<div class=\"glossymenu\">";
echo create_list( $list );
echo "</div>";
When I run it, it outputs the following:
<div class="glossymenu"><a class="menuitem submenuheader">Settings</a>
<div class="submenu">
<ul>
<li><li><a id="4">Menu Items</a></li>
<li><a id="6">General Settings</a></li>
</div>
</li></ul>
<a class="menuitem submenuheader" href="">Grid Settings</a>
<div class="submenu">
<ul>
<li><li><a id="2">Grid Demo</a></li>
<li><a id="3">setGridOptions</a></li>
</div>
</li></ul>
</div>
</div>
As you can see there are extra <li> tags, the </ul> is in the wrong
spot (should be after the </div>) Other than that, it is working great.
The other thing I can't figure out is if I have a root menu item with no children, I would love it to have a different output like
<a id="8">No Children Menu Item</a>
Instead of:
<a class="menuitem submenuheader">No Children Menu Item</a>
The second example would create the make it show up the little (+/-) for expanding and contracting and wouldn't allow me to click on it. For clarification on the <a> tags, I am using javascript to do a .get() based off the id which is why there is no href or url shown.
UPDATE
It is working correctly and I posted it on Github for anyone that wants it.
https://github.com/ajhalls/php-accordian-menu
Try this :
<?php
include 'db.php';
$sql = "SELECT * FROM menu_items WHERE status = 'ACTIVE' ORDER BY pid ASC, sortorder ASC, menu_item_name ASC";
$query = $db->query($sql);
$menu_items = array();
while($data = $query->fetch(PDO::FETCH_ASSOC)) {
if($data['pid'] == 0) {
$menu_items[$data['id']] = array();
$menu_items[$data['id']]['id'] = $data['id'];
$menu_items[$data['id']]['name'] = $data['menu_item_name'];
$menu_items[$data['id']]['url'] = $data['menu_url'];
$menu_items[$data['id']]['children'] = array();
} else if($data['pid'] != 0) {
$tmp = array();
$tmp['id'] = $data['id'];
$tmp['name'] = $data['menu_item_name'];
$tmp['url'] = $data['menu_url'];
array_push($menu_items[$data['pid']]['children'],$tmp);
unset($tmp);
}
}
function create_list($arr)
{
$html = "";
foreach($arr as $key => $value) {
if(count($value['children']) > 0) {
$html .= ' <a class="menuitem submenuheader" href="'. $value['url'] .'">'.$value['name'].'</a>
<div class="submenu">
<ul>';
foreach($value['children'] AS $child) {
$html .= ' <li>
<a id="'.$child['id'].'">'.$child['name'].'</a>
</li>';
}
$html .= ' </ul>
</div>';
} else{
$html .= ' <a id="'.$value['id'].'">'.$value['name'].'</a>';
}
}
return $html;
}
echo "<div class=\"glossymenu\">";
echo create_list($menu_items);
echo "</div>";
?>
I've been doing some researching and discovered this nice website http://validator.w3.org/
After running a test i see that my navigation get loads of error.
The error i get is the ul in the coding. I don't close it off because i give it it styling if a condition is met else close it.
But this is not a valid solution according to w3 which gives me some errors.
Can you help me find a fix for my navigation so it gets valid?
With thanks Jim
<ul id="nav-container">
<?php
$select_category = mysql_query("SELECT * FROM menu WHERE hidden = 0 ORDER BY menu ASC");
while ($ln = mysql_fetch_array($select_category)) {
$idcat = $ln['nmr'];
$catname = $ln['menu'];
$catsname = str_replace(' ', '-', $ln['menu']);
echo '<li>';
if($catname == "carparts") {
echo '<span><strong>'.$catname.'</strong></span>';
} else {
echo '<span><strong>'.$catname.'</strong></span>';
}
This gives me the errors: (fixed)
echo '<ul';
if(isset($_GET['cats'])) {
if ($_GET['cats'] == $idcat) {
echo ' style="display:block;">';
} else {
echo '>';
}
The fix: also deleted the closing bracket at the end.
echo '<ul';
if(isset($_GET['cats']) && $_GET['cats'] == $idcat){
echo ' style="display:block;">';
} else {
echo '>';
}
/fix finish
//Error finish
$select_sub = mysql_query("SELECT * FROM submenu WHERE nmrmenu = '$idcat' AND hidden = 0");
while ($lsub = mysql_fetch_array($select_sub)) {
$subname = $lsub['submenu'];
$subsname = str_replace(' ', '-', $lsub['submenu']);
$pil = 'ยป';
$brnr = $lsub['nmr'];
if(isset($_GET['cat'])) {
if ($_GET['cat'] == $brnr) {
$subname = '<u>'.$lsub['submenu'].'</u>';
} else {
$subname = $lsub['submenu'];
}
}
echo '<li><span><strong> '.$subname.' </strong></span></li>';
}
echo '</ul>';
echo '</li>'; // to this location
}
echo '</li>'; //relocated this up one line
} **//Deleted this one!**
?>
</ul>
Update
The output gets like this which is horrible: (fixed)
<ul</li><li><span><strong>name</strong></span>
<ul</li><li><span><strong>name</strong></span><ul</li>
New: now the ul problem is gone, but it's complaining about the li's. Any clue why?
Error: document type does not allow element "li" here; missing one of "ul", "ol", "menu", "dir" start-tag
<ul id="nav-container">
<li>
<span><strong>name</strong></span>
<ul>
<li>
<span><strong> name </strong></span>
</li>
</ul>
</li>
</ul>
/update
You have </ul> and then immediately follow it with a <li>. You can't have a list item outside of a list.
I have a nested foreach loop running through categories in a database. When the user is viewing a category the category ID is set and that menu item gets it's class set as "active".
The issue I'm having is when a user is viewing a subcategory, I still need it's PARENT to be set to active.
// Run through category ID's and output info
foreach ($GLOBALS['AKB_CLASS_HELPER']->tree->nodesByPid[$catid] as $rowid) {
$row = $GLOBALS['AKB_CLASS_HELPER']->catsById[$rowid];
//Check to see if user has access
if (($accessible_cats !== false) && (!in_array($row['categoryid'],$accessible_cats)) && ($row['security'] == "private")) {
continue;
}
// If we're viewing a category, output "active" as a class in the menu
if (isset($GLOBALS['CategoryId'])
&& ($GLOBALS['CategoryId']) == $row['categoryid']) {
$activeCat = "active";
} else {
$activeCat = "";
}
//Pass through global variables
$GLOBALS['Link'] = GetUrl('category', $row['categoryid']);
$GLOBALS['Text'] = $GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders(strip_tags($row['name']));
$GLOBALS['Active'] = $activeCat;
$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryRowCell');
//If subcategories exist
if (isset($GLOBALS['AKB_CLASS_HELPER']->tree->nodesByPid[$rowid])
&& is_array($GLOBALS['AKB_CLASS_HELPER']->tree->nodesByPid[$rowid])
&& !empty($GLOBALS['AKB_CLASS_HELPER']->tree->nodesByPid[$rowid])) {
$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryGridHeaderSub');
//For every subcategory, output data
foreach ($GLOBALS['AKB_CLASS_HELPER']->tree->nodesByPid[$rowid] as $subrowid) {
$subrow = $GLOBALS['AKB_CLASS_HELPER']->catsById[$subrowid];
//Here's the part I can't get working. Need to output "active" to the PARENT id($row I believe)
if (isset($GLOBALS['CategoryId'])
&& ($GLOBALS['CategoryId']) == $subrow['categoryid']) {
$activeCat = "active";
} else {
$activeCat = "";
}
//Set Global Variables
$GLOBALS['Link'] = GetUrl('category', $subrow['categoryid']);
$GLOBALS['Text'] = $GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders(strip_tags($subrow['name']));
$GLOBALS['Active'] = $activeCat;
$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryRowCellSub');
}
}
$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('CategoryGridFooterSub');
}
So the part I need to figure out is in the nested foreach loop, how can I affect the parent ID or something of the like.
Current Output when viewing parents(Working perfectly)
<li class="categoryItem active">Parent 1
<ul class="categorySubList">
<li class="categorySubItem"><a class="categorylink" href="/categories/Parent+1/Subcategory+1/">Subcategory 1</a></li>
<li class="categorySubItem "><a class="categorylink" href="/categories/Parent+1/Subcategory+2/">Subcategory 2</a></li>
</ul>
Current Output when viewing children(not working)
<li class="categoryItem">Parent 1<!--this list item needs to be active-->
<ul class="categorySubList">
<li class="categorySubItem active"><a class="categorylink" href="/categories/Parent+1/Subcategory+1/">Subcategory 1</a></li>
<li class="categorySubItem "><a class="categorylink" href="/categories/Parent+1/Subcategory+2/">Subcategory 2</a></li>
</ul>