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()));
}
}
-
$columns[$column]
doesn't exist - I can't imagine what you're trying to do, but that's a mistake.Joshxtothe4 : Why? For each $columns as $column would mean it does exist? -
Change your loop from
foreach($columns as $column) { $$column = $columns[$column] ? 'YES' : 'NO'; }
To:
foreach($columns as $key=>$column) { $$column = $columns[$key] ? 'YES' : 'NO'; }
Alnitak : that's still incorrect - $columns is an array of constant strings, and _not_ the bound results from the query -
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.Joshxtothe4 : Hmm, this makes no difference. I want to get the result of each bound result and evaluate it, and then assign a corresponding element based on its contents to YES or NO.Alnitak : if I understand you correctly, that's what my third code block doesJoshxtothe4 : Not exactly, I want to be able to echo out say $column[EGGS] and have it display yes or no instead of 0 or 1.Alnitak : Yes, that's what it does (except it's called $tmp and not $column). I didn't use $column as the array name because that's the iteration variable in your foreach loop.Joshxtothe4 : Hmm, ok. Is this a normal or effieicent way to do this, to have a tmp array?Alnitak : not necessarily, but your apparent original intent was to overwrite $EGGS (as per second code block). Within the loop you could instead have done $var = $column . '_B'; $$var = $$column ? 'yes' : 'no', to create a new variable $EGGS_B based on the value of $EGGS.Joshxtothe4 : that is a bit beyond me at the moment. Thankyou for your help though. -
Change the loop to:
foreach($columns as $column) { $$column = $$column ? 'YES' : 'NO'; }
-
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.
Joshxtothe4 : why is it not a good coding style?Alnitak : because it can be hard to follow and can cause security issues when used in the wrong context. I've seen bad coders use this to create automatic variables based on CGI scripts - not realising that this allowed the user to create their own variables! It's the same problem as PHP's register_globals
0 comments:
Post a Comment