douwei1950 2009-02-05 16:18 采纳率: 0%
浏览 35
已采纳

循环数组问题

I have a simplified ajay script, from which I have removed all nonrelevant code. The problem I am having is first with my columns array and the subsequent foreach loop. I want to go through each element and change the corresponding element to YES if true and NO if false, I don't see why it isn't working.

If there are any problems such as syntax errors or braces or such, they are a problem from simplifying my code, and are not present in the version on my machine.

<?php
$con = mysqli_connect("localhost", "", "", "");
if (!$con) {
    echo "Can't connect to MySQL Server. Errorcode: %s
". mysqli_connect_error();
    exit;
}
$con->set_charset("utf8");
    $query1 = 'SELECT EGGS, SALAD, TREES, REVISED FROM AUCTIONS WHERE ARTICLE_NO = ?';
    if ($getRecords = $con->prepare($query1)) {
        $getRecords->bind_param("s", $pk);
        $getRecords->execute();
        $getRecords->bind_result($EGGS, $SALAD, $TREES, $REVISED);
        while ($getRecords->fetch()) {
        $columns = array('EGGS', 'SALAD', 'TREES', 'REVISED');
            foreach($columns as $column) {
                $$column = $columns[$column] ? 'YES' : 'NO';
            }
            imageSize = imageResize($PIC_URL, 250, 300);
            echo "<h1>".$EGGS."</h1>";
        }
    }
function imageResize($imageURL, $maxWidth, $maxHeight) {
    $imageSize["width"] = 0;
    $imageSize["height"] = 0;
    $size = getimagesize($imageURL);
    if ($size) {
        $imageWidth  = $size[0];
        $imageHeight = $size[1];
        $wRatio = $imageWidth / $maxWidth;
        $hRatio = $imageHeight / $maxHeight;
        $maxRatio = max($wRatio, $hRatio);
        if ($maxRatio > 1) {
            $imageSize["width"] = $imageWidth / $maxRatio;
            $imageSize["height"] = $imageHeight / $maxRatio;
            return $imageSize;
        } else {
            $imageSize["width"] = $imageWidth;
            $imageSize["height"] = $imageHeight;
            return $imageSize;
        }
    } else {
        die(print_r(error_get_last()));
    }
}
  • 写回答

5条回答 默认 最新

  • duanpi7578 2009-02-05 16:31
    关注

    Your loop is wrong - $columns['EGGS'] doesn't exist:

    $columns = array('EGGS', 'SALAD', 'TREES', 'REVISED');
    foreach($columns as $column) {
        $$column = $columns[$column] ? 'YES' : 'NO';
    }
    

    it should be:

    $columns = array('EGGS', 'SALAD', 'TREES', 'REVISED');
    foreach($columns as $column) {
        $$column = $$column ? 'YES' : 'NO';
    }
    

    or better still:

    $tmp = array();
    $columns = array('EGGS', 'SALAD', 'TREES', 'REVISED');
    foreach($columns as $column) {
        $tmp[$column] = $$column ? 'YES' : 'NO';
    }
    

    so that you're not over-writing your bound variables.

    also note that you should move that constant array declaration outside of your while() loop for performance reasons.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。