Jump to content

how to print out all certian json data in database?


et4891

Recommended Posts

What I'm trying is I have a page which you can input 3 angel integers and submit. After you submit the data will be saved in database using json encode and another page to retrieve the data and make the angles to draw a pie chart.

{"angle1":"60","angle2":"60","angle3":"90","submit":"Submit"}

The above is what's saved into a row

I used

<?php
$sql = "SELECT * FROM wp_options WHERE option_name LIKE 'angle%' ORDER BY option_name";
$options = $wpdb->get_results($sql);
foreach ( $options as $option )
{
echo '<p><b>'.$option->option_name.'</b> = '
.esc_attr($option->option_value).'</p>'.PHP_EOL;

$line = json_decode($option->option_value, true);
}
echo '<span style="color:#f00;">'.'Angel 1 : '.$line['angle1'].'</span><br>';
echo '<span style="color:#0f0;">'.'Angel 2 : '.$line['angle2'].'</span><br>';
echo '<span style="color:#00f;">'.'Angel 3 : '.$line['angle3'].'</span><br>';

This prints out angel 1 : ## where ## is the angle entered and so on. I also made a simple 2d piechart which shows the angles.

the problem I'm having is if I submit another 3 angles then my result only shows the MOST RECENT angles entered even if there are two or more in the database.

For example in my database I can see

{"angle1":"60","angle2":"60","angle3":"90","submit":"Submit"}
{"angle1":"60","angle2":"60","angle3":"180","submit":"Submit"}
{"angle1":"30","angle2":"60","angle3":"180","submit":"Submit"}

but the result only prints 30 60 and 180 instead of printing all three. Anyone mind giving me a hand on how I can print all three data out or at least all three sets of the angles. I believe once I figured that out I can then print out all the piecharts with all the angles instead right now only the most recent angle and the piechart are printed.

Thanks a lot people~

 

ok I'm so stupid I should have echo those lines inside the foreach loop but then I believe my piechart scripts should be inside the foreach loop too but the thing is my script contains lots of html tags and so on  and below is my codes

<canvas id="piechart1" width="100" height="100"></canvas>
	<script src="<?php echo get_stylesheet_directory_uri().'/piechart.js'; ?>"></script>
	<script>
	    var chartId = "piechart1";
	    var colours = ["#f00", "#0f0", "#00f"];
	    var angles =  [<?php echo $line['comp2052_angle1'].','.
	    						  $line['comp2052_angle2'].','.
	    						  $line['comp2052_angle3'];
	    			   ?>];
	    piechart(chartId, colours, angles);
	</script>

how can I implement those codes into foreach easily instead of echo them out bit by bit?

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.