Jump to content

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.

Edited by AyKay47

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>';
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.