Jump to content

Very Simple for all but me!


unistake

Recommended Posts

OK! this is what I have so far!...

It is meant to be a sort of referral tracking program! The users have a unique 'code' .

All the people they have referred will have the same value as 'code' typed in a column called 'friend'

Therefore this program is meant to show a counter of the amount of people referred! - But it is not working!

Anyone got any ideas?! Thanks

 
<?php
  $user = $_COOKIE["user"];
  
include("db.php");

$user = $_COOKIE["user"];
  $cxn = mysqli_connect($host,$username,$password,$database)
         or die ("couldn't connect to server");
  $query = "SELECT code FROM Member WHERE user='$user'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
$nrows = mysqli_num_rows($result);

while($row = mysqli_fetch_assoc($result)) {
extract($row);
}

if ($code !=0) {

$query = "SELECT * FROM Member WHERE friend='$code'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
$nrows = mysqli_num_rows($result);
$referred = $nrows-1;
echo "$referred";
}
else { 
echo "0";
}
?>

ok tried that but the echos are exactly like the line below

SELECT code FROM Member WHERE user='testing-user'

 

also the second does not show because of the IF statement. It just skips it on to the ELSE statement and echos "0".

 

There is definately a value in the right mysql fields.

HINT for the future:

 

If you want help debugging and someone asks you insert some debug code to see what the output is, post the output, otherwise they are just working blind.

 

I'm off to spend my time elsewhere. Good luck.

You have a problem with your loop, as your code now stands, the while loop will go through all the record and then do the "if". The "if" will only act on the last record retrieved, not all of them. Also, don't use extract, but use the array, $row, itself. Try this:

<?php
  $user = $_COOKIE["user"];
  
include("db.php");

$user = $_COOKIE["user"];
  $cxn = mysqli_connect($host,$username,$password,$database)
         or die ("couldn't connect to server");
  $query = "SELECT code FROM Member WHERE user='$user'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
$nrows = mysqli_num_rows($result);

while($row = mysqli_fetch_assoc($result)) {

       if ($row['code'] !=0) {

          $query = "SELECT * FROM Member WHERE friend='" . $row['code'] . "'";
          $result = mysqli_query($cxn,$query)
                  or die ("Couldn't execute query.");
          $nrows = mysqli_num_rows($result);
          $referred = $nrows-1;
          echo $referred . '<br>';
    } else  
         echo "0<br>";
}
?>

 

Ken

You are using the same variable "$result" for the two different mysqli_query() calls, you need to use different variables:

<?php
  $user = $_COOKIE["user"];
  
include("db.php");

$user = $_COOKIE["user"];
  $cxn = mysqli_connect($host,$username,$password,$database)
         or die ("couldn't connect to server");
  $query = "SELECT code FROM Member WHERE user='$user'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
$nrows = mysqli_num_rows($result);

while($row = mysqli_fetch_assoc($result)) {

       if ($row['code'] !=0) {

          $query = "SELECT * FROM Member WHERE friend='" . $row['code'] . "'";
          $rs = mysqli_query($cxn,$query)
                  or die ("Couldn't execute query.");
          $nrs = mysqli_num_rows($rs);
          $referred = $nrs-1;
          echo $referred . '<br>';
    } else  
         echo "0<br>";
}
?>[/code[

Ken

Still echoing "0"!

Is there any other way that I can do this sort of referral thing?

All I want to do is to allow my users to make a unique referral code. So that their friends can type it in when registering.

Then the user can track his/her friends.

 

Any suggestions would be appreciated.

That is what i have been doing.... this is everything.

 

<?php
  
include("db.php");

$user = $_COOKIE["user"];
  $cxn = mysqli_connect($host,$username,$password,$database)
         or die ("couldn't connect to server");
  $query = "SELECT code FROM Member WHERE user='$user'";
  $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
$nrows = mysqli_num_rows($result);

while($row = mysqli_fetch_assoc($result)) {

       if ($row['code'] !=0) {

          $query = "SELECT * FROM Member WHERE friend='" . $row['code'] . "'";
          $rs = mysqli_query($cxn,$query)
                  or die ("Couldn't execute query.");
          $nrs = mysqli_num_rows($rs);
          $referred = $nrs-1;
          echo $referred;
    } else  
         echo "0";
}
?>

 

Database: Member

userID    user          friend            code

28        123name                      testcode

31        user123    testcode

 

Everything i have given you is "as is".

I see you have shown us the $code values at last.

 

The numeric value of 'testcode' is 0

 

<?php

$code = 'test';
if ($code==0)
{
    echo 'code is zero';   // this executes
}


?>

 

Change your code to

 

if ($row['code'] != '')

Archived

This topic is now archived and is closed to further replies.

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