I'm having a problem with a database query. The paragraph labeled problem gets right to the problem. The stuff before that is for background and context.
Ok, so I have a website for a group of musicians that allows artists to put their music up for download. Each of the five artists has a Menu that I want to be displayed on any content that belongs to them. Ie, their bio, their shows page and any album or song that they contributed to. The albums and songs were simple the function I made uses the following code:
<?php
function mytheme_artist_nav_visibility($artist) {
//this part checks for artist tags
$i = 0;
$nodeTerms = array_chunk(taxonomy_node_get_terms_by_vocabulary(arg(1),3),1,false);
$n = 0;
//make sure that this is a standard node/$nid path
if (arg(0) == 'node') {
foreach ($nodeTerms as $nodeArtist) {
if ($nodeArtist[0]->name == $artist) {
//if any of the artist tags matches the input artist, 1 is added to $i
//this makes sure that $i will return true even if there is a true and then a false
$i++;
}
$n++;
}
}
I don't know if that's the best way to do it, but it works, so whatever. What I've been struggling with is something that checks the url alias to see if that page belongs to a certain artist. For example, for an artist named "The Clash", the above code would see if the node has an artist tag "The Clash" while the below code would check to see if the url a visitor went to is "mysite.com/theclash/*".
//this part tests the path alias
$artistLower = strtolower(str_replace(' ','',$artist));
$whereLike = implode(array('%',$artistLower,'%'));
$src = implode(array(arg(0),'/',arg(1)));
$result = db_query("SELECT * FROM url_alias WHERE src='$src' AND dst LIKE '$whereLike'");
if ($i || (db_fetch_array($result))) {
return TRUE;
}
}
?>
The Problem
Okay, so the problem I'm having is that my queries aren't returning any results. I'm pretty certain, from playing around with it, that the problem is with my LIKE. But hell, if i query the database directly with the query that prints if I print "SELECT * FROM url_alias WHERE src='$src' AND dst LIKE '$whereLike'";, i get back exactly what I want. I'm thinking of taking drupal's db_query() out of the loop altogether and using mysql_query(), connecting to the database manually if need be.
Comments
I don't see where you have a
I don't see where you have a value defined for $artist in your str_replace at line 2 of your second chunk of code... what is that being set too... if not defined... you wouldn't get anything back from the DB...
all the code is part of the
all the code is part of the same function. so $artist is input into the function when it's called.
I think you just need to
I think you just need to change
to
Percent signs (%) are special to db_query, the proposed change is the recommend one. You could also double up the percent signs in $whereLike.
snap
Ok. That sounds famliar. As I posted below, I found another, better solution, but thanks for the info. This will be super useful on other projects.
The solution
Hell yeah.
I went into the block.module file, found the code it uses to take the input in the pages field to make blocks visible or invisible on certain pages. And I found that path= line. Then I just used strstr() to search $path for the artist's lowercase name.