Re: need to do a string replace of "asc" to "desc" or "desc" to "asc" first occurrence only



Ok using your function:

[PHP]
if (!function_exists('change_first_order_flag')) {
function change_first_order_flag($orderBy){
$tmp = explode('__SPLIT__HERE__', preg_replace('`(asc|desc)`i',
'__SPLIT__HERE__$1', $orderBy));
if (count($tmp) > 1) {
// there are at least 2 elements
// therefore, it was in there at least once
if (substr(strtolower($tmp[1]), 0, 3) == 'des') { // it was in
descending order
$tmp[1] = 'as' . substr($tmp[1], 3);
} else { // it was in ascending order
$tmp[1] = 'des' . substr($tmp[1], 3);
}
}
return join($tmp);
}
}
[/PHP]

Produced the following MySQL query syntax error:

[quote]
Fatal error: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'des, upper(s.student_firs' at line 1 using query:
[/quote]

I'll keep looking at it but apparently it chops the ORDER BY clause
incorrectly if the original ORDER BY clause contains ".. asc"

Phil
Justin Koivisto wrote:
comp.lang.php wrote:
[PHP]
$orderBy = 's.app_date desc, s.last_name asc, s.first_name asc, s.mi
asc';
if ($_REQUEST['willDesc']) {
$ascArray = array('asc' => 'desc', 'desc' => 'asc'); // ARRAY OF
ALL ORDERING POSSIBILITIES
$junk = preg_match('/([\s\t]+)a|[de]sc(,?.*$)/i', $orderBy,
$matchArray);
$orderBy = substr($orderBy, 0, strpos($orderBy, $matchArray[1])) .
' ' . $ascArray[$matchArray[1]] .
substr($orderBy, strpos($orderBy, $matchArray[1]) +
strlen($matchArray[1]), strlen($orderBy));
}
[/PHP]

Basic premise:

I have a SQL "ORDER BY" clause that will be configured like $orderBy 's
value. However, the very first occurrence of "desc" might instead be
"asc". If the very first occurrence is "asc", it must become "desc";
likewise, if the very first occurrence is "desc", it must become "asc".

I tried Regular Expressions but the pattern failed every single time to
match and replace, so I gave up and tried a string function/RegExp code
combination, also to no avail.

I am not sure how to make this work so I need help figuring out how to
do this.

Thanx
Phil



function change_first_order_flag($orderBy){
$tmp = explode(
'__SPLIT__HERE__',
preg_replace(
'`(asc|desc)`i',
'__SPLIT__HERE__$1',
$orderBy
)
);

if(count($tmp)>1){
// there are at least 2 elements
// therefore, it was in there at least once
if(substr($tmp[1],0,3)=='des'){
// it was in descending order
$tmp[1]='as'.substr($tmp[1],3);
}else{
// it was in ascending order
$tmp[1]='des'.substr($tmp[1],3);
}
}

return join($tmp);
}


--
Justin Koivisto, ZCE - justin@xxxxxxxxx
http://koivi.com

.



Relevant Pages

  • Re: need to do a string replace of "asc" to "desc" or "desc" to "asc" first occurrence only
    ... Produced the following MySQL query syntax error: ... You have an error in your SQL syntax; ... I'll keep looking at it but apparently it chops the ORDER BY clause ...
    (comp.lang.php)
  • Re: CROSS JOIN
    ... > was that depreciating the original FROM .. ... WHERE syntax would never ... > The problem is that the WHERE clause is done after the FROM clause. ... but thats just outer join stuff. ...
    (comp.databases)
  • Re: Extending define with where clause
    ... > parallel to lambda*, which implements automatic handling of keyword ... "where" clause is used that the new syntax really matters. ... introduced with the help of let, letrec, letrec*, or named let -- are ...
    (comp.lang.scheme)
  • Re: Not Exists joining 2 tables
    ... "'code' is a bad name for a key column" is a valid complaint. ... EXISTS clause with a correlated subquery properly, ... SQL+ syntax and start using the ANSI SQL syntax that seems ... Then the only criteria in the where clause ...
    (comp.databases.ms-sqlserver)
  • Re: update query: still having problems
    ... Only the records from Department that have a rate value present in sheet1, ... That is how an INNER JOIN works when there is no duplicated values (in one ... you add an extra WHERE clause. ... Your code throws up a syntax error: ...
    (microsoft.public.access.queries)