...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
March 26, 2012

<DIV> inside link (<a href="">) tag

Question by kc rajput

I want to make a div click-able and but inside this <div> i have another <div> which also should be click-able or linked.

HTML

<a href="#">
    <div class="box">
       <div class="plus"><img src="aaa.jpg"/></div>
    </div>
</a>

CSS

.box{
    float:left;
    width:100px;
    height:100px;
}
.plus{
    float:left;
    width:30px;
    height:30px;
}

Can i make both <div>s to link and for different links?

and Is this proper way use div inside a href ?

Answer by Starx

No, the link assigned to the containing <a> will be assigned to every elements inside it.

And, this is not the proper way. You can make a <a> behave like a <div>.

An Example [Demo]

CSS

a.divlink { 
     display:block;
     width:500px;
     height:500px; 
     float:left;
}

HTML

<div>
    <a class="divlink" href="yourlink.html">
         The text or elements inside the elements
    </a>
    <a class="divlink" href="yourlink2.html">
         Another text or element
    </a>
</div>
Read more

How to get stream from camera to web page?

Question by Mohan

I have started developing module which shows the live video stream from the camera on web page.

But, I don’t know how to show the video stream live in web page.

Is there any JavaScript function helps to show live view?
Does object tag in HTML allow us to embed live video ?

Could someone guide me on how I can do this?

Answer by Starx

If you are mentioning about the Live Streaming. For this you either need Streaming Video Server or HTTP Streaming.

  1. Streaming Video Server

    IT is the server from which you will stream your video. They have to be extremely fast, able to handle many connections at a time, depending on user’s connection speed etc. To have this, you can

    • Buy your own, or
    • Sign up for a hosted streaming plan with an ISP (Internet Service Provider)
  2. Or, you need HTTP Streaming

    To make it as simple as it can be

    • Create a video file in a common streaming media format
    • Upload the file to your web server
    • Make a simple hyperlink to the video file, or use special HTML tags to embed the video in a web page.

Some helpful posts and plugins

Read more
March 25, 2012

PHP regular expression split

Question by Matthew Underwood

I will make this quick and simple

I have a query and I want to split it so that it returns an array with words after the : symbol. This is what I have so far and it does as expected but returns the whole string before the matched needle. I just want the array to contain words after the : symbol.

<?php  $arr = preg_split('/[:]/', $query, 0); 

   print_r($arr);

 ?>

This returns

    Array
(
    [0] => SELECT user_name FROM test WHERE user_name = 
    [1] => user_name
)

using this query

“SELECT user_name FROM test WHERE user_name = :user_name”

thanks for any help

Answer by Taylor Dondich

Try this:

preg_match_all("/:(w+)/", $string, $matches);

By doing this, all your elements that are after a : will be in $matches[1] sub-array. I’m assuming you want to handle SQL statements with multiple named parameters. So for example:

SELECT user_name FROM test WHERE user_name = :user_name and last_name = :last_name

Will result in $matches being:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(10) ":user_name"
    [1]=>
    string(10) ":last_name"
  }
  [1]=>
  array(2) {
    [0]=>
    string(9) "user_name"
    [1]=>
    string(9) "last_name"
  }
}

Answer by Starx

You might want to use, preg_match() for this

$query = "SELECT user_name FROM test WHERE user_name = :user_name";
preg_match('/:(w+)/', $query, $matches);
echo $matches[1]; //gives your user_name
Read more

How to set languages for two jquery ui datepickers

Question by Vincent

I have two jquery ui datepickers in my page and I would like to apply a specific language for each one of them (let says german for one datepicker and italian for the other datepicker)

Now here is the problem: the last language file that is called apply its settings to all the datepickers (in this case, italian is applied in both inputs).
I’m using the following code, what should I change to apply a specific language to each of the datepickers? thanks

<script type="text/javascript" src="http://jquery-ui.googlecode.com/svn/trunk/ui/i18n/jquery.ui.datepicker-de.js"></script>

<script type="text/javascript" src="http://jquery-ui.googlecode.com/svn/trunk/ui/i18n/jquery.ui.datepicker-it.js"></script>

$(function(){

    $.datepicker.setDefaults($.datepicker.regional['de']);
    $( "#en" ).datepicker();

    $.datepicker.setDefaults($.datepicker.regional['it']);
    $( "#it" ).datepicker();


    });

Answer by Starx

This is how you do it: [Read Manual]

$.datepicker.setDefaults($.datepicker.regional['de']); 
                      //  ^ first set a default locale
$("#en").datepicker($.datepicker.regional['en']);  
                 // ^ then different locale for different
$("#it").datepicker($.datepicker.regional['it']);  
Read more

PrettyPhoto – passing unique parameter from iframe window to parent page

Question by Gublooo

I’ve recently started using prettyphoto to display a video.

This is my current setup

<link rel="stylesheet" href="/css/prettyPhoto.css" type="text/css" media="screen" charset="utf-8" />
<script src="/js/jquery.prettyPhoto2.js" type="text/javascript" charset="utf-8">

<script type="text/javascript" charset="utf-8">
  $(document).ready(function(){
    var lastClicked = null;
    $("a[rel^='prettyPhoto']").prettyPhoto({
    callback: function()
    {           
        if(lastClicked != null) {
                var topicid = lastClicked.data("topicid"); 
                $.post('/course/close-video', {topic_id: topicid });
                lastClicked = null;
        }
    }
    }).click(function (){
        lastClicked = $(this);
});
</script>


<a data-topicid="<?php echo $topic->topic_id;?>" href="/course/play-video/topic_id/<?php echo $topic->topic_id;?>?iframe=true&width=470&height=340" rel="prettyPhoto" title="<?php echo $topic->topic_name;?>">
<img src="/images/videos/<?php echo $image_name;?>" width="170" height="103" alt="<?php echo $topic->topic_name;?>"/>
</a>

This is what is happening

1) When a user clicks on the link – the play-video php action is called which retrives the video url from database and passes so that it can be played on the popup window. This works fine.

2) Now the play-video also generates a unique id which is passed on to the page (iframe window) where the video is played. Right now I’m just displaying that value on the page. I can have that unique id stored as a hidden field or as a div value.

3) Now when the user closes this window – how can I access this unique id in the callback function of pretty photo which is in the main page.

Thanks a lot
Appreciate your time

Answer by Starx

Create a variable on the main page.

var UniqueId; //Unique ID that will arrive from the iframe

Now a function to write the value

function setUniqeId(val) {
    UniqueId = val;
}

Now inside the iframe, where the id, has already been receive, pass it to the parent like

parent.setUniqueId(TheIdThatIHaveReceived);

Update:

Make sure the script to read is after the DOM is loaded. One of the early ways of ensuring this, is placing the script after the Elements

<input id="topic_user_id" type="text" />
<script>
var unique_id = document.getElementById("topic_user_id").value;
parent.setUniqueId(unique_id);
</script>

One of the modern techniques would be to create a event handler like

window.onload = function() {
    var unique_id = document.getElementById("topic_user_id").value;
    parent.setUniqueId(unique_id);
};
Read more

How to write a PHP code on simple math expression?

Question by user1290968

I am a newbie of PHP coding, and want to do a simple math example, d=(a+b)/c.

The values of a and b will be submitted by the user from a webpage containing PHP form.
The value of c is predefined in the PHP page.

When the user click “submit” in this webpage.
I expect to get the result of d in the bottom of the same webpage.

Any example code will be greatly appreciated.

Answer by ocanal

<form method="POST" action="">
<input type="hidden" id="c" name="c" value="10" />
<label for="a">a: </label>
<input type="text" id="a" name="a" /> <br />
<label for="b">b: </label>
<input type="text" id="b" name="b" />
<input type="submit" />
</form>
<br />

<?php
if (isset($_POST["a"]) && isset($_POST["b"]) && isset($_POST["c"])) {
    if (is_numeric($_POST["a"]) && is_numeric($_POST["b"]) && is_numeric($_POST["c"])) {
        $d = ($_POST["a"] + $_POST["b"])/$_POST["c"];
        echo "d = ". $d;
    } else {
        echo "all values must be numeric!"
    }
}
?>

Answer by Starx

It is not that different from other languages. Assuming you are using POST to submit the form.

$d = ($_POST['a'] + $_POST['b'])/$c;

WARNING: Injection Vulnerability if used in SQL query. But shouldn’t be a problem while learning.

To display the result. Use echo statement, which prints out the text/variable to the browser.

<?PHP echo $d; ?>

Place this snippet, where you want to display.

For example:

<div> Here is the result: <?PHP echo $d; ?> </div>
Read more

query the two table for draw in one

Question by geronimo76

Here are my tables in mysql:

        ----------------1-------------------------  
        +---------------------------+----------+
        | email                     | attempts |
        +---------------------------+----------+
        | email1@gmail.com          |       70 |
        | email2@me.com             |        1 |
        | email3@hotmail.com        |        1 |
        | email4@gmail.com          |      115 |
        +---------------------------+----------+
       ---------------2------------------------
        +----------+---------------------------+
        | accesses | email                     |
        +----------+---------------------------+
        |       24 | email1@gmail.com          |
        |        0 | email2@me.com             |
        |        0 | email3@hotmail.com        |
        |       90 | email4@gmail.com          |
        +----------+---------------------------+

query the two table for draw in one

Email   Accesses    Attempts
email1    24            70
email2     0             1
email3     0             1
email4    90           115

it is the sql:

  1. first -> select accesses, email from user;
  2. second -> select email,count(*) as intentos from userstats where intento =1 group by email;

how do I do this?
I need a query to do draw. thank’s in advance..

Answer by IT ppl

SELECT table1.email as Email, 
    table2.accesses as Acesses, 
    table1.attempts as Attempts
from table1 
INNER JOIN table2 
on table1.email = table2.email

Answer by Starx

You can use INNER JOIN to do this

SELECT 
    t1.email, t1.accesses , t2.attempts 
FROM table1 t1
INNER JOIN 
    table2 t2 ON
        t2.email = t1.email;
Read more

php add inputs with same name to mysql

Question by gogu

I have a form with some generated input fields like theese:

 <input value="March" name="month[March]" type="hidden"> 
<input value="April" name="month[April]" type="hidden"> 
<input value="May" name="month[May]" type="hidden">

I need to add the data from each input to mysql, but i don`t know how if they have name[monthname].The table structure is id, name and month_name. So i need to add the input data to month_name column. Thanks!

Answer by Starx

You can iterate over different input with same name in this way

foreach($_POST['month'] as $value) {
   echo $value;
   //Use the value in the query, it gets April March may, one by one
}

P.S: Assuming you are using POST method to submit the form.

Read more

How to check if selector has qtip?

Question by rjmcb

How can I check if an element is instantiated with a qtip plugin?

For example

$('input').qtip("hasqtip");

Answer by Starx

An very easy way would be to apply the plugin using a class selector like, in anchors

$("a.qtip").qtip(); //Apply qtip, to only those links with qtip class on them

Then, to check if a link has qtip on them, check their class

$('a').click(function() { //whenever a link is cliked
   if($(this).hasClass('qtip')) { //check if it has qtip class on them
     //if it has

     //SCREAM: yes i found a qtip :D 
   }
});
Read more

codeIgniter -How to access a text file from a model

Question by user225269

I’m trying to access the mime.types file from a model in codeIgniter.

function get_mime_type($filename, $mimePath = '../../assets'){ 
        $fileext = substr(strrchr($filename, '.'), 1); 
        if (empty($fileext)) return (false); 
        $regex = "/^([w+-./]+)s+(w+s)*($fileexts)/i"; 
        $lines = file("$mimePath/mime.types"); 
        foreach($lines as $line){ 
            if (substr($line, 0, 1) == '#') continue; // skip comments 
            $line = rtrim($line) . " "; 
            if (!preg_match($regex, $line, $matches)) continue; // no match to the extension 
                return ($matches[1]); 
        } 
        return (false); // no match at all 
    } 

I’ve already tried the following when I call it:

$this->get_mime_type($filename, $mimePath = '../../assets');

$this->get_mime_type($filename, $mimePath = '/zenoir/assets');

$this->get_mime_type($filename, $mimePath = 'http://localhost:8080/zenoir/assets/mime.types');

But no luck. The mime.types file is in the assets folder and the model is inzenoir/application/models
enter image description here

The error was:

A PHP Error was encountered

Severity: Warning

Message: file(../../assets/mime.types) [function.file]: failed to open stream: No such file or directory

Filename: models/files.php

Line Number: 46

Answer by Starx

Why are you getting into so much trouble for the mime types, use mime_content_type() instead.

echo mime_content_type($mimePath.$filename);

And the problem with your code is the path issue. Use BASEPATH constant and traverse from there instead. You will omit all these sort of path issue.

Read more
...

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