...

Hi! I’m Starx

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

SQl Statement Sum

Question by helloman

Hey guys I am trying to sum these tables and I can’t figure out what I’m doing wrong

My first table is:

Reserve
ReserveID   MembershipID   PlayerCount   Time    CourseID

My second table is

Courses
CourseID   Name

My sql statement that I am trying is here:

Select Sum(Reserve.Player_Count)Total
From Reserve 
Left Join Courses 
On Courses.CourseID = Reserve.ReserveID 
Where Time = "2012-04-09 07:10:00" 
And Courses.Name = "Lake" (or I had Courses.CourseId = "1")

Answer by Starx

You seem to mixing alias. Avoid them

Select Sum(Reserve.Player_Count)Total
From Reserve 
Left Join Courses 
On Courses.CourseID = Reserve.ReserveID 
Where Reserve.Time = "2012-04-09 07:10:00" 
--     ^ Right here  
And Courses.Name = "Lake" (or I had Courses.CourseId = "1")
Read more

Only update fields that are not null

Question by user1325305

I have been trying to figure out how to update the fields only if they are not null (otherwise NULL overrides the unchanged fields). It doesn’t matter that the fields can not be null.

I have tried both the COALESCE and ISNULL, however I must not be using them in the right way as I’m not use to the Dreamweaver coding. The code is below:

<i>if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "catalogue")) {
  $updateSQL = sprintf("UPDATE catalogue SET catbox1=%s, catbox2=%s, catbox3=%s, catbox4=%s, catbox5=%s, catbox6=%s WHERE id=%s",
                       GetSQLValueString($_POST['catbox1'], "text"),
                       GetSQLValueString($_POST['catbox2'], "text"),
                       GetSQLValueString($_POST['catbox3'], "text"),
                       GetSQLValueString($_POST['catbox4'], "text"),
                       GetSQLValueString($_POST['catbox5'], "text"),
                       GetSQLValueString($_POST['catbox6'], "text"),
                       GetSQLValueString($_POST['id'], "int"));</i>

Answer by Starx

There are few mistakes in you code, But these can be typos.

  1. NO PHP Wrapper: You have include your mysqlcode with portion
  2. Use IS NOT NULL instead of ISNULL

Basic way of what you are trying to do is use a query like this

UPDATE `tablename` SET `fieldName` = 'value' WHERE `fieldName` IS NOT NULL
Read more

What is this PHP Syntax: ($variation_id>0) ? $variation_id : $item_id;

Question by user1325258

Is someone able to explain the meaing of the following statment, and the type of php it is reffering to so I can do further research:

$foo = ($variation_id>0) ? $variation_id : $item_id;

I have tried search but don’t really know what i’m searching for.

What I am trying to find out is the name and meaning of the following syntax
? / : and is ($variation_id>0) just a shorthand if statement ?

— I just stumbled upon conditional variables although a nice simple explanation would still be appreciated.

Answer by LAW

That is a ternary condition. If the condition on the left before the ‘?’ is true, it executes the statement after the ‘?’ and before the colon. if not, then it executes the statement after.

In english, this says “use the variation id if it is greater than zero, else use item id”

Answer by Starx

That structure is called ternary structure and in a short form of If … Else

Your Snippet:

$foo = ($variation_id>0) ? $variation_id : $item_id;

Converts to

if($variation_id>0) {
   $foo = $variation_id;
} else {
   $foo = $item_id;
}

Basically, the syntax will come down to something like this

$variable = (<CONDITION>) ? <TRUE EXPRESSION> : <FALSE EXPRESSION>;

You can also combine multiple ternary structure in one line, but it better if you use normal if, if this is over complicated.

Read more

Jquery Toggle Slider & Toggle Text

Question by Jay J.

I’m trying to slide down a div after the “Show” link is clicked and change that text to “Hide” but it doesn’t seem to work so well.

<div id="mobilemenu"><a id="#menutoggle" href="#">Show</a></div>
<div id="slider">Content to slide in under #mobilemenu</div>

I tried solving with some of the other StackOverflow answers, but I couldn’t get it to work so not sure what I’m doing wrong.

Answer by Starx

Update your anchor. Remove the # from the id and attach an event handler.

$("#menutoggle").click(function() {
    $("#slider").toggle();
});
Read more

$.ajaxSetup does not set content type for Get requests

Question by Deeptechtons

Code 1

$.ajax({url:"1.aspx/HelloWorld",type:"GET",dataType:"json",contentType:"application/json"});

Request Response for Code1

Code 2

$.ajaxSetup({
   contentType: "application/json",
   dataType: "json"
});

$.get("1.aspx/HelloWorld","",$.noop,"json");

Request Response for Code 2

Code1 effectively sets both the content-type and datatype
Code2 does not set the content-type Is this Intended or Have i to do Voodoo stuff for making it work ?

Answer by Kevin B

I would just create a quick wrapper for the ajax method.

$.myAjax = function(url,data){
    return $.ajax({
        contentType: "application/json",
        url: url,
        data: data || {},
        type: "GET",
        dataType: "json"
    });
}
// used with
$.myAjax("foobar.asp").done(function(data){
    console.log(data);
}).fail(function(){
    console.log(arguments);
});

The reason that the header isn’t getting passed is that if the contentType isn’t specified for the given request and there is no data, the contentType is not set. It may be a bug since the contentType was set in the ajaxSetup, but I’m not positive on that.

Answer by Starx

$.ajaxSetup hold the default options for most of the all ajax request, but does not send an ajax request it self.

But $.ajax is the actual function that sends the request.


On Code 1:

It simply sends a GET Ajax request with comprehensive method $.ajax()

On Code 2:

The default options for all the ajax request are set before any request are sent. Then when the actual request is made using $.get, this parameters no longer have to be defined.


Update

This seems to be a bug. Such problem occurs when you are sending $.get request without any data. Check here. You need to see the request headers using firebug or similar.

Read more

From Database language to PHP

Question by FGatlin

I need some help please. I have this from my database:

SELECT DISTINCT 
    iwi.item Item, ppd.item_des Des, iwi.qty_on_hand QOH, iwi.qty_allocated QA, 
    iwi.qty_on_order QOO, iwi.min_qty Minimum, MAX(ppm.expected_date)Exp_Date
FROM inv_warehouse_items iwi, pur_po_master ppm, pur_po_detail ppd)
WHERE IWI.CO = '100'
AND iwi.item_key = ppd.item_key
AND ppm.po_key = ppd.po_key
AND iwi.warehouse = '01'
AND iwi.item IN ('BXHP335',
'BXHP435',
'BXHP535',
'BXHP644',
'BXHP743',
'BXHP965',
'BXHP10741',
'BXHP10748',
'BXHP1253',
'BXHP1257',
'BXHP121210',
'BXHP151410',
'BXHP16114',
'BXHP181411',
'BXHP241612',
'BX1195',
'BX6636',
'BXHP201512',
'BXHP1949',
'BXHP2015',
'BXHP201515',
'BXHP351011',
'BXHP241814',
'BXHP2257',
'BXHP1824')

GROUP BY iwi.item, iwi.qty_on_hand, iwi.qty_allocated, iwi.qty_on_order, 
     iwi.min_qty, ppd.item_des
ORDER BY iwi.item"

And I need it to print out with php.

Answer by Starx

May be like this

$query = "...";
$result = $mysqli_query($db, $query);
while($row = mysqli_fetch_assoc($result)) {
    print_r($row);
}
Read more

How to target this HTML in CSS?

Question by MotiveKyle

I’m trying to style Vanilla Forums and I just can’t seem to figure out to select this class <li class="Item Announcement"></li> so that I can style it.

I don’t know why it’s being so difficult. Why would this not work?

.Item Announcement {
background-color: #FFF;
{

Answer by j08691

Try:

li.Item.Announcement {
    background-color: #FFF;
}

That list item has two classes applied to it (Item and Announcement). So to target that with CSS, you need to prefix each class with a period and then remove the spaces. Leaving the spaces in the CSS selector would apply it to a descendant element that had the class.

Quick jsFiddle example.

Answer by Starx

The problem is that with spaces, its is no longer one selector but two, so you need to select using multiple class selector.

.Item.Announcement {
    background-color: #FFF;
}
Read more

Merging $.get and $.ready

Question by cwolves

Put simply I have an ajax call that sometimes completes before the page is loaded, so I tried wrapping the callback in $( fn ), but the callback doesn’t fire if the page is loaded… Anyone have a good solution to this?

$.get( '/foo.json', function( data ){
    $( function(){
        // won't get here if page ready beats the ajax call

        // process data
        $( '.someDiv' ).append( someData );
    } );
} );

And yes, I know that if I flipped the order of those it would always work, but then my ajax calls would be needlessly deferred until the document is ready.

Answer by Bergi

No, functions which are added to onDOMready will always be executed, and will instantly be executed if the event has already happened. See the docs at http://api.jquery.com/ready/: Only if you would use $(document).bind("ready") it will not get executed when the event had already fired.

A possible reason for non-firing event handlers are other handlers throwing an error. jQuery has an internal array for to-be-executed handlers, and the method which handles the native DOM load event just iterates over it. If one of the earlier handlers rises an error, the loop breaks and other handlers added afterwards will not be invoked.

Answer by Starx

It wont hurt to do this.

var getData;
$.get( '/foo.json', function( data ){
    getData = data;
});
$(function() {
    var intt = setTimeinterval(function() {
         if(getData.length) {
              //do your task
              clearInterval(intt);
         }
    }, 100);
});
Read more

easyToolTip jquery shows two tables

Question by Behrooz A

I have this code :

<td><table class="inner">
                        <?php 
                        foreach($inner_info as $k=>$v)
                        {
                            if($v['type']=="pardakhti"){
                                echo "<tr><td title='<table>
                                <tr>
                                <td><strong>نوع پرداخت</strong></td>
                                <td>".convert_pay_type($v['pay_type'])."</td>
                                </tr>
                                <tr><td>تاریخ</td><td>".$v['date']."</td></tr>

                                </table>'>".$v['amount']."</td></tr>";
                            }
                            else{
                                echo "<tr><td>0</td></tr>";
                            }
                        }
                        ?>
                    </table>
                </td>

and the jquery part is :

 $(document).ready(function(){  $("td").easyTooltip();
    $("td").easyTooltip({       tooltipId: "easyTooltip2",  });      });

and CSSes are :

#easyTooltip2{
padding:5px 10px;
    border:1px solid #195fa4;
    background:#195fa4 url(bg.gif) repeat-x;
    color:#fff;
    }
#easyTooltip2 table{
    margin: 0px;
    font-size: 10px;
}
#easyTooltip2 td{

    font-size: 10px;
    border:none;

}
    .inner td {
    text-align: center;
    height: 10px;
    box-shadow: none;

    padding: 10px;
}

but it appears wrongly, the screenshot link : The screeenshot

what’s wrong here?
as you can see there is an underlay table with wrong width and height and padding that should completely be removed . why is there two tables?

thank you in advance

Answer by Starx

That is just plain wrong. A whole table as an atrribute??

<td><table class="inner">
        <?php 
        foreach($inner_info as $k=>$v)
        {
            if($v['type']=="pardakhti"){
                echo "<tr><td><table>
                <tr>
                <td><strong>نوع پرداخت</strong></td>
                <td>".convert_pay_type($v['pay_type'])."</td>
                </tr>
                <tr><td>تاریخ</td><td>".$v['date']."</td></tr>

                </table>'".$v['amount']."</td></tr>";
            }
            else{
                echo "<tr><td>0</td></tr>";
            }
        }
        ?>
    </table>
</td>

Even with this, there are two tables coming up.

I suggest this markup

<td>
        <?php 
        foreach($inner_info as $k=>$v)
        {
            if($v['type']=="pardakhti"){
                echo "<strong>نوع پرداخت</strong>".
                convert_pay_type($v['pay_type']).
                "<span>تاریخ</span>".
                $v['date'].
                $v['amount'];
            }
            else{
                echo "0";
            }
        }
        ?>
</td>
Read more

Can not insert css image into pseudo :before

Question by swan

I am trying to insert a background image into a header:before element, but jquery fails.

HTML

content

 <a href="image.jpg" class="image-source">Try insert image into .header:before</a>

JS

 $('.image-source').click(function() {
   var src = $(this).attr('href');
    // slash is okay
    $('.header:before').css('backgroundImage', 'url(/' + src + ')'); 
    return false;
 });

CSS:

.header {
  position: relative;
  background: url(anotherimage.jpg) no-repeat;
}
.header:before  {
  position:absolute;
  top:0;
  left:0;
  right:0;
  bottom:0;
  margin:auto;
  min-width:50%;
  min-height:50%;
  content: "";
}

Does anyone know if I am dealing with a bug here, or missed an obvious?
Note, I can not use .header itself, because I am working with multiple background image, thats why I need :before.

UPDATE:
Might be this:

Answer by Starx

You cannot manipulate Pseudo Elements using jQuery. They are not part of DOM. You might want to avoid ugly workaround for this as well.

One of the most ugliest but my best workaround is this

var css = '.image:after { content: url("https://www.google.com/images/srpr/logo3w.png");  width: 400px; height: 100px; }';

$("#replaceable").html(css);
$("div").addClass('image');

$("div").click(function() {
    var css = '.image:after { content: url("http://www.google.com/logos/2012/elias_lonnrot-2012-hp.jpg");  width: 400px; height: 100px; }';
    $("#replaceable").html(css);
});

Demo

Read more
...

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