开发者

PHP/SQL script not working

开发者 https://www.devze.com 2023-02-11 00:33 出处:网络
I\'m trying to generate the top 10 tags, I think it\'s pretty straightforward. //generate 10 top tags $tagSQL = mysql_fetch_array(mysql_query(\"SELECT * FROM tags\"));

I'm trying to generate the top 10 tags, I think it's pretty straightforward.

    //generate 10 top tags
$tagSQL = mysql_fetch_array(mysql_query("SELECT * FROM tags"));
$topArray = array();
foreach($tagSQL as $poland)
{
    if($poland["tagID"] == 1)
    {
        $topArray[0] = $topArray[1] + 1;
    }
    if($poland["tagID"] == 2)
    {
        $topArray[1] = $topArray[2] + 1;
    }
    if($poland["tagID"] == 3)
    {
        $topArray[2] = $topArray[3] + 1;
    }
    if($poland["tagID"] == 4)
    {
        $topArray[3] = $topArray[4] + 1;
    }
}
function printTopTags()
{
    $n = 0;
    foreach($topArray as $buddha)
    {
        $n = $n + 1;
        if(sizeOf($topArray) > $n)
        {
            $hersheyBar = " ";
        }
        else
        {
            $hersheyBar = "";
        }
        $finalFinalEndArray = mysql_fetch_array(mysql_query("SELECT tagName FROM tags WHERE tagID开发者_运维技巧 = '$buddha'");
        foreach($finalFinalEndArray as $waterBottle)
        {
            echo $waterBottle . $hersheyBar;
        }
    }   
}

The only problem I'm having is that it thinks I have a syntax error somewhere in the code, but no matter which lines I omit the syntax error stays.

Also, is there an easier way to do my if statements for 10 different array spots, rather than the four?


You are missing a ")" at the end of this statement

It should be

$finalFinalEndArray = mysql_fetch_array(mysql_query("SELECT tagName FROM tags WHERE tagID = '$buddha'"));


optimystique is right about the syntax error.

Regarding your second question, shmeeps' method works for this example, but if you happen to have another situation where you need to compare one variable to a number of different values, then you can use a switch statement instead of a whole bunch of ifs. For example (From the above linked PHP manual page):

<?php
if ($i == 0) {
    echo "i equals 0";
} elseif ($i == 1) {
    echo "i equals 1";
} elseif ($i == 2) {
    echo "i equals 2";
}
?>

is the same as:

<?php
switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}
?>


The if-then statements should be able to be generalized as

    $topArray[($poland["tagID"] - 1)] = $topArray[$poland["tagID"]] + 1;

with no if-then statements at all.

I don't see any syntax errors. Is there more to this script?

Edit: Saw the error now, this line

$finalFinalEndArray = mysql_fetch_array(mysql_query("SELECT tagName FROM tags WHERE tagID = '$buddha'");

Should be

$finalFinalEndArray = mysql_fetch_array(mysql_query("SELECT tagName FROM tags WHERE tagID = '$buddha'"));

Note the extra )

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号