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 6357083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:05:48+00:00 2026-05-24T23:05:48+00:00

I’m trying to create a list using a loop within a loop. I have

  • 0

I’m trying to create a list using a loop within a loop. I have 3 tables.

T1: faculty
T2: keywords
T3: facID, keywordID

I’ve created a select statement to cross join the rows and spit out something like this:

Faculty Name A
keyword-a keyword-b keyword-c

Faculty Name B
keyword-a keyword-d keyword-f

Everything works great except I need to add commas to the keyword list and my code isn’t doing the trick. My keywords are still looping through without the comma.

 <?php while ($row = mysql_fetch_assoc($result)) { ?>

 <?php if ($row['facID'] !== $lastID ) { ?>
<?php echo $row['facname']; ?><br />
    <?php $lastID = $row['facID'];  ?> 
 <?php }  ?>

 <?php $kwords = array();
foreach($row as $k => $v) {
     if (strpos($k, 'kword') === 0) {
     $kwords[] = $v;
     }
  } 
  echo implode(', ', $kwords);

  }  ?>

Any suggestions? I’m a noob and I’m hoping it’s something very obvious!

  • 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-24T23:05:50+00:00Added an answer on May 24, 2026 at 11:05 pm

    There seem to be a few issues with your code, so I’ll try to address them all.

    First, you have a lot of opening and closing <?php> tags, and it’s really messing with the readability of your code. Consider keeping as much code as possible contained into a single <?php> code block. For example, instead of this:

     <?php if ($row['facID'] !== $lastID ) { ?>
    <?php echo $row['facname']; ?><br />
        <?php $lastID = $row['facID'];  ?> 
     <?php }  ?>
    

    …you can consolidate all of that PHP code into this:

    <?php 
      if ($row['facID'] !== $lastID ) {
        echo $row['facname'] . "<br />";
        $lastID = $row['facID']; 
      }
    ?>
    

    Next, you’re not outputting any sort of visual break after echoing out your implode()ed array. This would lead to the next heading being output on the same line as the output of your previous heading. For example, your first two headings will end up like this:

    Faculty Name A
    keyword-a keyword-b keyword-cFaculty Name B
    keyword-a keyword-d keyword-f
    

    Notice how Faculty Name B is at the end of the line of keywords?

    Finally, I think the problem you’re reporting is that you’re getting two keywords that are linked together. For example, if you had two rows of data with the same facility id, one with keywords keyword-a and keyword-b and another with keyword-c and keyword-d, you would see that output visually without a comma between keyword-b and keyword-c.

    In other words, instead of this:

    keyword-a, keyword-b, keyword-c, keyword-d
    

    …you’re instead seeing this:

    keyword-a, keyword-bkeyword-c, keyword-d
    

    This is also caused by the lack of a visual break between implodeed lines, but I believe the problem is deeper than that. I believe you want all keywords for a given facility to be shown on a single line, not broken across multiple lines. For that, you need to move where you reinitialize your array so that it gets reinitialized at the same time you switch to a new heading. Try something like this:

    $kwords = array();
    while ($row = mysql_fetch_assoc($result)) { 
      if ($row['facID'] !== $lastID ) {
        if ($kwords) {
          echo implode(", ", $kwords) . "<br />";
          $kwords = array();
        }
        echo $row['facname'] . "<br />";
        $lastID = $row['facID'];
      }
      foreach($row as $k => $v) {
        if (strpos($k, 'kword') === 0) {
          $kwords[] = $v;
        }
      }
    }
    if ($kwords) {
      echo implode(", ", $kwords);
    }
    echo "<br />";
    

    This still isn’t the best code, but you can refactor it.

    The idea here is that the array gets output and reset every time the facility changes, so that the array encompasses all keywords for that facility and they all get output together, rather than being reported only as part of the database row they were returned with. After the loop completes, you have to manually report the last row since the usual reporting is taken care of within the loop.

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

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

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.