Recreational_Champ Posted November 14, 2008 Share Posted November 14, 2008 I would like to know if there is a more efficient way than what I already have. <?php $ip = gethostbyaddr($_SERVER['REMOTE_ADDR']); require_once ('mysql_connect.inc.php'); $query = "INSERT INTO logs VALUES(NULL,{$_GET['a_id']},'$ip',NULL)"; mysql_query($query) or die("Not inserted". mysql_error()); $query = "SELECT * FROM answers,logs where answers.question_id={$_GET['q_id']} and logs.answer_id=answers.answer_id"; $result = mysql_query($query) or die('Error, query failed'); $num_rows = mysql_num_rows($result); $sum_1; $sum_2; $sum_3; if($_GET['q_id'] == 1) { $query = "SELECT COUNT(answer_id) AS times_chosen FROM logs where answer_id=1"; $result = mysql_query($query) or die('Error, query failed'); $row = mysql_fetch_array($result); $sum_1 = $row['times_chosen']; $query = "SELECT COUNT(answer_id) AS times_chosen FROM logs where answer_id=2"; $result = mysql_query($query) or die('Error, query failed'); $row = mysql_fetch_array($result); $sum_2 = $row['times_chosen']; $query = "SELECT COUNT(answer_id) AS times_chosen FROM logs where answer_id=3"; $result = mysql_query($query) or die('Error, query failed'); $row = mysql_fetch_array($result); $sum_3 = $row['times_chosen']; } if($_GET['q_id'] == 2) { $query = "SELECT COUNT(answer_id) AS times_chosen FROM logs where answer_id=4"; $result = mysql_query($query) or die('Error, query failed'); $row = mysql_fetch_array($result); $sum_1 = $row['times_chosen']; $query = "SELECT COUNT(answer_id) AS times_chosen FROM logs where answer_id=5"; $result = mysql_query($query) or die('Error, query failed'); $row = mysql_fetch_array($result); $sum_2 = $row['times_chosen']; $query = "SELECT COUNT(answer_id) AS times_chosen FROM logs where answer_id=6"; $result = mysql_query($query) or die('Error, query failed'); $row = mysql_fetch_array($result); $sum_3 = $row['times_chosen']; } if($_GET['q_id'] == 3) { $query = "SELECT COUNT(answer_id) AS times_chosen FROM logs where answer_id=7"; $result = mysql_query($query) or die('Error, query failed'); $row = mysql_fetch_array($result); $sum_1 = $row['times_chosen']; $query = "SELECT COUNT(answer_id) AS times_chosen FROM logs where answer_id=8"; $result = mysql_query($query) or die('Error, query failed'); $row = mysql_fetch_array($result); $sum_2 = $row['times_chosen']; $query = "SELECT COUNT(answer_id) AS times_chosen FROM logs where answer_id=9"; $result = mysql_query($query) or die('Error, query failed'); $row = mysql_fetch_array($result); $sum_3 = $row['times_chosen']; } $percent_1 = ($sum_1 / $num_rows) * 100; $percent_2 = ($sum_2 / $num_rows) * 100; $percent_3 = ($sum_3 / $num_rows) * 100; mysql_free_result($result); mysql_close($connection); ?> Quote Link to comment https://forums.phpfreaks.com/topic/132776-solved-more-efficient-way/ Share on other sites More sharing options...
Maq Posted November 15, 2008 Share Posted November 15, 2008 Could you explain what this is supposed to do rather than me reading through your code and guessing? Quote Link to comment https://forums.phpfreaks.com/topic/132776-solved-more-efficient-way/#findComment-690513 Share on other sites More sharing options...
.josh Posted November 15, 2008 Share Posted November 15, 2008 I would like to know if there is a more efficient way than what I already have. Yes. ...or were you really asking for someone to rewrite your code for you? Quote Link to comment https://forums.phpfreaks.com/topic/132776-solved-more-efficient-way/#findComment-690515 Share on other sites More sharing options...
Maq Posted November 15, 2008 Share Posted November 15, 2008 What makes you think there is a efficient way? Is your script slow? Does it look bad? What is the reason for you posting here? Do you have any theoretical ideas that you think may be better? Quote Link to comment https://forums.phpfreaks.com/topic/132776-solved-more-efficient-way/#findComment-690625 Share on other sites More sharing options...
Barand Posted November 15, 2008 Share Posted November 15, 2008 Those 9 queries (3 sets of 3) could be reduced to single query, if that's what you mean, by using a query similar to your first one, joining answers with log, querying by question_id and grouping counts by answer_id Quote Link to comment https://forums.phpfreaks.com/topic/132776-solved-more-efficient-way/#findComment-690721 Share on other sites More sharing options...
Recreational_Champ Posted November 22, 2008 Author Share Posted November 22, 2008 What makes you think there is a efficient way? Is your script slow? Does it look bad? What is the reason for you posting here? Do you have any theoretical ideas that you think may be better? Sorry for not specifying exactly what I was trying to accomplish; I really thought the code was short and strait forward enough to follow without explanation. I don't just want to code for a solution, but code in the most efficient way as possible. I feel as though my solutions are elementary in their structure and do think that it looks "bad" to have repetitive processes. Yes. ...or were you really asking for someone to rewrite your code for you? I guess I kinda wanted for someone to post a solution not because I wanted it done for me, but to understand what I could do to improve my self in the future Those 9 queries (3 sets of 3) could be reduced to single query, if that's what you mean, by using a query similar to your first one, joining answers with log, querying by question_id and grouping counts by answer_id YES! YES! YES! That is the solution I was looking for: select logs.answer_id, count(*) from logs,answers where answers.question_id=$_GET['variable'] and logs.answer_id=answers.answer_id group by answer_id I had yet to discover grouping Thank you so very much for your time! Quote Link to comment https://forums.phpfreaks.com/topic/132776-solved-more-efficient-way/#findComment-696550 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.