473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

[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

Feb 3 '06 #1
5 3152
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 - ju****@koivi.com
http://koivi.com
Feb 3 '06 #2
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:

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:
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 - ju****@koivi.com
http://koivi.com


Feb 4 '06 #3
comp.lang.php wrote:

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:

> 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:

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

Phil


Change:
$tmp[1] = 'des' . substr($tmp[1], 3);

To:
$tmp[1] = 'des' . substr($tmp[1], 2);

That should do it... I was in a hurry to get out of the office, so i
didn't test it.

Feb 4 '06 #4
Much thanx! That worked!!

Of course, I did a slight variation:

$tmp[1] = 'desc' . substr($tmp[1], 3);

Phil

Justin Koivisto wrote:
comp.lang.php wrote:

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:

> > 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:

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

Phil


Change:
$tmp[1] = 'des' . substr($tmp[1], 3);

To:
$tmp[1] = 'des' . substr($tmp[1], 2);

That should do it... I was in a hurry to get out of the office, so i
didn't test it.


Feb 5 '06 #5

"comp.lang.php" <ph**************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
[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


this function should work as long as asc and desc are not in the column
names, and there is only 1 instance of asc or desc.

<?php
function change_first_order_flag($orderBy){
$o=$orderBy;
$start=0;
do {
$a=stripos($o, "order by", $start);
if ($a===false) {} else {
$a+=strlen("order by ");
$start=$a;
$ascpos=stripos($o, "ASC", $a);
$descpos=stripos($o, "DESC", $a);
if ($ascpos===false) {
if ($descpos===false) {
//nothing to modify
} else { //desc found
$o=substr($o, 0, $descpos-1) . "ASC" . substr($o,
$descpos+strlen("DESC"));
$start=$a+strlen("ASC");
}
} else {
if ($descpos===false) {
$o=substr($o, 0, $ascpos-1) . "DESC" . substr($o,
$ascpos+strlen("ASC"));
$start=$a+strlen("DESC");
} else {
//impossible situation! both ASC and DESC found!
print "error: both ASC and DESC found in ORDER BY
statement"
}
}
//find another column in the ORDER BY series
$c=strpos($o, ",", $start);
if ($c===false) {
break;
} else {
$start=$c+1;
}
}
} while (true);
return $o;
}
?>


Feb 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Wm | last post by:
I'm trying to get a handle on the best way to handle setting up my listings so that I display groups of about 25 records per page. I currently have a page that returns over 1,000 names/addresses,...
2
by: Louis | last post by:
Hi, Does anyone know of a script that will give "weighted job duration"? I want to use it, to identify which jobs are hogging the CPU. That is for a given server, list the sql agent jobs...
0
by: Smokey Grindle | last post by:
I'm trying desperatly to add a sort order arrow to my grid view and have this code below ofr it... but when the code runs the girdview sortexpression is always = "" never the sort name... why?! i...
8
by: phillip.s.powell | last post by:
This query produces the following error: I'm sorry but I must have this "column" in the query, it's vital for required sorting order (you have to sort image_location_country in alphanumeric...
16
by: rodchar | last post by:
hey all, you know when you log in to msdn and go to subscription downloads, the header of the page is fixed and the body of the page scrolls, is that done with frames? thanks, rodchar
1
by: itamar82 | last post by:
I am getting the following error: Microsoft OLE DB Provider for SQL Server error '80040e14' Incorrect syntax near the keyword 'WHERE'. for the sql below: SELECT TourId FROM (SELECT...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.