dikkuh Posted September 11, 2013 Share Posted September 11, 2013 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/282078-create-variables-from-query-output/ Share on other sites More sharing options...
Solution Psycho Posted September 11, 2013 Solution Share Posted September 11, 2013 (edited) 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 September 11, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/282078-create-variables-from-query-output/#findComment-1449148 Share on other sites More sharing options...
dikkuh Posted September 11, 2013 Author Share Posted September 11, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/282078-create-variables-from-query-output/#findComment-1449150 Share on other sites More sharing options...
dikkuh Posted September 11, 2013 Author Share Posted September 11, 2013 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/282078-create-variables-from-query-output/#findComment-1449154 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.