Re: array problems
| From: | John Wells | Date: | Wed, 16 Aug 2006 19:05:55 +0000 |
| Subject: | Re: array problems | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-240780@lists.php.net to get a copy of this message | ||
On 8/16/06, David Tulloh <david@tulloh.id.au> wrote:
Chris G wrote: foreach ($user_input_array as $user_input) {The downside to this approach is that you'll end up making a SQL call for each value in the array. An alternative would be to build a comma-delimited string of values and use SQL's IN() check. Example: [code] // CAUTION: Validate your data to protect from SQL Injection, like David says! // Assuming you've done this, $per_ids_array contains valid/clean/safe data // that was originally submitted through $_GET['per_id']. // Let's turn that array into a comma-delimited string using implode() $per_ids_string = implode(',', $per_ids_array); // Now plug the string into our SQL query: $query = 'SELECT rep_l_per_id, rep_value_perc FROM report_values WHERE rep_l_per_id IN (' . $per_ids . ') ORDER BY rep_l_per_id ASC'; // execute $result = mysql_query($query) or die (mysql_error()); // Before looping through our result set, initialize two variables:$data_member = array(); # Create an empty arrayDo SQL query stuffforeach ($sql_results as $sql_member) { $data_member[] = $sql_member; }$data[] = $data_member;} You can now do your function call as $gbarplot = new GroupBarPlot($data);
$gbp_data = array(); // this will be the final array to be passedto GroupBarPlot()
$temp_id = NULL; // use this to determine the result set has movedto a new rep_|_per_id while($line = mysql_fetch_array($result)) {
// IF $temp_id does not match the current rep_|_per_id,
// create a new element of $gbp_data as an array.
// Also, set $temp_id to this rep_|_per_id value to use in the
next iteration.
if ($temp_id != $line['rep_|_per_id']) {
$temp_id = $line['rep_|_per_id'];
$gbp_data[$temp_id] = array();
}
// Add rep_value_perc as an array element
$gbp_data[$temp_id][] = $line['rep_value_perc'];
}
// Now pass to your function:
$gbarplot = new GroupBarPlot($gbp_data);
[/code]
It's all untested, but I *think* will work out of the box.
And now a bit of bonus advice. The SQL query that you provided earlier has a giant SQL injection attack problem. I'd recomend reading a little about SQL injection attacks and getting it patched up.Yeah, this cannot be stressed enough!!! HTH, John W