Jump to content

Create variables from Query output


dikkuh
Go to solution Solved by Psycho,

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
Share on other sites

  • Solution

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>";
}
Edited by Psycho
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>";
	}		
?>
Link to comment
Share on other sites

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.