Set Gmap Icon to different categories with php - php

I am a rookie with PHP, so here's my question.
I want to add a custom marker ICON on GoogleMap to each category.
Here what I want to do: icon:
"if php listing-type=6 ?>/images/red-marker.png"<br/>
"if php listing-type=7 ?>/images/blue-marker.png"
The code I have:
options: {
icon : "<?php echo get_template_directory_uri(); ?>/images/red-marker.png"
},
Here it´s the full code:
http://descubrime.com.ar/fullcode.txt
And here its the site:
http://descubrime.com.ar/
I am completly lost!

I am not clear on what you are asking, but I am almost sure you want something like this. Please alert me if you were asking something else.
Since you are a beginner, I will try and explain myself.
<?php
$parentDirectory = get_template_directory_uri();
$listingType = getListingType();
$icon = ""
if($listingType == 6)
{
$icon = $parentDirectory . "/images/red-marker.png"
}
else
{
$icon = $parentDirectory . "/images/blue-marker.png"
}
?>
options: {
icon : "<?php echo $icon; ?>"
},
Your basic question is to inject the path of your icon dynamically in your javascript. So anywhere before the options line, you start to declare the above php code and declare your parentDirectory and the listing type. I have used a method getListingType() which can be anything to get the listing (6, 7 whatever). Then based on the type I prepare my $icon variable which contains the dynamic path to your actual file.

Related

Changing link text php from wordpress shortcode

I am working with a client's wordpress affiliate site.
We are using two plugins in conjunction with each other.
ACF (Advanced Custom Fields)
EasyAzon (This inserts an affiliate link via shortcode into your post)
I have set up custom fields for this shortcode, and coded it into my them like this.
<div class="top-link"> <?php the_field('link-button'); ?></div>
This outputs:
"<div class="top-link">
Buy Now
</div>"
And now I want to use the same function "<?php the_field('link-button'); ?>" at the bottom of my page.
However I would like it to display text from yet another ACF field "<?php the_field('bottom-link'); ?>" within the anchor tag.
But if there is no text in that field, I want it to use the text "Click to buy on Amazon"
So here is what I have:
<div class="top-link"><?php the_field('link-button'); ?></div>
<!-- The post content -->
<div class="bottom-link"> <?php the_field('link-button'); ?></div>
<?php
$link = get_field('bottom-link');
if (get_field('bottom-link') != '') {
$link = get_field('bottom-link');
}
else {
$link = "Click to Buy Amazon";
}
?>
<script>
var link = '<?php echo $link; ?>';
jQuery( document ).ready(function() {
jQuery('.bottom-link a').text(link);
});
</script>
I'm pretty new to both languages, and maybe complicating things a lot. Any thoughts would help!
Here is the dev site live if you're interested in looking at that.
http://tttrend.com/wtbag/gifts-for-men/general/test-post/
Here is what you should do
<?php
$link = get_field('bottom-link');
if (!isset($link) || empty($link) ) {
$link = "Click to Buy Amazon";
}
?>
Also if you are using the same field over and over, try getting the results in a variable and then using the variable instead. Using get_field, sends a request to the database and using it over and over again might slow your application down.

How To: PHP elseIf to change div depending on the URL Contents

Issue: I have a php file that I do not have access to but I need to be able to format it. It generates several different pages depending on the search. I have been able to change a div using php if else statements in the header that the php calls to. (See Below)
What I would like to do is have a statement that will change the div depending on what is in the url, now the issue with this is that one of the pages I need to change, has the "submitIt" in the url which is what I am using to change the first div.
I have tried to change the code so that it calls to a different portion of the url but because the url still contains the first bit "submitIt" it calls to that div, rather than the new div.
Is there a way to call the elseif statement where if it contains "submitIt and "MEDIA_TYPE_ID" then it would print out the new div?
<?php $url = $_SERVER["REQUEST_URI"];
if (strpos($url, "submitIt")) {
echo '<div style="width:560px;" class="app-h3 app-table">';
}elseif (strpos($url, "MEDIA_TYPE_ID")) {
echo '<div style="width:560px;" class="app-h3 app-table-none">';
}else {
echo '<div style="width:560px;" class="app-h3">';
}
?>
I think you are looking for the && operator. It means 'and'.
if (strpos($url, "submitIt") && strpos($url, "MEDIA_TYPE_ID")) {
// do something
}
If both of those are true, it will execute the code block.

$_GET not working when trying to view php image in a lightbox style

I have something that im currently working on, however it appears that the $_GET doesn't completely work.
I have a JavaScript light box that brings up an image in a little window, this works however i can only guess that it is using the same URL over and over again.
However when i view the source for the page (and even click one of the links in the source) it will display the correct data.
But the lightbox only seems to display the first image.
This is the JavaScript
<script>
//Checkes if any key pressed. If ESC key pressed it calls the lightbox_close() function.
window.document.onkeydown = function (e)
{
if (!e){
e = event;
}
if (e.keyCode == 27){
lightbox_close();
}
}
</script>
<script>
//This script makes light and fade divs visible by setting their display properties to block.
//Also it scrolls the browser to top of the page to make sure, the popup will be on middle of the screen.
function lightbox_open(){
window.scrollTo(0,0);
document.getElementById('light').style.display='block';
document.getElementById('fade').style.display='block';
}
</script>
<script>
//This makes light and fade divs invisible by setting their display properties to none.
function lightbox_close(){
document.getElementById('light').style.display='none';
document.getElementById('fade').style.display='none';
}
</script>
I wont show the CSS i dont think thats relivant (If someone wants it then ask away)
The relevant part that creates the links is this, its part of a ForEach statement all PHP
$i = 0;
foreach ($nrows as $nrow)
{
$id = $nrow['id'];
$rid = $nrow['RaidID'];
$bid = $nrow['BossID'];
$normal = $nrow['NormalKills'];
$heroic = $nrow['HeroicKills'];
$boss = substr($nrow['BossName'], 0, 3);
$p1 = $id + $bid.".php";
$image = $boss . $p1;
#echo $image;
echo $bid;
if ($oid != $rid)
{
$i = 0;
}
if ($i == 0) {
?><td style="width: 176px;"><center><b><?php echo $nrow['raid']; ?> </b></center></td> </tr><?php
$i++;
}
?><tr><td style="width: 176px;"><div align="left"><?php echo $nrow['BossName']; ?><div id="light"><img src="bossdata/template.php?boss=<?php echo $bid;?>"></a></div><div id="fade" onClick="lightbox_close();"></div>
</div>
<?php
if ($heroic == 0)
{
if ($normal > 0)
{
echo '<img src="images/whiteskull.png" align="right" alt="Normal Kill">';
}
else
{
echo '<img src="images/redx.png" align="right" alt="Not Killed">';
}
}
else
{
echo '<img src="images/redskull.png" align="right" alt="Normal Kill">';
}
?>
</td></tr><?php
$oid = $id;
}
Now this all works, and it actually displays an image with data, however no matter what link i click the boss data is always from the first one on the list.
To me this means that the data is getting through, and reaching the the right parts on image so its "Working", but all the links do the same thing and show the same data :(
*Removed last code Bulk
You have multiple div with the same ID "light" since you create them in a foreach loop.
<div id="light">
Your function lightbox_open() opens all the divs that have id "light".
document.getElementById('light').style.display='block';
That's why you always see the first lightbox. Because the others are behind the first one.
you should try something like this :
function lightbox_open(elem){
window.scrollTo(0,0);
elem.getElementByClass('light').style.display='block';
elem.getElementByClass('fade').style.display='block';
}
And change this :
<a href="#" onclick="lightbox_open();">
By this :
<a href="#" onclick="lightbox_open(this);">
And replace id by class in your div definition :
<div class="light">
$_GET is working correctly in your code.
The issue is in the way you are combining JavaScript and PHP in the second code box. First, all of your divs have the same ID: "light" which is wrong because they all IDs are meant to be unique within the HTML document. You need to identify them uniquely, for example appending the BossID to them.
After identifying each div uniquely you'll have to edit lightbox_open and lightbox_close so they can receive the BossID of the divs that you want to show and hide.

adding CSS class to link dynamically

Can anybody tell me how can I add a css class to this links when click on it?
This is my links :
echo '<li>Tutor</li>
<li>Institute</li>';
My problem is how I identify which link is clicked by users?
I assume you want to add a class to a link if it is the current link being viewed.
Since each link has a different GET parameter you could use that to identify it.
echo '<li><a '. (isset($_GET['tutor']) ? 'class="current"' : '') .' href="?tutor=link&subject='.urlencode($subject).'">Tutor</a></li>
<li><a '. (isset($_GET['institute']) ? 'class="current"' : '') .' href="?institute=link&subject='.urlencode($subject).'">Institute</a></li>';
Using a pure JavaScript solution you could do:
<a id="tutor" href="?tutor=link&subject='.urlencode($subject).'" onclick="changeClass(this)">Tutor</a>
function changeClass(link) {
if (link.id == "tutor") {
link.className = "current";
}
}
Edit:
You can identify which link was clicked by the link variable passed into the changeClass function. The link variable will include an id property which will tell you the identifier of the a tag.
you can achieve this using javascript or jquery
$(this).css('newclassname')
thats the example of jquery
updated
you can bind it to a function to get the hold of the clicked element
$('a').click(function() {
$(this).css('current');
alert($(this).html()); // this will pop up tutor if its clicked
});

WordPress: Hide page element if there is no database data to fill it

Hopefully this is simple to solve. I have a few elements which pull in form data previously entered into the database, which are added to the post as custom fields. I extract this using a php query as such:
<a href="<?php echo get_post_meta($post->ID, 'societywebsite', true); ?>" target="_blank" >Website</a>
Obviously, this appears in the Loop (content.php) and will get added to any appropriate post. Sometimes, however, there won't be any data because a user chose not to enter a website into the 'societywebsite' field on a form. When this happens, there's no need for a link to 'Website' to be there, because it wouldn't go anywhere useful.
What I'd like to know is how to have an If clause that checks if the data exists and then shows the link only if it does. I don't mind javascript, but the cleaner and less 'hacky' the solution, the better, as content.php will run multiple times for search results.
Thanks so much for any help or advice you can provide.
Another way to do this, more according to the Wordpress Codex is the following...
$societywebsite = get_post_meta($post->ID, 'societywebsite', true);
if ($societywebsite != '') {
echo '<a href="' . $societywebsite . '" target="_blank" >Website</a>';
}
You can add an 'else' at the end of this for debugging purposes.
Assuming that field is a string for a url, you could do something like this.
if (strlen(get_post_meta($post->id, 'societywebsite', true)) < 1) {
echo '<a href="' . get_post_meta($post->ID, 'societywebsite', true) . '" target="_blank" >Website</a>';
}
That will very simply check if that field has a string that is more than 1 characters. If it is more than 1 characters, it will display the link. If not...it won't do anything.

Categories