Monday, February 21, 2011

Using ZK Tree component, how do I remove Treeitems from a Treechildren node

Does anyone know how to remove Treeitems from a Treechildren node in ZK? I have tried using an iterator and removeChild but a ConcurrentModificationException!

List<Treeitem> myTreeItems = treechildren.getChildren();

Iterator<Treeitem> iterator = myTreeItems.iterator();

while (iterator.hasNext()){
   myItem = (Treeitem)iterator.next();
   parent.removeChild(myItem);
}

Any ideas?

From stackoverflow
  • That is not the correct way to remove the items, you need to do something like this.

    while (parent.getItemCount() > 0) {
       parent.removeChild(parent.getFirstChild());
    }
    

    This will provide the functionality that you require!

    More details on using the Tree component are available here.

    Justin.Eckner : Thanks, worked like a charm!
  • As what I saw in your case you want to remove all components which are all attached on a treechildren. I think the fastest way is:

    treechildren.getChildren().clear();
    

    just operate the result like a java.util.List.

php:how to check if string contains any of the listed keyword ?

i have an array of strings.

I have an array of keywords.

i loop through each string, and need to store them to mysql database if it contains any of the keywords.

currently i am using multiple stristr(), which is getting difficult.

is it possible to do something like stristr($string, array("ship","fukc","blah")); ?

From stackoverflow
  • try using in_array()

    for ($i = 0 ; $i < count($arrayString); $i++){
    
      for ($j = 0 ; $j < count($arrayKeyWord); $j++){
    
        if (in_array($arrayString[$i],$arrayKeyWord[$j]) == true){
          /* mysql code */
    
        }
    
      }
    
    }
    
  • I would advice you to use regular expresion for that

    snipet:

    preg_match_all('|(keyword1|keyword2|keyword3)|', $text, $matches);
    var_dump($matches);
    

    see the documentation of preg_match_all for reference

  • $to_be_saved = array();
    foreach($strings as $string) {
      foreach($keywords as $keyword) {
         if(stripos($string, $keyword) !== FALSE){
            array_push($to_be_saved, $keyword);
         }
      }
    

    }

    /*save $to_be_saved to DB*/
    
  • foreach ( $strings as $string ) {
      foreach ( $keywords as $keyword ) {
        if ( strpos( $string, $keyword ) !== FALSE ) {
          // insert into database
        }
      }
    }
    
  • if(in_array($string, array("ship","fukc","blah"))) {

    }

    see http://au.php.net/manual/en/function.in-array.php

What is a resonable maximum DB rows for built-in GridView paging?

I'm using a simple GridView to display some tabular data straight from a SQL Server DB. I know using the built-in paging functionality is inefficient because it pulls the entire dataset on every bind. At the moment that's fine, there are only a few dozen rows.

The data rows themselves are... about 6 nvarchar(50) columns, a couple ints, a couple floats.

The question is, at what point do I need to implement some custom paging? 500 rows? 5000? 50000?

Maybe a tough question to answer. Need more information?

From stackoverflow
  • It really depends on when it starts to slow down and how important it is to you. There is not magic number. You may be able to put indexes and other things in place to speed it up and not have to worry about it for a long time. Ideally, you should never return more than the rows being used.

  • Can you profile with test data? If so I highly recommend just trying increasing amounts of rows until it becomes too slow for you. By doing so you will probably gain some insight you cannot get any other way than trying yourself.

    Having said that, I personally never just bind from the database, I always have a glue object that manages the datasource and gets exactly what is needed for the page. It might very well not be worth spending the time on that for you though.

  • It depends on the number of users hitting the web application as well as the volume of data that needs to be displayed in the grid because by not using custom paging you are putting more load on the web server. Personally I always implement data paging when the volume > 1000 rows.

    Bryan : Finally, an answer with a number in it. :) I'm going to leave it as-is for now. Looks like we'll max out at 5000 rows, which is not a lot of data. I believe the GridView will handle this without trouble. Maybe I'll update this question with a new answer if I find otherwise.
  • Given the choice I would never use it, its not a great deal of work to implement custom paging and will give you a much more scalable solution. If you use the built in provider you 're always going to wonder at what point will it start slowing things down.

CodeIgniter form verification and class

I'm using the form validation library and have something like this in the view

<p>
        <label for="NAME">Name <span class="required">*</span></label>
        <?php echo form_error('NAME'); ?>
        <br /><input id="NAME" type="text" name="NAME" class="" value="<?php echo set_value('NAME'); ?>"  />
</p>

I'd like to add a class to the input that is dependent on the form error so i could have something like this

<input id="NAME" type="text" name="NAME" class="<?php echo $error;?>" value="<?php echo set_value('NAME'); ?>"

I realize that it would have to be a bit more complicated, but I'd like to this this without creating a custom rule callback for every field.

Thanks ~Daniel

From stackoverflow
  • I just realized, there is a way to do this with error delimiters

    $this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
    

    I this makes it much easier.

    But just out of curiosity, Is there any way of doing this with class in the input?

    Thorpe Obazee : What do you mean by class in the input?
    Daniel : Ideally... I'd like to be able to push another variable in. Right now you can set the message to be displayed in the callback. I would like to set an error class to assign css to. This would allow me to style it differently depending on the error.
  • If you set all your validation fields in your controller using:

    $this->validation->set_fields()
    

    You will have access to any possible error messages, which you could use in the following manner:

    <input id="NAME" name="NAME" value="<?php echo $this->validation->NAME; ?>" <?php echo (!empty($this->validation->NAME_error)) ? 'class="error"' : ''; ?> />
    

    http://codeigniter.com/user%5Fguide/libraries/validation.html

    As pointed out in the comments, the code above references the old validation library. The new 1.7 way to perform this task would be:

    <input id="NAME" name="NAME" value="<?php echo set_value('NAME'); ?>" <?php echo (!empty(form_error('NAME')) ? 'class="error"' : ''; ?> />
    
    Thorpe Obazee : This is really really wrong to teach the old validation class. You should go use the new Form Validation class here: http://codeigniter.com/user_guide/libraries/form_validation.html
    cballou : I added a solution for 1.7 as well.

Page not submitting in Firefox

Hi,

I've a user control registered in an aspx page. User control has a textbox and a submit button. Ascx code of user control is placed under a asp:panel. Using firefox, when user hits enter key, the page is not submitting to the server.However, this works fine in IE browsers. Am i missing something here?

From stackoverflow
  • Hi Ed, Try setting defaultbutton="urbuttonid" to the asp:panel

    Pandiya Chendur : Whether ur page is submitting or not when using firefox.... ur question is not clear....
  • Set property DefaultButton="submitbtn" for asp:panel.

    Replace submitbtn with your asp:button id.

  • Yea, what the others say. You've probably got a submit button earlier in the page that Firefox is associating you enter key press with.

    Use:

      <asp:panel .... DefaultButton="btSubmit">
         <asp:textbox ... />
         <asp:button id="btSubmit" ... />
        </asp:panel>
    

jQuery find() returning an empty string in Google Chrome

I'm using jQuery to setup an Ajax request that grabs an XML feed from a PHP script and then parses some information out of the feed and inserts it into the DOM. It works fine in Firefox; however, in Chrome, I am getting an empty string for the title element.

Here's the basic setup of the Ajax request:

$.get('feed.php', function(oXmlDoc) {
  $(oXmlDoc).find('entry').each(function() {
    $(this).find('title').text();
    $(this).find('id').text();
    // do other work...
  }); 
});

For what it's worth, here's the PHP script that's grabbing data from the feed. I'm using cURL because I'm making the request across domains (and because it was a quick and dirty solution for the problem at hand).

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $str_feed_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($curl);
curl_close($curl);

echo $xml;

The XML data is being returned correctly and I can see the values of the sibling nodes in Chrome (such as ID), but, for whatever reason, I continue to get an empty string for the title node.

Edit: As requested, here's a fragment of the relevant XML:

<entry>
    <id>http://TheAddress.com/feed/01</id>
    <title type="text">The Title of the Post</title>
    <author><name>Tom</name></author>
    <published>2009-11-05T13:46:44Z</published>
    <updated>2009-11-05T14:02:19Z</updated>
    <summary type="html">...</summary>
</entry>
From stackoverflow
  • The page you have in the example XML has an HTML entity in the title. This can cause issues. Here is a blog post I found on this issue.

    I wonder if the same goes for other special characters...

    The home page title looks like this:

    <title>The Address Hotels + Resorts</title>
    
  • I haven't tried it, but make sure that the xml is returned with the correct content type (text/xml). You can also set the dataType to xml at the jQuery.ajax(options).

    Tom : I ended up needing to specify the Content-Type as rss+xml in the PHP script. $.get() ended up working just fine after that.
  • Hi! I have the same problem. It seems than Chrome don't handle html tag in ajax. try changing "title" to "booktitle" in the XML and in the JS.

urlencode but ignore certain chars

It is possible to run the urlencode function without converting # or % ?

From stackoverflow
  • There are a number of examples in the comments section on the PHP Docs of urlencode for alternative functions. You could simply take the most appropriate function for your needs and remove any replacements for # and %.

    http://php.net/manual/en/function.urlencode.php

  • Can you not just do:

    $str = urlencode($str);
    $str = str_replace("%23", "#", $str);
    $str = str_replace("%25", "%", $str);
    
    cballou : Mmmm less lines. +1
    Patrick : nice solution!!!
  • I don't think so, but I guess you could replace the equivalent codes from the encoded string back with # and %.

  • As far as I know, it's not possible with the urlencode function itself, but you could do a simple string replacement on the encoded results to achieve that:

    function my_urlencode($input){
       $input=urlencode($input); 
       return str_replace(array("%25","%23"),array("%","#"),$input);
    }