Simon Tite 15 Light Poster

Sounds to me like an HTML problem, if you can see the characters correctly in, say, phpadmin, that would mean that the database is storing and retrieving them correctly.

Your HTML code has to tell the browser which character set it is using, a line in the <head> section such as this

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

might help. (the xhtml+xml bit may not be applicable to your site, you may have to investigate the alternatives).

Of course, if your site is already correctly displaying utf-8 characters (characters which did not come from mySql), then probably the html is not at fault...

Simon Tite 15 Light Poster

Thank you both for your efforts. Aaargh!

I can't understand why not, but neither solution works to double-count the slash, I'm afraid.

Essential, yours has an additional quirk in that it doesn't begin counting till the 2nd character--not a big deal, by itself. Anyway I've posted your work here. I will study it some more.

Simon, you are absolutely right of course about the redundant lines. So it all boils down to the below--which at least does the basic count properly, though for some reason it doesn't count the slashes twice. :-/

function CheckFieldLength(fn,wn,rn,mc) {
    var str = fn.value;  // This needs to be whatever's in the text box
    var pos = str.IndexOf("/");
    var len = fn.length;

    if (pos >= 0) {  
        len = len + str.match(/\//g).length;
    }

    document.getElementById(wn).innerHTML = len;
    document.getElementById(rn).innerHTML = mc - len;
    }

Fourth line needs to be var len = fn.value.length . Now we have two almost working solutions: combining the two, in my function change the middle bit (the if clause) to:

if (pos >= 0) {  
        len = len + str.split(/[\/]/).length;
    }

- this puts Essential's count method (which counts the slashes as double) into your original function (which counts from the first keypress) - then, if you can test it on Firefox (or something not IE!) - if that works, then try on IE.

Simon Tite 15 Light Poster

Nah, I really don't think that's it, I still think that you've got something wrong with your code. Did you try the line I gave you? (That would only work for %address% of course). Perhaps you could give a fuller sample of the code - although I don't mean pages and pages of it, just a bit more than 3 lines, showing how you are initialising the arrays etc.

However, if it is a resource limitation, then here's some settings from my php.ini file:

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

max_execution_time = 30     ; Maximum execution time of each script, in seconds
max_input_time = 60 ; Maximum amount of time each script may spend parsing request data
;max_input_nesting_level = 64 ; Maximum input variable nesting level
memory_limit = 128M      ; Maximum amount of memory a script may consume (16MB)

... but I think you would have to have much lower limits than that to have a program bomb out with just 100 odd array elements, unless they are very long. (160 records x 100 000 bytes each, 16 000 000 bytes) - you don't have records of a hundred thousand bytes. Do you???

If you read that preg_replace is "more powerful" than str_replace, then that is just that it has more powerful search abilities than a simple text replacement, not that it works any faster or has any greater capacity than str_replace.

Simon Tite 15 Light Poster

If I understand your question correctly you have an array of about 100 values, all of which may (or may not) contain the text string "%address%". This array is called $content. Then you want to replace the text "%address% wherever it may be found in $content, with the text in the variable $address. That is you are searching an array, replacing one value ("%address%") with one other value (the text found in $address). If this is the case, then the search and replace arguments should be simple variables, not arrays. Thus: $content = str_replace("%address%",$address,$content); (If I have misunderstood the question, perhaps you could clarify it?)

The search and replace arguments should only be arrays if they contain many different search/replace combinations, in which case it's like repeating the simpler form once for each element of the search array.

Which should you use, str_replace or preg_replace? Well, RTFM as they say!

From the PHP manual page for str_replace (http://be.php.net/manual/en/function.str-replace.php):

If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of ereg_replace() or preg_replace().

There is no limit (that I know of) to the size of an array, other than that of memory capacity of your computer, and possibly there may also be a limit set in you php.ini file, or somewhere else accessible to the system administrator. Anyway, if there is a limit, it's sure to be a lot more than a paltry 100 or so records!

Simon Tite 15 Light Poster

You're quite right, Simon: your single line

var len = fn.value.length + fn.value.match(/\//g).length;

does precisely what my awkward two lines did.

While this counts perfectly once a '/' enters the textbox, it unfortunately does no counting till it sees one.

Since the original line

var len = fn.value.length;

by itself counts perfectly when the box has no '/' (i.e. it doesn't know to double for the forward slash), it makes me wonder if an if - else statement might work? Something (that I am clueless how to properly write) sort of like this:

var str= value  // This needs to be whatever's in the text box
var pos=str.IndexOf("\/")
if (pos>=0)
{
var len = fn.value.length + fn.value.match(/\//g).length;
} 
else 
{
var len = fn.value.length
}

Could something like this possibly work? If so, my problem is to get it to read whatever is entered in the box into str. Users will often enter text that has no URL or, most often, a URL at the very end, so they'll want the counter to be keeping a tally from the start even if it doesn't see a slash.

Ok, this looks like it might be that the match(...) actually returns a null value if no match is found, and the length of a null is probably a null, and adding a null to the original length might give a null or zero, which could explain the problem.

So, your latest approach looks promising, however the slash in the IndexOf shouldn't need escaping, …

Simon Tite 15 Light Poster

Just seen the latest version of your script -

// fieldname, warningname, remainingname, maxchars
function CheckFieldLength(fn,wn,rn,mc) {
 var len = fn.value.length;
 var len = len + fn.value.match(/\//g).length;


  if (len > mc) {
    fn.value = fn.value;
    len = len;
  }
  document.getElementById(wn).innerHTML = len;
  document.getElementById(rn).innerHTML = mc - len;

I don' quite know what the if clause in the middle is doing (nothing?), and I don't know whether re-declaring "var len" twice might be causing a problem, but maybe the script should be simplified to

// fieldname, warningname, remainingname, maxchars
function CheckFieldLength(fn,wn,rn,mc) {

 var len = fn.value.length + fn.value.match(/\//g).length;

  document.getElementById(wn).innerHTML = len;
  document.getElementById(rn).innerHTML = mc - len;
}
Simon Tite 15 Light Poster

Now I see more clearly, Simon: your line of code does work, even with the escaped slash: however, the catch is that the script counts nothing until it sees the character that's in your code. In other words, if the string in the textbox lacks a forward slash, the counter does nothing.

I think there was a cross-posting there!

I don't quite understand your problem any more - if the string in the textbox lacks a forward slash, then surely nothing is what it *should* do? Or rather, it should add zero to the count, which is what you want anyway.

Simon Tite 15 Light Poster

You've not just mis-read it have you?

If F is a forward slash, and B is a backward slash, then the line is: len = len + fn.value.match(FBFFg).length; I tested this out by hacking a W3Schools example, which you can find at http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_match_regexp

Just go there, change the lines

var patt1=/ain/gi;
document.write(str.match(patt1));

to

var patt1=/\//g;
document.write(str.match(patt1).length);

..then the example should count the number of slashes in the text line above.

If the example works, and your changed code doesn't, then I'm stuck too. Maybe it's something to do with some other part of the software changing the slash, perhaps also escaping it with a backslash before your subroutine sees it. Maybe, that would explain why a slash counts as two characters?

Guessing wildly here, perhaps if you tried counting occurences of \/ , or using the F B notation as above, for easier reading, try: len = len + fn.value.match(FBBBFFg).length that is F for the regexp opening delimiter, BB for an escaped backslash, BF for an escaped forward slash, F for the regexp closing delimiter, g for global search of the whole string.

Don't you just hate regular expressions?

Simon Tite 15 Light Poster

This is the section of JS code which returns the count:

// fieldname, warningname, remainingname, maxchars
function CheckFieldLength(fn,wn,rn,mc) {
  var len = fn.value.length;
  if (len > mc) {
    fn.value = fn.value;
    len = len;
  }
  document.getElementById(wn).innerHTML = len;
  document.getElementById(rn).innerHTML = mc - len;
}

Here is some code which should add 1 to the length of text for every "/" character found, that is each / character will then be counted twice.

// fieldname, warningname, remainingname, maxchars
function CheckFieldLength(fn,wn,rn,mc) {
  var len = fn.value.length;
////////// Add this line ////////////
  len = len + fn.value.match(/\//g).length;
/////////////////////////////////////
 if (len > mc) {
    fn.value = fn.value;
    len = len;
  }
  document.getElementById(wn).innerHTML = len;
  document.getElementById(rn).innerHTML = mc - len;
}

The code is in the file http://faculty.kutztown.edu/rjensen/js/charCount.js.

Simon Tite 15 Light Poster

Glad it worked for you, alex. It would be nice for me (and for others) if you could flag this thread as "solved". (And maybe give me a plus point for my answer?).

Hope your project goes well,

Simon

Simon Tite 15 Light Poster

This should work (I haven't actually tried it yet though)

SELECT file_id, 
	sum(if (status = 0,1,0)) sum0,
	sum(if (status = 1,1,0)) sum1,
	sum(if (status = 2,1,0)) sum2,	
FROM pl_statements
GROUP BY file_id

You don't say which database you are using, the example above should work in MySql, but IF statements are not very standardized between databases. (One other, maybe Oracle or maybe Access, maybe uses IIF instead)

diafol commented: Big hand! Thanks +5
Simon Tite 15 Light Poster

Now, I'm not sure how to answer your question about the SQL engine. I'm using VB6 with Access97 database if that helps.

That's the answer!

Now it's quite a few years since I used the Access database, but I seem to remember that perhaps it doesn't like doing ORDER BY after GROUP BY. The specification of the SQL language used to be simply: SELECT ... FROM ... ORDER BY ... GROUP BY ... HAVING ... - and this means that you will have to use a subquery to get the results you want.

Firstly, maybe you should get the VB6 crap out of the way and just type your queries direct into the Access application. Anyway, that's up to you, but I'll just give you my suggestions as straight SQL statements to save typing all those quotes and underscord!

If you can get to a working query which outputs the correct results, but not sorted by the total points, this is a good starting point. I guess it should look something like this:

SELECT pt.PoolTeamID AS teamid, pt.TeamName AS teamname, Sum(ps.Points) as pts
FROM PlayerStats ps, PoolTeams pt
WHERE ps.Weeknumber = 10
AND ps.Status = 'True'
AND ps.PoolTeamID = pt.PoolTeamID
ORDER BY pt.PoolTeamId, pt.TeamName
GROUP BY pt.PoolTeamId, pt.TeamName

This should give you one line per teamid, with the correct total points for that team. The ORDER BY is required here, because the GROUP BY subtotalling works on change of …

navaidstech commented: Excellent suggestion solved my dilemma +1
Simon Tite 15 Light Poster

Not too sure which Sql engine you are using, but the following syntax should work with most of them:

sSQL = "Select  PS.PoolTeamID, PT.TeamName, Sum(PS.Points) as pts From " _
        & " PlayerStats PS, PoolTeams PT Where  " _
        & " PS.Weeknumber = 10 AND " _
        & " PS.Status = 'True' AND " _
        & " PS.PoolTeamID = PT.PoolTeamID " _
        & " Group By PS.PoolTeamID  " _
        & "Order By pts Desc "

Points to note are:

You want to sort on an aggregated field, sum(PS.Points), so you need to give it an alias, using the "sum(PS.Points) as pts" .. some versions of SQL differ in this syntax, the most common alternative is just use space instead of the word as.

You only need to group on the team id, presuming the team id is unique, and the team name is just a description.

There is never usually any need to group on an aggregated function such as SUM, unless you expect only a few unique discrete values for the sum.

Simon Tite 15 Light Poster

You don't say which programming language you are using to compose your sql statement, but as a first shot I would suggest you program something like this:-

If the search term contains one word:

SELECT * FROM descriptions
WHERE desc LIKE '%firstword%'

If the search term contains two words:

SELECT * FROM descriptions
WHERE desc LIKE '%firstword%'
AND desc LIKE '%secondword%'

and so on, one extra AND clause for each word.

However, (although I've never tried this), there are features in mySql that allow you to create indexes on tables which support "natural language searching", so if you want a more sophisticated search ability check out this part of the mySql manual:

http://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html

---
Sorry, posted this before I saw Darkagn's reply!

darkagn commented: Excellent suggestion and citation. Polite and helpful answer to the OP's question. +3
Simon Tite 15 Light Poster

stripslashes is a function which returns a string. It has no effect on the string itself. A statement such as "stripslashes($input)" has no effect on the $input variable as such. You probably intended something like "$input = stripslashes($input)".
HTH

Simon Tite 15 Light Poster

A clearer example might help. How exactly are you filling the array, how are you outputting the result, what result are you expecting, and what result do you get?
Example:

$total = array();
$total[] = 10; 
$total[] = 15; 
$total[] = 5; 
print ("sum is " . array_sum($total) )

This prints out "sum is 30", which is what I expected!