473,545 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($matchAr ray[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 3181
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($matchAr ray[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_or der_flag($order By){
$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($t mp[1],3);
}else{
// it was in ascending order
$tmp[1]='des'.substr($ tmp[1],3);
}
}

return join($tmp);
}
--
Justin Koivisto, ZCE - ju****@koivi.co m
http://koivi.com
Feb 3 '06 #2
Ok using your function:

[PHP]
if (!function_exis ts('change_firs t_order_flag')) {
function change_first_or der_flag($order By){
$tmp = explode('__SPLI T__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(strtolo wer($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($matchAr ray[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_or der_flag($order By){
$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($t mp[1],3);
}else{
// it was in ascending order
$tmp[1]='des'.substr($ tmp[1],3);
}
}

return join($tmp);
}
--
Justin Koivisto, ZCE - ju****@koivi.co m
http://koivi.com


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

Ok using your function:

[PHP]
if (!function_exis ts('change_firs t_order_flag')) {
function change_first_or der_flag($order By){
$tmp = explode('__SPLI T__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(strtolo wer($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_exis ts('change_firs t_order_flag')) {
function change_first_or der_flag($order By){
$tmp = explode('__SPLI T__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(strtolo wer($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.goo glegroups.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($matchAr ray[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_or der_flag($order By){
$o=$orderBy;
$start=0;
do {
$a=stripos($o, "order by", $start);
if ($a===false) {} else {
$a+=strlen("ord er by ");
$start=$a;
$ascpos=stripos ($o, "ASC", $a);
$descpos=stripo s($o, "DESC", $a);
if ($ascpos===fals e) {
if ($descpos===fal se) {
//nothing to modify
} else { //desc found
$o=substr($o, 0, $descpos-1) . "ASC" . substr($o,
$descpos+strlen ("DESC"));
$start=$a+strle n("ASC");
}
} else {
if ($descpos===fal se) {
$o=substr($o, 0, $ascpos-1) . "DESC" . substr($o,
$ascpos+strlen( "ASC"));
$start=$a+strle n("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
7686
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, which is obviously not very efficient. Does anyone have any suggestions on the best way to approach this, or recommendations on what pitfalls to...
2
2455
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 ordered by: (avg job duration in minutes) times (avg num of times job runs in a given day).
0
1205
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 want the image to show up on only the column sorting on... protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if...
8
3272
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 order, however, that column can also be null, BUT all NON-NULL fields MUST BE FIRST before all NULL fields!) I'm not sure what's happening, please...
16
2264
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
3344
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 ROW_NUMBER() OVER (ORDER BY BaseTours.DateCreated DESC) AS RowNumber
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.