Jump to content

Parsing data from mysql


pjungers

Recommended Posts

I think what I'd like to do is very simple, but I just can't figure out how to accomplish the task.  Fair warning -- I'm pretty new to PHP and any help is appreciated.

 

I have a form that takes in multiple fields of data and stores it in a mysql database.  I'm trying to output the data to another page on my website and I'm able to do that, but I'm really looking to output it in a way that's easily formatted using CSS.

 

This is on a wordpress site and I'm using the following code to output my data:

 

<?php 
$result = $wpdb->get_results ("SELECT field_val FROM wp_cformsdata WHERE field_name <> 'page' OR 'Fieldset1'", OBJECT);
foreach ($result as $teaminfo)
{
echo $teaminfo->field_val . "<br/>";
}
?>

 

Obviously, this just outputs the data with a <br> after each field value.  I'd like to see if there's a way to individually echo each field, such as "echo $teaminfo->name;" or "echo $teaminfo->address;" so that I can wrap each echo within a CSS class.

 

Alternatively, would there be a way to echo the field_val wrapped around <span class="field_name"></span> ?  This would also suit my needs, but I'm not sure how to accomplish it.

 

Also, unfortunately I don't have remote access to mysql with my hosting company, but I've included the output from myphpadmin when I run describe wp_cformsdata; :

 

mysqldescribe.png

 

Thank you in advance for your help.

Link to comment
https://forums.phpfreaks.com/topic/255334-parsing-data-from-mysql/
Share on other sites

depends what methods you are calling in your get_results object. can we see the contents of that?

 

Sorry for not including any info on that as it's built into Wordpress.  Here's the documentation they have on it:

 

"Generic, mulitple row results can be pulled from the database with get_results. The function returns the entire query result as an array. Each element of this array corresponds to one row of the query result and, like get_row, can be an object, an associative array, or a numbered array."

 

It's on this page: http://codex.wordpress.org/Class_Reference/wpdb under the SELECT Generic Results heading.

 

Could you modify your code like this an post here output

 

<?php 
$result = $wpdb->get_results ("SELECT field_name, field_val FROM wp_cformsdata WHERE field_name <> 'page' OR 'Fieldset1'", OBJECT);
print_r($result);
?>

 

Here is what I receive (returns added for readability):

 

Array ( [0] => stdClass Object ( [field_name] => Fieldset1 [field_val] => My Fieldset ) [1] => stdClass Object ( [field_name] => Team Name 
[field_val] => Lincoln Dominators ) [2] => stdClass Object ( [field_name] => Player Name [field_val] => Joe Player ) [3] => stdClass Object ( 
[field_name] => Position [field_val] => OF ) [4] => stdClass Object ( [field_name] => Number [field_val] => 3 ) [5] => stdClass Object ( 
[field_name] => Bats [field_val] => R ) [6] => stdClass Object ( [field_name] => Throws [field_val] => R ) [7] => stdClass Object ( [field_name] 
=> Batting Average [field_val] => .300 ) [8] => stdClass Object ( [field_name] => High School [field_val] => Lincoln High School ) [9] => stdClass 
Object ( [field_name] => Graduation Year [field_val] => 2012 ) [10] => stdClass Object ( [field_name] => GPA [field_val] => 3.0 ) [11] => 
stdClass Object ( [field_name] => Player Name [field_val] => Jim Player ) [12] => stdClass Object ( [field_name] => Position [field_val] => 1B ) 
[13] => stdClass Object ( [field_name] => Number [field_val] => 5 ) [14] => stdClass Object ( [field_name] => Bats [field_val] => L ) [15] => 
stdClass Object ( [field_name] => Throws [field_val] => L ) [16] => stdClass Object ( [field_name] => Batting Average [field_val] => .280 ) [17] 
=> stdClass Object ( [field_name] => High School [field_val] => Lincoln High School ) [18] => stdClass Object ( [field_name] => Graduation Year 
[field_val] => 2013 ) [19] => stdClass Object ( [field_name] => GPA [field_val] => 3.2 ) [20] => stdClass Object ( [field_name] => Team Name 
[field_val] => Xjcvnnlkxnv ) [21] => stdClass Object ( [field_name] => Player Name [field_val] => Dsffnds ) [22] => stdClass Object ( [field_name] 
=> Position [field_val] => Kjfdssdj ) [23] => stdClass Object ( [field_name] => Number [field_val] => Sjkkjdsv ) [24] => stdClass Object ( 
[field_name] => Bats [field_val] => ) [25] => stdClass Object ( [field_name] => Throws [field_val] => ) [26] => stdClass Object ( [field_name] 
=> Batting Average [field_val] => ) [27] => stdClass Object ( [field_name] => High School [field_val] => ) [28] => stdClass Object ( [field_name] 
=> Graduation Year [field_val] => ) [29] => stdClass Object ( [field_name] => GPA [field_val] => ) [30] => stdClass Object ( [field_name] => 
Player Name [field_val] => ) [31] => stdClass Object ( [field_name] => Position [field_val] => ) [32] => stdClass Object ( [field_name] => 
Number [field_val] => ) [33] => stdClass Object ( [field_name] => Bats [field_val] => ) [34] => stdClass Object ( [field_name] => Throws 
[field_val] => ) [35] => stdClass Object ( [field_name] => Batting Average [field_val] => ) [36] => stdClass Object ( [field_name] => High School 
[field_val] => ) [37] => stdClass Object ( [field_name] => Graduation Year [field_val] => ) [38] => stdClass Object ( [field_name] => GPA 
[field_val] => ) )

I'd like to see if there's a way to individually echo each field, such as "echo $teaminfo->name;" or "echo $teaminfo->address;" so that I can wrap each echo within a CSS class.

 

Alternatively, would there be a way to echo the field_val wrapped around <span class="field_name"></span> ?  This would also suit my needs, but I'm not sure how to accomplish it.

 

I've managed to figure out the alternative way of doing this with this code:

 

$result = $wpdb->get_results ("SELECT field_name, field_val FROM wp_cformsdata WHERE field_name <> 'page' OR 'Fieldset1'", OBJECT);
foreach ($result as $teaminfo)
{
$classname = str_replace(' ', '', $teaminfo->field_name); 
echo "<span class=\"";
echo $classname;
echo "\">";
echo $teaminfo->field_val;
echo "</span>";
}

 

This doesn't provide me with as much flexibility as I had hoped, so I'd still like to know if there's a way I can echo out each field and loop it for each form submission.

 

As you can see from the phpmyadmin screenshot from above, there's a field titled "sub_id" which increments with each form submission.  I'd like to echo out each field individually and then loop it through each "sub_id".

 

This is just one of those situations where I know what I want to do, but don't know exactly how to accomplish it.  Any help is appreciated.  Thank you!

 

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.