Re: [Dave] Displaying in rows of 2 Is this possible?

8 posts by 2 authors in: Forums > CMS Builder
Last Post: November 29, 2010   (RSS)

Re: [zickey] Displaying in rows of 2 Is this possible?

By Chris - October 12, 2010

Hi zickey,

That's somewhat problematic. Using a Text Box Field would be safer because you'd know there would always be the same character between lines, but with a Wysiwyg Field lots of things can find their way into the HTML, especially if you copy-and-paste.

Either way you do it, you'll want to first take a look at what you're dealing with. Use the following code to take a look at exactly what data you'll be working with:

<?php echo showme($my_record['my_field']) ?>

(Substitute the name of your record variable and field name above.)

Let's assume that you end up with something like this:

<p>line one</p>
<p>line two</p>
<p>line three</p>
<p>line four</p>


You can use preg_match_all to pattern match everything inside each <p /> tag:

<?php preg_match_all("/<p>(.*?)<\/p>/", $my_record['my_field'], $matches) ?>

<?php $count = 0 ?>
<table width="191" border="0" align="left">
<tr>
<?php foreach ($matches[1] as $line): ?>
<td>
<?php echo $line ?>
</td>
<?php $maxCols=3; if (@++$count % $maxCols == 0): ?>
</tr><tr>
<?php endif ?>
<?php endforeach ?>
</tr>
</table>


Does that help? Please let me know if you have any questions.
All the best,
Chris

Re: [chris] Displaying in rows of 2 Is this possible?

By Mikey - October 12, 2010

Thanks Chris works nice! In regards to the problems that could result from copy-and-paste. If I where to convert this to populate from a text box instead of a WYSIWYG, could I use the same code, or do I need to make any changes to it to use with a texbox field? ... assuming the field name is the same.

Thanks,
Zick

Re: [zickey] Displaying in rows of 2 Is this possible?

By Chris - October 12, 2010

Hi zickey,

A Text Box Field will separate lines with "<br />\n" (where \n is a newline) unless you've set the "Disable auto-formatting" setting on the field.

You can split on "<br />\n" like this:

<?php $count = 0 ?>
<table width="191" border="0" align="left">
<tr>
<?php foreach (explode("<br/>\n", $my_record['my_field']) as $line): ?>
<td>
<?php echo $line ?>
</td>
<?php $maxCols=3; if (@++$count % $maxCols == 0): ?>
</tr><tr>
<?php endif ?>
<?php endforeach ?>
</tr>
</table>


If you've set "Disable auto-formatting", you'll want to change the first argument of explode to "\n".

I hope this helps! Please let me know if you have any questions.
All the best,
Chris

Re: [chris] Displaying in rows of 2 Is this possible?

By Mikey - October 12, 2010

Thanks Chris!
Nice to have two options to present to the client. I think they'll go with the WYSIWYG option so they can include web links to businesses, but I'll have to make it clear to them not to copy-and-paste and instead build their business address listings directly in the CMS.

Cheers, Zick

Re: [chris] Displaying in rows of 2 Is this possible?

By Mikey - November 27, 2010

Chris, I'm using the code you provided to split some records into columns with bullets, but I'm getting some blank bullet areas. Is there a way to trim out the bullet list that doesn't actually have anything applied to it. For example, I've got a bullet at the top of all my list that has nothing beside the bullet and at the end of the each record's list results I have another bullet without anything else beside it.

Example:

• Accounting
• Custodian
• Receptionist


I'm getting the records from a checkbox list.

<?php $count = 0 ?>
<table border="0" align="left" cellpadding="0" cellspacing="0" id="job_list_table">
<tr>
<?php foreach (explode("\t", $record['job_list']) as $line): ?>
<td valign="top">
<div id="job_list_columns">
<ul><li><?php echo $line ?></li></ul></div>
</td>
<?php $maxCols=2; if (@++$count % $maxCols == 0): ?>
</tr><tr>
<?php endif ?>
<?php endforeach ?>
</tr>
</table>

Re: [zick] Displaying in rows of 2 Is this possible?

By Mikey - November 27, 2010

Chris, I got this figured out. I found a thread that had the solution I needed to trim out empty results. Here's the thread if anyone else need to figure this out. I've updated the code below with the fix.
http://www.interactivetools.com/iforum/Products_C2/CMS_Builder_F35/Re%3A_%5BDave%5D_Displaying_in_rows_of_2_____Is_this_possible_P83928/gforum.cgi?post=79155;search_string=trim%20empty;guest=63885067&t=search_engine#79155

Chris, I'm using the code you provided to split some records into columns with bullets, but I'm getting some blank bullet areas. Is there a way to trim out the bullet list that doesn't actually have anything applied to it. For example, I've got a bullet at the top of all my list that has nothing beside the bullet and at the end of the each record's list results I have another bullet without anything else beside it.

Issue Example:

• Accounting
• Custodian
• Receptionist


Fixed Example with trim added to code as seen below:
• Accounting
• Custodian
• Receptionist

I'm getting the records from a checkbox list.

<?php $count = 0 ?>
<table border="0" align="left" cellpadding="0" cellspacing="0" id="job_list_table">
<tr>
<?php foreach (explode("\t", trim ($record['job_list'])) as $line): ?>
<td valign="top">
<div id="job_list_columns">
<ul><li><?php echo $line ?></li></ul></div>
</td>
<?php $maxCols=2; if (@++$count % $maxCols == 0): ?>
</tr><tr>
<?php endif ?>
<?php endforeach ?>
</tr>
</table>

Re: [zick] Displaying in rows of 2 Is this possible?

By Chris - November 29, 2010

Hi zick,

Glad to hear you got everything sorted out. :)
All the best,
Chris