November 30, 2010

Newbie stuck on a jQuery question…conditional statement

Question by flinx777

So I’m a complete newbie and stuck on a conditional statement. Here’s my code:

<script type="text/javascript">
$(document).ready(function(){
 if($('span.fc_cart_item_price_total') == 0) {
  $(span.fc_info).addClass('foo');
 };
});
</script>

So I’m attempting to see if span with a class of “fc_cart_item_price_total” has a value of “0”, to then add a class of “foo” to the span with a class of “.fc_info”. This code above is not working. Here’s the HTML:

<span class="fc_info">Info 1</span><br />
<span class="fc_cart_item_price_total">$0.00</span><br />
<span class="fc_info">Info 2</span>

Here’s the other challenge I have. I’m trying to select the span with the value of “fc_info” before the span with the class of “fc_cart_item_price_total” but have no idea of how to just select this one span.

Answer by einaros

For each cart item whose price is $0.00, this will add the “foo” class to the preceding (and only the preceding) fc_info.

$(function() {
    $(".fc_cart_item_price_total:contains($0.00)").each(function(i, n) {
        (n=$(n)).prevAll(".fc_info:first").addClass("foo");
    });
});

Answer by Starx

To make the above code working

<script type="text/javascript">
$(document).ready(function(){
 if($('span.fc_cart_item_price_total').html() == 0) {
  $(span.fc_info:first).addClass('foo');
 };
});
</script>

And to select the first class fc_info

Use $(".fc_info:first") as your selector

November 29, 2010

Rename and uploaded file?

Question by acctman

Is there a simple way to rename an upload file? I’d like to append $_POST['bid'] to the front of the file that was uploaded.

Answer by Starx

Rename a already uploaded file

Use rename function http://php.net/manual/en/function.rename.php

It is as easy as

rename ("/folder/file.ext", "/folder/newfile.ext");

Rename a file during upload

move_uploaded_file($_FILE['myfile'][tmp_name],$_POST['bid'].$_FILE['myfile'][name]);

How to move blog title in the middle of the screen?

Question by Startup Crazy

I am very new to blogging.My blog has its title description at the top left but I want it to be in the middle of the screen and the description of the blog beneath the image.Thanks in advance.

EDIT:

I did:

   <title style="text-align:center"><data:blog.pageTitle/></title >

But the title is still at the topleft corner.

Answer by Stephan Muller

The <title> element isn’t used for the title displayed on your website, only for the title in the browser’s top bar.

You’ll have to edit the CSS file and set #titlewrapper { text-align: center }

Alternatively you can add style="text-align: center" in the <div id='titlewrapper'> in your HTML.

Answer by Starx

Well, I got it. The title is wrapped with a <h1> which is by default aligned at the left. so change it’s CSS to text-align:center;

Use this

#titlewrapper h1 { text-align:center; }

What sort of page weight / size should I be aiming for when optimising for mobile?

Question by Simon Scarfe

I’m looking to optimise the mobile browser experience in a small webapp, using the awesome jQuery mobile to do so.

It goes without saying that a user doesn’t want to DL 200k of data, I’m just trying to draw the line between using external and internal URLs. Is there any existing guideline of what sort of page sizes / loading times I should be shooting for? I’d prefer to stick to internal URLs (keep the mobile interface effectively in one place from a maintenance point of view), but am weary of bogging the user down with lots of information that they have no intention of viewing.

Answer by galambalazs

One thing to note is that e.g. iPhone won’t cache components bigger than 25K.

Also consider minifing and gzipping your code. And @jfar got one thing right. HTTP requests can be a huge performance hit, so you may also want to reduce them as much as possible.

Answer by Starx

I would say try to lower your page as much to 100kb, because if u manage to do so loading different pages as per user request might be negligible.

However, loading the big pile of content at a time and using simple javascript show and hide, to display the content in almost realtime manner might impress the viewer too.

November 28, 2010

Deleting an array element with PHP

Question by user478419

I have an array with several elements. The array keys are numeric.

I now want to remove a certain element. All I know is the element’s content, not the key.

Is there an easy way to remove this element from the array?
Or do I need to loop through all elements of the array to get the key and then unset the element with that key?

Answer by Codemwnci

You can use the array search function to get the key

$key = array_search('searchterm', $items); 
unset($items[$key]);

Answer by Starx

sure

foreach($myarray as $key=>$item) {
    if($item=='myknowncontent') { unset($myarray[$key]; }
}
November 26, 2010

Problem with form submission

Question by shalu

I am facing a problem. I am having a form. Its action is ac.php. I want to change the action to sit.php.

For this what i did

   <script type="text/javascript">
     document.getElementById("digForm").innerHTML="<form enctype='multipart/form-data' id='product_addtocart_form' method='post' action='http://localhost/bracelatest2/index.php/checkout/cart/add/uenc/aHR0cDovL2xvY2FsaG9zdC9icmFjZWxhdGVzdDIvaW5kZXgucGhwL2JsYWNrLWJyYWNlLmh0bWw_X19fU0lEPVU,/product/203/'>";
   </script>

<div id="digForm">
    <form action="ac.php">
</div>

Now I am using ajax and changing the inner html of div myform by

    <form action="sit.php">

But when i hit submit form is going to ac.php not to sit.php.

Please help me.

Answer by Starx

If you just want to change the attribute then use

document.getElementById('myform').action = 'sit.php';

Check the demo here Shalu http://jsfiddle.net/bR2KA/

jQuery question regarding if statement and position moving

Question by alexcu

I’m trying to write a program where basically the user clicks on an image and the image will then move to the right by 25px. The image moves when I click on it, but I tried to add a piece of code which makes the image return to the left of the window when it passes the right side of the window. I’ve tried using an If statement in the animation’s procedure but it doesn’t seem to work. Here’s what I have:

$('#image').click(function() {
$(this).animate({
   left: '+=155',
function() {
    if ($(this).left > $(document).width)  {
 $(this).left = 0
  }
   };

    });

});


Am I using the wrong syntax or is my function wrong? Thanks for your help.

Answer by Starx

Where is your duration?

Try this,

$('#image').click(function() {
     $(this).animate(
            {
               left: '+=155'
            },
            5000,
            function() {
                if ($(this).left > $(document).width)  {
                $(this).left = 0
                }    
       );
});

How to insert chinese character in mysql table?

Question by user319198

I want to save data in Chinese language in mysql table.

Can anyone tell me what settings I have to do for it in mysql and PHP.

My table will save data in both English and Chinese some column English other Chinese . Is it possible with single table.

Any help will be appreciated.

Answer by Guixing Bai

Use UTF-8 when you create table

create table xxx ()CHARACTER SET = utf8;

Use UTF-8 when you insert to table

set names utf8; insert into xxx (xx,x);

Answer by Starx

Set the character set to UTF-8, in mysql.

Check this for reference

http://confluence.jetbrains.net/display/TCD/Configuring+UTF8+Character+Set+for+MySQL
http://dev.mysql.com/doc/refman/5.0/en/charset-mysql.html

...

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