...

Hi! I’m Starx

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

Getting <divs>'s to align next to each other

Question by Cameeob2003

I have the following code I am trying to get working correctly:

<div id="newspost_bg">
                    <article>
                        <p>
                        <header><h3>The fast red fox!</h3></header>
                        This is where the article resides in the article tag. This is good for SEO optimization.
                        <footer>Read More..</footer>
                        </p>
                    </article>
                </div>
                <div id="newspost_bg">
                    hello
                </div>
                <div id="newspost_bg">
                    hello
                </div>
                <div id="advertisement">
                    <script type="text/javascript"><!--
google_ad_client = "ca-pub-2139701283631933";
/* testing site */
google_ad_slot = "4831288817";
google_ad_width = 120;
google_ad_height = 600;
//-->
</script>
                </div>

Here is the css that goes with it:

#newspost_bg{
    position: relative;
    background-color: #d9dde1;
    width:700px;
    height:250px;
    margin: 10px;
    margin-left: 20px;
    border: solid 10px #1d2631;
    float:left;
}

#newspost_bg article{
    position: relative;
    margin-left: 20px;
}
#advertisement{
    float: left;
    background-color: #d9dde1;
    width: 125px;
    height: 605px;
    margin: 10px;
}

The problem I’m experiencing is that the advertisements im trying to get setup will align with the last with the id of newspost_bg but im looking to havce it align to the top of the container it is in. I dont know if this is enough info, if not please let me know what you might need. Im new to the web coding scene so any and all critiques help me.

Answer by Anthony

Here are a few issues:

First, you have a div with an article inside, which, from what I can tell isn’t necessary, since the article can take the place of the div. Then you have a p to hold the header, article body, and footer, whenps should never have header elements (h1, h3, etc) even in the older spec and it breaks the idea of using an article tag.

Second, as mentioned, you have three divs all with the same id.

Third, you are using relative positioning for the main divs, which I don’t think helps with the floating (maybe I’m wrong), and relative positioning really only helps for child elements that are absolute positioned.

Having said that last point, I could be wrong, because the following works for me:

HTML:

<section id="articles">
    <article class="newspost_bg">
        <header><h3>The fast red fox!</h3></header>
            <p>This is where the article resides in the article tag. This is good for SEO optimization.</p>
        <footer>Read More..</footer>
    </article>
    <article class="newspost_bg">
        hello
    </article>
    <article class="newspost_bg">
    hello
    </article>
</section>
<aside id="advertisement">
    <script type="text/javascript"><!--
    google_ad_client = "ca-pub-2139701283631933";
    /* testing site */
    google_ad_slot = "4831288817";
    google_ad_width = 120;
    google_ad_height = 600;
    //-->
    </script>
</aside>

Notice that I’m using a section element to wrap the article elements and using the aside for the advertisment block. You could put divs inside either for further purposes, but the above is a light, utility-wrapper free document, which I think is a good place to start before adding in further divs, etc.

CSS:

#articles {
 position: absolute;   
}
#articles > article {
    background-color: #d9dde1;
    width:300px;
    height:250px;
    margin: 10px;
    margin-left: 20px;
    margin-right: -140px;
    border: solid 10px #1d2631;
}

#advertisement {
    float: right;
    background-color: #d9dde1;
    width: 125px;
    height: 605px;
    margin: 10px;
    border: 1px dashed black;
}​

Notice that the section, as the wrapper for the article elements, is set to absolute, and no other positioning, floats, or negative margins are set to the articles. The aside is set to float right, which makes it float to the top of it’s parent (in this case, we assume the body or html tag) which is also the parent of the section, so they are side by side.

I do admit that I’m not clear why the section (or div, or whatever) has to be set to absolute for the float to actually push the others aside, but I’m sure someone else here can clear that up.

Answer by Starx

You are attempting to configure three div width 700px to align next to each other. Unless the screen is really big, this is extremely complication.

Other than this You are using same id, which is semantically incorrect. Give class name instead.

Then modify your css to something like this

.newspost_bg{
    position: relative;
    background-color: #d9dde1;
    width:700px;
    height:250px;
    margin: 10px;
    margin-left: 20px;
    border: solid 10px #1d2631;
    float:left;
}

.newspost_bg article{
    position: relative;
    margin-left: 20px;
}
Read more

How to refresh/get new google adsense content?

Question by Zakukashi

I have a website where new content is updated via ajax when the user scrolls down. A kind of infinite page where as long as the user is scrolling new data comes. I have an adsense skyscraper code on the side but its at the top of the page and I would like to refresh when new content is loaded through Ajax so that I can then position it more down. Is this possible? and is this legal? Here is a close example of what I need: http://imgur.com/gallery
As you can see they have an ad on the right and everytime new content is updated via scroll a new ad is shown as well as keeping the older one in place.

Answer by Starx

This is not allowed. Read the TOS of adwords.

But a way to do it would be

var googleCode = '...your google code';
document.getElementById('googlediv').innerHTML(googlecode);

But, read this answer for more information.

Read more

jQuery load function and tables

Question by Syfaro

On my site, it displays a table of data.

I’ve got a link that will load another table of data from a URL.

$("#another").click(function() {
    var newGeneration = $('<p />').load('another.php?cycle=' + i);
    $('tbody').append(newGeneration);
    i++;
});

Is there a way to get it so that it doesn’t add a p tag and just directly load the file into the tbody? I tried changing it to tr, but that just made trs inside of trs.

The code that displays another result

echo "<tr>";
echo "<td>" . $keys[$k] . "</td>";
echo "<td>" . $jobs[$k] . "</td>";
echo "</tr>";

What I don’t want to happen (like it is now)

don't want the p tag here

Answer by Starx

I think its better to send a get request instead of complicating the use of load.

$("#another").click(function() {
    $.get('another.php', { 'cycle' : i }, function(data) {
         $('tbody').append(data);
    });
});
Read more

Best password reset behavior for missing account

Question by smparkes

I’m wondering what the best behavior is when a user requests a password reset for an email that doesn’t exist.

Context: user is not logged in. They just enter an email and hit a reset button.

  1. If I tell the user requesting the reset immediately that the account doesn’t exist, that’s both a bit of security hole and a privacy issue.
  2. If I do nothing and it’s an innocent mistake (they thought they had an account), they’ll be wondering what the heck happened. Most mysterious option, least subject to abuse.
  3. I can send an email that says a password reset has been requested but there’s no account (and should be ignored blah blah blah). This seems the least noxious but it is a little subject to abuse.

Update: On further consideration, I don’t really so how 1 is a big deal since they can get the same information by simply trying to sign up/use the same email … unless I’m missing something …

Answer by Starx

I would do something like this

  • Ask for the username or email
  • If that email or username is present, send all the email to the person, with the reset password.

Finished 🙂

Read more

Why is my jQuery code not able to read an apostrophe?

Question by the_boy_za

I have a web form that automatically checks the checkboxes when the page loads based on the data stored in the MYSQL database. Everything works just fine with the exception of data that contains an apostrophe. Here’s my code:

JSON:

 [{"pkFavorietemerken":"1","merken":"Adidas"},{"pkFavorietemerken":"2","merken":"Boss"},{"pkFavorietemerken":"3","merken":"Burberry"},{"pkFavorietemerken":"4","merken":"Christian Dior"},{"pkFavorietemerken":"5","merken":"D&G"},{"pkFavorietemerken":"6","merken":"Diesel"},{"pkFavorietemerken":"7","merken":"Dolce & Gabanna"},{"pkFavorietemerken":"8","merken":"Emporio Armani"}]

JQUERY:

   $.getJSON("jason.php", function(data) {

      $.each(data, function(){


     $("[value='" + this.merken + "']").attr("checked","checked");


       });

   });

HTML:

    <form name="form1" method="post" action="something.php">                        
    <ul>
        <li><input type="checkbox" name="merk[]" value="Adidas"/>Adidas</li>
        <li><input type="checkbox" name="merk[]" value="Airforce"/>Airforce</li>
        <li><input type="checkbox" name="merk[]" value="Armani"/>Armani</li>
        <li><input type="checkbox" name="merk[]" value="Asics"/>Asics</li>
        <li><input type="checkbox" name="merk[]" value="Bikkemberg"/>Bikkemberg</li>
        <li><input type="checkbox" name="merk[]" value="Bjorn Borg"/>Bjorn Borg</li>
        <li><input type="checkbox" name="merk[]" value="BlueBlood"/>BlueBlood</li>
        <li><input type="checkbox" name="merk[]" value="Boss"/>Boss</li>
        <li><input type="checkbox" name="merk[]" value="Brunotti"/>Brunotti</li>
        <li><input type="checkbox" name="merk[]" value="Burberry"/>Burberry</li>
    </ul>

THIS DOESN’T WORK:

    <li><input type="checkbox" name="merk[]" value="Levi's"/>Levi's</li>

Answer by AKX

For Levi's, the resulting selector ends up being "[value='Levi's']". I guess the selector engine chokes on it. I’m not sure if it supports escaping (Levi's) — if it doesn’t, you can do something like

var merken = this.merken;
$("input:checkbox").each(function(){
    if(this.value == merken) this.checked = true;
});

instead.

Answer by Starx

The problem is of the apostrophe, you have to escape it.

Here is a simply way to do so

function addslashes( str ) {
    return (str+'').replace(/([\"'])/g, "\$1").replace(//g, "\0");
}
$("[value='" + addslashes(this.merken) + "']").attr("checked","checked");
Read more

How to use current date variable to add new column in table?

Question by Abdullah Adam

i want to alter table add column with the current date and hour like below its not adding column but when i remove variable and make it simple text its add and please also inform me how to make this column name unique so that we not able to add same column again in mysql again

    //get current date 
            $cname = date("Ymdh");
            echo "v1" . $cname ;

    //add curent date column if yet not added 
        mysql_query("ALTER TABLE  groups_ids add " . $cname . "VARCHAR(35)") ;

Answer by Starx

The problem is in the query you wrote. First you have escape the column name using a backtick (`), after that there is no space before the datatype

mysql_query("ALTER TABLE  groups_ids add `" . $cname . "` VARCHAR(35)") ;
                                                       //^ No space here
Read more

Mouseover and mouseleave trigger every time I move the mouse inside the given element

Question by bcloutier

I am creating a site where you hover the mouse over an image and it shows an element (in this case by expanding its height from 0 to 154px).

jQuery(document).ready(function(){
    jQuery("#dropdown-menu-create .dropimage").mouseover(function(){
        jQuery("#dropdown-menu-create .toggle").animate({height:"154px"},200);
    });
    jQuery("#dropdown-menu-create .dropimage").mouseout(function(){
        jQuery("#dropdown-menu-create .toggle").animate({height:"0"},200);
    });
});

The content expands when the mouse enters and collapses when the mouse leaves, but every time the mouse is moved within the element the content expands and collapses repeatedly until the mouse leaves the element.

I’ve never had this issue before, and I have used these functions in the past, so i’m at a loss as to what is going on. Any ideas?

Edit:

jQuery(document).ready(function(){
    jQuery("#dropdown-menu-create .dropimage").mouseenter(function(){
        jQuery("#dropdown-menu-create .toggle").stop(true,true).animate({height:"154px"},200);
    });
    jQuery("#dropdown-menu-create .dropimage").mouseleave(function(){
        jQuery("#dropdown-menu-create .toggle").stop(true,true).animate({height:"0"},200);
    });
});

I am now using the code above and still getting the exact same issue. I have tried all variations of the .stop function (false,false)(false,true)(true,false)(true,true). The issue occurs differently with each one, but it still occurs.

ULTIMATE EDIT:

The problem was that the content that the mouse went over was covered up by different content once the function was called. Therefore, at any given point the mouse was simultaneously entering and leaving the image. Solved the issue by moving the function call to a different element.

Answer by Starx

Since you are trying to implement this on a container, your correct event handler are mouseenter and mouseleave.

Example:

$("#dropdown-menu-create .dropimage").mouseenter(function(){
    $("#dropdown-menu-create .toggle").animate({height:"154px"},200);
});
$("#dropdown-menu-create .dropimage").mouseleave(function(){
    $("#dropdown-menu-create .toggle").animate({height:"0"},200);
});
Read more

Detect when Browser Scrolls Below a Certain Point

Question by user1277170

I have a webpage filled with divs, and when the page reaches 300px from the bottom, it loads more divs.
I’m having trouble finding a detection that works everywhere.

I need some JavaScript (i.e. not JQuery) if statement to put into the body’s onscroll function that will detect the browser scrolling below 300px from the bottom, which works with IE, FF, Chrome, Opera, Safari, Android browsers, iBrowsers, etc.

Answer by Starx

You have to know the position of the point, then checks the window scrollTop

if ($(window).scrollTop() > point) {
 //Carry On
}
Read more

how to upload multiple files

Question by bonny

i like to post a number of images on my site.

it works for single post when i just use one image. as i tried to use with multiple images it doesnt work anymore and i dont understand why this doesnt work. to show my code:

if (isset($_POST['var1'];
 ...
 ...        $Bild1 = $_FILES['image1']; 
            $Bild2 = $_FILES['image2'];
            $Bild3 = $_FILES['image3'];
            $Bild4 = $_FILES['image4']; 
            $Bild5 = $_FILES['image5'];
            $Bild6 = $_FILES['image6'];
            $Bild7 = $_FILES['image7'];
            $Bild8 = $_FILES['image8'];
            $Bild9 = $_FILES['image9'];
            $Bild10 = $_FILES['image10'];

                $errors = array();
                $allowed_extension = array('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif');

                $file_name_1 = $_FILES['image1']['name'];
                $file_name_2 = $_FILES['image2']['name'];
                $file_name_3 = $_FILES['image3']['name'];
                $file_name_4 = $_FILES['image4']['name'];
                $file_name_5 = $_FILES['image5']['name'];
                $file_name_6 = $_FILES['image6']['name'];
                $file_name_7 = $_FILES['image7']['name'];
                $file_name_8 = $_FILES['image8']['name'];
                $file_name_9 = $_FILES['image9']['name'];
                $file_name_10 = $_FILES['image10']['name'];

                $split_1 = explode('.', $file_name_1);  
                $split_2 = explode('.', $file_name_2);
                $split_3 = explode('.', $file_name_3);
                $split_4 = explode('.', $file_name_4);
                $split_5 = explode('.', $file_name_5);
                $split_6 = explode('.', $file_name_6);
                $split_7 = explode('.', $file_name_7);
                $split_8 = explode('.', $file_name_8);
                $split_9 = explode('.', $file_name_9);
                $split_10 = explode('.', $file_name_10);

                $split_1[0] = '1';
                $split_2[0] = '2';
                $split_3[0] = '3';
                $split_4[0] = '4';
                $split_5[0] = '5';
                $split_6[0] = '6';
                $split_7[0] = '7';
                $split_8[0] = '8';
                $split_9[0] = '9';
                $split_10[0] = '10';

                $file_basename_1 = $split_1[0];
                $file_basename_2 = $split_2[0];
                $file_basename_3 = $split_3[0];
                $file_basename_4 = $split_4[0];
                $file_basename_5 = $split_5[0];
                $file_basename_6 = $split_6[0];
                $file_basename_7 = $split_7[0];
                $file_basename_8 = $split_8[0];
                $file_basename_9 = $split_9[0];
                $file_basename_10 = $split_10[0];

                $file_extension_1 = strtolower(end(explode('.', $file_name_1)));
                $file_extension_2 = strtolower(end(explode('.', $file_name_2)));
                $file_extension_3 = strtolower(end(explode('.', $file_name_3)));
                $file_extension_4 = strtolower(end(explode('.', $file_name_4)));
                $file_extension_5 = strtolower(end(explode('.', $file_name_5)));
                $file_extension_6 = strtolower(end(explode('.', $file_name_6)));
                $file_extension_7 = strtolower(end(explode('.', $file_name_7)));
                $file_extension_8 = strtolower(end(explode('.', $file_name_8)));
                $file_extension_9 = strtolower(end(explode('.', $file_name_9)));
                $file_extension_10 = strtolower(end(explode('.', $file_name_10)));

                $file_size_1 = $_FILES['image1']['size'];
                $file_size_2 = $_FILES['image2']['size'];
                $file_size_3 = $_FILES['image3']['size'];
                $file_size_4 = $_FILES['image4']['size'];
                $file_size_5 = $_FILES['image5']['size'];
                $file_size_6 = $_FILES['image6']['size'];
                $file_size_7 = $_FILES['image7']['size'];
                $file_size_8 = $_FILES['image8']['size'];
                $file_size_9 = $_FILES['image9']['size'];
                $file_size_10 = $_FILES['image10']['size'];

                $file_tmp_1 = $_FILES['image1']['tmp_name'];
                $file_tmp_2 = $_FILES['image2']['tmp_name'];
                $file_tmp_3 = $_FILES['image3']['tmp_name'];
                $file_tmp_4 = $_FILES['image4']['tmp_name'];
                $file_tmp_5 = $_FILES['image5']['tmp_name'];
                $file_tmp_6 = $_FILES['image6']['tmp_name'];
                $file_tmp_7 = $_FILES['image7']['tmp_name'];
                $file_tmp_8 = $_FILES['image8']['tmp_name'];
                $file_tmp_9 = $_FILES['image9']['tmp_name'];
                $file_tmp_10 = $_FILES['image10']['tmp_name'];

                $file_newname_1 = $file_basename_1 .".". $file_extension_1;
                $file_newname_2 = $file_basename_2 .".". $file_extension_2;
                $file_newname_3 = $file_basename_3 .".". $file_extension_3;
                $file_newname_4 = $file_basename_4 .".". $file_extension_4;
                $file_newname_5 = $file_basename_5 .".". $file_extension_5;
                $file_newname_6 = $file_basename_6 .".". $file_extension_6;
                $file_newname_7 = $file_basename_7 .".". $file_extension_7;
                $file_newname_8 = $file_basename_8 .".". $file_extension_8;
                $file_newname_9 = $file_basename_9 .".". $file_extension_9;
                $file_newname_10 = $file_basename_10 .".". $file_extension_10;

                $path ='a/b/c/'.$id.'/'.$new_file_id.'/';

                if (in_array($file_extension_1, $allowed_extension)=== false){
                    $errors[] = 'errormessage1';
                }
                if (in_array($file_extension_2, $allowed_extension)=== false){
                    $errors[] = 'errormessage2';
                }
                if (in_array($file_extension_3, $allowed_extension)=== false){
                    $errors[] = 'errormessage3';
                }
                if (in_array($file_extension_4, $allowed_extension)=== false){
                    $errors[] = 'errormessage4';
                }
                if (in_array($file_extension_5, $allowed_extension)=== false){
                    $errors[] = 'errormessage5';
                }
                if (in_array($file_extension_6, $allowed_extension)=== false){
                    $errors[] = 'errormessage6';
                }
                if (in_array($file_extension_7, $allowed_extension)=== false){
                    $errors[] = 'errormessage7';
                }
                if (in_array($file_extension_8, $allowed_extension)=== false){
                    $errors[] = 'errormessage8';
                }
                if (in_array($file_extension_9, $allowed_extension)=== false){
                    $errors[] = 'errormessage9';
                }
                if (in_array($file_extension_10, $allowed_extension)=== false){
                    $errors[] = 'errormessage10';
                }

                if ($file_size_1 > 2097152){
                    $errors[] = 'errormessage1';
                }
                if ($file_size_2 > 2097152){
                    $errors[] = 'errormessage2';
                }
                if ($file_size_3 > 2097152){
                    $errors[] = 'errormessage3';
                }
                if ($file_size_4 > 2097152){
                    $errors[] = 'errormessage4';
                }
                if ($file_size_5 > 2097152){
                    $errors[] = 'errormessage5';
                }
                if ($file_size_6 > 2097152){
                    $errors[] = 'errormessage6';
                }
                if ($file_size_7 > 2097152){
                    $errors[] = 'errormessage7';
                }
                if ($file_size_8 > 2097152){
                    $errors[] = 'errormessage8';
                }
                if ($file_size_9 > 2097152){
                    $errors[] = 'errormessage9';
                }
                if ($file_size_10 > 2097152){
                    $errors[] = 'errormessage10';
                }

                if (empty($errors)) {

                if (move_uploaded_file($file_tmp_1, $path . $file_newname_1)) {
                    echo 'success1'; 
                }

                if (move_uploaded_file($file_tmp_2, $path . $file_newname_2)) {
                    echo 'success2'; 
                }

                if (move_uploaded_file($file_tmp_3, $path . $file_newname_3)) {
                    echo 'success3'; 
                }

            }

is there also a way to shorten this code? thanks a lot.

Answer by Starx

You are doing it so wrong. To much redundancy in your code

$allowed_extension = array('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif');
foreach($_FILES as $file) {
   // Carry on like this
   $file_name = $file['name'];
   $size = $file['size'];

   //However, you might want to extract the extension like this
   $ext = pathinfo($file_name, PATHINFO_EXTENSION);
   //...


}
Read more

Validate Int Value in JavaScript

Question by user70192

I am trying to figure out how to ensure that a user enters an int into a text field with JavaScript. Currently, I have the following:

var myVal = $("#myField").val();
if (isInt(myVal)) {
  alert("Awesome!");
} else {
 alert("No Good");
}

function isInt(i) {
    if ((i != null) && (i != undefined) && (i.length > 0)) {
        alert(typeof i);
        return (typeof i == 'number' && /^-?d+$/.test(i + ''));
    }
    return false;
}

If I enter 123 into the text field, I have noticed that the typeof i is “string”. I’m not sure how to perform this kind of validation. Can someone please let me know? I was suprised I didn’t have any success when I Googled this.

Answer by Engineer

​function isInt(myVal) {
    return /^[+-]?d+$/.test(myVal);
}

Answer by Starx

Here is a short and sweet way

function isInt(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}
Read more
...

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