Current location: Hot Scripts Forums » Programming Languages » PHP » Creating a dynamic table with 3 columns per row

Creating a dynamic table with 3 columns per row

Reply
  #1 (permalink)  
Old
Newbie Coder
 
Join Date: Mar 2004
Location: Toronto, Ontario, Canada
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Red face Creating a dynamic table with 3 columns per row

Hi guys,

I have this very frustrating problem

I'm trying to make a dynamic table with only 3 columns per row. So every 3 items, I need a new row.

This code works fine, in fact, some may benefit from it:

PHP Code:

<?php


echo "<pre>";

$input_array = array('1''2''3''4''5''6''7''8');
$input_array array_chunk($input_array3true);

echo 
"<table width=\"500\" border=\"0\" cellspacing=\"5\" cellpadding=\"0\">\n";

foreach (
$input_array as $set) {
    echo 
"<tr>\n";
    foreach (
$set as $key) {
        echo 
"  <td>$key</td>\n";
    }
    echo 
"</tr>\n";
}

echo 
"</table>\n";

echo 
"</pre>";

?>
Now, here is my problem. The array I created above is static. I want a dynamic array that is created from MySQL values. How can I do that? I can provide more details if needed, I'm just not sure what I need to make this happen.

Is there a MySQL function that will do this for me? mysql_fetch_array only fetches an array for one row in the MySQL table unless I loop it. I was thinking I could use PHP's array appending functions.

Please, I would love any help
Reply With Quote

Featured Marketplace Scripts (view more)

  #2 (permalink)  
Old
duesi's Avatar
Wannabe Coder
 
Join Date: Jun 2006
Posts: 225
Thanks: 0
Thanked 0 Times in 0 Posts
I think you can use the push function for this:

PHP Code:

    $sql="SELECT name FROM table"
__________________
Duesi

"One of the great skills in using any language is knowing what not to use, what not to say" (Ron Jeffries)

http://www.swissbytes.de
Reply With Quote
  #3 (permalink)  
Old
Newbie Coder
 
Join Date: Mar 2004
Location: Toronto, Ontario, Canada
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the quick reply.

I looked at array_push but I'm dealing with multi-dimensional arrays. Each product has its relative details (i.e. ID, product name, price, etc.), and array_push is only for appending single values to the end of the array.

Maybe array_merge?
Reply With Quote
  #4 (permalink)  
Old
duesi's Avatar
Wannabe Coder
 
Join Date: Jun 2006
Posts: 225
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry, i hit the button too quick, my code wasn't finisehd.
OK, If your array is mutlidimensional, array_push will not help.
__________________
Duesi

"One of the great skills in using any language is knowing what not to use, what not to say" (Ron Jeffries)

http://www.swissbytes.de
Reply With Quote
  #5 (permalink)  
Old
UnrealEd's Avatar
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 27 Times in 27 Posts
Quote:
Originally Posted by Rahil
Is there a MySQL function that will do this for me? mysql_fetch_array only fetches an array for one row in the MySQL table unless I loop it. I was thinking I could use PHP's array appending functions.
as you are going to loop over the array with php array functions, you could just use a while-loop to loop over the entries mysql returned:
PHP Code:

echo "<table border=\"1\">\n";

$res mysql_query("SELECT * FROM table");
while(
$row mysql_fetch_row($res)){
  echo 
"<tr>\n";
  foreach(
$row as $key => $value) echo "<td>$value</td>\n";
  echo 
"</tr>\n";
}
echo 
"</table>"
Greetz,
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Table in DOM (Add/Remove Rows) codexomega JavaScript 0 08-16-05 11:43 AM
Rotating Images in a Table Row Roses6088 HTML/XHTML/XML 2 07-20-05 02:57 PM
problem with dynamic table noviceforever HTML/XHTML/XML 1 07-31-04 09:09 PM
problem with dynamic table noviceforever JavaScript 3 07-10-04 10:16 AM
Help with dynamic select in dynamic table noviceforever JavaScript 1 06-23-04 01:20 PM


All times are GMT -5. The time now is 07:12 AM.
vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.