Jump to content

Create variables from Query output


dikkuh

Recommended Posts

I have a query which generates the following output:

[custom_fields] => track_length="4,307 km" track_record="1:19.880" record_holder="Mr.X" car="AUDI"

 

I want to display the output in a table like:

 

Track Length:       4,307 km

Track Record:      1:19.880

Record Holder:    Mr.x

Car:                     Audi

 

Any idea how I can achieve this ?

Link to comment
https://forums.phpfreaks.com/topic/282078-create-variables-from-query-output/
Share on other sites

Hmm, are you saying that the text "track_length="4,307 km" track_record="1:19.880" record_holder="Mr.X" car="AUDI"" is a value that is stored in a single field called "custom_fields"? You should really parse the data and store the values individually. With what you have, it would be difficult to do any searching, sorting, analyzing of that data. But, with what you have, this might work (of course you'll need to format the output into how you want it - this will just extract it into a usable array):

while($row = mysql_fetch_assoc($result))
{
    $customFieldsSrc = explode('" ', $row['custom_fields']);
    $customData = array();
    foreach($customFieldsSrc as $fieldString)
    {
        list($name, $value) = explode('="', $fieldString);
        $customData[$name] = $value;
    }
    echo "<pre>" . print_r($customData, 1) . "</pre>";
}

Thanks for your fast response, unfortunately the value is in a single field in the database and I can't change that.

 

I tried the code you suggested and I now have:

<?php
	{
		$customFieldsSrc = explode(' ', ($row->custom_fields));
		$customData = array();
		
			foreach($customFieldsSrc as $fieldString)
			{
				list($name, $value) = explode('="', $fieldString);
				$customData[$name] = $value;
			}
				echo "<pre>" . print_r($customData, 1) . "</pre>";
	}		
?>

It generates the following output:

 

Array
(
    [track_length] => 4,307
    [km"
track_record] => 1:19.880"
record_holder
    [bartsch"
car] => VW
    [uP!"] =>
)
 

I think we are close

My mistake had an error with the explode command.

This is the correct code for me, many thanks for helping me out.

<?php
	{
		$customFieldsSrc = explode('"', ($row->custom_fields));
		$customData = array();
		
			foreach($customFieldsSrc as $fieldString)
			{
				list($name, $value) = explode('="', $fieldString);
				$customData[$name] = $value;
			}
				echo "<pre>" . print_r($customData, 1) . "</pre>";
	}		
?>

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.