...

Hi! I’m Starx

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

Limiting Html Textarea

Question by Roland

I’m having a textarea

<textarea name="Users[address]" id="Users_address"></textarea> 

Now I do not want the user the enter more than 25 characters per line and not more than 3 lines, is this possible to validate and achieve and how? The validation can be done in javascript or php

Answer by alex

jQuery

As the user types, this will remove text which doesn’t match the rules.

var maxLines = 2,
    maxLineWidth = 5;

$('#Users_address').bind('change keyup paste drop', function() {
   var value = $(this).val(),
       lines = value.split('n'),
       linesLength = lines.length;

    if (linesLength > maxLines) {
       lines = lines.slice(0, maxLines);
       linesLength = maxLines;
    }

    for (var i = 0; i < linesLength; i++) {
        if (lines[i].length > maxLineWidth) {
          lines[i] = lines[i].substring(0, maxLineWidth);  
        } 
    }

    $(this).val(lines.join('n'));
});

jsFiddle.

PHP

Where $str is your user inputted string.

define('MAX_LINES', 10);
define('MAX_LINE_LENGTH', 25);

$lines = explode("n", $str);

if (count($lines) > MAX_LINES) {
    echo 'Too many lines.';
}

foreach($lines as $line) {
    if (strlen($line) > MAX_LINE_LENGTH) {
        echo 'Too many chars wide';
        break;
    }
}

This will reject text which doesn’t match the rules. To turn any text into text that follows the rules (possibly dropping characters), just convert the jQuery above to PHP.

Answer by Starx

Check this helpful tutorial from Matt.

Read more

Need suggestions for naming 2 classes in PHP

Question by Jared

I have one class user that is initiated in /update/user.php when update.php is started. This class handles user related queries on the database, such as edit, delete, create, etc.

I’m in the process of creating a new class also named user in /src/user.php that will perform user related queries on the database but only to retrieve them (and output) them. Such as to retrieve their user id, usergroup, e-mail, etc.

I find the need to name them both user but obviously there will be conflicts when update.php is retrieved. What suggestions can you give for naming these two different classes, even though they are seperate areas in my library but perform similar operations?

Using PHP 4.

Answer by Fraser

If they both query, but only one writes, then I would suggest something like.

UserWrite
UserRead

or

UserCreate
UserAccess

Answer by Starx

I would suggest you create one class to create, edit, delete, and retrieve records related to the users. Then manage user Roles to limit the access to the methods, rather than creating two separate class for one object class.

Explanation with comparison to real world.

Let’s say we have class Car. When user with Mechanical Role (or a mechanic) uses the car class then, he will able to access methods like repair(), openHood(), where as when users with driving role (or drivers) will access drive() methods.

In this scenarios also it is very inappropriate to create CarMechanic class and CarDriver class.

I think I made my point.

Read more

Position fixed content overlapping problem

Question by saorabh

This is my html5 markup

<div id="header">
</div>
<div id="content">
</div>
<div id="footer">
</div>

Now used css

header{ height: 95px;position: fixed;width: 100%;min-width: 980px;}
footer{background:#000000;bottom:0;height:30px;position:fixed;width:100%;min-width: 980px}

Now my problem is when i put any content inside

<div id="content">
</div>

The content comes from top:0 which is not required. I dont want to use padding or margin top over content. Is there any way from which content comes below header.

Answer by Marcel

The main reason is because the <header> is position:fixed taking it out of the document flow. You’ll need to add margin or padding to either the <body> or your <content>(??) element. Also, if using HTML5 elements, add this to the top of your CSS rules for compatibility with older browsers.

/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}

Taken from Eric Meyer’s CSS Reset.

Answer by Starx

Follow those HTML5 tags, that are present. If you going to create your own, then that might have consequencies.

Here follow this.

<header>
</header>
<section>
</section>
<footer>
</footer>

To learn about valid HTML5 tags please follow this link.

Read more

How to apply style classes to td classes?

Question by martincarlin87

what I am trying to find out is the proper syntax to apply some style to each individual td in my table below:

<section id="shows">
<!-- HTML5 section tag for the shows 'section' -->

<h2 class="gig">Shows</h2>

<ul class="gig">

    <!-- Start the table -->
    <table>
        <tr>
            <!-- Setup the header row -->
            <th>When</th>
            <th>Where</th>
            <th>Start</th>
            <th>Finish</th>
        </tr>

        <?php
            // some PHP to fetch all the gig entries from the shows table
            $shows_query = "SELECT * FROM shows ORDER BY date ASC";
            $shows = mysql_query($shows_query);
            // a loop to place all the values in the appropriate table cells
            while ($show = mysql_fetch_array($shows)){
            //begin the loop...
            ?>

        <!-- Start the row -->
        <tr>

            <!-- Format the date value from the database and print it: -->
            <td class="when"><?php 
            $date = date("l, F j, Y", strtotime($show['date'])); 
            echo "$date";
            ?></td>

            <td class="venue"><?php
            echo $show['venue'];
            ?></td>

            <!-- Format the the start and end times and print them: -->
            <td class="start"><?php
            $time = date("G:i", strtotime($show['time'])); 
            echo "$time";
            ?></td>

            <td class="finish"><?php
            $until = date("G:i", strtotime($show['until']));
            echo "$until";
            ?></td>

            <!-- Finish this row -->
        </tr>

        <!-- Some space before the next row -->
        <div class="clear"></div>

        <?php 
            // close the loop:
             }
             ?>
        <!-- Finish the table -->
    </table>
</ul>

</section>

The styling that I have at the moment is:

#shows table.gig { font-size: 25px; }
#shows td.finish { margin-left: 50px;}

I did have a class for the table itself but not sure if it’s necessary.

The font-size works but what I can’t figure out is how to apply the style to the td, th, tr elements etc. I have tried several things but can’t seem to get it to work!

Any help much appreciated.

Thanks.

Answer by moleculezz

Give the table a class name and then you target the td’s with the following:

table.classname td {
    font-size: 90%;
}

Answer by Starx

A more definite way to target a td is table tr td { }

Read more
March 14, 2011

conditional statement if all is empty, don't show

Question by J82

<ul class="artist-links">
    <?php if(!empty($artist_website)) { ?><li class="artist-website"><a href="<?=$artist_website?>" target="_blank"><img src="<?php bloginfo('template_url') ?>/images/artist-website.png" alt="<?php the_title(); ?> Website" /></a></li><?php } ?>
    <?php if(!empty($artist_youtube)) { ?><li class="artist-youtube"><a href="http://youtube.com/<?=$artist_youtube?>" target="_blank"><img src="<?php bloginfo('template_url') ?>/images/artist-youtube.png" alt="<?php the_title(); ?> Youtube" /></a></li><?php } ?>
    <?php if(!empty($artist_twitter)) { ?><li class="artist-twitter"><a href="http://twitter.com/<?=$artist_twitter?>" target="_blank"><img src="<?php bloginfo('template_url') ?>/images/artist-twitter.png" alt="<?php the_title(); ?> Twitter" /></a></li><?php } ?>
</ul>

I want to make this unordered list show up only if at least one of the variables (artist-website, artist-youtube, artist-twitter) isn’t empty. So if all of the variables are empty, then this list won’t show up at all. What can I add before this code to make this work?

Answer by Jason

Just check at the beginning if they are all empty:

Code

<?php if(!(empty($artist_website) && empty($artist_youtube) && empty($artist_twitter))) : ?>

<ul class="artist-links">
    <?php if(!empty($artist_website)) { ?><li class="artist-website"><a href="<?=$artist_website?>" target="_blank"><img src="<?php bloginfo('template_url') ?>/images/artist-website.png" alt="<?php the_title(); ?> Website" /></a></li><?php } ?>
    <?php if(!empty($artist_youtube)) { ?><li class="artist-youtube"><a href="http://youtube.com/<?=$artist_youtube?>" target="_blank"><img src="<?php bloginfo('template_url') ?>/images/artist-youtube.png" alt="<?php the_title(); ?> Youtube" /></a></li><?php } ?>
    <?php if(!empty($artist_twitter)) { ?><li class="artist-twitter"><a href="http://twitter.com/<?=$artist_twitter?>" target="_blank"><img src="<?php bloginfo('template_url') ?>/images/artist-twitter.png" alt="<?php the_title(); ?> Twitter" /></a></li><?php } ?>
</ul>

<?php endif; ?>

Explanation

Basically we check each variable for the condition “empty”. If all 3 are empty, the result of AND(true,true,true) is true. Then we see the “!” or the NOT operator, which reverses that true and makes it a false. So if all the variables are empty, then do no print the lines in between.

You may also notice the “:” / “endif” syntax i’ve used, which I find much neater when splicing into tags of html.

Answer by Starx

I would suggest changing your design a little bit.

//Initialize the variables and define a fixed array for easy manipulation
if($website) { $artist['website'] = $website; }
if($youtube) { $artist['youtube'] = $youtube; }
if($twitter) { $artist['twitter'] = $twitter; }

// If somehow, $artist contains values
if(count($artist)) { ?>
<ul class="artist-links"
   <? foreach($artist as $key=>$value) { ?>
      <li class="artist-<?=$key?>">
         <a href="<?=$value?>" target="_blank">
           <img src="theimage.jpg" />
         </a>
      </li>   
   <? } ?>
<?
}
?>
Read more
February 22, 2011

jquery.animate background-position doesn't work

Question by ran levi

I’m trying to create a background position change with animation.
but for some reason it’s not working.

<script language="javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
<div class="moveme" style="height:80px;width:240px;background-image:url('http://www.google.de/intl/en_com/images/srpr/logo1w.png');background-repeat:no-repeat"></div>

<script language="javascript">
    $('.moveme').css("background-position","0px 0px").animate({backgroundPosition:"-100px 10px"});
</script>

ideas?

Answer by Cristy

jQuery doesn’t support this on default.

You can use a plug-in like : http://plugins.jquery.com/project/backgroundPosition-Effect

EDIT:

I was wrong, it works:

$(function() {
  $('.moveme').css("backgroundPosition","0px 0px").animate({"backgroundPosition":"-100px 10px"});
});

It works if you type in the address bar, but it seems I can’t make this work on jsfiddle, strange… xD

Answer by Starx

Wrap it inside a

$(document).ready(function() {
     $('.moveme').css("backgroundPosition","0px 0px").animate({backgroundPosition:"-100px 10px"});
});
Read more
February 11, 2011

Fullscreen vlc with controls at the bottom

Question by c2h2

I’m new to css and going to make a VLC embed into a fullscreen webpage (a kiosk app) with some controls at bottom of the screen.

can you tell me how css should be written?

---------------------------------------------------------
|div-doc                                                |           
|+-----------------------------------------------------+|
|| div-vlc                                             ||
||                                                     ||
||                                                     ||
||                                                     ||
||                  MOIVE HERE                         ||
||                                                     || no y-scroll
||                                                     ||
||                                                     ||
||                                                     ||
||                                                     ||
|+-----------------------------------------------------+|
|+-----------------------------------------------------+|
||div-control                                          ||
|+-----------------------------------------------------+|
---------------------------------------------------------
                    no x-scroll
note: 1. div-doc has 2 sub-divs (div-vlc and div-control)
      2. div-control always at bottom
      3. background of everything should be black

Thanks Guys

–EDIT 1–

body{background:black}
#div-doc { width: 100%; height: 100% }
#div-vlc { height: 95%}
#div-control {height: 5%}

But why i’m still getting y-scroll when div-doc specifies 100%?

Answer by Starx

A simple CSS to show the layout over the full page with out the scroll bar would be something like this

body { margin:0; padding:0; overflow: hidden; }
#div-doc { width: 100%; height: 100%; }
Read more

how to select random unique records on each execution of the SQL Query

Question by Ashish

I have a table “masterurls” it has morethan 1 million records. I want to fetch random records each time the query executed. It should not have any of the records that were fetched in previous executions. I’m already have this query:

SELECT m.url FROM masterurls ORDER BY RAND() LIMIT 200

The problem is the above query returns only first 200 hundred records and randomizes it each time.

Answer by The Scrum Meister

Since you can pass a seed parameter to the RAND() function, you can “paginate” the random results by generating a seed before the first page.

Sample code:
For the first page (varies by language):

int seed = Math.abs(new Random().nextInt());

SQL query:

SELECT url FROM masterurls ORDER BY RAND({seed}) LIMIT 200;

Store the seed somewhere (for web-based applications you can use a url parameter or session).
For the next pages:

SELECT url FROM masterurls ORDER BY RAND({seed}) LIMIT 200 * {pageNumber}, 200;

Note: Sorting by RAND() is a heavy operation, you might be better off storing a indexed column with the Hash Code of the url, then using a module-based or other random functions.

Answer by Starx

How are you going to know if the url is already accessed before. My best suggestion would be setting a flag to know this in the table. Add a field like view in the table which will accept two values 1 or 0, 1 for already accessed and 0 for not accessed. Then you could use

SELECT m.url FROM masterurls m WHERE view='1' ORDER BY RAND() LIMIT 200;
Read more
January 17, 2011

<ul> within another <ul> inherits style

Question by JamWaffles

I have the following structure in some HTML:

<ul class="li_inline">
    <li>
        <ul class="li_block">
            <li>Stuff</li>
            <li>Stuff under stuff</li>
        </ul>
    </li>
    <li>
        <ul class="li_block">
            <li>Stuff</li>
            <li>Stuff under stuff</li>
        </ul>
    </li>
</ul>

With the CSS like this:

.li_inline li
{
    display: inline;
}

.li_block li
{
    display: block;
}

What I would like to happen is to have the two inner <ul>s side by side, but any <li>s inside them to be below each other. This is so I can get a sidebar and main body side by side, but elements inside them behave normally (ie. one below the other).

Can someone suggest some CSS I can use so that the inner (li_block) lists’ <li> elements are displayed as block elements, but the <ul>s themselves are displayed side by side?

Thanks,

James

Answer by Starx

Use a reset rule.

ul ul { list-style:none; padding: 5px 20px; margin: 5px 10px; }

In your case using the !important can get your job done. But try not to use it

UPDATE

Solution: http://jsfiddle.net/Starx/KHjmP/ (FF3+, Safari 4+, IE8+)

Read more
...

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