...

Hi! I’m Starx

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

Binary Search Trees, how do you find maximum?

Question by Rachel Moss

I’ve been working with Binary Search Trees in my spare time, and I want to be able to delete nodes from a tree.

In order to get this to work, I need to find the maximum value. How do you go about doing that? Pseudo-code or hints would be appreciated. I’m stuck and not exactly sure how to even begin this.

Answer by Starx

A simple pseudocode would be this. It it is indepandant to binary search I think.

int maxi = 0
foreach(array as item) // or any other loop
    if item>maxi then maxi = item
Read more

how to add jquery-fileupload plugin

Question by ALi

I have used carrierwave and followed the link
But not able make it like demo of jquery-fileupload link

Can anyone guide me where to place the files of js file and images and how to include javascript in html.

if any tutorial or example of Rails, that will be great…
Thanks

Answer by Starx

Different people have different opinion about where it is OK to keep the files. Generally,

  1. Javascript are kept in js or script
  2. Images are kept in img or images or media

Including JS on the HTML is done as this

<script type='text/javascript' src='path/to/jsfile.js'></script>
Read more

How do I pass a javascript variable to php and reload a frame

Question by bisslad

Ok this may sound a little weird but what i have is a frameset generated by php with codeigniter framework.

In one frame i am displaying rows of database query results with links attached to the row index of each record.

What I want to happen, is that you click one of the links and it brings up a more detailed summary of the record in the right hand frame.

While I am aware of the fact that php is server side etc. i was trying to make the link reload the right hand frame with the value of the index from the left hand side, then within the controller i can query the database using the index and push back the same summary page with the full record.

Answer by Starx

You need to send an AJAX request to the server with the variable. You can google on How to send ajax request to server.

However, Here is a simple example of this, using jQuery

$.post("/next/page", {
   'variable1' : 'value1'
}, function(data) {
   //on success handler
});
Read more

Simple slideshow with navigation overlay

Question by Willard

I’m currently re-designing my portfolio site & I would like to use the best non-flash method possible to create the following slideshow shown in my layout:

http://oi49.tinypic.com/xeifeq.jpg

I would only have 3 slides or so in the show, and I would like to overlay left/right nav buttons as shown.

So how do I go about this?

Answer by Starx

There are tons of options. With javascript libraries like jQuery, a lot of animations that needed flash once, can be easily done.

Try googling, jQuery Sliders

Read more

php display blank and error on mysqli_fetch_array

Question by raindrop

I receive a blank page when I try connect to mysql, based on the error. php doesn’t like the 3rd line

$link = "SELECT * FROM `Table` WHERE `id`='$id'";
$results = mysqli_query($db,$link);
$qdata = mysqli_fetch_array($results);

This code is working on my old server, so mysqli_fetch_array seems absolute? Or did I miss something else on my php config?

Thanks!

This is the error message
Fatal error: Call to undefined function mysqli_connect() in /opt/php_script/connect.php on line 14

Line 14 is

$link = "SELECT * FROM `Table` WHERE `id`='$id'";

Answer by Starx

Your mysqli extendion is not enabled. Check your php.ini, go to the extensions part and enable it.

Read more

Trimming hashed password (for user identification)

Question by GARR

http://www.vidyasocks.com/forums.php?id=1&id=1

as you can see at the bottom I am using hash as a way to identify users. How can I trim off the rest after 10 characters?

Answer by Jason Fuerstenberg

Trimming runs the risks of hash collisions where two users potentially share the same trimmed portion.

http://en.wikipedia.org/wiki/Collision_(computer_science)

Hashing algorithms go to great length to avoid this possibility so modification of the hashed results in any way is not recommended.

Answer by Starx

Use substr():

$hash = substr($hash,0,10);
Read more

Eliminating warning in HTML

Question by user1221330

I am creating 3 jsp pages. There are 4 boxes with different id in each pages (i.e. box1, box2, box3, box 4 in page 1; box 5, box6, box7, box8 in page 2; box9, box10, box11, box12 in page 3). Below is the sample code in page 1:

<div class="dragableBox" id="box1">CAT</div>
<div class="dragableBox" id="box2">DOG</div>
<div class="dragableBox" id="box3">HORSE</div>
<div class="dragableBox" id="box4">TIGER</div>

In each page there is also a script. In the script I deliberately use all those ids above as parameters of a function. Below is the sample code in page 1:

dragDropObj.addSource('box1',true);
dragDropObj.addSource('box2',true);
dragDropObj.addSource('box3',true); 
dragDropObj.addSource('box4',true); 
dragDropObj.addSource('box5',true); 
dragDropObj.addSource('box6',true); 
dragDropObj.addSource('box7',true); 
dragDropObj.addSource('box8',true); 
dragDropObj.addSource('box9',true); 
dragDropObj.addSource('box10',true);    
dragDropObj.addSource('box11',true);    
dragDropObj.addSource('box12',true);    

I must do this because as far as I know this is the only way for my program to work. The problem I encounter is that each time the program started, a warning appears:
“The source element with id box5 does not exist”

Although the program still works fine with this warning, I still want to eliminate the warning.

My question here is:
How can I stop such warning from appearing?
Is there a kind of error catching method in HTML?

Answer by Starx

Check the existence of element before adding them.

if (typeof document.getElementById('box5') !== 'undefined'){
    // continue
}
Read more

Converting the structure of an array

Question by Joseph

I have the following array:

Array ( 
   [0] => Information.pdf
   [1] => Presentation.pdf
   [2] => Brochure.pdf
) 

I want to convert it into a nested array in the following format using PHP to make it compatible with the CakePHP 2.1.1 email class:

Array (  
   [Information.pdf] => Array ( 
        [file] => /files/Information.pdf 
        [mimetype] => application/pdf  
   ) 
   [Presentation.pdf] => Array ( 
        [file] => /files/Presentation.pdf 
        [mimetype] => application/pdf  
   ) 
   [Brochure.pdf] => Array ( 
        [file] => /files/Brochure.pdf 
        [mimetype] => application/pdf  
   ) 
)

Any ideas on how to do this? The “mimetype” can be hardcoded.

Answer by ThiefMaster

$nested = array();
foreach($flat as $filename) {
    $nested[$filename] = array(
        'file' => '/files/'.$filename,
        'mimetype' => 'application/pdf'
    );
};

For mimetype guessing, the fileinfo extension would be a good choice.

Answer by Starx

Use a foreach to iterate through the array items and use mime_content_type() to get the mimetype.

$temp = array(); //A temporary array to store new array
foreach($array as $filename) { //Supposing $array is your original array
    $file = '/files/'.$filename;
    $temp[$filename] = array(
        'file' => $file,
        'mimetype' => mime_content_type($file)
    );
};
Read more

Jquery is Ready wrapper

Question by Mahan

is there any javascript code wrap that can be use to wait for Jquery to load and create a callback after loading jquery successfully?

something like this

JqueryWait(function(){
   //my magnificent jquery codes ^^
});

I want a code wrap something like this…

Explanation:
I’m using headjs and i loaded jquery in a separate code of headjs. now my other scripts relies on the jquery and that’s why that other script must know if the jquery is already loaded. I know about the callback function of headjs but my other script can’t be put on the callback function

Answer by qwertymk

Try this:

function JqueryWait(fn) {
    if (jQuery) fn();
    else setTimeout(function() { JqueryWait(fn); }, 100);
}

JqueryWait(function(){
   //my magnificent jquery codes ^^
});

Answer by Starx

You dont need extra function to do this. jQuery’s $(document).ready(function() { }); is enough for this.

$(function() {
    //ever code written inside here is run, after the jQuery & DOM is ready
});

Anways, there is another way to do check if jQuery has been loaded or not

if (typeof jQuery == 'undefined') {
   //... jquery not loaded
}
Read more
...

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