I have following code, sorry it's pretty big for posting, i want to
split the output into 2 colomns, been trying almost all still can't
get it working
Harik,
First off, have you checked the manual
(http://uk.php.net/manual/en/function.mysql-result.php) and the
recommendations against the use of mysql_result()? In this situation, one of the
mysql_fetch_... functions might
be a better choice because both data fields could be extracted in one operation. Apart
from 'efficiency', the
other reason for my saying this (I have never used _result()) is that I can't see
where the manual talks about
what _result() returns when it gets to an end-of-data situation (the _fetch_ functions
return FALSE and are thus
perfect for use in controlling while loops).
Now to logic. May I offer a short programming lesson? If you are an experienced
programmer and this is 'talking
down' to you, I apologise. However this question has been asked (in a few guises) a
couple of times recently, so
lets get an answer up on the FAQs/archives...
This program requires two processing loops: one to cycle through the available topic
data, the other to display
results in a two-column HTML table. Most of us think in terms of starting at the
beginning (get the data) and
working through to the end (display the results) - from the script provided it seems a
reasonable assumption
that you have done exactly this, and that everything is working properly (up to that
last step before the
end). However computers are logical but not rational. Sometimes it is worth starting
at the end and working
back towards the beginning!
The 'second loop is described as being responsible for presenting the results (in
this example, a table with
two columns). There are three logical parts to the entire process: setting up the HTML
code for the table,
looping through the rows (and columns), closing off with the end of table HTML code.
In pseudo code it looks like:
prepare the web page (as necessary) and echo the TABLE tag
for each row of the table
echo the TR tag
echo the first column (TDdata/TD)
echo the first column (TDdata/TD)
echo the /TR tag
echo the /TABLE tag and close the web page
Quick aside: with two columns the above is acceptable. If there were numerous columns,
but also because the
other processing required here brings added complexity, an inner loop might be
appropriate, thus:
prepare
for each row
TR
for each column
echo the TDdata/TD
/TR
finish-off
Now let's talk about the topic-data loop process. Again, divide the logic into three
parts: setting up MySQL and
SELECTing the primary data, looping through each of the SELECTed records, and cleaning
up afterwards. The
existing script is neatly laid out to show this. In pseudo code, the script's logic
becomes:
set-up db and query table
for each row returned
perform secondary query/assemble and format full report data
present topicId, topicName, and counter
close down
That being the case, can all of the processing of individual (in-bound) data records
be packaged into a
user-defined function? (modular programming) If the function returns true/false as
well as the next data record,
then the boolean state can be used to detect the end-of-data condition:
set up
while ( not end-of-data )
getnextrow()
close down
OK, so much for all that, how do the two halves get put back together? There are two
loops, one cycling through
HTML table tags, and the other cycling through a DB recordset/resultset. Because the
output format is the
important part/has the higher number of logical components/subdivisions, it must
dominate the logic. Thus the
data-provisioning part should be inverted - instead of squirting out data like a hose,
which the HTML then has
to be 'fitted around'; it should be set up like an on-demand drip feed, so that when
the HTML requires a
'squirt' of data, it is delivered, one swallow at a time! Let's do this in two stages
- first of all, with no
data processing (ie just reporting/important part first). Combine the two set up
code-blocks, and the two
close-down code blocks and outline the HTML processing:
prepare the page (as necessary) and echo the TABLE tag
set-up db and query table
---
for each row of the table
echo the TR tag
for each column
echo the column data (TDdata/TD)
echo the /TR tag
---
echo the /TABLE tag
close down (the DB)
But how do we know how many rows to put in the table? Answer: we don't, and so have to
set up the loop controls
accordingly. if you prefer to continue using for-loops you can control these by use of
the numOfRows =
mysql_num_rows ($result) code; or if you make the outer into a while-loop then the
idea of the boolean
(mentioned above) brings a process-while-there's-still-data logic into play:
Function getnextrow(...)
perform secondary query/assemble and format full