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



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

  • dynamic sql in PL/SQL
    ... IN DATE, i_to_date IN DATE, orderBy IN NUMBER DEFAULT 1, all_joined OUT ... clauses of SQL inside the procedure. ... However it ignores the orderBy parameter inside the procedure. ... Do I have to use dynamic SQL here? ...
    (comp.lang.java.databases)
  • Re: Insert Into Tbl Error
    ... Well after a little deeper research, I see this is a common problem ... I saw a bunch of threads on using the OrderBy ... clause and using Identity Index (a little vague on both of them for ...
    (microsoft.public.access.queries)
  • Re: SQL Confusion in DA.Fill()
    ... >> The SQL that I'm sending to my server doesn't work unless I run it ... >> The Fill method fails whenever OrderBy contains more than one field. ... this works in the ISQL window of my DBMS controller: ... When I set OrderBy to ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: FORMS!reference too complex to run SQL query error
    ... I don't know how I forgot the orderby. ... I added the order by clause and I was good. ... so sorry for my stupidity. ... Prev by Date: ...
    (comp.databases.ms-access)
  • Re: Form is grow
    ... and what is the code with OrderBy?? ... All the filter does is act like a where clause. ...
    (microsoft.public.access.forms)