Jump to content

Problem with repeating echos


plznty

Recommended Posts

I'm trying to make it so that if the $row data is more than just "" (empty) then echo if not then run the scan on the next number.

It just keeps repeating the values all the way through?

Help?

Thanks.

function scan($random) {
    if ($random == 1) {
        $query = "SELECT * FROM users WHERE clicks > 20 AND status='1' ORDER BY RAND() LIMIT 1";
        $result = mysql_query($query);
        $row = mysql_fetch_assoc($result);
        if (isset($row['id'])) {
                        echo '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>';
        } else {
            scan(2);
        }
    }
    if ($random == 2) {
        $query = "SELECT * FROM users WHERE clicks > 10 AND status='1' ORDER BY RAND() LIMIT 1";
        $result = mysql_query($query);
        $row = mysql_fetch_assoc($result);
        if (isset($row['id'])) {
                        echo '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>';
        } else {
            scan(3);
        }
    }
    if ($random == 3 OR 4 OR 5) {
        $query = "SELECT * FROM users WHERE clicks > 0 AND status='1' ORDER BY RAND() LIMIT 1";
        $result = mysql_query($query);
        $row = mysql_fetch_assoc($result);
        echo '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>';
    }
}

$random = rand(1, 5);
scan($random);

Link to comment
Share on other sites

I'm not sure what this function is meant to do, but personally I'd have it return the HTML if it's successful, or increment the value of $random and move to the next IF condition if not:

 

function scan($random) {
    if ($random == 1) {
        $query = "SELECT * FROM users WHERE clicks > 20 AND status='1' ORDER BY RAND() LIMIT 1";
        $result = mysql_query($query);
        $row = mysql_fetch_assoc($result);
        if (isset($row['id'])) {
            return '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>';
        }

        $random++;
    }
    if ($random == 2) {
        $query = "SELECT * FROM users WHERE clicks > 10 AND status='1' ORDER BY RAND() LIMIT 1";
        $result = mysql_query($query);
        $row = mysql_fetch_assoc($result);
        if (isset($row['id'])) {
            return '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>';
        }

        $random++;
    }
    if (in_array($random, array(3,4,5)) {
        $query = "SELECT * FROM users WHERE clicks > 0 AND status='1' ORDER BY RAND() LIMIT 1";
        $result = mysql_query($query);
        $row = mysql_fetch_assoc($result);
        return '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>';
    }
}

 

Then to call it:

 

echo scan($random);

 

Also you can't have an expression like: $random == 3 OR 4 OR 5 .. The best alternative here is to use in_array like I have done in the code above.

 

Although I'm sure this function could be improved a lot more if I understood what it was meant to do.

Link to comment
Share on other sites

Did you use !empty() or empty()? Problem being with your code is that the problem could lie in a number of places. Have you debugged each query result to ensure they're what you expect? e.g..

 

    if ($random == 1) {
        $query = "SELECT * FROM users WHERE clicks > 20 AND status='1' ORDER BY RAND() LIMIT 1";
        $result = mysql_query($query);
        $row = mysql_fetch_assoc($result);
        // DEBUGGING
        var_dump($row);
        // --------
        if (isset($row['id'])) {
            return '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>';
        }

        $random++;
    }

 

Have you added echo statements within each IF block to flag which are actually being evaluated as true?

 

function scan($random) {
    if ($random == 1) {
        echo 'random = 1'; // DEBUG
        $query = "SELECT * FROM users WHERE clicks > 20 AND status='1' ORDER BY RAND() LIMIT 1";
        $result = mysql_query($query);
        $row = mysql_fetch_assoc($result);
        if (isset($row['id'])) {
            return '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>';
        }

        $random++;
    }
    if ($random == 2) {
        echo 'random = 2'; // DEBUG
        $query = "SELECT * FROM users WHERE clicks > 10 AND status='1' ORDER BY RAND() LIMIT 1";
        $result = mysql_query($query);
        $row = mysql_fetch_assoc($result);
        if (isset($row['id'])) {
            return '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>';
        }

        $random++;
    }
    if (in_array($random, array(3,4,5)) {
        echo 'random = 3 or 4 or 5'; // DEBUG
        $query = "SELECT * FROM users WHERE clicks > 0 AND status='1' ORDER BY RAND() LIMIT 1";
        $result = mysql_query($query);
        $row = mysql_fetch_assoc($result);
        return '<html><body style="margin: 0px;"><a href="http://www.ryansocratous.com/banner/DELETE/click.php?u=' . $row['activation'] . '"><img src="http://www.ryansocratous.com/banner/DELETE/image.php?id=' . $row['id'] . '"></img></a></body></html>';
    }
}

 

Debugging is really an essential skill you'll need to learn, but try those for now and see if they highlight the problem.

Link to comment
Share on other sites

Its outputting the values apart from

random = 1random = 2 when $random = 1;

It needs to be able to skip $random = 1 if there is no result given.

What a headache, ty for debugging etc.

Must be to do with what values its getting on the $row[blah]

Link to comment
Share on other sites

basically if it does get a value from either the $random == 1 or the $random == 2 it will just add another 1 or two on

 

$random == 1

true then

$random == 2

true then

$random == 3

 

When I want it to be if $random == 1 do that and then stop

if the $random == 1 and then a row is unset then go to $random == 2.

Hope you can try help me again.

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.