Jump to content

PHP Sorting


bp90210

Recommended Posts

i am trying to modify existing code to sort alphabetically. right now it returns the results without sorting them.  the last line is return ( $output_str );

the formatting of each string output is  "Name of Item - (LINK)"  i want it sorted by the name of item.  would the best way to convert the string output to an array and then sort?  i need help in doing this the best way.

Link to comment
https://forums.phpfreaks.com/topic/86596-php-sorting/
Share on other sites

here is the code if it makes more sense looking at it.

 

<?php 


// ----------------
// Search Functions
// ----------------

function search ( $search_str, $max_results=10 ) {

	// Read entries by month, year and/or day. Generate HTML output.
	//
	// Used for the main Index page.
	global $lang_string, $blog_config, $user_colors;

	// To avoid server overload
	sleep(1);

	$output_str = '';

	$entry_file_array = blog_entry_listing();

	$words=@split( ' ', strtoupper( $search_str ) );

	if ( strlen( $search_str ) > 1) {
		for ( $i = 0; $i < count( $words ); $i++) {
			$words[$i] = trim($words[$i] );
		}

		if ( $max_results <= 0 ) {
			$max_results=100;  //RESULT COUNT (default 10)
		}

		$results=0;
		// Loop through entry files
		for ( $i = 0; $i < count( $entry_file_array ); $i++ ) {
			if ( $results > $max_results ) {
				break;
			}

			list( $entry_filename, $year_dir, $month_dir ) = explode( '|', $entry_file_array[ $i ] );
			$contents = sb_read_file( 'content/' . $year_dir . '/' . $month_dir . '/' . $entry_filename );
			$j = 0;
			$found = true;
			$text = strtoupper( $contents );
			while ( ( $j<count( $words ) ) && ( $found ) ) {
				if ( $words[$j] !== '' ) {
					$found = $found && ( strpos( $text, $words[$j] ) !== false );
				}
				$j++;
			}
			if ( $found ) {
				$results++;
				$blog_entry_data = blog_entry_to_array( 'content/' . $year_dir . '/' . $month_dir . '/' . $entry_filename );
				$output_str  .= '<B><font color="FFFFFF">' . $blog_entry_data[ 'SUBJECT' ] . ' </B></font>';
				$output_str  .= '(<a href="index.php?entry=' . sb_strip_extension( $entry_filename ) . '" title="' . format_date( $blog_entry_data[ 'DATE' ] ) . '">' . 'Review' . '</a>)<br />';
			//	$output_str  .= '<a href="index.php?entry=' . sb_strip_extension( $entry_filename ) . '" title="' . format_date( $blog_entry_data[ 'DATE' ] ) . '">' . $blog_entry_data[ 'SUBJECT' ] . '</a><br />';
			}
			// Search Comments
			if ( $blog_config[ 'blog_enable_comments' ] == true ) {
				$comment_file_array = sb_folder_listing( 'content/' . $year_dir . '/' . $month_dir . '/' . sb_strip_extension( $entry_filename ) . '/comments/', array( '.txt', '.gz' ) );

				for ( $k = 0; $k < count( $comment_file_array ); $k++ ) {
					$comment_filename =  $comment_file_array[ $k ];
					//We only want to search inside comments, not the counter
					if ( strpos($comment_filename, 'comment') === 0 )
					{
						$contents_comment = sb_read_file( 'content/' . $year_dir . '/' . $month_dir . '/' . sb_strip_extension( $entry_filename ) . '/comments/' . $comment_filename );
						$found_in_comment = true;
						$l = 0;
						$text = strtoupper( $contents_comment );
						while ( ( $l< count( $words ) ) && ( $found_in_comment ) ) {
							if ( $words[$l] !== '' ) {
								$found_in_comment = $found_in_comment && ( strpos( $text, $words[$l] ) !== false );
							}
							$l++;
						}
						if ( $found_in_comment ) {
							$results++;
							if ( $found == false ) {
							//	list( $blog_subject, $blog_date, $blog_text ) = explode('|', ( $contents ) );
					//			$blog_entry_data = blog_entry_to_array( 'content/' . $year_dir . '/' . $month_dir . '/' . $entry_filename );
					//		$output_str  .= $blog_entry_data[ 'SUBJECT' ] . '<br />';
							}

							// list( $comment_author, $comment_date, $comment_text ) = explode('|', ( $contents_comment ) );
							$comment_entry_data = comment_to_array( 'content/' . $year_dir . '/' . $month_dir . '/' . sb_strip_extension( $entry_filename ) . '/comments/' . $comment_filename );

						//	global $theme_vars;
					//		if ( $blog_config[ 'blog_comments_popup' ] == 1 ) {
					//			$output_str  .= '   <a href="javascript:openpopup(\'comments.php?y='.$year_dir.'&m='.$month_dir.'&entry='. sb_strip_extension($entry_filename).'\','.$theme_vars[ 'popup_window' ][ 'width' ].','.$theme_vars[ 'popup_window' ][ 'height' ].',true)">' . $comment_entry_data[ 'NAME' ] . '</a><br />';
					//		} else {
					//			$output_str  .= '   <a href="comments.php?y=' . $year_dir . '&m=' . $month_dir . '&entry=' . sb_strip_extension( $entry_filename ) . '" title="' . format_date( $comment_entry_data[ 'DATE' ] ) . '">' . $comment_entry_data[ 'NAME' ] . '</a><br />';
					//		}
						}
					}
				}
			}
		}
		// Search static pages
		$static_file_array = sb_folder_listing( 'content/static/', array( '.txt', '.gz' ) );
		for ( $i = 0; $i < count( $static_file_array ); $i++ ) {
			$static_filename =  $static_file_array[ $i ];
			$contents_static = sb_read_file( 'content/static/' . $static_filename );
			$found_in_static = true;
			$j = 0;
			$text = strtoupper( $contents_static );
			while ( ( $j< count( $words ) ) && ( $found_in_static ) ) {
				if ( $words[$j] !== '' ) {
					$found_in_static = $found_in_static && ( strpos( $text, $words[$j] ) !== false );
				}
				$j++;
			}
		//	if ( $found_in_static ) {
		//		$results++;
		//		$blog_static_data = static_entry_to_array( 'content/static/' . $static_filename );
		//		$output_str  .= '<a href="static.php?page=' . sb_strip_extension( $static_filename ) . '" title="' . format_date( $blog_static_data[ 'DATE' ] ) . '">' . $blog_static_data[ 'SUBJECT' ] . '</a><br />';
		//	}
		}
	}
		return ( $output_str );
}
?>

Link to comment
https://forums.phpfreaks.com/topic/86596-php-sorting/#findComment-442455
Share on other sites

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.