July 22, 2012

php include (cannot get correct path)

Question by Sandro Dzneladze

I have a file that resides in:

/Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/sidebar-left-big.php

I have another file in sub directory:

/Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/template_directorypress/_gallerypage.php

And in _gallerypage.php I have php include:

<?php include('../sidebar-left-big.php'); //left sidebar, category navigation and ads ?>

Error I get:

Warning: include(../sidebar-left-big.php) [function.include]: failed to open stream: No such file or directory in /Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/template_directorypress/_gallerypage.php on line 9

Warning: include() [function.include]: Failed opening '../sidebar-left-big.php' for inclusion (include_path='.:') in /Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/template_directorypress/_gallerypage.php on line 9

It seems to me I’m doing everything correctly.

I thought that maybe problem is that _gallerypage.php is loaded via include in another file, so ../ relative to that leads to error. But error doesn’t say anything as to where it thinks path to sidebar-left-big.php is.

Answer by Jerzy Zawadzki

use include dirname(__FILE__).'/../sidebar-left-big.php';

Answer by Starx

Yes, You are right.

When you include the _gallerypage.php from another file, it does take the path relative to itself. So, you should fix this.

The best way, might be avoid such difficulties. There are number of ways to do this. Like, One would be define a global root in a constant and include every thing, everywhere as per it.

For example:

define("BASE" , "/wordpress"); //Define a base directory

//now whenever you are including and requirng files, include them on the basis of BASE

require(BASE."/admin/.../some.php");

how to navigate from one tab to other by clicking on a hyperlink using jquery ui tabs

Question by Hardworker

Could any one help me on how to navigate from first tab to second tab by clicking a hyperlink in first tab using JQUERY UI tabs?

Answer by Shant

You can refer the jQuery UI Tabs documentation for your problem, its very well mentioned

var $tabs = $('#example').tabs(); // first tab selected

$('#my-text-link').click(function() { // bind click event to link
    $tabs.tabs('select', 2); // switch to third tab
    return false;
});

Answer by Starx

You can switch between tabs freely by following the indexes using select method.
An example:

$("#tabs").tabs('select', 1);

Attach this snippet to the click handler of the link on the content of first tabs.

Example:

For a link like this:

<a href="some.html" id="nextTab">Go to Next Tab</a>

jQuery:

$("#nextTab").click(function() {
     $("#tabs").tabs('select', 1);
});
July 20, 2012

detect cross domain redirects on ajax request

Question by Sush

We have our authentication delegated to another domain (Window Identify framework, federated authentication setup). Now, if the the session timed out before an ajax request , server redirects the request to authentication server. Since it becomes a cross domain call, ajax request is cancelled by the browser. Is there a way i can detect this in jquery/javascript ?

I inspected the status property of the xhr object which set to 0 in such case, but is it a good indicator for cancelled requests? (I am using jquery $.ajax to make ajax requests)

Answer by Starx

Actually, there isn’t any definite way to detect this, unless you define it manually.

For example: Store you domain name in a var

var domain = "http://www.domain.com";

Next, whenever you have a URL you need to check, if it belongs to same domain, you can check like this:

var url = "http://www.domain.com/page.html";
if(url.indexOf(domain) >0) {
   //Yes it belongs to same domain
}

Note: This is rather a very simple example to give you an idea

July 18, 2012

PHP echo part of a string from right to left

Question by shwebdev

I am trying to get the ID from the end of this string the 4305 after the -. The code below works from left to right and shows 150. How can i make it work from right to left to show the 4305 after the -?

  $mystring = "150-Adelaide-Street-Brisbane-Cbd-4305";
  $mystring = substr($mystring, 0, strpos($mystring, "-"));
  echo $mystring;

Updated: This does what i need but i’m sure there is a better way to write it:

  $mystring = "150-Adelaide-Street-Brisbane-Cbd-4305";
  $mystring = substr(strrev($mystring), 0, strpos(strrev($mystring), "-"));
  echo strrev($mystring);

Answer by Michael Mior

You can use strrpos to get the last hyphen in the string, and then take the rest of the string after this character.

$mystring = "150-Adelaide-Street-Brisbane-Cbd-4305";
$mystring = substr($mystring, strrpos($mystring, "-") + 1);
echo $mystring;

Answer by Starx

Most easiest way is definitely using explode.

By using explode, you can split the string into an array with each parts accessible as individual identifiers using the indexes.

Usage Example:

  $mystring = "150-Adelaide-Street-Brisbane-Cbd-4305";
  $mystring = explode("-", $mystring);
  echo $mystring[count($mystring)-1]; //Extract the last item
July 17, 2012

css get height of screen resolution

Question by FishBowlGuy

im having a hard time getting the height of lower screen resolution because my screen resolution is 1920×1080 does anyone know how to get the height and width screen resolution?
because my friend has 1024×768 resolution when he checked my work into his computer, its all messed up, this is my only problem when it comes to CSS the height and width.

Answer by Starx

It is not possible to get the height of the screen from CSS. However, using since CSS3 you can use media queries to control the display of the template as per the resolution.

If you want to code on the basis of height using media queries, you can define style-sheet and call it like this.

<link rel="stylesheet" media="screen and (device-height: 600px)" />

jquery .load() not working onclick

Question by loriensleafs

I’m trying to have a site set up so when the page loads there is a div that dynamically pulls it’s content from a .html page I have set up. I have a bunch of thumbnails at the top and when you click one I want the content from a different .html document to replace what every was in that div that was dynamically loaded into the first time.

To do this I’m trying to use the jquery .load() feature. What I tried to do was set up a function:

<script type="text/javascript"> 
$(document).ready(function space_load() {

$('#SPACE_TOTAL').load('http://www.klossal.com/portfolio/space.html');
}
</script>

and then tried to launch it using:

onclick="space_load();"

this didn’t work and I was wondering if anyone could help me with this. The other thing I was wondering is if this were to work, would it replace the content that was previously loaded into there? I might be getting a head of myself and it just does this on it’s own.

Answer by Starx

First, your code has invalid structure

$(document).ready(function() {    
    function space_load() {
        $('#SPACE_TOTAL').load('http://www.klossal.com/portfolio/space.html');
    }    
    space_load(); //Now call the function
});

However, you can trim it down do this

$(function() {
    $('#SPACE_TOTAL').load('http://www.klossal.com/portfolio/space.html');
});

But, since you want this on click of an element. This is what you need:

$(function() {
    $("#yourlinkid").click(function() {
        $('#SPACE_TOTAL')
           .html('<img src="preloader.gif" />')
           .load('http://www.klossal.com/portfolio/space.html');
    });
});
July 15, 2012

jQuery hide + CSS hover issue with mouse

Question by Xander Guerin

Has anyone else noticed that when you have the CSS:hover effect applied to an element, hide that element and keep the mouse perfectly still, the hover effect is still present until the mouse moves?

I’ve has a search but can’t seem to find any other threads similar. I know it is probably easy but I cannot find the solution and it will cause me to end up in Bedlam.

To see what I mean, take a look at this Fiddle: http://jsfiddle.net/NsMKN/ and

  1. Click the black box to expand it
  2. move the cursor outside the original blackness like where the red X is
  3. click to hide and keep the mouse cursor PERFECTLY still
  4. notice the black box is still red???

When the cursor moves, the :hover is not applied as it should, but it there a way to do this without having to move the mouse and without having to apply the hover effect using jQuery myself (leaving it to CSS)?

Update: I’ve marked Starx as the answer as it does appear to be an IE thing. Thanks for the help guys.

awesome piccy

Answer by Starx

Let me split your code.

<div class="tester">
    <div class="content">
        apple, banana, carrot, lettuce, celery, beetroot
    </div>
</div>

Here, the div .content is inside .tester which wraps itself with respect to the what is inside, on general cases.

.tester:hover
{
   background-color:red; 
}

Your CSS is also applied to the same wrapper div i.e. .tester. So, that when the mouse is over to this part, its background color will change.

$('.tester').click(function () {
    $(this).children('.content').toggle();
});

Now, when you toggle the inner element to make it visible. The dimensions of the .tester will change according to the inner elements. Although it is fixed in your case, DOM also has to consider its children. Try to do the same with this fiddle.

Example Showing the Issue

Due to this reason The the mouse will still be over the div .tester. So, style of .tester:hover will still be applied.

Now, when you the toggle .content, the wrapper div .tester will retain the previous state, until further event occurs.

Browsers like IE, does not seem to update its DOM properties until next event occurs.

July 13, 2012

How to use remote method of jquery validation?

Question by Sachin

This is my validation code

$('#reg_form').validate({
            onkeyup: false,
            errorClass: 'error',
            validClass: 'valid',
            rules: {
                username: {
                    required: true,
                    minlength: 5,
                    //remote: "checkusername.php"
                },
                password: {
                    required: true,
                    minlength: 5,
                },
                confirm_password: {
                    required: true,
                    minlength: 5,
                    //equalTo: "#password",
                },
                secretQuestion: "required",
                secretAnswer: "required",
                emailId: {
                    required: true, 
                    email: true,
                    remote: "checkemail.php"
                },
                termsConditions: "required",                
            },
            messages:{
                username: {
                    required: "Please enter Username",
                    minlength: "Please enter atleast 5 characters",

                },
                password: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long"
                },
                confirm_password: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long",
                    //equalTo: "Please enter the same password as above" 

                },
                secretQuestion: "Please select your question",
                secretAnswer: "Please enter your secret answer",
                emailId: {
                    required: "Please enter a valid email address",
                    remote: "Email is aleardy is exists! please try with onother",
                    },
                termsConditions: "Please accept our Terms and COnditions"
            },
            highlight: function(element) {
                $(element).closest('div').addClass("f_error");
            },
            unhighlight: function(element) {
                $(element).closest('div').removeClass("f_error");
            },
            errorPlacement: function(error, element) {
                $(element).closest('div').append(error);
            }
        });

Answer by Starx

There is very easy explanation of the remote method on the jQuery plugin page.

As per my suggestions to you! to understand the method better, use the method in expanded form.

emailId: {
    required: true, 
    email: true,
    remote: {
        url: "checkmail.php", //the url of the page you are querying
        type: "post", //the method you are going to use to send the data
        data: { //the data you are going to send
           email: function() { 
             return $("#email").val(); //send the value of email
           }
        }
    }
},

But, your code should work the way it is.

Active menu background color

Question by user1522624

I have a vertical menu. It is .glossymenu . The menu item is accessed using css as .glossymenu a.menuitem . I want to change the background color when the menu item is selected or when the menu item is active. I am trying to use the following JQuery:

$(".glossymenu a.menuitem").click(function(){
  $(this).siblings(".active").removeClass("active");
  $(this).addClass("active");
});

But, I am unable to resolve my issue using this. Any ideas, how to change the background color of the menu item, when it is selected. It should not change when we click outside the menu item in blank area, when the menu is active.
Thanks,
Prasad

Answer by Starx

Active state is available in CSS and it much better to use that one instead.

a.menuitem:active { background-color: #c00; }

how to connect html pages to mysql database?

Question by lilian

i want to know how to connect html pages to mysql database? what are the codes?
i also want to know how to add new data to mysql database when i’m using html page and also how to publish the data?

Answer by Starx

HTML are markup languages, basically they are set of tags like <html>, <body>, which is used to present a website using , and as a whole. All these, happen in the clients system or the user you will be browsing the website.

Now, Connecting to a database, happens on whole another level. It happens on server, which is where the website is hosted.

So, in order to connect to the database and perform various data related actions, you have to use server-side scripts, like , , etc.

Now, lets see a snippet of connection using MYSQLi Extension of PHP

$db = mysqli_connect('hostname','username','password','databasename');

This single line code, is enough to get you started, you can mix such code, combined with HTML tags to create a HTML page, which is show data based pages. For example:

<?php
    $db = mysqli_connect('hostname','username','password','databasename');
?>
<html>
    <body>
          <?php
                $query = "SELECT * FROM `mytable`;";
                $result = mysqli_query($db, $query);
                while($row = mysqli_fetch_assoc($result)) {
                      // Display your datas on the page
                }
          ?>
    </body>
</html>

In order to insert new data into the database, you can use phpMyAdmin or write a INSERT query and execute them.

...

Please fill the form - I will response as fast as I can!