Problem with php query to find ‘related entries’...
Posted: 21 August 2004 09:25 AM   [ Ignore ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23522
Joined  05-20-2002

On my single entry pages, php parsed on output, I’m pulling in ‘related entries’ with a php query.  These are story reviews and recs- and I want to pull up entries with the same title (which is the story title) and all titles by the same author- author being a custom field {auth}, NOT the author of the article.

Works fine unless I have a ’ in the title.  Now, I know this probably means I need to use stripslashes(), but I’m fuzy on how exactly I need to change the code.  Here’s the relevant bit:  (it’s inside a weblog tag)

?php
$myid= ‘{entry_id}’;
$mytitle= ‘{title}’;
$myauth= ‘{auth}’;
global $DB;

echo ‘<br><br><b>Related Entries:</b><ul>More entries on <i>’;
echo $mytitle;
echo ‘</i>’;

$sql = “SELECT title, weblog_id, url_title FROM exp_weblog_titles WHERE title = ‘$mytitle’ AND entry_id != $myid”;
$query = $DB->query($sql) or die (“Invalid query”);
     
      foreach($query->result as $row) {
      $title2 = $row[‘title’];
          $weblog_id2 = $row[‘weblog_id’];
          $url_title2 = $row[‘url_title’];

if ($title2 == ‘’)
{
echo ‘<li>None</li>’;
}
else
{
echo ‘<li>’;
echo $title2;
echo $url_title2;
echo ‘</li>’;
}
}

That’s the basic gist of it.  Works fine as long as there are no special characters to escape- example - but not with a title like Life’s Aftermath.

Like I say, I pretty much know what the problem is, I’m just not dead sure how to fix it.  I dinked around a bit, but thought I’d ask those who actually know what they’re doing.  Any hints- greatly appreciated.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 21 August 2004 09:45 AM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  12375
Joined  04-29-2002

$value = stripslashes($value);

where $value is what you’re wanting to strip out the slashes.

 Signature 

Quick Reference - EE Trial Options - EE Wiki - Docs for updating a build

Profile
MSG
 
 
Posted: 21 August 2004 11:32 AM   [ Ignore ]   [ # 2 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23522
Joined  05-20-2002

Sue- thanks for the quick response, but the trouble is I’m not exactly certain where to strip the slashes.  I’ve tried a number of different (and increasingly nonsensical) approaches.  Here’s what I’d started off doing:

$sql = “SELECT title, weblog_id, url_title FROM exp_weblog_titles WHERE stripslashes(title) = ‘$mytitle’ AND entry_id != $myid”;
$query = $DB->query($sql) or die (“Invalid query”);
     
      foreach($query->result as $row) {
      $title2 = $row[stripslashes(‘title’)];
          $weblog_id2 = $row[‘weblog_id’];
          $url_title2 = $row[‘url_title’];

Which made the most sense to me- I believe this one threw an error.  I’ve added ’ s and ” s and I’ve tried addslashes to the mytitle to pull the variables and then strip the slashes before echoing- basically, I’ve just started randomly changing the code (which works for my surprisingly often!).

For example:

$mytitle = addslashes($mytitle);
echo ‘</i>’;

$sql = “SELECT stripslashes(title), weblog_id, url_title FROM exp_weblog_titles WHERE title = ‘$mytitle’ AND entry_id != $myid”;
$query = $DB->query($sql) or die (“Invalid query”);
     
      foreach($query->result as $row) {
      $title2 = $row[‘title’];
          $weblog_id2 = $row[‘weblog_id’];
          $url_title2 = $row[‘url_title’];

Yea- that throws an error!

Anyway- any more advice on where I should change things up?  I understand the logic of what I’m trying to do, but the little syntax detials of how to link it all together are jamming me up.  Appreciate the help- this is driving me batty!

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 21 August 2004 11:39 AM   [ Ignore ]   [ # 3 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  12375
Joined  04-29-2002

I’ll do do some fiddling on my site, and see if I can do something similar for ya.

 Signature 

Quick Reference - EE Trial Options - EE Wiki - Docs for updating a build

Profile
MSG
 
 
Posted: 21 August 2004 11:46 AM   [ Ignore ]   [ # 4 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23522
Joined  05-20-2002

Man- thanks Sue, I know you must be swamped with real work!  It’s just frustrating because I bloody well should be able to figure it out, but nothing I’ve tried has worked.  I’m probably missing something really obvious- but I’m missing it consistently!

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 21 August 2004 01:00 PM   [ Ignore ]   [ # 5 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  12375
Joined  04-29-2002

I got it working on my site..

Here’s what’s really stored in the database:

entry_id title url_title
121 Sue\‘s Related Entries sues_related_entries
122 Sue\‘s Related Entries related_entries1

I cheated a bit and didn’t use the exp:weblog:entries stuff..

But, when you get the {title} returned back to you, it is missing the slashes.
So, you’d add slashes to {title}

I have $mytitle = “Sue’s Related Entries”, and you’d have the {title} equivalent of “Sue’s Related Entries”


<?php
$myid= ‘121’;
$mytitle= “Sue’s Related Entries”;
//{title}
$mytitle= addslashes($mytitle)
//to put the slashes back in

$myauth= ‘{auth}’;
global $DB;

echo ‘<br><br><b>Related Entries:</b><ul>More entries on <i>’;
echo stripslashes($mytitle);

//now you use stripslashes to NOT display the slashes

echo ‘</i>’;

$sql = “SELECT title, weblog_id, url_title FROM exp_weblog_titles WHERE title = ‘$mytitle’ AND entry_id != $myid”;
$query = $DB->query($sql) or die (“Invalid query”);

foreach($query->result as $row) {
$title2 = $row[‘title’];
$weblog_id2 = $row[‘weblog_id’];
$url_title2 = $row[‘url_title’];

if ($title2 == ‘’)
{
echo ‘<li>None</li>’;
}
else
{
echo ‘<li>’;
echo “$title2 - ” ;
echo $url_title2;
echo ‘</li>’;
}
}
?>


See it here: Related entries test for Rob1

 Signature 

Quick Reference - EE Trial Options - EE Wiki - Docs for updating a build

Profile
MSG
 
 
Posted: 21 August 2004 01:04 PM   [ Ignore ]   [ # 6 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  12375
Joined  04-29-2002

Or you can keep the $mytitle and do this:

$sql = “select title, weblog_id, url_title from exp_weblog_titles where title = addslashes(”$mytitle”) and entry_id !=$myid”;

Did that make it easier or more complicated?

 Signature 

Quick Reference - EE Trial Options - EE Wiki - Docs for updating a build

Profile
MSG
 
 
Posted: 21 August 2004 01:06 PM   [ Ignore ]   [ # 7 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  12375
Joined  04-29-2002

the addslashes there might not work..
Try this instead:

$stitle = addslashes($mytitle);

then..

$sql = “select title, weblog_id, url_title from exp_weblog_titles where title = “$stitle” and entry_id !=$myid

 Signature 

Quick Reference - EE Trial Options - EE Wiki - Docs for updating a build

Profile
MSG
 
 
Posted: 21 August 2004 01:27 PM   [ Ignore ]   [ # 8 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23522
Joined  05-20-2002

Man, thanks Sue- I’d have never figured this out.  Actually, it looks like the main problem was generated by my use of:

$mytitle= ‘{title}’;

When I tried modifying what I’d done based on your code, it didn’t work.  So… I just flat copied what you had (missing a closing ; up there somewhere, but otherwise, direct copy).  That, of course, worked.  However, when I went in and changed:

$mytitle= “Sue’s Related Entries”;
back to
$mytitle= ‘{title}’;

It DIDN’T work again.  Argh!  Which is really weird- because it works as long as there are no ’ s in the title.  But at least now I knew what the issue was, so I did:

$myid= ‘{entry_id}’;
global $DB;

$sql = “SELECT title FROM exp_weblog_titles WHERE entry_id = $myid”;
$query = $DB->query($sql) or die (“Invalid query”);

foreach($query->result as $row) {
$mytitle = $row[‘title’];
}

$mytitle= addslashes($mytitle);
//to put the slashes back in

$myauth= ‘{auth}’;
global $DB;

echo ‘<br><br><b>Related Entries:</b><ul>More entries on <i>’;
echo stripslashes($mytitle);

//now you use stripslashes to NOT display the slashes

echo ‘</i>’;

$sql = “SELECT title, weblog_id, url_title FROM exp_weblog_titles WHERE title = ‘$mytitle’ AND entry_id != $myid”;
$query = $DB->query($sql) or die (“Invalid query”);

foreach($query->result as $row) {
$title2 = $row[‘title’];
$weblog_id2 = $row[‘weblog_id’];
$url_title2 = $row[‘url_title’];

if ($title2 == ‘’)
{
echo ‘<li>None</li>’;
}
else
{
echo ‘<li>’;
echo “$title2 - ” ;
echo $url_title2;
echo ‘</li>’;
}
}

Which is horribly, horribly inelegant, but since it’s a single entry page, I figured it doesn’t matter much.  What’s REALLY odd is even though I’m pulling $mytitle from the database, I STILL have to add the slashes.  In fact, if I print it without adding the slashes, the slashes aren’t there.  Freaky.

Um- long story short- thanks a bunch.  I just couldn’t localize the problem!

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 21 August 2004 01:29 PM   [ Ignore ]   [ # 9 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23522
Joined  05-20-2002

Ack- you post fast!  I’ll dink around with the two other options- but it was definitely something to do with how I was defining ‘mytitle’.  Oddness.

But yes- I can definitely make it work now!

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 21 August 2004 01:49 PM   [ Ignore ]   [ # 10 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  12375
Joined  04-29-2002

Some sites have stripslashes already turned on.. that may be the difference.

So add or remove slashes as needed, but the $stitle is a workaround. smile

 Signature 

Quick Reference - EE Trial Options - EE Wiki - Docs for updating a build

Profile
MSG
 
 
Posted: 21 August 2004 01:53 PM   [ Ignore ]   [ # 11 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23522
Joined  05-20-2002

Ah- bet that’s it.  And yep, I’ll play with $stitle because it makes a lot more sense than doing two queries.  Whew!!  Thanks again, because this is really something I wanted to be able to do!

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64907 Total Logged-in Users: 38
Total Topics: 81852 Total Anonymous Users: 34
Total Replies: 440056 Total Guests: 235
Total Posts: 521908    
Members ( View Memberlist )