Data attribute value from MySQL - php

I'm new to PHP and MySQL so i'm not quite sure what i am doing wrong here. I am using the jQuery plugin quicksand to create a filterable portfolio. The plugin uses custom data attribute data-tag inside the li item to populate the filter nav.
What I am trying to do is use a foreach loop to populate the contents of the ul. The problem is that the filter nav won't auto populate when I use php to get the value of the data attribute from the gallery_tag column in my mySQL table.
<?php
$pagetitle = "Red Penguin - Our Work";
$navcurrent = "work";
$headTitle = "RECENT WORK";
$headsubTitle = "SOME OF OUR RECENT WORK";
include_once('includes/headersub.php');
include_once('includes/connection.php');
include_once('includes/project.php');
$project = new Project;
$projects = $project->fetch_all();
?>
<div class="row">
<nav id="filter"></nav>
<section id="container">
<ul id="stage" class="three-up">
<?php foreach($projects as $project) { ?>
<li class="gallerylist" data-tag="<?php echo $project['gallery_tag']; ?>">
<a href="project.php?id=<?php echo $project['gallery_id']; ?>">
<img src="<?php echo $project['gallery_thumb']; ?> " alt="<?php echo $project['gallery_proj']; ?>" />
<?php echo $project['gallery_title']; ?>
</a>
</li>
<?php } ?>
</ul>
</section>
The error that comes up in the log is in the jquery line:
tags = elem.data('tags').split(',');
The log comes back with this error: "Uncaught TypeError: Cannot call method 'split' of undefined" for the above line.
I'm not quite sure why this is a conflict that causes the jquery to be unable to read the value of the data-attribute as taken from the gallery_tag column of my table. Any help would be appreciated.

I guess there is a spelling mistake in your html you have data-tag and you are trying to get the elem.data('tags') it should be elem.data('tag')

your elem.data('tags') returns undefined. So split() would not work on it.

Maybe I'm wrong, but I think you need: tags = elem.data('tags').split(' ');

Related

Add additional queries to url by link

I have a site that uses query strings to retrieve the data like so:
<div id="menu-sort-dropdown" class="search-filter-item">
<p><?php echo $query_sort_title; ?></p>
<ul class="dropdown-menu">
<li>Newest</li>
<li>Oldest</li>
<li>Alphabetically</li>
</ul>
</div>
<div id="menu-category-dropdown" class="search-filter-item">
<p><?php echo $query_category_title; ?></p>
<ul class="dropdown-menu">
<li>All</li>
<li>Coaching</li>
<li>Conversation</li>
<li>Craft</li>
<li>Creativity</li>
</ul>
</div>
It works great getting the data like:
teachings/?sort=SORT_NAME_ASC
or
teachings/?category=Creativity
but I can do both like:
teachings/?category=Creativity&sort=SORT_NAME_ASC
I can't wrap my head around how to add that. If I just append the strip it will become a mess.
The following code doesn't 'duplicate' the values in the url if you keep clicking the category or sort. It's made to copy/paste and run.
// to check the values
echo '<pre>';
print_r($_GET);
echo '</pre>';
echo '<hr>';
function foo($type, $value){
$args = $_GET;
$args[$type] = $value; // prevent duplication
return http_build_query($args); // returns new query string
}
?>
SORT_DATE_LIT_ASC
<br>
SORT_NAME_ASC
<br>
Coaching
<br>
Conversation
You can also have a code to remove any of those. Add it right after the previous code (also copy/paste). Take a look:
<?php
function bar($type){
$args = $_GET;
unset($args[$type]);
return http_build_query($args); // returns new query string
}
?>
<hr>
Remove SORT
<br>
Remove CATEGORY
As for now your dropdown element do a simple action - go to provided url. If you want to select a few values then your elements should store selected value instead of open url. You can do this with for example this JS code
var menus = [];
function addElement(type, element) {
menus[type] = element;
}
The type above is number index of your menu type. For example 0 can be for sort and 1 for category - this is for ensure that you can select only one value from menu type. Now you can replace your code with something like this
<div id="menu-sort-dropdown" class="search-filter-item">
<p><?php echo $query_sort_title; ?></p>
<ul class="dropdown-menu">
<li>Newest</li>
<li>Oldest</li>
<li>Alphabetically</li>
</ul>
</div>
<div id="menu-category-dropdown" class="search-filter-item">
<p><?php echo $query_category_title; ?></p>
<ul class="dropdown-menu">
<li>All</li>
<li>Coaching</li>
<li>Conversation</li>
<li>Craft</li>
<li>Creativity</li>
</ul>
</div>
To go to the prepared URL you need to add another element like for example a button (or something else with calling openUrl function after mouse click)
<input type="button" onclick="openUrl('?')" value="Go to URL">
And the JS code for openUrl function
function openUrl(prefix) {
var url = prefix + menus.join('&');
document.location.href = url;
}

Hide Div title if there's no content

I'm very newbie regarding PHP, I'm trying to configure my wordpress Loop.
I figured out how to display my customfield in my template, I manually added a title before my taxonomy and custom fields, but I'd like it doesn't show if the taxonomy is empty.
Here is the code:
<div class="customfields">
<h2 class="widgettitle">My Title</h2>
<?php echo do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
I would very much appreciate your help!
Thanks a ton,
T.
So code should be
<?php $taxonomy = do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<div class="customfields">
<?php if(!empty($taxonomy)) : ?>
<h2 class="widgettitle">My Title</h2>
<?php echo $taxonomy; ?>
<?php endif; ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
If you want to show content in HTML only if certain variables contain a value then you can create a simple if statement that uses PHP's isset function to determine whether or not the variables are set, and if true place some HTML on the page. You can read more about isset here.

What does data attribute stand for in anchor(<a>) tag in PHP?

I'm using Drupal CMS. I'm a newbie in Drupal and PHP.
In one of my *.tpl.php file I've a PHP code snippet as follows:
<div class="form-section">
<h3>Job Alerts: jobs delivered to your inbox! <strong>(optional) </strong> <span class="info-ico"><em><?php echo bfstring('tooltip_register_job_alerts'); ?></em></span></h3>
<?php $alerts = bevforce_get_user_option($user->uid, 'alert', false); ?>
<ul class="jobs-alerts-list">
<?php foreach ($alerts as $a) : ?>
<li><?php echo $a['value']['alert_name']; ?> edit</li>
<?php endforeach; ?>
</ul>
<p><!--<a class="popup-loader" href="<?php echo url(); ?>?bf-ajax=create-job-alert&page=register"><strong>Create Job Alert</strong></a>-->
<a class="popup-loader" href="/?bf-ajax=create-job-alert&page=register">Create New Job Alert</a>
</p>
</div>
I'm not getting what's the purpose of data-oid="<?php echo $a['oid']; ?>" in anchor tag(<a>). I've never seen such attribute anywhere in <a> tag.
Following is another code snippet from my PHP code :
<li><a class="popup-loader" href="/?bf-ajax=delete-job-alert&eid=&oid=0">Remove</a></li>
If I hover the mouse cursor over the hypertext Remove, I'm getting the following URL:
xyz.com/bf-ajax=delete-job-alert&oid=3805462
How could this happen that I'm passing the value oid=0 in query string but it is showing some different value when I hover the hypertext? Is this happening due to the data-oid attribute we used in <a> tag above?
So in short my doubt is what's the purpose of attribute data-oid in <a> tag and how the value is getting changed from the value I set in the code?
Can anyone clear my above doubts?
Thanks in advance.
The data-* attribute is primarily for JavaScript in HTML 5. See: http://html5doctor.com/html5-custom-data-attributes/
Using the jQuery library, it's really easy to reference a data attribute: $("#someLink").data("name") for something like Click Me -- the .data("name") is simply for data-name.

It is not displaying all images

I have a application here: application
In the demo I am using a basic jquery slider which page is here: page info
Now the issue I am having is that it displays the images in question 1, but not in question 2. Now before I included the slider, it displayed the images in all questions. But since I included the slider, then it only displays images in first question only. How can I get images to be displayed in all questions?
CODE:
<form action='results.php' method='post' id='exam'>
<?php
foreach ($arrQuestionId as $key=>$question) {
?>
<div class='lt-container'>
<p><?php echo htmlspecialchars($arrQuestionNo[$key]) . ": " . htmlspecialchars($arrQuestionContent[$key]); ?></p>
<?php
//start:procedure image
$img_result = '';
if(empty($arrImageFile[$key])){
$img_result = ' ';
}else{
?>
<div id="banner-slide">
<ul class="bjqs">
<?php foreach ($arrImageFile[$key] as $i) { ?>
<li><img alt="<?php echo $i; ?>" height="200" width="200" src="<?php echo 'ImageFiles/'.$i; ?>"></li>
<?php } ?>
</ul>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#banner-slide').bjqs({
animtype : 'slide',
height : 200,
width : 200,
responsive : true,
randomstart : true
});
});
</script>
<?php
}
//end:procedure image
?>
</div>
<?php
}
?>
</form>
You have two divs on the page with the same ID. #1 that is a no no and bad HTML. You will need to initiate your slider on each div independently.
$('#banner-slide1').bjqs({ //ETC
$('#banner-slide2').bjqs({ //ETC
Is that enough to understand where you went wrong and why it's not working. JQuery doesn't know which banner-slide to use, or it's actually only using the first one, because it knows there should only be one ID per page.
I don't know how your slider plugin works, but you may be able to change the ids to classes in the divs, and then start the slider with:
$('.banner-slide').bjqs({ //ETC
OR
$('.banner-slide').each(function(){
$(this).bjqs({ //ETC
It depends on how the plugin works.
Element ID should be unique to a single element. You are not allowed to give two elements the same ID. Try changing the IDs to banner-slide1 and banner-slide2.

Attribute assign image to each multi option

I have created a multi option attribute so that I show an image for each option but i can not get it to work.
I have used this code from another post on here to get a list of the options to show.
The code I used is:
<?php if($_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)): ?>
<h4>Suitable for:</h4>
<ul><li><?php
$_comma = ",";
$_list = "</li><li>";
echo str_replace($_comma,$_list,$_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)) ?>
</li></ul>
<?php endif; ?>
So this now shows a list of the options, one of on top of each other.
As I said I would like an image to be shown for each option.
I thought the best way would be to have divs and assign an image to each div.
I was hoping that I could get the output to be:
<div class="option1"></div>
<div class="option2"></div>
<div class="option3"></div>
<div class="option3"></div>
instead of the output that the code above has:
<ul>
<li>option1</li>
<li>option2</li>
<li>option3</li>
<li>option4</li>
</ul>
Change your code to
<?php if($_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)): ?>
<h4>Suitable for:</h4>
<div class="<?php
$_comma = ",";
$_list = "\"></div><div class=\"";
echo str_replace($_comma,$_list,$_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)) ?>
</div>
<?php endif; ?>

Categories