[FX.php List] PHP Question
Steve Hannah
shannah at sfu.ca
Thu Jul 20 13:06:38 MDT 2006
Good point, Dan. It would fail in this case. Adding a while loop
after the foreach loop would take care of the problem.
$counter=0;
echo '<table>';
foreach ($foundSet['data'] as $key=>$foundData){
if ( $counter % 3 == 0 ) echo '<tr>';
echo '<td width="33%"> Your stuff here</td>';
if ($counter % 3 == 2) echo '</tr>';
$counter++;
}
while ( ($counter++ % 3)<3){
echo '<td></td>';
if ( $counter % 3 == 0 ) echo '</tr>';
}
echo '</table>';
Although another (better?) way is not to use tables at all, but
instead to use css:
foreach ( $foundSet['data'] as $key=>$foundData){
echo '<div style="float: left; width: 33%"> jour stuff here</div>';
}
Best regards
Steve
On 20-Jul-06, at 11:19 AM, DC wrote:
> steve,
>
> i think there is a flaw in the example you gave... wouldn't the
> code posted result in an unclosed row if the mod value (column
> count) was not a factor of the array count?
>
> here's a little general function set i put together that will close
> rows properly - i think it could still be improved however with the
> addition of the ability to add missing cells if the number of cells
> does not fill the last row. (exercise for the reader i guess.)
>
> this wraps up variable column number table making into one tidy
> little function:
>
> <?php
> // shows how to make a generic
> // variable column table maker
>
> // dummy table data array akin to calendar
> $t = range(1, 30);
> // change this to change number of cols
> $x = 7;
> // that's it, call the function
> print_r(make_x_column_table($t,$x));
>
> // iterate over an array generate table
> // with variable number of columns
> function make_x_column_table($t,$x) {
> $cols = '';
> $table = '';
> $c = count($t)-1;
> // loop relies on zero indexed
> // keys to determine table close
> // here we force zero index keys
> $t = array_values($t);
> foreach ($t as $key => $data) {
> // we wrap each data bit in a column
> if ($c>=$key) {$cols .= column($data);}
> // only wrap the mod value in a row or the last row
> if (($key % $x)+1 === $x OR $c==$key) {
> // add on the row
> $table .= row($cols);
> // erase the cols
> $cols = '';
> }
> }
> // at end of array wrap in table
> return table($table);
> }
>
> // customize these to your liking
> // they are here so HTML is sequestered from PHP
> function column($s) {return '<td>'.$s.'</td>';}
> function row($s) {return '<tr>'.$s.'</tr>';}
> function table($s) {return '<table border="1" cellpadding="10">'.
> $s.'</table>';}
> ?>
>
> http://dbmscan.com/snippets/ in the Miscellaneous section
>
> enjoy,
> dan
>
> Steve Hannah had written:
>> Along the same lines but using the modulus operator...
>> $counter=0;
>> echo '<table>';
>> foreach ($foundSet['data'] as $key=>$foundData){
>> if ( $counter % 3 == 0 ) echo '<tr>';
>> echo '<td width="33%"> Your stuff here</td>';
>> if ($counter % 3 == 2) echo '</tr>';
>> $counter++;
>> }
>> echo '</table>';
>> On 20-Jul-06, at 10:12 AM, Bob Patin wrote:
>>> While I've not done it, I know how I would go about this:
>>>
>>> $counter = 4;
>>> foreach($foundSet['data'] as $key=>$foundData){
>>> if ($counter = 4){
>>> $counter = 0;
>>> ?>
>>> <?php
>>> <tr> //create a new table row
>>> ?>
>>> <?php
>>> }
>>> if ($counter < 4){ ?>
>>> <td width=33%>... create your new column, specify column width
>>> here as well (I assume it'd be 33%)
>>> $counter = $counter + 1;
>>> <?php
>>> }
>>> }
>>> ?>
>>>
>>> This might need tweaking; I just dashed this off, but I think
>>> this would work fine...
>>>
>>> Let me know how it goes...
>>>
>>> Bob Patin
>>> Longterm Solutions
>>> bob at longtermsolutions.com <mailto:bob at longtermsolutions.com>
>>> 615-333-6858
>>> http://www.longtermsolutions.com
>>>
>>> CONTACT US VIA INSTANT MESSAGING:
>>> AIM or iChat: longterm1954
>>> Yahoo: longterm_solutions
>>> MSN: tech at longtermsolutions.com
>>> <mailto:tech at longtermsolutions.com>
>>> ICQ: 159333060
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> So essentially you'd have a loop within a loop; the main loop is
>>> looping through the found set. The internal loop creates columns
>>> (3 at the max for your example), then exits the loop to start a
>>> new row.
>>>
>>>
>>> On Jul 20, 2006, at 11:56 AM, David Tinoco wrote:
>>>
>>>> I guess this is more a php question, but does anyone know how to
>>>> write a piece of code that could write the html for the
>>>> foundcount of a query from the database in columns, as well as
>>>> rows?
>>>> For example:
>>>> Category1 Category2 Category3
>>>> Category4 Category5 Category6
>>>> Instead of:
>>>> Category1
>>>> Category2
>>>> Category3
>>>> Category4....and so on.
>>>> Thanks!
>>>> David
>>>>
>>>> -------------------------------------------------------------------
>>>> -----
>>>> With MSN Spaces email straight to your blog. Upload jokes,
>>>> photos and more. It's free! It's free! <http://clk.atdmt.com/MSN/
>>>> go/msnnksac0030000001msn/direct/01/?href=http://www.imagine-
>>>> msn.com/spaces>
>>>> _______________________________________________
>>>> FX.php_List mailing list
>>>> FX.php_List at mail.iviking.org <mailto:FX.php_List at mail.iviking.org>
>>>> http://www.iviking.org/mailman/listinfo/fx.php_list
>>>
>>>
>>> _______________________________________________
>>> FX.php_List mailing list
>>> FX.php_List at mail.iviking.org <mailto:FX.php_List at mail.iviking.org>
>>> http://www.iviking.org/mailman/listinfo/fx.php_list
>> ---------------------------------------------------------------------
>> ---
>> _______________________________________________
>> FX.php_List mailing list
>> FX.php_List at mail.iviking.org
>> http://www.iviking.org/mailman/listinfo/fx.php_list
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list
>
More information about the FX.php_List
mailing list