...

Hi! I’m Starx

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

Importing XML in to phpmyadmin database

Question by paulyay

I’m trying to import XML from this site http://data.gov.uk/dataset/car-parks to a phpmyadmin database, so I can use it in a google maps mashup.

I’m new to this and not sure how to go about getting the XML in to the database. Do I create the database columns first and then import the data?

Answer by Starx

There is a LOAD XML method in mysql, through which you can import the data from XML into your database.

An Example:

LOAD XML LOCAL INFILE '/pathtofile/file.xml' 
INTO TABLE `tablename` (fieldl1, field2, ...);
Read more

CSS3 move then animate

Question by Paul Thompson

I am trying to animate my pages using css3 transitions.
I need to be able to move an element and then animate using css3.
For instance move a hidden div off the screen and then animate it back on the screen.
Sounds strange I know but I have simplified my explanation and really do need this functionality. So in the code below I want to move the element 400px left and then animate it to 0. So….no animation move 400px, add CSS3 transition class and animate to 0px. Hope I have explained it ok…thanks for any help.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>My test page</title>
<style type="text/css">
    .box{
        height:100px;
        width:100px;
        background-color:green;
        position:absolute;
        top:100px;
    }
    .animator{
        -webkit-transition: all 1.5s ease;
        -moz-transition: all 1.5s ease;
        transition: all 1.5s ease;
    }
</style>
<script type="text/JavaScript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('.box').css({left:'400px'});
        $('.box').addClass('animator');
        $('.box').css({left:'0px'});

    });

</script>
</head>
<body>
<div class="box">1</div>
</body>
</html>

Answer by Starx

I think this should do

$('.box').animate({left:'400px'}).fadeIn().addClass('animator').animate({left:'0px'});
Read more

Blured background image with css

Question by MSajjadi

Is there anyway to blur an image with CSS, specially for backgrounds?

Answer by phpGeek

Yes, there is.
You can use two different backgrounds, one with sharp color (the main picture) and another blurred picture:

body  {
  background: url(images/bg-solid.jpg) no-repeat;
}

#page-wrap {
  background: url(images/bg-blurry.jpg) no-repeat fixed;
  width: 500px; margin: 40px auto;
}

To see the detailed tutorial click here

EDIT:
For an image you can use svg filters as below:
The <feGaussianBlur> element is used to create blur effects:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <defs>
    <filter id="f1" x="0" y="0">
      <feGaussianBlur in="SourceGraphic" stdDeviation="15" />
    </filter>
  </defs>
  <rect width="90" height="90" stroke="green" stroke-width="3"
  fill="yellow" filter="url(#f1)" />
</svg> 
  • The id attribute of the element defines a unique name for
    the filter
  • The blur effect is defined with the element
  • The in=”SourceGraphic” part defines that the effect is created for
    the entire element
  • The stdDeviation attribute defines the amount of the blur
  • The filter attribute of the element links the element to the
    “f1” filter
    See here for detailed explanation

Answer by Starx

This is not possible through CSS.

You have to process the image using a server-side language and generate a blurry one and then use it in the CSS backgrounds.

Read more

Check if a YouTube video has been removed using PHP?

Question by user1334448

Possible Duplicate:
Check if Youtube and Vimeo-clips are valid

I have a website which links to a lot of YouTube videos but a lot of the time these videos are removed by either the user or YouTube. This means my site has a lot of dead videos on it and there are so many it would be a nightmare to check them all manually.

So I am looking for a way to check if the YouTube video is still valid using PHP. I have seen a lot of threads with people asking the same question but I can’t get any of the posted solutions to work.

Answer by Starx

Send request to this URL, to verify the existence of a video

http://gdata.youtube.com/feeds/api/videos/<videoid>

Here is an usage of it.

$headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $videoId);
if (!strpos($headers[0], '200')) {
    echo "The YouTube video you entered does not exist";
    return false;
}
Read more

is it possible to capture scrollstart on an element other than $(document) in Jquery/Javascript?

Question by frequent

I’m trying to use the Overthrow Plugin, which is a polyfill for overflow:auto on mobile devices. Sample here.

My question:
How can I detect scrollstart/scrollstop on a div.element that is scrollable? Is this possible at all, or do the scroll/scrollstart/scrollstop events only work on $(document)?

Specifically, if I have two panels on my page, which both are scrollable, is there any way to make this work:

 $(document).on('scrollstart','div:jqmData(panel="main"), div:jqmData(panel="menu")'), function() {
    console.log("scroll detected");
    });

Thanks for some inputs!

EDIT:
My HTML looks like this:

<html class="ui-popover-mode">
// OR depending on screen size
<html class="ui-splitview-mode">
   // panel 1
   <div data-panel="main" class="ui-panel">
       <div data-role="page" class="ui-page" id="mainOne">
           <div data-role="content" class="ui-content"></div>
       </div>
       <div data-role="page" class="ui-page" id="mainTwo">
           <div data-role="content" class="ui-content"></div>
       </div>
   </div>
   // panel 2
   <div data-panel="menu" class="ui-panel">
       <div data-role="page" class="ui-page" id="menuOne">
           <div data-role="content" class="ui-content"></div>
       </div>
       <div data-role="page" class="ui-page" id="menuTwo">
           <div data-role="content" class="ui-content"></div>
       </div>
   </div>
   // panel 3
   <div data-panel="pop" class="ui-panel">
       <div data-role="page" class="ui-page" id="popOne">
           <div data-role="content" class="ui-content"></div>
       </div>
       <div data-role="page" class="ui-page" id="popTwo">
           <div data-role="content" class="ui-content"></div>
       </div>
   </div>
</html>

Screen Mode is set depending on screen size. In every panel there can be multiple pages, which each have a content section, which is scrollable using overthrow.

I want to bind to scrollstart in main and menu content section in splitview-mode AND main content sections in popover-mode.

Originally my selector tried to capture this and only attach to the relevant content elements:

$(document).on('scrollstart','.ui-splitview-mode div:jqmData(panel="main") .ui-content,  .ui-splitview-mode div:jqmData(panel="menu") .ui-content, .ui-popover-mode div:jqmData(panel="main") .ui-content, ', function() {
        // do sth
    });

Agreed this is complex and did not work. But it attached events only to the necessary elements. Now I’m doing like this:

$('.ui-content').on('scrollstart', function() {
        // check here if .ui-content is on the correct panel
    });

So I now have a lot of unnecessary bindings and only check if the binding is correct after the event is detected vs. the other way I would only attach the event to the required elements.

That’s what I was wondering.

Answer by Starx

You can directly select the element and attach the scroll event. Simple example

$("#selector").on('scrollstart', function() {
    //...do stuff
});
Read more

jQuery autocomplete not working for the first attempt

Question by Bashanta Dahal

I have tried to implement dynamic autocomplete in my program. It is working perfectly after first input. But it doesn’t show suggestions for the first attempt. However, server is responding the required source for autocomplete. Here is my code.

       $('.autocomplete').live('keyup', function(){
        $this = $(this);
        var search = $this.val();
        $.ajax({
            url:'/package/index/search/keyword/'+search+'/format/json',
            async: false,
            success: function(res){
                //console.log(res.options);
                //console.log(res.defined_ids);
                staticObject = res.defined_ids;
                $this.autocomplete({
                    source: res.options
                });
            }
        });            
    });

Server side Code is

    $keyword = $this->_getParam('keyword');
    $elementDetailModel = new Package_Model_ElementDetail();
    $arr = $elementDetailModel->searchElementDetail($keyword);
    $this->view->options = $arr['options']; // returns in the format array("test2","my new test", "night stay in delux room")
    $this->view->defined_ids = $arr['defined_ids']; // returns in the format array(21::21=>"test2", 22::22=>"my new test", 24::24=>"night stay in delux room")

when I console logged defined_ids and options in firebug, I got following response when I typed ‘t’ in the text field.
options:

[“test2”, “my new test”, “night stay in delux room”]

defined_ids:

Object { 21::21=”test2″, 22::22=”my new test”, 24::24=”night stay in delux room”}

Any help would be appreciable. Thanks in advance.

Answer by Starx

The format displayed from the firebug, is not that of a JSON. It is an array, which are accessed using indices.

When you are displaying the output, make sure you json_encode() the array first, then display it.

For example, with respect to the question, you final array should look something like this

$array['options'] = array("test2", "my new test", "night stay in room");
//Then you should echo the encoded the array to json

echo json_encode($array);

Next, make sure you views are turned off this request.

Read more

How to make PHP date and time update automatically

Question by jameswildi

I have a script which displays the date and time.

How can I add something to make it update automatically instead of on page refresh.

Here is the script:

<?php
$hourdiff = 0; // Replace the 0 with your timezone difference (;
$site = date("l, d F Y g:i a",time() + ($hourdiff * 3600));
echo $site;
?>

Can anyone help. Thanks.

Answer by Starx

PHP is a server side language, and only ways you are going to send request to the server, are through redirection, navigation, refresh or through an ajax request.

So, You will need Javascript for that.

Here is a simple demo

setInterval(function() {
    var currentTime = new Date ( );    
    var currentHours = currentTime.getHours ( );   
    var currentMinutes = currentTime.getMinutes ( );   
    var currentSeconds = currentTime.getSeconds ( );
    currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;   
    currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;    
    var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";    
    currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;    
    currentHours = ( currentHours == 0 ) ? 12 : currentHours;    
    var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
    document.getElementById("timer").innerHTML = currentTimeString;
}, 1000);
Read more

AND condition not working in MySQL Query

Question by dotman14

Please i need your help with my script. Whenever i include

AND maintable.semester_name = '$semester_name'

in the MySQL Query, it returns 0 for both values of semester_name, when actually
only one is supposed to have a value of 0 when echo $nums is processed. When i
remove

AND maintable.semester_name = '$semester_name'

the query gives normal results as i expect.

Thanks.

$query = "SELECT *             
   FROM maintable 
   WHERE maintable.matric_no = '$matric_no'
   AND   maintable.session = '$session'
   AND   maintable.semester_name = '$semester_name'
   AND   maintable.level = '$level'";
$result = mysql_query($query);
$nums = mysql_numrows($result);
echo $nums ;                  

TABLE STRUCTURE

COURSES
  course_id int(100) 
  course_code varchar(100) 
  course_title varchar(100) 
  course_unit int(10) 

MAINTABLE
  maintable_id int(255) 
  matric_no int(10) 
  session varchar(10) 
  semester_name varchar(10) 
  course_code varchar(10) 
  level int(10) 
  score int(10) 
  grade varchar(4) 

RESULT_UPLOAD
  upload_id int(10) 
  session varchar(10) 
  semester_name  varchar(10) 
  course_code varchar(10) 
  level varchar(10) 

SEMESTER
  semester_id int(10) 
  semester_name varchar(10) 

STUDENT
  matric_no int(10) 
  first_name varchar(100) 
  last_name varchar(100) 
  other_name varchar(100) 
  level int(10) 

USERS
  users_id int(10) 
  title varchar(20) 
  first_name varchar(20) 
  last_name varchar(20) 
  username varchar(20) 
  password varchar(100) 
  register_date datetime 
  tmp_name varchar(100) 
  type varchar(20) 
  name varchar(20) 
  size int(10) 

YEAR
  level_id int(10) 
  level int(10) 

Answer by Andreas Linden

The query is correct. I guess your conditions really don’t match any rows in the table. Just check what values you have in the database and what your are passing into the conditions.

Answer by Starx

Your query is correct, but with bit of ambiguity. The following query is run as exactly you are doing, but with less words.

$query = "SELECT *             
   FROM maintable 
   WHERE matric_no = '$matric_no'
   AND   session = '$session'
   AND   semester_name = '$semester_name'
   AND   level = '$level'";

Now, about your problem, the only way this might be happening may be due to no record is matching the records in your database.

Try to get the query you are running with echo $query and run the query directly from phpmyadmin to see how many result set you will get.

Read more

Mysqli_Query warning: mysqli_query() expects parameter 1 to be mysqli

Question by shaymaa

i got this error in my code and i don’t know how to solve it
my code:

<?php
session_start();
include_once"connect_to_mysql.php";

$db_host = "localhost";
// Place the username for the MySQL database here
$db_username = "root"; 
// Place the password for the MySQL database here
$db_pass = "****"; 
// Place the name for the MySQL database here
$db_name = "mrmagicadam";

// Run the actual connection here 
$myConnection= mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("mrmagicadam") or die ("no database");        
$sqlCommand="SELECT id, linklabel FROM pages ORDER BY pageorder ASC";
$query=mysqli_query($myConnection, $sqlCommand) or die(mysql_error());
$menuDisplay="";


while($row=mysql_fetch_array($query)) {
    $pid=$row["id"];
    $linklabel=$row["linklabel"];
$menuDisplay='<a href="index.php?pid=' .$pid . '">' .$linklabel. '</a><br/>';
}
mysqli_free_result($query);

?>

and this is error:
Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in C:xampphtdocslimitlessconnect_to_mysql.php on line 17

can you help me please

Answer by Starx

You are using improper syntax. If you read the docs mysqli_query() you will find that it needs two parameter.

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

mysql $link generally means, the resource object of the established mysqli connection to query the database.

So there are two ways of solving this problem

Using mysql_query()

$myConnection= mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("mrmagicadam") or die ("no database");        
$sqlCommand="SELECT id, linklabel FROM pages ORDER BY pageorder ASC";
$query=mysql_query($sqlCommand) or die(mysql_error());

Or mysqli_query();

$myConnection= mysql_connect("$db_host","$db_username","$db_pass", "mrmagicadam") or die ("could not connect to mysql"); 
$sqlCommand="SELECT id, linklabel FROM pages ORDER BY pageorder ASC";
$query=mysqli_query($myConnection, $sqlCommand) or die(mysql_error());
Read more

Ajax loader div in zend framework

Question by KJA

through my application I use common Ajax loader Div, i show it always when i make an ajax request

submitHandler: function(form) { 

                $('#loader').show();
                $(form).ajaxSubmit({
                    type: 'POST',
                    url: "/exampleactions/example",
                    data:{
                    },
                    success: function(response_data){
                        $('#loader').hide();

where is the best place to put this div to use it every where ?

shall i put it layout.php ?

Answer by Starx

IMO

  • There is not going to be a 100% correct answer for this.
  • Its totally up to the design of the application.

But, if the same loader is used through out the site, and nothing is loaded outside AJAX, then its probably a good idea to keep the div in the layout and keep it hidden until needed.

Read more
...

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