Hey there.
I have been trying to populate theme_table with multiple rows in a while operation. How to I encode $rows() as an array that theme_table will iterate through several rows?
237 while ($data[] = db_fetch_object($result))
238 {
239
240 $rows = array($data[$count]->OBJ1,$data[$count]->OBJ2);
241
242
243 $count++;
244 }
245 $output .= theme_table($rows, $attributes = NULL);
This returns only the last row.
237 while ($data[] = db_fetch_object($result))
238 {
239
240 $rows = array($data[$count]->OBJ1,$data[$count]->OBJ2);
241 $output .= theme_table($rows, $attributes = NULL);
242
243 $count++;
244 }
245
This returns one table per line.
I am certain it's a basic Drupal concept that I missing. Can anyone take a second to clue me in?
Thanks in advance.
- Dan.
Comments
I do this: <?php$header =
I do this:
Hopefully this skeleton can help you find the problem in your own code.
Contact me to contract me for D7 -> D10/11 migrations.
Try this and see if its
Try this and see if its working
--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com
Closer still
OK, I have something closer to what I am looking for. It makes the rows for the proper number of entities, but it's not populating the tables with any content. It's making the alternate colored bars, and there's the right amount of bars. Here's the code:
10 {
20 $result = db_query("SELECT * FROM `generic_table_name` WHERE 1");
30 $output = ''; $count = 0; $num_rows = FALSE;
40
50 $caption=t("List of objects");
60 while ($data = db_fetch_object($result))
70 {
80 $row = array();
90 $row[] = array($data->OBJ1, $data->OBJ2);
100 $rows[] = $row;
110
120 }
130
140 $output .= theme_table($header, $rows, $attributes = NULL, $caption);
150 return $output;
160
170 }
This produces all the rows I am expecting, but with nothing in them.
When I reference $rows[0] in line 140 I get the Object name, from OBJ1 and the property of OBJ1 represented by OBJ2 on the one line. The reference changes when I alter [x].
When I remove $header and $caption I get the following Error:
warning: htmlspecialchars() expects parameter 1 to be string, array given
Anyone have any ideas? I would appreciate some more feedback, and appreciate that which has been already given!
- Dan.
1) Show us your actual code.
1) Show us your actual code. It's hard to decipher code that isn't real.
2) To make your SQL queries more efficient, you should select the column names from the table, not *. With *, the db engine has to first query the table names, then get the data for those names. Even if you are getting all columns, you should still list them explicitly, this will increase your query efficiency. And on top of this, it will make it easier for us to decipher your problem, seeing as we can't see your column names when you use *.
Contact me to contract me for D7 -> D10/11 migrations.
Real Code.
Hey.
Thanks for the info on not using *.
As for real code thats pretty much it. The only thing I changed was table names. I was attempting to be generic to prevent distraction by the content.
Replace "List of Objects" with "List of Relationships". OBJ1 with Name, and OBJ2 with Affiliation "Select Name, Affiliation from relations where 1";
The structure is completely identical. I can get rid of $num_rows = FALSEl I haven't had the need to keep that there. And $count.
The call to theme_table(... or theme('table'... both produce the same result.
Any help is greatly appreciated.
- Dan.
From
From http://api.drupal.org/api/function/theme_table/6 :
So, your while loop should be:
or the equivalent:
Got it.
Thanks M8.
I figured it out at the same time you posted this here.
I love the Drupal community!
- Dan.
Make sure to read
Make sure to read http://drupal.org/writing-secure-code as well; your code likely needs some escaping (http://drupal.org/node/28984).
New Clue - View Source
On a hunch I took a look at the source and found something interesting, but I have no idea how to fix it.
this is a snippet from my
this is a snippet from my previous module that uses table with theme_table function, see if this can work with you or you can get some clue out of it
--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com