...

Hi! I’m Starx

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

Set value of hidden html label in php

Question by user1247412

I wish to change the value of html hidden label to a value of php variable. . And set its visibility to true. Can anyone tell me how to do this?

Answer by Jordan

<?php

    $errorMessage = "Yes, there was an error";

?>

...

<?php
    if ($errorMessage) {
        print("<p>$errorMessage</p>");
    }
?>

I hate PHP so much.

Answer by Starx

Labels cannot be hidden, unless CSS is used.

To hide an element by default, use hidden element as

<input type="hidden" />

In case of using CSS is acceptable

CSS

.hidden { visibility: hidden; }

PHP

<?php $labelvalue = "somevalue"; ?>
<label for="someinput" name="somename" class="hidden"><?php echo $labelvalue; ?></label>
Read more

$.getJSON is slow

Question by Abhishek Kumar

I am using $.getJSON to pull inventory of objects (100 items, not a large set) but its taking 8-10 seconds for XHR call.

Would like to learn if there is something I am missing or something I could do to expedite my program?

Answer by Starx

There are many things involved on it. Two main bottleneck I think are these

  • The server script generating the JSON might be causing the problem or taking long process time to generate the output.

  • You have too many scripts on your page like effect or slider, that the getJSON, is running slow

In most of the cases, the first one is a silent killer. In order to test your script, make a getJSON request to page, which simply displays the JSON string, without any processing. Something very lightweight like this

{ "test" : "value" }

In PHP

echo ‘ { “test” : “value” }’;

And make a getJSON request to this page and check if the script is running fast. In case it is fast, the problem is in your server script.

Read more

Opening a fancyBox with a certain aspect ratio

Question by smares

Does anyone know how I can open a resizable fancyBox with a certain aspect ratio (for example 16:9)?

Answer by Adam Prax

There isn’t a native way of easily resizing Fancybox. You can do some simple math with screen.height to open a Fancybox in a particular aspect ratio relative to the current screen resolution.

height = screen.height/4;

$("#test").fancybox({
           'width' : 16/9. * height,
           'height' : height,
           'autoDimensions' : false
      }); 
});

Answer by Starx

AFAIK, Fancybox right now, does not support this. However, You can control the dimension of fancy box size

("#yourbox").fancybox({
    'width'             : 680,
    'height'             495,
    'showCloseButton'   : false,
    'titlePosition'     : 'inside',
    'titleFormat'       : formatTitle
});
Read more

Newbie: Why the Color of the Background Pattern .png image is not changing?

Question by cupcake

I have browsed many questions here but none of them is relevant to this query.

I am just trying to Change the color of a Background .png image. But its color is Not Changing. Then, I convert the .png image To .gif format & tried again. But this time too, the color of .gif image Not change.

Image Link = http://www.flickr.com/photos/34259785@N00/6803476044/

Please tell that Why the color of image is Not Changing. Following is the code: –

background-color: #3399FF;
background-image: url("images/diagonal_striped_brick.gif");

Answer by Starx

Just made a demo. The image you are using is not transparent.


Update: How to make the image transparent?

  • Open the image with an image editor like photoshop.
  • Changed the opacity value of image
  • Save it as .png for better quality.

Now, the code from above will be able to give you the desired affect.

Read more
March 3, 2012

What am I missing in this SQL?

Question by exculuber

I’ve written some SQL for a MySQL database.

SELECT U.Id, U.Name, U.Surname 
  FROM match M 
 INNER JOIN created_by C 
    ON C.MatchId = M.Id 
 INNER JOIN user U 
    ON U.Id = C.UserId 
 WHERE M.Id = 3

I’m going crazy because it doesn’t seem wrong but the interpreter says there is a syntax error here near 'match M INNER JOIN created_by C ON C.MatchId=M.Id INNER JOIN user U O'.

Thanks for any advice.

Answer by Michael Berkowski

MATCH is a MySQL reserved keyword. Enclose it in backticks if you intend to use it as a column or table name.

SELECT U.Id, U.Name, U.Surname 
  FROM `match` M 

Answer by Starx

I think user & match is an reserved keyword. You need to escape it. Try the following query

SELECT U.Id, U.Name, U.Surname 
  FROM `match` M 
 INNER JOIN `created_by` C 
    ON C.MatchId = M.Id 
 INNER JOIN `user` U 
    ON U.Id = C.UserId 
 WHERE M.Id = 3
Read more

Can I require_once a parent before session_start() to be able to include a child object in $_SESSION?

Question by JDelage

In PHP, I must require_once() the classes of the objects I want to put in session before session_start().

If I want to put in session an instance of a class which is a child of a parent class, can I just require_once() the parent class, or do I need to list all the children classes?

Answer by nategood

If I understand you correctly (assuming that you have individual files with each class), you will need to require_once the file/class for the parent and the child class (otherwise the child class won’t know it’s parent class). session_start should really have nothing to do with it.

Answer by Starx

Yes, you will need to require the child classes & parent classes both. Because

  • It is of child class, of whose you are trying to add an instance
  • The object is the child, will eventually use method from the classes, which might go up to parent classes also

SO, bottom line. Yes, You do need to require all the classes.

Read more

ON DUPLICATE KEY UPDATE refuses to update

Question by Wade Urry

Having some troubles with ON DUPLICATE KEY UPDATE in MySQL. Below is the query im trying to run.

INSERT INTO `Overall` ( `rsn` , `starting_xp` , `starting_lvl` ) VALUES ( 'iWader' , '195843626' , '2281' ) ON DUPLICATE KEY UPDATE `current_xp` = '195843626' AND `current_lvl` = '2281'
  • It inserts fine, but when there is a duplicate it doesnt update, and doesnt throw any errors.
  • Running the query through PMA returns no error and doesnt update
  • Removing the ON DUPLICATE KEY UPDATE section returns a duplicate key error

This is the structure of my table

CREATE TABLE IF NOT EXISTS `overall` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `rsn` varchar(12) NOT NULL,
  `starting_xp` int(10) unsigned NOT NULL,
  `starting_lvl` int(10) unsigned NOT NULL,
  `current_xp` int(10) unsigned NOT NULL,
  `current_lvl` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `rsn` (`rsn`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

Answer by Starx

After ON DUPLICATE KEY UPDATE you should not need to use and with the fields, use , instead.

ON DUPLICATE KEY UPDATE `current_xp` = '195843626', `current_lvl` = '2281'
Read more

Target buttons in an iframe?

Question by pufAmuf

I’m doing an Android Web catalog, and for that reason I had to use iframes. I wanted to ask, however, is it possible to click a button in an iframe and make the code work in the main page?

This is the code I have:

     function initFastButtons() {
        new FastButton(document.getElementById("CloseButton"), runclose);
        new FastButton(document.getElementById("OpenButton"), runopen);
     };
     function runclose() {
        $('.full-preview-viewer').hide();
     };
     function runopen() {
        $('.full-preview-viewer').show();
     };

And this is what a button in an iframe looks like:

<input id="OpenButton" type="image" src="products/special/product1.png" name="image" width="336" height="593"></input>

Thanks everyone 🙂

Answer by Jason Bury

You can move the definitions for “runclose()” and “runopen()” to your parent window (the document that is creating the iframe). Then, in your iframe you can modify your initFastButtons() setup to reference those functions as “parent.runclose” and “parent.runopen”. Note that normally this sort of thing is limited when the pages are not requested from the same domain.

Consider these two pages, “inner.html” and “outer.html”:

outer.html:

<html>
<head>
<script type="text/javascript">
window.do_action = function () {
    alert('the button was clicked!');
}
</script>
</head>
<body>
    <iframe src="inner.html"></iframe>
</body>
</html>

inner.html:

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
    $('button').click(parent.do_action);
});
</script>
</head>
<body>
    <button>Click Me!</button>
</body>
</html>

Answer by Starx

You can click a button inside an iframe using the following technique.

var $currentIFrame = $('#IFrameId');
$currentIFrame.contents().find("#OpenButton").trigger("click");
Read more

how to open a page and force an iframe load?

Question by Lucas Matos

I would like to know is it possible to, when clicking a link, open a main page and force to load another page in a frame inside the main page?

Any idea?

Answer by Starx

Include the following js script on the main page and you are done

function loadframe(){
  var i = document.getElementById("iframeID");
  i.src = "path/to/file";
};

// Check for browser support of event handling capability
if (window.addEventListener)
    window.addEventListener("load", loadIframe, false);
else if (window.attachEvent)
    window.attachEvent("onload", loadIframe);
else window.onload = loadIframe;
Read more

php mysql select with array in to array

Question by maanu

I need to select ids from database with arrays.
my english not good. I think the best way to show my codes

the form result print_r($malayalam); like this Array ( [0] => helo [1] => hi[2] => how)

I need to select its ids from table. my code is not correct. any way let me show you here

$results=mysql_query("SELECT ml_id FROM ml_table WHERE word = '$malayalam'"); 
$numrows=mysql_num_rows($results);
if($numrows!=0){
    $ml_row = mysql_fetch_array($results);
    $ml_id = $ml_row['ml_id'] ;
    echo "Malayalam ID " . $ml_id . "<br />";
}

I need to add all my result in to another array.

is that possible ?
if u have any idea could you answer to me please

Answer by maanu

finally fainally found solution with the help of answers

$rArray = mysql_query("SELECT ml_id FROM ml_table WHERE word IN ('".implode("', '", $malayalam)."')"); 
if(mysql_num_rows($rArray)>0){
    $temp_rows = array();       
    while(($row = mysql_fetch_array($rArray))) {
        $temp_rows[] = $row['ml_id'];
    }
}

the result of print_r($temp_rows) coming like this Array ( [0] => 123 [1] => 234 [2] => 312)

thank to all

Answer by Starx

If I understood properly, the following is what you need

$results=mysql_query("SELECT * FROM ml_table WHERE word = '$malayalam'"); 
if(mysql_num_rows($results)>0){

    $newArray = array(); //Create a new array

    while($ml_row = mysql_fetch_array($results)) {
        $ml_id = $ml_row['ml_id'] ;
        $newArray[$ml_id] = $ml_row;
        echo "Malayalam ID " . $ml_id . "<br />";
    }

    //$newArray is your new array accesible from id

}
Read more
...

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