webbiegurrl Posted March 22, 2017 Share Posted March 22, 2017 Please be kind as I am simply modifying an existing script to suit my needs and I am still just learning PHP. I have this code foreach( $sorters as $sortvalue ) if( $sortvalue == '' ) echo '<li> <a href="' . $list_url . $connector . $sortfield . '=none">None given</a> </li>'; else echo '<li> <a href="' . $list_url . $connector . $sortfield . '=' . $sortvalue . '">' . $sortvalue . '</a> </li>'; echo '</ul>'; which lists the names of the countries that members live in. I have modified this to show the country's flag instead of the name. foreach( $sorters as $sortvalue ) if( $sortvalue == '' ) echo '<li> <a href="' . $list_url . $connector . $sortfield . '=none">None given</a> </li>'; else echo '<a href="' . $list_url . $connector . $sortfield . '=' . $sortvalue . '"><img src="' . $flags . str_replace(' ', '_', $sortvalue) . '.gif" title="' . $sortvalue . '" alt="' . $sortvalue . '"></a><br>'; This works very nicely, but I also wanted to display the number of members for each country, so I further modified the code and got this foreach( $sorters as $sortvalue ) /* $dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password); foreach($dbh->query('SELECT country, COUNT(*) FROM $table GROUP BY country') as $row) { */ if( $sortvalue == '' ) echo '<li> <a href="' . $list_url . $connector . $sortfield . '=none">None given</a> </li>'; else if ($row['COUNT(*)'] != "1") echo '<a href="' . $list_url . $connector . $sortfield . '=' . $sortvalue . '"><img src="' . $flags . str_replace(' ', '_', $sortvalue) . '.gif" title="' . $sortvalue . '" alt="' . $sortvalue . '"></a> ' . $row['COUNT(*)'] . ' fans<br>'; else echo '<a href="' . $list_url . $connector . $sortfield . '=' . $sortvalue . '"><img src="' . $flags . str_replace(' ', '_', $sortvalue) . '.gif" title="' . $sortvalue . '" alt="' . $sortvalue . '"></a> ' . $row['COUNT(*)'] . ' fan<br>'; } The section that is currently commented out will give the correct number, but makes all the flags the same. Or with it commented out, the flags are correct, but (obviously) no values. I can't seem to trial & error my way through getting both working. Can someone tell me what I'm doing wrong please? Quote Link to comment Share on other sites More sharing options...
requinix Posted March 22, 2017 Share Posted March 22, 2017 The problem is trying to do two loops at once: the first over $sorters and the second over the counts. Can you get rid of $sorters and just use the query? It will include the list of countries automatically so... Quote Link to comment Share on other sites More sharing options...
webbiegurrl Posted March 22, 2017 Author Share Posted March 22, 2017 Not exactly sure how to do that, though I do understand what you mean. Even though the query works, it seems very crude next to the existing code, is there anyway of using what's already there to add what I need? Here's the full page <? require 'config.php'; require_once( 'mod_errorlogs.php' ); require_once( 'mod_owned.php' ); require_once( 'mod_members.php' ); require_once( 'mod_settings.php' ); // function to clean data if( !function_exists( 'clean' ) ) { function clean( $data ) { $data = trim( htmlentities( strip_tags( $data ), ENT_QUOTES ) ); if( get_magic_quotes_gpc() ) $data = stripslashes( $data ); $data = addslashes( $data ); return $data; } } // get listing info and member type $info = get_listing_info( $listing ); $member_type = ( $info['listingtype'] == 'fanlisting' ) ? 'fans' : 'members'; // explode the fields for sorting and make sure there are no whitespace $sortarray = explode( ',', $info['sort'] ); foreach( $sortarray as $i => $s ) $sortarray[$i] = trim( $s ); // what sorting fields have been selected already by the visitor? // obtain from $_GET array $set = 0; $selected = array(); // what have we selected? $sortfield = $sortarray[$set]; // what field are we currently sorting? while( isset( $sortarray[$set] ) && isset( $_GET[$sortarray[$set]] ) ) { // while the user has selected a value to sort with $selected[$sortarray[$set]] = $_GET[$sortarray[$set]]; if( $selected[$sortarray[$set]] == 'all' ) $selected[$sortarray[$set]] = '%'; $set++; if( isset( $sortarray[$set] ) ) $sortfield = $sortarray[$set]; } //$set--; $sorters = get_member_sorter( $listing, $set, $selected ); // are we still going to show the sorting? if( count( $sortarray ) == $set ) // this is the last? return; // create list URL $list_url = $info['listpage']; $connector = '?'; if( substr_count( $list_url, '?' ) > 0 ) if( $info['dropdown'] == 1 ) $connector = '&'; else $connector = '&'; // get anything that's already in $selected and put it in the URL $in_url = array(); foreach( $selected as $field => $value ) { $field = clean( $field ); $value = clean( $value ); if( in_array( $field, $in_url ) ) continue; $list_url .= "$connector$field=$value"; if( $info['dropdown'] == 1 ) $connector = '&'; else $connector = '&'; $in_url[] = $field; } // if owner selected the dropdown method if( $info['dropdown'] == 1 ) { ?> <script type="text/javascript"> <!-- function change( form ) { var dropDown = form.elements[0]; var myIndex = dropDown.selectedIndex; if( dropDown.options[myIndex].value != "0" ) { window.open( "<?php echo $list_url . $connector . $sortfield ?>=" + dropDown.options[myIndex].value, target = "_self" ); } } // end --> </script> <form method="get" action="<?php echo $list_url ?>" class="show_sort_form"> <p> <select name="<?php echo $sortfield ?>" onchange="change( this.form );"> <option value="0"> Select sort option</option> <option value="all"> All <?php echo $member_type ?></option> <?php foreach( $sorters as $sort ) if( $sort == '' ) echo '<option value="none"> None given </option>'; else echo '<option value="' . $sort . '"> ' . $sort . '</option>'; ?> </select> </p> </form> <?php } else { ?> <p class="show_members_showing_what">from <?=count($sorters) ?> Countries</p> <p class="center"><a href="">Sorted by Country</a> | <a href="<?=$siteurl?>/sort.php?country=all">Full List</a></p> <div class="flaglist"> <? foreach( $sorters as $sortvalue ) $dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password); foreach($dbh->query('SELECT country, COUNT(*) FROM $table GROUP BY country') as $row) { if( $sortvalue == '' ) echo '<li> <a href="' . $list_url . $connector . $sortfield . '=none">None given</a> </li>'; else if ($row['COUNT(*)'] != "1") echo '<a href="' . $list_url . $connector . $sortfield . '=' . $sortvalue . '"><img src="' . $flags . str_replace(' ', '_', $sortvalue) . '.gif" title="' . $sortvalue . '" alt="' . $sortvalue . '"></a> ' . $row['COUNT(*)'] . ' fans<br>'; else echo '<a href="' . $list_url . $connector . $sortfield . '=' . $sortvalue . '"><img src="' . $flags . str_replace(' ', '_', $sortvalue) . '.gif" title="' . $sortvalue . '" alt="' . $sortvalue . '"></a> ' . $row['COUNT(*)'] . ' fan<br>'; } } ?> </div> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.