[PHP] spliting results into 2 colomns

2001-10-31 Thread Daniel Harik


Hello Guys

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

here the link, with this code online http://funzz.cjb.net/itfaq/kbase.php

 --
 ?
include mainfile.php;

$connection = mysql_connect (127.0.0.1, *, );
if ($connection == false){
echo mysql_errno().: .mysql_error();
  exit;
}
$query = select topicid,topicname from nuke_topics order by topicname;
$result = mysql_db_query (postnuke, $query);

if ($result){
  echo table border=1;

  $numOfRows = mysql_num_rows ($result);

  for ($i = 0; $i  $numOfRows; $i++){ 
$topicid = mysql_result ($result, $i, topicid);
$topicname = mysql_result ($result, $i, topicname);

$result1 = mysql_query(select time from nuke_stories where topic=$topicid);
$counter=mysql_num_rows($result1);

preg_match (/([A-Z])/i, $topicname); 
$letter=$topicname[0];

if($lastletter!=$letter)echo trtdfont 
size=5b$letter/b/font/td/tr;


echo trtda href=search.php?query=topic=$topicid$topicname /a 
(b$counter/b) 

/td/tr; 
$lastletter=$letter;
  }
  echo /table;
} 
else{ 
echo mysql_errno().: .mysql_error().BR; 
}
mysql_close ();

include footer.php;

?

--

Thank You very much, in advance

Have a nice day :-)


-- 
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]




Re: [PHP] spliting results into 2 colomns

2001-10-31 Thread DL Neil

 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 

Re[2]: [PHP] spliting results into 2 colomns

2001-10-31 Thread Daniel Harik

WoW, thank you very much DL Neil, this is the best reply i've ever got during my
time on the web (since 1994).


-- 
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]




Re: [PHP] spliting results into 2 colomns

2001-10-31 Thread Daniel Harik

I't just came into my mind, if 1 simple split that arrey in to 2
arrays, it would be much simplier and wouldn't need second loop, i
just wanted to know how can i split 1 array in 2 arrays

Thank You


-- 
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]