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:
- If $current_marks > $previous_marks, then value of $current_marks should be displayed in green color along with pass.png image next to the value.
- Else value of $current_marks should be displayed in red color along with fail.png image next to the value.
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.
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.