Jump to content

[SOLVED] Help with associative array


seavers

Recommended Posts

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

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.