Arrange the results of a foreach in a table

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Sagaroth
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Jan 14, 2021 2:07 am

Thu Jan 14, 2021 2:14 am

Hello everyone!

I am currently developing a module for my site that will allow me to classify each solution to help a disability by type.
Here's an example:

Code: Select all

Type : Visual disability
Solution : Add screen readers
For this, I have a database in which these types and solutions are found. In this database, the solutions of one type or another are not in a defined order. They can be put randomly, when a user adds one.

I need to display, in a table, these Types first (as a title), then the solutions corresponding to this type.

I manage to generate a display thanks to a foreach loop, but my solutions are all mixed up and therefore not clearly sorted.


This is how it is currently displayed

Code: Select all

Visual disability

    Audio-described cut-scenes

    Highlighted path to follow

    Sensitivity settings for all the controls

    Screen readers on menus

    Slow down the game speed
But I would need it to look like this:

Code: Select all

Visual disability

    Audio-described cut-scenes

    Highlighted path to follow

    Screen readers on menus

    Slow down the game speed

Physical disability

    Sensitivity settings for all the controls
In my example above, only the solution "Sensitivity settings for all the controls" is part of the "Physical disability" type. The rest is part of the "Visual disability" type.

Here is the code I currently use to retrieve my information from my database and display it. It's a Wordpress.

Code: Select all

/*====== Accessibility Options ======*/

function cloux_accessibility_options() {
	if ( function_exists( 'rwmb_meta' ) ) {
		$output = "";
		$accessibility_options = rwmb_meta( 'accessibility-options' );
		$accessibility_options_status = rwmb_meta( 'accessibility-options-status' );
		if( $accessibility_options_status == "1" ) {
			if( !empty( $accessibility_options ) ) {
				$output .= '<div class="accessibility-options widget-box">';
					$output .= cloux_title( $title = esc_html__( 'Accessibility Options', 'cloux' ), $style = "style-4", $align = "left", $colored_title = '' );
					$output .= '<ul>';
						$output .= '<li class="title visual_disability">' . esc_html__( 'Visual disability', 'cloux' ) . '</li>';
					$output .= '</ul>';

						foreach ( $accessibility_options as $accessibility_options ) {
							if( !empty( $accessibility_options ) ) {
								$accessibility_name =  isset( $accessibility_options['accessibility-options-name'] ) ? $accessibility_options['accessibility-options-name'] : '';
								$accessibility_type =  isset( $accessibility_options['accessibility-type-name'] ) ? $accessibility_options['accessibility-type-name'] : '';
								$accessibility_type = get_term( $accessibility_type, 'accessibility' );
								$accessibility_type_name = $accessibility_type->name;
								$availability =  isset( $accessibility_options['availability'] ) ? $accessibility_options['availability'] : '';

							if ( $accessibility_type_name == "Visual disability" ){
								if( !empty( $accessibility_name ) or !empty( $availability )) {
						$output .= '<ul>';
									$output .= '<li class="item accessibility_name">';
										if( !empty( $accessibility_name ) ) {
											$accessibility_name = get_term( $accessibility_name, 'accessibility' );
											if( ( !empty( $accessibility_name ) ) ) {
												$output .= '<a href="' . get_term_link( $accessibility_name ) . '">' . esc_attr( $accessibility_name->name ) . '</a>';
											}
										}
									$output .= '</li>';
									$output .= '<li class="item visual_disability status">';
										if( $availability == "1" ) {
											$output .= '<i class="fas fa-check" aria-hidden="true"></i>';
										} else {
											$output .= '<i class="fas fa-times" aria-hidden="true"></i>';
										}
									$output .= '</li>';
								}
							}
							
							
							if ( $accessibility_type_name == "Motor/Physical disability" ){
								if( !empty( $accessibility_name ) or !empty( $availability )) {
						$output .= '<ul>';
									$output .= '<li class="item accessibility_name">';
										if( !empty( $accessibility_name ) ) {
											$accessibility_name = get_term( $accessibility_name, 'accessibility' );
											if( ( !empty( $accessibility_name ) ) ) {
												$output .= '<a href="' . get_term_link( $accessibility_name ) . '">' . esc_attr( $accessibility_name->name ) . '</a>';
											}
										}
									$output .= '</li>';
									$output .= '<li class="item visual_disability status">';
										if( $availability == "1" ) {
											$output .= '<i class="fas fa-check" aria-hidden="true"></i>';
										} else {
											$output .= '<i class="fas fa-times" aria-hidden="true"></i>';
										}
									$output .= '</li>';
								}
							}
							
							
						$output .= '</ul>';
						}
					}
				$output .= '</div>';
			}
		}
		return $output;
	}
}

Thank you in advance for all the help you can give me!
Post Reply