473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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("SELECT * FROM mccnews, topics where mccnews.topicid = topics.topicid ",$connect);

while($myrow = mysql_fetch_array($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
Dec 1 '06 #1
2 1897
Firstoff, please use the code formatting for your posts. It would help.

[PHP]$result = mysql_query("SELECT * 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("SELECT * FROM topics",$connect);
while($topic = mysql_fetch_array($topics)){
echo '<h2>' . $topic['topic_title'] . '</h2>';
$news_items = mysql_query("SELECT * FROM mccnews where mccnews.topicid = " . $topic['topicid'],$connect);
while($myrow = mysql_fetch_array($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
Dec 1 '06 #2
Firstoff, please use the code formatting for your posts. It would help.

[PHP]$result = mysql_query("SELECT * 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("SELECT * FROM topics",$connect);
while($topic = mysql_fetch_array($topics)){
echo '<h2>' . $topic['topic_title'] . '</h2>';
$news_items = mysql_query("SELECT * FROM mccnews where mccnews.topicid = " . $topic['topicid'],$connect);
while($myrow = mysql_fetch_array($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
Dec 5 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: kaptain kernel | last post by:
i'm trying to find a simple way of printing out the keys in an array generated by mysql_fetch_array which returns a single row. i.e. while ($row=mysql_fetch_array($result) ) { print out...
5
by: james | last post by:
I am new to PHP and am trying to run a simple query and display the result, with no luck. Here is the code I am using. <?php //start session session_start(); //store cmpid from querystring...
15
by: Good Man | last post by:
Hey there I have a dumb question.... Let's say i have a database full of 4000 people.... I select everything from the database by: $result = mysql_query("SELECT * FROM People");
2
by: Dave Moore | last post by:
Hi All, I've got a simple query hopefully somebody can clear up for me. I need to make a query on a database to select a set of table rows, using something like: $result = mysql_query($query);...
4
by: Marcel Brekelmans | last post by:
Hello, I seem to get an extra empty field in every 'mysql_fetch_array' command I issue. For example: I have a simple table 'tblName': ID Name 1 Jane 2 Joe 2 Doe
1
by: JackM | last post by:
I'm not sure if this qualifies as a mysql or a php question so I'm asking in both groups. I am pulling the results of a mysql query from my database and want to print the results into a two...
9
by: Petr Vileta | last post by:
Hi, I'm new here and excuse me if this question was be here earlier. I have a simple code <html><body> <?php <?php $link = mysql_connect("localhost", "user", "password") or die("Grr: " ....
1
by: launchpad67a | last post by:
Hello, I'm retrieving database info and turning it into an xml file. The xml file has to be structured exactly like this example below: Here is what I need: <xml> <listitem name="Video 1"...
3
by: jmooney5115 | last post by:
Hey. I am new to php and am trying to learn. What I'm doing is querying(did I spell this right?) a mySQL database and putting the results into a table on a webpage. I have worked for hours on this...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.