> 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 (<TD>data</TD>)
      echo the first column (<TD>data</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 <TD>data</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 (<TD>data</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 report data
      present topicId, topicName, and counter
      set end-of-data true/false (if needed for while-loop control )

   getnextrow()
   while ( not end-of-data)
      echo the <TR> tag
      while ( end-of-data) and (column is 1 or 2)
         echo the column data (<TD>data</TD>)
         getnextrow()
      echo the </TR> tag

or

   for each row ( num_rows / 2 and rounded up )
      echo the <TR> tag
      for each column
         if ( getnextrow() <> end of data) echo the column data (<TD>data</TD>)
      echo the </TR> tag

In case you're wondering: why are there two getnextrow()-s in the 'while-loop' example 
solution? The first takes
care of the nil-result scenario. The second getnextrow() is not getting the data for 
the row being output in the
HTML, it is checking to see if there is more data for the next column-row and thereby 
feeding the boolean
conditions that control that code. Note that if a nil-data result is acceptable, and 
if in that situation the
table should not appear at all (avoiding the strange display of a 'table' with column 
headings but no data rows,
for example) then the first getnextrow() should be part of the <TABLE> set up 
processing, and control if there
is any such processing at all !


> Have a nice day :-)

=and you,
=dn


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to