Joshxtothe4
Joshxtothe4

Reputation: 4193

looping array problem

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\n". 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()));
    }
}

Upvotes: 0

Views: 160

Answers (5)

Sebastian Hoitz
Sebastian Hoitz

Reputation: 9373

But I just want to add that while using variables like $$COLUMN seems to be a nice feature, it later can get really messy and produces a lot of extra variables.

Why don't you just create a temporary array, holding all YES/NO pairs?

In addition, PHP does not allow variables to contain numbers. I don't know what will happen when you create a variable like this:

$name = "123variable";
$$name = "foo";

I will have to check that out.

Edit: I just saw that you bind the query results to some variables only. Still I think this is not a good coding style.

Upvotes: 1

Jack Sleight
Jack Sleight

Reputation: 17118

Change the loop to:

foreach($columns as $column) {
    $$column = $$column ? 'YES' : 'NO';
}

Upvotes: 0

Alnitak
Alnitak

Reputation: 339816

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.

Upvotes: 1

TJ L
TJ L

Reputation: 24452

Change your loop from

foreach($columns as $column) {
     $$column = $columns[$column] ? 'YES' : 'NO';
}

To:

foreach($columns as $key=>$column) {
     $$column = $columns[$key] ? 'YES' : 'NO';
}

Upvotes: 0

Greg
Greg

Reputation: 321618

$columns[$column] doesn't exist - I can't imagine what you're trying to do, but that's a mistake.

Upvotes: 0

Related Questions