TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Table generator? (http://www.talkphp.com/general/2502-table-generator.html)

marxx 03-21-2008 09:17 AM

Table generator?
 
Hi all and happy eastern! =)

I have on my new project quite many tables where I collect infos and such. I'm totally pissed of to write all over those tables and thought that if I need to changes something (class name etc) I have to go throught those all.. no way!

So, I know that it is possible to make function which generate table but would need some example of that.

Preferences as query and field names.. below is something what I came in my mind but I just can't get it work.. eh..

PHP Code:


function maketable($field$query) {

$fields[] = array($field);

print(
"<table cellspacing=\"0\" cellpaddign=\"5\" width=\"100%\">\n");

$arr mysql_query($query) or die(mysql_error());
while(
$fetch mysql_fetch_array($arr)) {

  foreach(
$fields as $f) {

    print(
"<tr><td>".$fetch[$f[0]]."</td><td>".$fetch[$f[2]]."</td></tr>\n");

  }
}
print(
"</table>\n\n");


Thanks for all help! =)

freenity 03-21-2008 11:15 AM

I think this should work:

PHP Code:


function maketable($field$query) {

$fields[] = array($field);

print(
"<table cellspacing=\"0\" cellpaddign=\"5\" width=\"100%\">\n");

$arr mysql_query($query) or die(mysql_error());
while(
$fetch mysql_fetch_array($arr)) {
  echo 
"<tr>\n";
  foreach(
$fields as $f) {
    print(
"<td>".$fetch[$f]."</td>\n");
  }
  echo 
"</tr>";
}
print(
"</table>\n\n");


Also, unless you send an array of array to $field, you can't have indices on $f, when you do foreach($fields as $f)

marxx 03-21-2008 03:57 PM

Hi Freenity, thanks for answering.

It seems that I'm missed something, I can't get it work..
I even try copy/paste hole php but no.

Then this came up. How should I make calling this function?

Now I have like this:
PHP Code:

maketable("'id', 'f1', 'f2', 'f3', 'f4'","SELECT * FROM test"); 


// edit:

I print_r $fiels and this came up
PHP Code:

Array ( [0] => Array ( [0] => idf1f2f3f4 ) ) 

So I have to explode $fields.. ok..


Thanks for all help!


All times are GMT. The time now is 02:07 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0