Jump to content

[SOLVED] More efficient way?


Recreational_Champ

Recommended Posts

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);



?>

Link to comment
Share on other sites

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  ;D

 

Thank you so very much for your time!

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.