Printing mysql_fetch_array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gthomas
    New Member
    • Dec 2006
    • 2

    #1

    Printing mysql_fetch_array

    hi, i am having trouble printing the records form my database. each row has a topic and headline. the topic should print and then the headlines for that topic, then the next topic and headlines.

    healthcare
    headline1
    headline2
    sports
    headline3
    headline4
    headline5...


    $result = mysql_query("SE LECT * FROM mccnews, topics where mccnews.topicid = topics.topicid ",$connect) ;

    while($myrow = mysql_fetch_arr ay($result))
    {//begin of loop
    $name = $myrow['name'];
    $hklink = $myrow['hklink'];
    $headline = $myrow['headline'];
    $first_headline = $headline;

    // build $content table
    $content .= table_header();
    $content .= "<tr>";
    $content .= "<td>" . $name . "</td>";
    $content .= "<td>" . $hklink . "</td>";
    $content .= "<td>" . $headline . "</td>";
    $content .= "</tr>";
    $sontent .= table_footer;

    }//end of loop
  • seangates
    New Member
    • Dec 2006
    • 19

    #2
    Firstoff, please use the code formatting for your posts. It would help.

    [PHP]$result = mysql_query("SE LECT * FROM mccnews, topics where mccnews.topicid = topics.topicid" ,$connect);[/PHP]
    This just shows all of the rows from the mccnews table. You need some way to limit it.

    I think what you are trying to do requires a nested loop. Like this:
    [PHP]$topics = mysql_query("SE LECT * FROM topics",$connec t);
    while($topic = mysql_fetch_arr ay($topics)){
    echo '<h2>' . $topic['topic_title'] . '</h2>';
    $news_items = mysql_query("SE LECT * FROM mccnews where mccnews.topicid = " . $topic['topicid'],$connect);
    while($myrow = mysql_fetch_arr ay($news_items) ){
    $name = $myrow['name'];
    $hklink = $myrow['hklink'];
    $headline = $myrow['headline'];
    $first_headline = $headline;

    // build $content table
    $content .= table_header();
    $content .= "<tr>";
    $content .= "<td>" . $name . "</td>";
    $content .= "<td>" . $hklink . "</td>";
    $content .= "<td>" . $headline . "</td>";
    $content .= "</tr>";
    $sontent .= table_footer;

    }
    }[/PHP]
    Let me know if this is what you were intending.

    Sean

    Comment

    • gthomas
      New Member
      • Dec 2006
      • 2

      #3
      Originally posted by seangates
      Firstoff, please use the code formatting for your posts. It would help.

      [PHP]$result = mysql_query("SE LECT * FROM mccnews, topics where mccnews.topicid = topics.topicid" ,$connect);[/PHP]
      This just shows all of the rows from the mccnews table. You need some way to limit it.

      I think what you are trying to do requires a nested loop. Like this:
      [PHP]$topics = mysql_query("SE LECT * FROM topics",$connec t);
      while($topic = mysql_fetch_arr ay($topics)){
      echo '<h2>' . $topic['topic_title'] . '</h2>';
      $news_items = mysql_query("SE LECT * FROM mccnews where mccnews.topicid = " . $topic['topicid'],$connect);
      while($myrow = mysql_fetch_arr ay($news_items) ){
      $name = $myrow['name'];
      $hklink = $myrow['hklink'];
      $headline = $myrow['headline'];
      $first_headline = $headline;

      // build $content table
      $content .= table_header();
      $content .= "<tr>";
      $content .= "<td>" . $name . "</td>";
      $content .= "<td>" . $hklink . "</td>";
      $content .= "<td>" . $headline . "</td>";
      $content .= "</tr>";
      $sontent .= table_footer;

      }
      }[/PHP]
      Let me know if this is what you were intending.

      Sean
      Hi Sean,
      Yes, it was the help that I what is was looking for. Thank you very much. I just have to ensure that a topic does not get added that does not have a headline.
      Thanks again.
      GT

      Comment

      Working...