473,811 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Formatting mysql_fetch_arr ay results in HTML table

Hi,

This looks like a simple problem, but I just can't seem to wrap my
head around it.

I'm trying to build a table dynamically, based on a result set from
mysql. There will be 9 results for a full page, with 3 table rows of 3
table cells. One result per cell. But all of the pages won't be full.

Here's one of many ways I've tried to do it:

<html>
<head>
<title>test</title>
</head>
<body>
<table width="100%" border="1">
<?php

include_once('d b.inc.php');

$query = "SELECT DISTINCT title FROM mytable LIMIT 9";
$result = mysql_query($qu ery);

while ($row = mysql_fetch_arr ay($result)) {
$title = $row['title'];
}

for ($i = 0; $i < 3; $i++) {
print ' <tr>' . "\n";

for ($e = 0; $e < 3; $e++) {
print ' <td align="center"> ' . "\n".
' title : ' . $title . "\n".
' </td>' . "\n";
}
print ' </tr>' . "\n";
}
?>
</table>
</body>
</html>

This prints out 3 rows of three like I hoped, but $title is the same
in all 9 cells. I also have tried it with the for loops inside the
while loop, which prints out 9 rows of 9 cells 9 times!

I know I'm probably missing something obvious, so maybe someone can
point out what I'm doing wrong?
Jul 17 '05 #1
1 4107
I noticed that Message-ID: <s0************ *************** *****@4ax.com>
from bill contained the following:
This prints out 3 rows of three like I hoped, but $title is the same
in all 9 cells. I also have tried it with the for loops inside the
while loop, which prints out 9 rows of 9 cells 9 times!


Your problem is that you need to generate the table, whether or not you
have results. This means that the table generation code must exist
independently of the calls to the results array. Fortunately
mysql_fetch_arr ay() steps to the next record each time it is called so
if you put it in a function it should insert consecutive records until
they are all used. However the table creation can continue.

Something like (untested):

<table width="100%" border="1">
<?php

include_once('d b.inc.php');

$query = "SELECT DISTINCT title FROM mytable LIMIT 9";
$result = mysql_query($qu ery);

function next_row() {
$row = mysql_fetch_arr ay($result);
$title = $row['title'];
if($title==""){ $title="&nbsp;" ;}
return $title;
}

for ($i = 0; $i < 3; $i++) {
print ' <tr>' . "\n";

for ($e = 0; $e < 3; $e++) {

print ' <td align="center"> ' . "\n".
' title : ' .next_row() . "\n".
' </td>' . "\n";
}
print ' </tr>' . "\n";
}
?>
</table>

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2407
by: Dan Weeb | last post by:
Hi All, I have struggled through this far with help from many of you so thanks. I am stuck again. I am really new to this so don't be harsh :-) There are a few problems. You can run the script here http://www.pbrown.com/research/test1.php to see the formatting issues Formatting Issues
4
1984
by: Slant | last post by:
I have been using MySQL in conjunction with PHP for many years now... I have been using the exact same method for not only connecting to a database and performing the queries but also displaying the results. I've been told numerous times that my methods are out-dated. I'm more than willing to cede to this possibility but I need help getting out of the rut!!! Can anyone help me? Here are some examples:
1
1670
by: billbealey | last post by:
I'm struggling how to format out in a nice html table after doing a query. Query is: $result = mysql_query("SELECT state_name, county_name, party_name, votes FROM regions, county, party, results where ... order by region_name,constit_name, votes DESC") Note: i am odering by 'state', then 'county', then ordering the parties
1
1552
by: billb | last post by:
I'm struggling how to format out in a html table after doing a query. Query is: $result = mysql_query("SELECT state_name, county_name, party_name, votes FROM regions, county, party, results where ... order by state_name, county_name, votes DESC") Note: i am odering by 'state', then 'county', then ordering the parties by no. of votes descending.
15
2690
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");
8
3542
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At the moment the printed output is usually going to Word. It's turning into an unholy mess, because I'm having to prepare umpteen different Word templates, and the queries that drive them, depending on what events a course has.
1
3913
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 column table. I know how to get the results into a single column table just fine using: while($row = mysql_fetch_array($result)) { print "<table border=2><tr><th>" . $row; print "<tr><td>";
2
1276
by: mpar612 | last post by:
Hi everyone, The code posted below retrieves all of the rows from a database table. It paginates them by limiting to 20 results per page. It is supposed to print 5 rows of results with 4 results per row. Everything works fine, except the first row returned from the database is never printed. It doesn't matter how the results are ordered or how many rows are in the db, the first row that is supposed to be printed is not printed. Does...
13
2616
by: wizardry | last post by:
Good evening - thanks in advance for you help! attached is my query, and html table layout. I'm trying to query 3 tbles in one select statement, and return the data to html table. it work before when i had it broken down into three select statements and three results.
0
9731
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10405
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.