Jump to content


Photo

Find the lowest value.


  • Please log in to reply
16 replies to this topic

#1 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 05 March 2006 - 11:40 AM

I am wanting to find the link that has the lowest unique hits and then redirect the visitor to it.
I have the value of the unique hits in the varibles $unique1, $unique2, and $unique3.
How would I go about doing this?
Thanks

I have the following code:

   1.  <?php ob_start() ?>
   2.  <html xmlns="http://www.w3.org/1999/xhtml">
   3.  <head>
   4.  <title>Title</title>
   5.  </head>
   6.  <body>
   7.  <?php
   8.  $host = 'localhost';
   9.  $username = '---';
  10.  $password = '---';
  11.  $db_name = '---';
  12.  $sql_connect = mysql_connect("$host", "$username", "$password") or die("MySQL Connection Failed: " . mysql_error());;
  13.  $db_connect = mysql_select_db("$db_name")or die("Could not select DB: " . mysql_error());;
  14.  
  15.  $count1 = mysql_query("SELECT * FROM s") or die(mysql_error());
  16.  $unique1 = mysql_num_rows($count);
  17.  $count2 = mysql_query("SELECT * FROM s1") or die(mysql_error());
  18.  $unique2 = mysql_num_rows($count);
  19.  $count3 = mysql_query("SELECT * FROM g") or die(mysql_error());
  20.  $unique3 = mysql_num_rows($count);
  21.  
  22.  
  23.  if () {
  24.  include("updatedb1.php");
  25.  header("location:--Link Removed--");
  26.  //echo "test1";
  27.  }
  28.  if () {
  29.  include("updatedb2.php");
  30.  header("location:--Link Removed--");
  31.  //echo "test2";
  32.  }
  33.  if () {
  34.  include("updatedb3.php");
  35.  header("location:--Link Removed--");
  36.  //echo "test3";
  37.  }
  38.  
  39.  ?>
  40.  </body>
  41.  </html>


#2 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 05 March 2006 - 01:58 PM

**Bump**
I need to get this done ASAP, could someone please assist me?
Thanks

#3 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 05 March 2006 - 10:13 PM

**Bump**
Anyone able to assist me please?
I'd like to get the solved ASAP.
Thanks

#4 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 06 March 2006 - 05:04 AM

Sheesh, on the 3rd page already.
**Bump**
Please help, I am stuck.
Thankz

#5 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 06 March 2006 - 08:06 AM

We aren't responding because none of us are sure what kind of help you need.

What is the specific problem you are having? Why are you counting 3 different tables? What does each table represent? Why aren't you using SELECT COUNT(*)?

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 06 March 2006 - 09:49 AM

Your if statements are completly invalid! They wont even work properly
if () {
  include("updatedb1.php");
  header("location:--Link Removed--");
  //echo "test1";
}
If statements require some form of arguemnt between the opnening closing brackets - ()

This is a proper if statement:
$a = 2;
if($a == 2)
{
    echo '$a equals to 2!';
}
else
{
    echo '$a does not equal 2!';
}

The below code you use a variable called $count. $count hasn't been defined any where so mysql_num_rows wont be working and most probably is stoping your script from running! You do have variables called $count1, $count2 and $count3. So on lines 16, 18 and 20 you need to change $count to $count1 on line 16, $count to $count2 on line 18 and change $count to $count3 on line 20.
  15.  $count1 = mysql_query("SELECT * FROM s") or die(mysql_error());
  16.  $unique1 = mysql_num_rows($count);
  17.  $count2 = mysql_query("SELECT * FROM s1") or die(mysql_error());
  18.  $unique2 = mysql_num_rows($count);
  19.  $count3 = mysql_query("SELECT * FROM g") or die(mysql_error());
  20.  $unique3 = mysql_num_rows($count);


#7 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 06 March 2006 - 02:11 PM

The reason the if statement are blank is because I have no clue as to what arguement to add. In which is what I was asking.
As for the $count it was a mistake when transfering it from dreamweaver to the site and removing personal info.
I forgot to copy that part.

It should be:
$count1 = mysql_query("SELECT * FROM s") or die(mysql_error());
$unique1 = mysql_num_rows($count1);
$count2 = mysql_query("SELECT * FROM s1") or die(mysql_error());
$unique2 = mysql_num_rows($count2);
$count3 = mysql_query("SELECT * FROM g") or die(mysql_error());
$unique3 = mysql_num_rows($count3);

My goal:
When somone go to this page it grabs the 3 values from the database and defines the values to the varibles $unique1, $unique2, $unique3.
I then want the PHP script to find what have the 3 varibles has the lowest value and then execute the correct include and header:location.

Overall Understanding:
I am a affiliate on a site aswell as 2 of my other friends. We want referrals so we're going to post on my forums, however we want the same number of visitors to be directed to each of our referral links.
Therefore in our post we are going to send them to this script, it will check the database, find which has the lowest amount of unique hits and then add +1 to that number (updates the DB) and sends them to the header:location.

When you don't understand a situation please let me know I will do try to explain it better, when nothing is said I feel as if I'm being ignored hince many other topics are receiving a response.

Thanks
-ShibSta




#8 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 06 March 2006 - 03:39 PM

[!--quoteo(post=352070:date=Mar 6 2006, 09:11 AM:name=ShibSta)--][div class=\'quotetop\']QUOTE(ShibSta @ Mar 6 2006, 09:11 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$count1 = mysql_query("SELECT * FROM s") or die(mysql_error());
$unique1 = mysql_num_rows($count1);
$count2 = mysql_query("SELECT * FROM s1") or die(mysql_error());
$unique2 = mysql_num_rows($count2);
$count3 = mysql_query("SELECT * FROM g") or die(mysql_error());
$unique3 = mysql_num_rows($count3);
[/quote]

First things first. If you're interested in the slowest loading page possible, the above is fine. However, I would recommend, instead, that you allow MySQL to do the counting for you. Observe :

list($unique1) = mysql_fetch_array(mysql_query('SELECT COUNT(*) FROM s'));

Or, to break it out into edible chunks :

$query = 'SELECT COUNT(*) FROM s';
$count1 = mysql_query($query) or die(mysql_error());
list($unique1) = mysql_fetch_array($count1);
mysql_free_result($count1);

[!--quoteo(post=352070:date=Mar 6 2006, 09:11 AM:name=ShibSta)--][div class=\'quotetop\']QUOTE(ShibSta @ Mar 6 2006, 09:11 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
My goal:
When somone go to this page it grabs the 3 values from the database and defines the values to the varibles $unique1, $unique2, $unique3.
I then want the PHP script to find what have the 3 varibles has the lowest value and then execute the correct include and header:location.
[/quote]

Hrm.. Just off the top of my head :

if (($unique1 <= $unique2) && ($unique1 <= unique3)) {
   # unique1 is the lowest
} else if (($unique2 <= $unique1) && ($unique2 <= unique3)) {
   # unique2 is the lowest
} else if (($unique3 <= $unique1) && ($unique3 <= unique2)) {
   # unique3 is the lowest
} else {
   # You should never reach this point
}

--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#9 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 06 March 2006 - 06:50 PM

[!--quoteo(post=352088:date=Mar 6 2006, 10:39 AM:name=XenoPhage)--][div class=\'quotetop\']QUOTE(XenoPhage @ Mar 6 2006, 10:39 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hrm.. Just off the top of my head :

if (($unique1 <= $unique2) && ($unique1 <= unique3)) {
   # unique1 is the lowest
} else if (($unique2 <= $unique1) && ($unique2 <= unique3)) {
   # unique2 is the lowest
} else if (($unique3 <= $unique1) && ($unique3 <= unique2)) {
   # unique3 is the lowest
} else {
   # You should never reach this point
}
[/quote]

Which would this code use if all values were the same?

#10 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 06 March 2006 - 09:04 PM

[!--quoteo(post=352153:date=Mar 6 2006, 01:50 PM:name=ShibSta)--][div class=\'quotetop\']QUOTE(ShibSta @ Mar 6 2006, 01:50 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Which would this code use if all values were the same?
[/quote]

Simulate the values and try it out.. :) It should run through the first if() statement if all 3 values are equal. But, it shouldn't matter unless you're looking to make sure one person gets the hit before everyone else.. *grin*

So, as an example, the hits will work out like this :

0 0 0
1 0 0
1 1 0
1 1 1
2 1 1
2 2 1
2 2 2
etc.
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#11 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 06 March 2006 - 10:19 PM

[!--quoteo(post=352220:date=Mar 6 2006, 04:04 PM:name=XenoPhage)--][div class=\'quotetop\']QUOTE(XenoPhage @ Mar 6 2006, 04:04 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Simulate the values and try it out.. :) It should run through the first if() statement if all 3 values are equal. But, it shouldn't matter unless you're looking to make sure one person gets the hit before everyone else.. *grin*

So, as an example, the hits will work out like this :

0 0 0
1 0 0
1 1 0
1 1 1
2 1 1
2 2 1
2 2 2
etc.
[/quote]

I just wanted to know because if it's random stats are goin' to jump around. Which is not fair.
I want it turn by turn as you pointed out it is.
I will go try this right now.
Thankx Alot!


#12 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 07 March 2006 - 08:30 PM

Erm, I put an error message after the last else() and the error is being displayed when all values are equal to 1

#13 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 07 March 2006 - 09:38 PM

[!--quoteo(post=352606:date=Mar 7 2006, 03:30 PM:name=ShibSta)--][div class=\'quotetop\']QUOTE(ShibSta @ Mar 7 2006, 03:30 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Erm, I put an error message after the last else() and the error is being displayed when all values are equal to 1
[/quote]

Hrm.. can you post your code please? You should never hit that else ....
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#14 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 07 March 2006 - 10:30 PM

<?php ob_start() ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<?php
$host = 'localhost';
$username = '----';
$password = '----';
$db_name = '----';
$sql_connect = mysql_connect("$host", "$username", "$password") or die("MySQL Connection Failed: " . mysql_error());;
$db_connect = mysql_select_db("$db_name")or die("Could not select DB: " . mysql_error());;

$count1 = mysql_query("SELECT * FROM link1") or die(mysql_error());
$unique1 = mysql_num_rows($count1);
$count2 = mysql_query("SELECT * FROM link2") or die(mysql_error());
$unique2 = mysql_num_rows($count2);
$count3 = mysql_query("SELECT * FROM link3") or die(mysql_error());
$unique3 = mysql_num_rows($count3);

if (($unique1 <= $unique2) && ($unique1 <= unique3)) {
    include("link1.php");
    header("location:----Link Removed----");
} else if (($unique2 <= $unique1) && ($unique2 <= unique3)) {
    include("link2.php");
    header("location:----Link Removed----");
} else if (($unique3 <= $unique1) && ($unique3 <= unique2)) {
    include("link3.php");
    header("location:----Link Removed----");
} else {
   echo "An Error Has Occured.";
}
?>
</body>
</html>


#15 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 08 March 2006 - 01:49 PM

Hrm.. I have no idea.. I used the below code as a test and it works fine...

<?php

$a = 0;
$b = 0;
$c = 0;

while ($a < 3) {
   if (($a <= $b) && ($a <= $c)) {
      print "a is lowest - ($a $b $c)\n";
      $a++;
   } else if (($b <= $a) && ($b <= $c)) {
      print "b is lowest - ($a $b $c)\n";
      $b++;
   } else if (($c <= $a) && ($c <= $b)) {
      print "c is lowest - ($a $b $c)\n";
      $c++;
   } else {
      print "ERROR! - ($a $b $c)\n";
      exit;
   }
}

?>

As an aside, if you switch to COUNT(*) instead of relying on mysql_num_rows, your code will execute *much* faster as the database grows...
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#16 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 08 March 2006 - 07:25 PM

Would you mind using the count(*) in my script and then posting it as I have no idea how to use it. I learn from reading PHP not the manual... (That thing just confuses me)

#17 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 08 March 2006 - 09:15 PM

[!--quoteo(post=352940:date=Mar 8 2006, 02:25 PM:name=ShibSta)--][div class=\'quotetop\']QUOTE(ShibSta @ Mar 8 2006, 02:25 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Would you mind using the count(*) in my script and then posting it as I have no idea how to use it. I learn from reading PHP not the manual... (That thing just confuses me)
[/quote]

Sure.. Observe :

   1.  <?php ob_start() ?>
   2.  <html xmlns="http://www.w3.org/1999/xhtml">
   3.  <head>
   4.  <title>Title</title>
   5.  </head>
   6.  <body>
   7.  <?php
   8.  $host = 'localhost';
   9.  $username = '---';
  10.  $password = '---';
  11.  $db_name = '---';
  12.  $sql_connect = mysql_connect("$host", "$username", "$password") or die("MySQL Connection Failed: " . mysql_error());;
  13.  $db_connect = mysql_select_db("$db_name")or die("Could not select DB: " . mysql_error());;
  14.  
  15.  list($unique1) = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM s") or die(mysql_error()));
  17.  list($unique2) = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM s1") or die(mysql_error()));
  19.  list($unique3) = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM g") or die(mysql_error()));
  21.  
  22.  
  23.  if () {
  24.  include("updatedb1.php");
  25.  header("location:--Link Removed--");
  26.  //echo "test1";
  27.  }
  28.  if () {
  29.  include("updatedb2.php");
  30.  header("location:--Link Removed--");
  31.  //echo "test2";
  32.  }
  33.  if () {
  34.  include("updatedb3.php");
  35.  header("location:--Link Removed--");
  36.  //echo "test3";
  37.  }
  38.  
  39.  ?>
  40.  </body>
  41.  </html>

--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users