...

Hi! I’m Starx

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

mysql_fetch_assoc inside if then statement

Question by BBuchanan

So, I’m still new with the php coding, still learning, etc. So, if this has been asked before I’m sorry. I’m writing this off the top of my head, so any mistakes, again, sorry.

I want to pull info from the database and if it’s a number then display text instead. So, I thought something like this would work (I have to check 1 – 12 with a specific word):

 if(mysql_fetch_assoc($result) == 1){
 echo "student";
 }else
  "generic error";

or since I have to do 1 – 12

if($row_members['ulevel'] == 1){
echo "Student";
}else
"generic error";

When users register, they are assigned a number as part of the insert, so there shouldn’t be any error.
So I guess it’s a multi-part question. How do I create a statement to check the ulvel and print it as text. 1 being a observer and 12 being the admin.

Answer by Starx

mysql_fetch_assoc() does not return integer value, it returns associative array of strings.

To check the status, you can simply put expression inside the if expression.

$row = mysql_fetch_assoc($result);
if($row['ulevel'] == 1){
 echo "student";
} else
  "generic error";

If you are checking among multiple rows

while($row = mysql_fetch_assoc($result)) {
    if($row['ulevel'] == 1){
     echo "student";
    } else
      "generic error";
}
Read more
April 4, 2012

How to write a non-english character cookie using javascript and let php reads it properly

Question by Sami Al-Subhi

my websites sometimes needs to read a javascript cookie using php but sometimes I get weird character set from some users like this `#16 3CFJ) DD%J,’1 while for some users it reads it properly. therefore, I think the problem is in the client-side. I use this method to write cookies:

    var expireDate = new Date();
        expireDate.setMonth(expireDate.getMonth() + 1);
    var value="Sami";
    document.cookie = "name="+value+";path=/;expires="+expireDate.toGMTString();

and this $_COOKIE['name']to read it using php.

Answer by Starx

Cookies cant be handled using headers. So,

Encode your cookie using base64_encode() and decode it using base64_decode() to read it.

To encode/decode in Javascript, this answer might help.

Read more

Horizontal scrollable images row without list

Question by Ixx

I want to create a scrollable row with thumbnails in a (smaller) container div.

Is this possible without wrapping the img in a list? I tried with

float:left
display: inline-block;

for the images

and/or

overflow: auto;

in the container div.

But the divs appear in a column and no horizontal scrollbar appears (only vertical when using overflow:auto).

jsfiddle: http://jsfiddle.net/8qJSr/1/

Answer by mikeymeows

By keeping the images within a wrapper that is scrolled inside of an outer wrapper you can achieve this quite easily.

jsfiddle link: here;

The HTML:

 <div id="wrapper">
   <div id="innerwrapper">
    <img />
    <img />
    <img />
    <img />
    <img />
    <img />
    <img />
   </div>
 </div>​

The CSS:

#wrapper { height: 100px; overflow: auto; width: 500px; overflow-y: hidden;}
img {height: 100px; width: 200px;float: left;}
#innerwrapper { width: 1200px;}​

Answer by Starx

float:left; will work fine. For a markup as such

<div id="collection">
   <img ... />
   ...
</div>

CSS

#collection {
    height: 200px;
    overflow: auto;
}
#collection img { float:left }
Read more

How to recursively build a <select> with unknown tree depth

Question by simone

I have a MySQL table with a tree data structure. The fields are _id, name and parentId. When the record hasn’t a parent, parentId defaults as 0. This way I can build an array and then recursively print each record.

The builded array looks like this:

Array
(
    [1] => Array
        (
            [parentId] => 0
            [name] => Countries
            [_id] => 1
            [children] => Array
                (
                    [2] => Array
                        (
                            [parentId] => 1
                            [name] => America
                            [_id] => 2
                            [children] => Array
                                (
                                    [3] => Array
                                        (
                                            [parentId] => 2
                                            [name] => Canada
                                            [_id] => 3
                                            [children] => Array
                                                (
                                                    [4] => Array
                                                        (
                                                            [parentId] => 3
                                                            [name] => Ottawa
                                                            [_id] => 4
                                                        )

                                                )

                                        )

                                )

                        )

                    [5] => Array
                        (
                            [parentId] => 1
                            [name] => Asia
                            [_id] => 5
                        )

                    [6] => Array
                        (
                            [parentId] => 1
                            [name] => Europe
                            [_id] => 6
                            [children] => Array
                                (
                                    [7] => Array
                                        (
                                            [parentId] => 6
                                            [name] => Italy
                                            [_id] => 7
                                        )

                                    [11] => Array
                                        (
                                            [parentId] => 6
                                            [name] => Germany
                                            [_id] => 11
                                        )

                                    [12] => Array
                                        (
                                            [parentId] => 6
                                            [name] => France
                                            [_id] => 12
                                        )

                                )

                        )

                    [8] => Array
                        (
                            [parentId] => 1
                            [name] => Oceania
                            [_id] => 8
                        )

                )

         )

 )

Printing an unordered list <ul> is very simple with recursion. Here’s the function I use:

function toUL ($arr) {

    $html = '<ul>' . PHP_EOL;

    foreach ( $arr as $v ) {

        $html.= '<li>' . $v['name'] . '</li>' . PHP_EOL;

        if ( array_key_exists('children', $v) ) {
            $html.= toUL($v['children']);
        }

    }

    $html.= '</ul>' . PHP_EOL;

    return $html;
}

But I’m stuck at printing a <select> in a tree-structured way:

Countries
-- America
---- Canada
------ Ottawa
-- Asia
-- Europe
---- Italy
---- Germany
---- France
-- Oceania

I thought to print -- as many times as the element’s depth, but I don’t know how to calculate the depth.

My question is: is it possible to build a <select> without knowing the depth?

Thank you in advance.

Answer by Starx

Pass a parameter to count the iteration like $pass

function toUL ($arr, $pass = 0) {

    $html = '<ul>' . PHP_EOL;

    foreach ( $arr as $v ) {           

        $html.= '<li>';
        $html .= str_repeat("--", $pass); // use the $pass value to create the --
        $html .= $v['name'] . '</li>' . PHP_EOL;

        if ( array_key_exists('children', $v) ) {
            $html.= toUL($v['children'], $pass+1);
        }

    }

    $html.= '</ul>' . PHP_EOL;

    return $html;
}
Read more

How to create an arrow in css3?

Question by Rohit Azad

How to create arrow in css3?
As like this .

enter image description here

enter image description here

jsfiddle http://jsfiddle.net/BdSP4/1/

Answer by Starx

Just adding alternative using CSS3’s rotate

Demo

CSS

.arrow { width: 20px; height: 20px; overflow: hidden; }
.arrow span  {
    display: block;
    border: 4px #000 solid;
    background: #fff;
    width: 14px;
    height: 14px;
    float:right;
    margin-right: -12px;

    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    transform: rotate(45deg);
    filter: progid:DXImageTransform.Microsoft.Matrix(
         M11=0.7071067811865476, M12=-0.7071067811865475, M21=0.7071067811865475, M22=0.7071067811865476, sizingMethod='auto expand');
    zoom: 1;
}

HTML

<div class="arrow"><span></span></div>
Read more

jQuery – Exclude an li from this function?

Question by CodeyMonkey

I have a ul list of 8 li’s on the last li it has the id #search – I don’t want the dropdown applid to this, how can I exclude it? here’s my code..

$(document).ready(function () {     
  $('#navigation li').hover(function () {
    // Show the sub menu
    $('ul', this).stop(true,true).slideDown(300);
  },
  function () {
  //hide its submenu
   $('ul', this).stop(true,true).slideUp(200);         
  });
});

Thanks

Answer by Starx

Use jQuery’s .not()

$('#navigation li').not("#search").hover(function () {
   // Show the sub menu
   $('ul', this).stop(true,true).slideDown(300);
},
function () {
  //hide its submenu
  $('ul', this).stop(true,true).slideUp(200);         
});
Read more

Timeout in Jquery $.post by emulating $.ajax

Question by newbie

How can we emulate the timeout of $.ajax using $.post?

Answer by Starx

$.POST is a preset version of $.ajax, so few parameter are already set.

As a matter of fact, a $.post is equal to

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

But, you can create your own post function to send the request through $.ajax at last.

Here is a custom POST plugin I just coded.

(function( $ ){
  $.myPOST = function( url, data, success, timeout ) {      
    var settings = {
      type : "POST", //predefine request type to POST
      'url'  : url,
      'data' : data,
      'success' : success,
      'timeout' : timeout
    };
    $.ajax(settings)
  };
})( jQuery );

Now the custom POST function is ready

Usage:

$.myPOST(
    "test.php", 
    { 
      'data' : 'value'
    }, 
    function(data) { },
    5000 // this is the timeout   
);

Enjoy 🙂

Read more

jQuery each number

Question by Ivan C.

I have a idea to make table like this

 1. something1
 2. something2
 3. something3

I have made a table:

<table>
   <tr><td id="rb">...
   <tr><td id="rb">...
</table>

so in #rb is this sequence number starting from 1.

I have this jQuery code but it doesn’t work. Can you pls help 🙂

            $('tr').each(function(index) 
            {
                  $('#rb').append(index);
             });

It just make

       012345

in the first #rb

Thanks in advance!

Answer by Starx

You should not give same id to more than one element within same HTML document. You can do the same using class="rb" instead.

$('tr').each(function(index) {
   $(this).find('.rb').append(index);
});
Read more

CSS – Group Multiple selectors to apply to one ID

Question by Paul

We wish to setup a mobile stylesheet for a client – we can only include one stylesheet due to the system setup.

Each page has an ID and due to the design of the site we would like to be able to target elements in each page slightly differently.

I know we can do this by targeting classes via #home .classname{}, but wondered if there was any way to group a bunch of classes under one prefix rather than individually such as –

 #home{
  .classname1{}
  .classname2{}
  .classname3{}
 } 

I’ve never read anything about this, just thought it’s something that would be really useful in our case and has probably been thought about in the past?

Cheers
Paul

Answer by Starx

You are looking for CSS Extension Library. The two most popular are:

  1. LESS
  2. SASS

Using either one of them, you can combine your style in this way

#home{
  .classname1;
  .classname2;
  .classname3;
 } 
Read more

How can I force two elements to always stay on the same line in a <td>

Question by Bogdan

The code is pretty simple:

<table id="tabel_user" style="width: 100%; border: 0; background-color: white;" cellpadding="0" cellspacing="0">
<tr>
    <td style="border: 0; padding: 0; padding-left: 5px;">
        <label for="abcd"><input class="check_useri" id="abcd" name="abcd" type="checkbox" /> abcd </label>
    </td>
</tr>
</table> 

They stay neatly on the same line unless the text in the label gets really long and the table needs to stretch to accomodate it, then the text sometimes gets forced below the checkbox. How can I stop it from doing that?

Answer by Lukas Eder

You can force inline elements to stay on the same line using the CSS property white-space:

<td style="white-space:nowrap;">
  this content will not be wrapped
</td>

Answer by Starx

Defining a white-space may be one of the way to get it done, but as a tabular data, its better if you fix the max size of width of the label instead

label { width: 100px; display: inline-block; }
Read more
...

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