0

I am working on a project that requires imploding an array with character separation. I have successfully used join and implode interchangeably in other parts of the project, but I can't get it to work in this section.

$dbQuery = "SELECT ftc.*, fc.name
            FROM facilities f
            LEFT JOIN facility_to_category ftc ON f.fid = ftc.fid
            LEFT JOIN facility_categories fc ON ftc.cid = fc.cid
            WHERE f.listing_year = '2011'
                AND fc.parent_cid = '2'
              AND f.fid = ('".$listing_fid."')";
    $dbResult = $dbc->query($dbQuery,__FILE__,__LINE__);
    $num_results = $dbc->num_rows($dbResult);
    echo '<h3>Demographics</h3>
    <div>';
    while($catdata = $dbc->fetch($dbResult)) {
        $demographics = array();
        $demographic_names = array('',trim($catdata->name));
        $demographics = implode('  '.chr(149).'   ',$demographic_names);   
        print $demographics; 
    }

The result is this:

Demographics
• Affluent • Children • Hard-to-Reach • Parents

instead of

Demographics
Affluent • Children • Hard-to-Reach • Parents

I've tried using double quotes instead of single quotes around '.chr(149).'. I've tried using commas or bars or just spaces. I've tried different ways of trimming and not trimming $catdata->name.

I also thought about trying string concatenation, but then I'll end up with an extra character at the end instead of the beginning. Implode or join seem the better way to go.

What am I missing?

1
  • Is $demographic_names[0] blank? Also, why are you setting $demographics = array(); then almost immediately turning it into a string? Commented Jan 24, 2012 at 21:01

5 Answers 5

1

I am not sure I understand what you are trying to do, but why not use

$demographics_names = array();
while($catdata = $dbc->fetch($dbResult)) {
    array_push($demographics_names, trim($catdata->name));
}
$demographics = implode('  '.chr(149).'   ',$demographic_names);   
print $demographics; 
Sign up to request clarification or add additional context in comments.

1 Comment

Moving the implode outside of the while statement helped!
1

Thank you, everyone, for your help! I ended up reformatting the query and array. Here is the final code:

$dbQuery = "SELECT ftc.*, fc.name
            FROM facilities f
            LEFT JOIN facility_to_category ftc ON f.fid = ftc.fid
            LEFT JOIN facility_categories fc ON ftc.cid = fc.cid
            WHERE f.listing_year = '2011'
                AND fc.parent_cid = '2'
              AND f.fid = ('".$listing_fid."')";
    $dbResult = $dbc->query($dbQuery,__FILE__,__LINE__);
    $num_results = $dbc->num_rows($dbResult);
    echo '<h3>Demographics</h3>
    <div>';
    while($catdata = $dbc->fetch($dbResult)) {
        $demographics = array();
        if (is_array($demographics)) {
        $demographic_names[] = trim($catdata->name);
        }
    }
    $demographics = join('  '.chr(149).'   ',$demographic_names);           
    print $demographics; 

The result is:

Demographics
Affluent • Children • Hard-to-Reach • Parents

Hopefully this will be helpful to someone else!

Comments

0

You're inserting a blank element into the array that you're imploding:

$demographic_names = array('',trim($catdata->name));
                           ^--- here

2 Comments

Thank you -- I tried removing that, but it somehow prevented the join character from working at all. The result was AffluentChildrenHard-to-ReachParents
Well this is because you only have 1 element in your $demographic_names array. implode() will only put the join character between 2 elements. Use @JeromeWAGNER's answer.
0

Seems like you have a blank demographic_name. Use array_filter on the resulting array.

Comments

0

Check the first array is empty.

while($catdata = $dbc->fetch($dbResult)) {
    $demographics = array();
    $demographic_names = array('',trim($catdata->name));
    if(!empty($demographic_names))
    {
      $demographics = implode('  '.chr(149).'   ',$demographic_names);   
      print $demographics; 
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.