Jump to content

Displaying Multiple Data Under 1 Column


a6april

Recommended Posts

Hello All,

 

I was hoping to get an understanding of displaying data from a specific column in a MySQL database using php.

 

for example lets say the column field is called features and the input contains many features of the record seperated by pipes | and semi colons. (This is imported by a csv file by the way)

 

Lot Description|Flat|Yes;Lot Description|Adjoins Greenbelt|Yes;Lot Description|Wooded Lot|Yes;Lot Description|Irregular|Yes;Outside Features|Deck|Yes;Outside Features|Other - See Remarks|Yes;Outside Features|Utility Shed|Yes; etc etc and so on

 

<td><?php echo $row_vvcarbonall['Features']; ?></td>

 

Would I create an array for this, or is this an if else scenario or is there a way to replace the output of ';' with perhaps a break or is there some other approach I should be considering.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/270869-displaying-multiple-data-under-1-column/
Share on other sites

I am on my way out the door, but I have come up with a possible solution to your issue. It is not pretty and I'm sure there is a much easier way to go about this, but this code will hopefully steer you in the right direction until someone else posts a better solution.

 

$text = 'Lot Description|Flat|Yes;Lot Description|Adjoins Greenbelt|Yes;Lot Description|Wooded Lot|Yes;Lot Description|Irregular|Yes;Outside Features|Deck|Yes;Outside Features|Other - See Remarks|Yes;Outside Features|Utility Shed|Yes';
$arr = explode(';', $text);
$lotArr = array();
foreach($arr as $val)
{
if(strpos($val, 'Lot Description') !== FALSE)
{
$i = str_replace('Lot Description|', '', $val);
if(!isset($lotArr['Lot Description']))
 $lotArr['Lot Description'] = array();
$lotArr['Lot Description'][] = $i;
}
if(strpos($val, 'Outside Features') !== FALSE)
{
 $i = str_replace('Outside Features|', '', $val);
 if(!isset($lotArr['Outside Features']))
   $lotArr['Outside Features'] = array();
 $lotArr['Outside Features'][] = $i;
 }
}

foreach($lotArr['Lot Description'] as $key => $val)
{
 if(strpos($val, '|Yes') !== FALSE)
   $lotArr['Lot Description'][$key] = str_replace('|Yes', '', $val);
}
foreach($lotArr['Outside Features'] as $key => $val)
{
 if(strpos($val, '|Yes') !== FALSE)
   $lotArr['Outside Features'][$key] = str_replace('|Yes', '', $val);
}
print_r($lotArr);

 

Results:

 

Array
(
[Lot Description] => Array
(
[0] => Flat
[1] => Adjoins Greenbelt
[2] => Wooded Lot
[3] => Irregular
)


[Outside Features] => Array
(
[0] => Deck
[1] => Other - See Remarks
[2] => Utility Shed
)


)

 

As you can see, it splits the lot description properties and the outside features properties into two arrays. The above code assumes a number of things that need to be clarified. But again, this was done rather quickly and hopefully it will give you an idea.

This will create the array like AyKay's and output it as bullet lists of features

 

<?php
$text = 'Lot Description|Flat|Yes;Lot Description|Adjoins Greenbelt|Yes;Lot Description|Wooded Lot|Yes;Lot Description|Irregular|Yes;Outside Features|Deck|Yes;Outside Features|Other - See Remarks|Yes;Outside Features|Utility Shed|Yes';
$features=array();
$arr = explode(';', $text);

/* create array of the features */
foreach($arr as $ftext) {
  $tmp = explode('|', $ftext);
  if ($tmp[2]=='Yes') {
        $features[$tmp[0]][] = $tmp[1];
  }
}

/* output formatted results */
foreach ($features as $ftype => $farr) {
  echo $ftype . '<ul>' .
    '<li>' . join('</li><li>', $farr) . '</li>' .
    '</ul>';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.