Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 943633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:27:57+00:00 2026-05-15T22:27:57+00:00

I’m a bit of a PHP newbie so go easy on me :) I

  • 0

I’m a bit of a PHP newbie so go easy on me 🙂

I am trying to return a category name for a searched entry, after determining whether the category is the lowest child.

My sql statement returns multiple duplicate entries (for each category and sub category that it is in – i.e. if an entry is in Cars > Parts > Engine > Crankshafts, it would appear 4 times, any of you who use Expression Engine should know that it does this 🙂 )

This is the SQL statement which returns the results:

SELECT wd.field_id_5, wd.field_id_7, wd.field_id_14, wd.field_id_15, wd.field_id_18, wd.field_id_20, wt.entry_id, wt.title, wt.url_title, cp.cat_id, c.cat_name, c.cat_url_title, c.parent_id FROM exp_weblog_data AS wd LEFT JOIN exp_weblog_titles AS wt ON wd.entry_id = wt.entry_id LEFT JOIN exp_category_posts AS cp ON wt.entry_id = cp.entry_id LEFT JOIN exp_categories as c ON cp.cat_id = c.cat_id WHERE wt.title LIKE '%$term%'

The following code is to try to derive the deepest child category, so as not to display the entry 4 times.

foreach ($query->result as $row)
{

  $entry_id = $row['entry_id'];
  $title = $row['title'];
  $url_title = $row['url_title'];
  $cat_id = $row['cat_id'];
  $cat_name = $row['cat_name'];
  $category_url = $row['cat_url_title'];
  $parent = $row['parent_id'];
  $image = $row['field_id_7'];
  $image_path = "example.com" . $image;
  $location = $row['field_id_14'];
  $country = $row['field_id_20'];
  $currency = $row['field_id_18'];
  $price = $row['field_id_5'];
  $postage = $row['field_id_15'];

  // if the  entry id doesnt exist in array already
  // add into array (all details)
  if ( ! array_key_exists($entry_id, $entries) )
  {
    $entries[$entry_id] = array($title, $url_title, $cat_id, $cat_name, $category_url, $parent, $image, $image_path, $location, $country, $currency, $price, $postage);
  }
}

$count = 0;

// for each entry in array, run function to find lowest child and display
function determine_child($entry_id, $cat_id, $cat_name)
{
  global $DB, $cat_name;

  $sql = "SELECT c.cat_id, c.cat_name FROM exp_categories AS c INNER JOIN exp_category_posts AS cp ON c.cat_id = cp.cat_id WHERE c.parent_id = '{$cat_id}' AND cp.entry_id = '{$entry_id}'";
  $query = $DB->query($sql);

  if ( $query->num_rows > 0 )
  {

    foreach($query->result as $cat_row)
    {
      $entry_id = $entry_id;
      $cat_id = $cat_row['cat_id'];
      $cat_name = $cat_row['cat_name'];

      determine_child($entry_id, $cat_id, $cat_name);
    }
  }
  else
  {
    return $cat_name;
  }
} // END FUNCTION


foreach ( $entries as $key => $val)
{
  $entry_id = $key;
  $cat_id = $val[2];
  $cat_name = $val[3];
  $cat_name = determine_child($entry_id, $cat_id, $cat_name);

  echo $val[0] . " - " . $cat_name . "<br />";
}

The above code echos out the correct category, but wont return it to the $cat_name variable in the foreach loop at the bottom.

I hope i’ve been clear enough, and thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:27:58+00:00Added an answer on May 15, 2026 at 10:27 pm

    There’s a few things that are a little confusing here.

    a) You shouldn’t be using global $cat_name in your function, since you are passing it as a parameter that changes several times, not having one global variable.

    b) Whenever you call determine_child with a category that does have children, your method doesn’t return anything.

    c) What exactly do you want to do in the case that there are multiple lowest level categories? For example, your determine_child method loops through all children – should it only take the first lowest level category it finds, or should it print out them all?

    If the answer to c is that you want it to just print out one category, then you can simply add a return statement in your if condition: return determine_child($entry_id, $cat_id, $cat_name); – though it would be better to get rid of the for loop and just use the first query result instead.

    If the answer to c is something different, that makes it quite a different problem – so would need to know exactly what you would like the output to be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.