seavers Posted August 23, 2007 Share Posted August 23, 2007 Hi, I have to search a db table, and count distinct records to get some results from a survey we've done. The record needs to be echoed along with the number of times it is recorded. Each field in the table has to be searched individually, so that we can see the answers for each question separately. I thought that I could do this with one query, maybe 'count distinct', but I couldn't get it to show both the value of the row, and the amount of times that value had been recorded. So, I have to do 2 queries, one getting the distinct values of the field, the other counting how many times they are recorded. Here's the 2 queries: First query: $search = $_POST['search']; mysql_select_db($database_survey, $survey); $query_survey_results = "SELECT DISTINCT * FROM survey group by '$search' having count(*) > 0"; $survey_results = mysql_query($query_survey_results, $survey) or die(mysql_error()); $row_survey_results = mysql_fetch_assoc($survey_results); $totalRows_survey_results = mysql_num_rows($survey_results); Second query: $age = $row_survey_results['age']; mysql_select_db($database_survey, $survey); $query_counting_query = "SELECT * FROM survey WHERE age = '$age'"; $counting_query = mysql_query($query_counting_query, $survey) or die(mysql_error()); $row_counting_query = mysql_fetch_assoc($counting_query); $totalRows_counting_query = mysql_num_rows($counting_query);?> <?php echo $row_survey_results['age']; echo " = "; echo $totalRows_counting_query /228 *100 ?> <br /> <?php } while ($row_survey_results = mysql_fetch_assoc($survey_results)); This works just fine when the variable '$search' value is 'age', but there are 40 or so other questions, so other values for '$search' will be passed, such as 'region'. So I need to select the associative array that is in the second query (row_survey_results['age'] dynamically but I couldn't work out how to do this. In the end I wrote a load of case breaks and the thing works, but instead of 50 or so lines of code I have 1600, so I guess I'm doing it all wrong! The question is 2 part: 1. Could I have puuled the recordset and count in one query? 2. How do you dynamically echo an associative array from a result, i.e. row_survey_results ['dynamic depending on value of $search variable']; Any help would be greatly appreciated, Thanks Quote Link to comment https://forums.phpfreaks.com/topic/66312-solved-help-with-associative-array/ Share on other sites More sharing options...
sasa Posted August 23, 2007 Share Posted August 23, 2007 use $row_survey_results[$search] Quote Link to comment https://forums.phpfreaks.com/topic/66312-solved-help-with-associative-array/#findComment-332042 Share on other sites More sharing options...
seavers Posted August 23, 2007 Author Share Posted August 23, 2007 Thanks Sasa, I tried was trying to use: $row_survey_results['$search'] that's why I turned to the case break. Should have tried: $row_survey_results[$search] which works like a treat. Quote Link to comment https://forums.phpfreaks.com/topic/66312-solved-help-with-associative-array/#findComment-332079 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.