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
https://forums.phpfreaks.com/topic/217214-problem-with-repeating-echos/
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.

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.

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.

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.