I'm not an expert in PHP and I'm trying to apply conditional formatting on data fetched from wpdb. To be fair, I'm trying to create shortcode for this. My motto is to fetch values for $previous_marks & $current_marks from the database and change the color of $current_marks according to if, else condition. According to condition:
I also need help in displaying images next to $current_marks value & I have no code for it:
Here's how my code looks like:
add_shortcode( 'marks_col', function () {
$subject = $_GET['subject'];
global $wpdb;
$previous_marks = $wpdb->get_results( "SELECT prev FROM grade WHERE sub='" .$subject. "'");
$current_marks = $wpdb->get_results( "SELECT current FROM grade WHERE sub='" .$subject. "'");
if ($previous_marks < $current_marks) {
echo "<p style="color:green">" .$current_marks. "</p>"; ##display pass.png next to $current_marks value
} else {
echo "<p style="color:red">" .$current_marks. "</p>"; ##display fail.png next to $current_marks value
}
} );
$subject is fetched from the website's URL: www.domain/results?subject=biology. Currently, on using the shortcode [marks_col] on WP installation, it's just printing 'Array' in red color & nothing else. Any help would be greatly appreciated.
I'm not an expert in PHP and I'm trying to apply conditional formatting on data fetched from wpdb. To be fair, I'm trying to create shortcode for this. My motto is to fetch values for $previous_marks & $current_marks from the database and change the color of $current_marks according to if, else condition. According to condition:
I also need help in displaying images next to $current_marks value & I have no code for it:
Here's how my code looks like:
add_shortcode( 'marks_col', function () {
$subject = $_GET['subject'];
global $wpdb;
$previous_marks = $wpdb->get_results( "SELECT prev FROM grade WHERE sub='" .$subject. "'");
$current_marks = $wpdb->get_results( "SELECT current FROM grade WHERE sub='" .$subject. "'");
if ($previous_marks < $current_marks) {
echo "<p style="color:green">" .$current_marks. "</p>"; ##display pass.png next to $current_marks value
} else {
echo "<p style="color:red">" .$current_marks. "</p>"; ##display fail.png next to $current_marks value
}
} );
$subject is fetched from the website's URL: www.domain.com/results?subject=biology. Currently, on using the shortcode [marks_col] on WP installation, it's just printing 'Array' in red color & nothing else. Any help would be greatly appreciated.
First a few notes:
The code based on your given question, although I think you're missing a WHERE statement in your query to fetch a specific persons 'marks' based on own projects.
add_shortcode( "marks", function( $atts ) {
//= defaults
$args = shortcode_atts( array(
'subject' => 'biology',
), $atts );
//= database query
global $wpdb;
$query = $wpdb->prepare( "SELECT prev, current FROM grade WHERE sub = %s", $args['subject'] );
$result = $wpdb->get_row( $query );
//= bail when no result
if( is_null( $result ) ) return;
//= conditional
if( $result[ 'prev' ] < $result[ 'current' ] ) {
echo "<p color=\"color: green;\">{$result['current']}<i class=\"far fa-check\"></i></p>";
} else {
echo "<p color=\"color: red;\">{$result['current']}<i class=\"far fa-times\"></i></p>";
}
} );
If needed you can easily debug objects and arrays on screen with the following code:
echo var_export( $array, true );
Hope this helps out!
I don't completely understand but you want to access data based on a "subject" which I suppose is the "User_ID" or something in that order. And also pass a "column" parameter. So you could request one of the following things with one shortcode.
If so we indeed need to alter the code.
ARRAY
then that means your variable is returning an array from the database. You can useforeach
to print the contents of the array: tutorialrepublic.com/faq/…, then decide what content you want from the array. – Pixelsmith Commented Aug 15, 2020 at 18:35