...

Hi! I’m Starx

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

Can one display an entire unmodified website on top of another one in a lightbox?

Question by alumanimac

Is it possible to do this with just javascript/jquery/ajax?
The idea is to make another website display unmodified i.e. display the pure html of the site in a view that hovers over the current view like a lightbox as opposed to in a new window or tab as one usually has in a browser.

Any descriptions, examples, or basic to advanced tutorials and books would be helpful here.

Thanks!

Answer by Starx

IF you want the completely usmodified version, the easiest way is to go with is <iframe>

Example:

<iframe src="http://www.google.com"></iframe>

If you are looking for a lightbox capable of showing iframe or html, here is a list (Thanks to JohnP) http://planetozh.com/projects/lightbox-clones/

Read more
March 22, 2011

Highlight one side border on hover – JavaScript

Question by Vijay

I am trying to provide an interface that allows the users to create a table structure. This is similar to how Microsoft Word provides the insert table functionality. I am using JavaScript for this purpose.

User should be able to select a border (line) and delete it to merge the cells. So, I have to highlight a one side border of a cell to be able to visualize it.

Is it possible to identify the mouse position when it moves on the border?

Answer by Starx

If you are OK, with Jquery, there here is a tutorial showing how to identify mouse coordinates.

jQuery(document).ready(function(){
   $(document).mousemove(function(e){
      $('#status').html(e.pageX +', '+ e.pageY);
   }); 
})

Source : http://docs.jquery.com/Tutorials:Mouse_Position

Read more

Can you lock updates which will change too many rows in a database?

Question by Abe Petrillo

Is there a way of implementing a rule in mysql which will block updates effecting over 10 rows?

For instance, lets say i have tbl1 with 50,000 rows and I do

update tbl1 set tb1.name = 'bob'

Then quickly follow on with a “OH NO! I FORGOT THE WHERE!?!?”.

The reason I ask is because a junior developer made this mistake and now its repair time :(.

Answer by nick rulez

You can prevent update (and delete) queries without where clause with this query

set sql_safe_updates=1;

Take a look at this link

http://dev.mysql.com/doc/refman/5.0/en/mysql-command-options.html#option_mysql_safe-updates

i-am-a-dummy is the correct switch for your colleague. 🙂

Answer by Starx

Use LIMIT

Example

update tbl1 set tb1.name = 'bob' LIMIT 0,10

This will only update the first 10 rows;

Read more
March 18, 2011

click a link or click submit without refreshing

Question by user634850

i have header, main_content, footer, right, and left content

my right content has a random link

i don’t want my right content to be refresh when I click a random link
and the main_content would be the output

is it possible that a web page without refreshing the page when you click a link or click submit button and still you can see the url on your browser what you have clicked? how do to that?

thanks!

Answer by Starx

Here, try these

<a href="#"> Your Link </a>

<input type="submit" onClick"return false;" />
Read more

how can I add a new column which counts the number of rows as serial number

Question by sadi

record of
id  fare    commission  routecode   vehicle number  productcode date    time    driver  owner name
15  12345   123 4533    1   3344    2011-03-18  00:00:00    yasir   saleem
20  a   a   3433    1   2333    2011-03-25  00:00:00    yasir   saleem
36  11111   11111   3433    1   2333    2011-03-25  16:13:12    yasir   saleem
9   1233    123 3433    nk-234  2333    2011-03-24  00:00:00    siddiq  aslam
21  1200    120 4533    nk-234  7655    2011-03-24  00:00:00    siddiq  aslam
22  1200    133333  0987    nk-234  2333    2011-03-11  00:00:00    siddiq  aslam
23  10000   11  4533    nk-234  7655    2011-03-19  00:00:00    siddiq  aslam
25  122 12  0987    nk-234  2333    2011-03-11  00:00:00    siddiq  aslam
26  1000    100 3344    nk-234  7655    2011-03-11  00:00:00    siddiq  aslam
27  1000    100 3344    nk-234  2333    2011-03-10  00:00:00    siddiq  aslam
34  100 10  3344    nk-234  2333    2011-03-18  00:00:00    siddiq  aslam
35  100 10  3344    nk-234  2333    2011-03-02  00:00:00    siddiq  aslam
5   1000    100 1234    wq1233  3344    2011-03-10  22:30:00    waqas   sami
6   2222    22  1234    wq1233  3344    2011-03-17  22:30:00    waqas   sami
24  a   a   4533    PSS-1234    7655    2011-03-02  00:00:00    salman  salam
42633   145175                          

I want to add another column before id which counts the number of

rows. It should start from 1 and increment by 1 for each row.

Answer by RichardTheKiwi

If you mean in a SELECT statement:

Say your select was

select * from tbl

It becomes

select @n := @n + 1 RowNumber, t.*
from (select @n:=0) initvars, tbl t

Notes:

  1. select @n:=0 is used to reset the global variable to 0
  2. @n := @n + 1 increases it by 1 for each row, starting from 1. This column is named “RowNumber”

Answer by Starx

I am not sure if i understand your question completely, but to add a column infront of id run this query

ALTER TABLE `yourtablename` ADD `yournewfield` VARCHAR( 50 ) NOT NULL BEFORE `id` 
Read more

Changing database records to xml file using php

Question by user643160

I am trying to pass items from a mysql database into an xml file using php. I have the php code that creates the xml file. But the values that are passed to it aren’t the ones from the mysql database. The database has 13 cols with 62 rows. I have five items in my foreach statement and when they displays on the web screen the values are output as follows:

Number of properties found : 62

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 R R R R R E E E E E W W W W W A A A A A h h h h h 1 1 1 1 1 < < < < < 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1

There are 65 items in the line above which is the 5 items in my foreach statement times the 13 cols in my database. This I think has something to do with it.

The following is my code:

<?php

@$db = new mysqli('localhost', 'root', '', 'siamsatire');

if (mysqli_connect_errno()) {
    echo 'error connecting to db';
    exit;
}
$query = "SELECT * from events";
$result = $db->query($query);
$num_results = $result->num_rows;
echo 'Number of properties found : <strong>' . $num_results . '</strong><br><br>';

for ($i=0; $i < $num_results; $i++) { 

    $row = $result->fetch_object(); 

    $name = $row->name; 
    $subtitle = $row->sub_title; 
    $date = $row->display_date; 
    $description = $row->slug; 
    $photo= $row->photo; 
    $thumb= $row->thumb; 

    /*echo '<tr>';
    echo "<td>$name</td>";
    echo "<td>$subtitle</td>";
    echo "<td>$date</td>";
    echo "<td>$description</td>";
    echo "<td>$photo</td>";
    echo "<td>$thumb</td>";1+0
    echo '<tr>';*/

} 

$doc = new DOMDocument("1.0");
$doc->formatOutput = true;

$r = $doc->createElement("events");
$doc->appendChild( $r );

foreach($row as $fieldvalue)
{
    $b = $doc->createElement( "event" );

    $name1 = $doc->createElement( "title" );
    $name1->appendChild( $doc->createTextNode( $fieldvalue['title'] ));
    $b->appendChild( $name1 );

    $subtitle1 = $doc->createElement( "subtitle" );
    $subtitle1->appendChild($doc->createTextNode( $fieldvalue['subtitle'] ));
    $b->appendChild( $subtitle1 );

    $date1 = $doc->createElement( "display_date" );
    $date1->appendChild($doc->createTextNode( $fieldvalue['display_date'] ));
    $b->appendChild( $date1 );

    $description1 = $doc->createElement( "slug" );
    $description1->appendChild( $doc->createTextNode( $fieldvalue['slug'] ));
    $b->appendChild( $description1 );

    $photo1 = $doc->createElement( "photo" );
    $photo1->appendChild( $doc->createTextNode( $fieldvalue['photo'] ) );
    $b->appendChild( $photo1 );

    $thumb1 = $doc->createElement( "thumb" );
    $thumb1->appendChild( $doc->createTextNode( $fieldvalue['thumb'] ) );
    $b->appendChild( $thumb1 );

    $r->appendChild( $b );
}

echo $doc->saveXML();
$doc->save("write.xml");

$result->free();
$db->close();
?>

Does anyone have any ideas as to what I’m doing wrong?

UPDATE


@starx – I changed my code around to look like this according to your code and this is what it looks like now.

<?php

    @$db = new mysqli( 'localhost', 'root', '', 'siamsatire');

    if (mysqli_connect_errno()) {
    echo 'error connecting to db';
    exit;
    }

    $query = "SELECT * from events";

    $result = mysql_query($query);  

    if(mysql_num_rows($result)) {
    $doc = new DOMDocument("1.0");
    $doc->formatOutput = true;

    while($row = mysql_fetch_assoc($result)) {
        $r = $doc->createElement( "events" );
        foreach($row as $field=>$value) {
            $tChild = $doc->createElement( $field );
            $tChild->appendChild( $doc->createTextNode($value) );
            $r->appendChild( $tChild );     
        }
        $doc->appendChild($r);
    }
        $doc->appendChild( $r );
        echo $doc->saveXML();
        $doc->save("write.xml");
    }

    //$result->free();
        //$db->close();
    ?>

And these are the errors I got with it.’

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:xampphtdocssiamsatire1.php on line 12

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:xampphtdocssiamsatire1.php on line 12

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:xampphtdocssiamsatire1.php on line 14'

Do you know why I got them?

I then changed mysql_query to mysqli_query which cut the errors down to:

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:xampphtdocssiamsatire1.php on line 12

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:xampphtdocssiamsatire1.php on line 14

Answer by Starx

Here is a better and correct solution

$query = "SELECT * from events";
$result = mysql_query($query);
if(mysql_num_rows($result)) {
    $doc = new DOMDocument("1.0");
    $doc->formatOutput = true;

    while($row = mysql_fetch_assoc($result)) {
        $r = $doc->createElement( "events" );
        foreach($row as $field=>$value) {
            $tChild = $doc->createElement( $field );
            $tChild->appendChild( $doc->createTextNode($value) );
            $r->appendChild( $tChild );     
        }
        $doc->appendChild($r);
    }
    $doc->appendChild( $r );
    echo $doc->saveXML();
    $doc->save("write.xml");
}

You can integrate above code with your library if you want.

UPDATE (after question Update)


Here is your working solution using mysqli

<?
@$db = new mysqli( 'localhost', 'root', '', 'siamsatire');
if (mysqli_connect_errno()) {
    echo 'error connecting to db';
    exit;
}
$query = "SELECT * from events";
$result = mysqli_query($db,$query);  
if(mysqli_num_rows($result)) {
    $doc = new DOMDocument("1.0");
    $doc->formatOutput = true;

        while($row = mysqli_fetch_assoc($result)) {
            $r = $doc->createElement( "events" );
            foreach($row as $field=>$value) {
                $tChild = $doc->createElement( $field );
                $tChild->appendChild( $doc->createTextNode($value) );
                $r->appendChild( $tChild );     
            }
            $doc->appendChild($r);
        }
        $doc->appendChild( $r );
        echo $doc->saveXML();
        $doc->save("write.xml");
}

//$result->free();
//$db->close();
?>
Read more
March 17, 2011

Chek Windows Vista using Javascript

Question by Pankaj

how can i found Windows Vista Service Pack version using javascript..

Answer by Starx

By using Javascript try
alert(navigator.userAgent));

However I am not sure if, it is cross browser. Better way would be to use server side languages.

By using PHP, you can do this.

<? echo $_SERVER['HTTP_USER_AGENT']; ?>

Both, Javascript and PHP outputs something like this

Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12

You can detect the OS using this output as Windows NT 6.1 is Windows 7

Read more
March 16, 2011

How do I remove an element from an array and insert into another position in PHP?

Question by deltanovember

For example suppose I have

$input = array(0, 1, 2, 3, 4, 5, 6, 7);

How would I remove element 5 and insert at position 2 leaving me with

0, 1, 5, 2, 3, 4, 6, 7

Answer by Starx

$input = array(0, 1, 2, 3, 4, 5, 6, 7);
array_splice($input, 2, 0, array($input[5])); //Place the a new array in the 3rd place of array
unset($input[6]); //remove the fifth element
array_splice($input, 0, 0); //to update the indexes

echo "<pre>".print_r($input,1)."</pre>"; //to view the array

Method without the need to unset and rearrange the index

$input = array(0, 1, 2, 3, 4, 5, 6, 7);
array_splice($input, 2, 0, array_splice($input,5,1)); 

Output

Array
(
    [0] => 0
    [1] => 1
    [2] => 5
    [3] => 2
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
)
Read more

repeating image background on input text field

Question by oipsl

I have a search bar that uses a background image that’s 200 by 25 px that uses the following css class

.searchBar{
    border-style: hidden;
    border-width: 0px;
    color: #FFFFFF;
    padding-left: 10px;
    padding-right: 10px;
    font-size:1em;
    background-image: url(images/searchBox2.gif);
    font-family: "calibri", "helvetica", sans-serif;
    margin-left:1%;
    width: 200px;
    height: 25px;
    outline: 0 none;
    vertical-align: middle;
    }

For some reason, it extends the element to a 220 by 27 field (the 10 padding on the left and right side and another 1 px from the top and bottom in another class) and the background image is repeated. It worked the way I wanted it before with the background not repeated until I recently added doctype html 4.01 transitional into my code. Here’s a link to a visual of what I mean:
Picture of Search Bar before and after

Answer by Starx

Padding adds up to total width of the element. See the example to know how to get same result.

Without padding

.searchbar {
    width: 200px;
}

With padding

.searchbar {
    width: 180px;
    padding: 0 10px; 
}

And to avoid the repeating background use background-repeat:no-repeat;

Here is your full solution

.searchBar{
    border-style: hidden;
    border-width: 0px;
    color: #FFFFFF;
    padding-left: 10px;
    padding-right: 10px;
    font-size:1em;
    background-image: url(images/searchBox2.gif);
    background-repeat: no-repeat;
    font-family: "calibri", "helvetica", sans-serif;
    margin-left:1%;
    width: 180px;
    height: 25px;
    outline: 0 none;
    vertical-align: middle;
    }

You can also use shorthand background to merge your background styles

background: url(images/searchBox2.gif) no-repeat;

You can also use shorthand padding to merge you padding-left and right

padding: 0 10px;
Read more
...

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