Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Not a member yet? Click here to register.
Forgot Password?

DB Query problem

Asked Modified Viewed 2,967 times
H
HobbyMan
H
Just some Guy
  • Veteran Member, joined since
  • Contributed 1,486 posts on the community forums.
  • Started 91 threads in the forums
  • Started this discussions
asked
Veteran Member

Is there any way to incorporate preg_replace into a db query instead of using multiple instances of REPLACE as in the snippet below?

$result = dbquery("SELECT item_id, name, details, datestamp FROM ".DB_TEST." WHERE status = '0' ORDER BY REPLACE(REPLACE(REPLACE(name,'#',''),'+',''),'/','') ASC");


I want to order the list by name ignoring all non alphanumeric characters.
0 replies

9 posts

C
Chan
C
Chan 0
Lead Developer of PHP-Fusion
  • Super Admin, joined since
  • Contributed 3,842 posts on the community forums.
  • Started 232 threads in the forums
  • Answered 6 questions
answered
Super Admin


$str = preg_replace(.......);
$result = dbquery(......... ORDER BY $str);


would work for you?
Edited by Chan on 05-04-2014 14:59,
0 replies
H
HobbyMan
H
Just some Guy
  • Veteran Member, joined since
  • Contributed 1,486 posts on the community forums.
  • Started 91 threads in the forums
  • Started this discussions
answered
Veteran Member

$string = preg_replace("/[^a-z0-9]+/i", "", name);

added before the query just generates a error...

Use of undefined constant name

ORDER BY preg_replace('/[^a-z0-9]+/i', '', name)


doesn't work either
0 replies
C
Chan
C
Chan 0
Lead Developer of PHP-Fusion
  • Super Admin, joined since
  • Contributed 3,842 posts on the community forums.
  • Started 232 threads in the forums
  • Answered 6 questions
answered
Super Admin

What exactly was need to replace in the order? We need to work around to get that. Are you assembling a filter?
0 replies
H
HobbyMan
H
Just some Guy
  • Veteran Member, joined since
  • Contributed 1,486 posts on the community forums.
  • Started 91 threads in the forums
  • Started this discussions
answered
Veteran Member

Here's the list ~ http://www.gplusphotopages.com/all_pages.php

I want to strip out the numbers & other characters to display them alphabetically.

Eg;
1 World-195 Countries-195 Pictures = W
100 Strangers Project = S

There must be some way of stripping out unwanted characters in a database query. I just can't find the answer.
0 replies
C
Chan
C
Chan 0
Lead Developer of PHP-Fusion
  • Super Admin, joined since
  • Contributed 3,842 posts on the community forums.
  • Started 232 threads in the forums
  • Answered 6 questions
answered
Super Admin

If it's up to me, I won't sort in SQL. (Save me hassle of hours of debugging when entry doesn't meet the preg_match condition later). I'll project them into an array where I can sort the key for more control reasons.



$result = dbquery("SELECT * FROM ".DB." "); // no sorting.

$output = array(); // init

while ($data = dbarray($result)) {

$title = trim(str_replace(range(0,9),'',' $data['title'])); // replace all numbers.

$title = str_replace(" ", "-", $data['title'])); // replace all whitespace to dash.
// now your title is formatted to all the conditions you want..

$output[$title] = $data;

}
sort($output);  // sort them here.

if ($output) {
  foreach($output as $title => $data) {
  // do your stuff here as good as a $data from while loop.
  }
}



Sorry, long approach, but only spend less minutes in debugging later.
0 replies
H
HobbyMan
H
Just some Guy
  • Veteran Member, joined since
  • Contributed 1,486 posts on the community forums.
  • Started 91 threads in the forums
  • Started this discussions
answered
Veteran Member

Many thanx, I seem to finally cracked it thanx to your help @hien
However, I had to play around with it a lot and tweak it quite a bit.

I tried many different things but eventually only ksort would output them as needed.

$result = dbquery("SELECT name FROM ".DB_TABLE." WHERE status = '0'"); // No sorting.
$output = array();
while ($data = dbarray($result)) {
   
$name = strtolower($data['name']); // lowercase
$one = preg_replace("/[^a-z]/i", "", $name); // Strip out non-alphanumeric chars
$two = trim(str_replace(range(0,9),"", $one)); // Strip numbers - possibly redundant
$three = str_replace("#", "", $two); // Strip Hash

$output[$three] = $data;
  }

ksort($output, SORT_NATURAL);
if ($output) {
  foreach($output as $three => $data) {
  echo "<b>".$data['name']."</b><br />\n";
  }
}


Thanx again :D


Update

Hmm, it works perfectly on my offline setup but when loaded to the site it sorts them by id instead of name.
Back to the drawing board :(
Edited by HobbyMan on 06-04-2014 14:57,
0 replies
C
Chan
C
Chan 0
Lead Developer of PHP-Fusion
  • Super Admin, joined since
  • Contributed 3,842 posts on the community forums.
  • Started 232 threads in the forums
  • Answered 6 questions
answered
Super Admin

Here is a dump function for you. Copy it into your function files. It will help you go through the process.


    function print_p($array) {
    echo "<div>\n";
    echo "<pre style='white-space:pre !important;'>";
    print_r($array);
    echo "</pre>";
    echo "</div>\n";
}




$output = array('0'=>'a', '2'=>'b');
// Usage
print_p($output); // will output formatted array dump

$string = 'text';
$string2 = 'text';
print_p("$string $string2"); // will output 'text text'



See what you get and I believe you can get it done.
0 replies
H
HobbyMan
H
Just some Guy
  • Veteran Member, joined since
  • Contributed 1,486 posts on the community forums.
  • Started 91 threads in the forums
  • Started this discussions
answered
Veteran Member

Thanx, I'll give it a go later on :)
0 replies
C
Chan
C
Chan 0
Lead Developer of PHP-Fusion
  • Super Admin, joined since
  • Contributed 3,842 posts on the community forums.
  • Started 232 threads in the forums
  • Answered 6 questions
answered
Super Admin

Np, but if possible please report back with codes if working k, so we can close the thread if its fixed :)
0 replies

Labels

None yet

Statistics

  • Views 0 views
  • Posts 9 posts
  • Votes 0 votes
  • Topic users 2 members

2 participants

H
H
Just some Guy
  • Veteran Member, joined since
  • Contributed 1,486 posts on the community forums.
  • Started 91 threads in the forums
  • Started this discussions
C
C
Chan 0
Lead Developer of PHP-Fusion
  • Super Admin, joined since
  • Contributed 3,842 posts on the community forums.
  • Started 232 threads in the forums
  • Answered 6 questions

Notifications

Track thread

You are not receiving notifications from this thread.

Related Questions

Not yet