Forum Moderators: coopster
I'm trying to write a function, currently, that looks to see if things are set, and if they are, they will display. If not, they'll just be called in as a blank spot. It's basically assembling an image tag, with a possible link wrapped around it.
So what I have is an if(isset) thing going on, so if the variable does have a value, it'll echo out the appropriate stuff. i.e. if "image" is set, then it'll display the <img src=' . $variable . '" />. if"alt" is set, then it'll plug in the alt, as well as the width and the height.
At least...that's what it's *supposed* to do.
What's happening is, when I have the "alt","width" and "height" nested inside the "image" section (because who really wants the alt tags and stuff to show up if there's no image to stick it in, right?) But when I place the "alt", "width", and "height" stuff *inside* the image, they will not echo out *anything*. I've tried to keep them completely separate, but their values will not pass. meaning, I keep all the "isset" stuff separated (and they echo out everything just perfectly), but when I try to assemble them to place the "alt", "width", and "height" stuff inside the image tag, I get a "not defined" error.
it's seriously perplexing me. If someone could take a look and tell me what I'm doing wrong, I'd appreciate it. Thank you!
function links_list() {
global $prefix, $alt, $width, $image, $link, $height;
$defines = returnConstants($prefix);
$list = array_keys($defines);
foreach($list as $key=>$value) {
$array = explode('_',$value);
$position = end($array);
if(strpos($value,'MANUFACTURER') != '') {
$p = 1;
while($p <= $position) {
if($p == $position) {
if(strpos($value,'ALT') != '') {
$a = $alt . '_' . $p;
if(!isset($a)) {
$alttext = '';
} else {
$alttext = ' alt="' . constant($a) . '"';
}
echo $alttext;
}
if(strpos($value,'WIDTH') != '') {
$w = $width . '_' . $p;
if(!isset($w)) {
$imgwidth = '';
} else {
$imgwidth = ' width="' . constant($w) . '"';
}
echo $imgwidth;
}
if(strpos($value,'HEIGHT') != '') {
$h = $height . '_' . $p;
if(!isset($h)) {
$imgheight = '';
} else {
$imgheight = ' height="' . constant($h) . '"';
}
echo $imgheight;
}
if(strpos($value,'IMAGE') != '') {
$i = $image . '_' . $p;
if(!isset($i)) {
$img = '';
} else {
$img = '<img src="images/' . constant($i) . '" />';
}
echo $img;
}
if(strpos($value,'LINK') != '') {
$l = $link . '_' . $p;
if(!isset($l)) {
$href = '';
} else {
$href = '<a href="' . constant($l) . '" />link here</a>';
}
echo $href;
}
}
$p++;
}
}
}
}
I'm not sure where you're getting this?
$defines = returnConstants($prefix); <-- this is a call to another function which pulls in all of my constants into an array $list = array_keys($defines); <-- this, of course, pulls all the items in the array by key and value (so I get a list of all my constants - defined or empty) foreach($list as $key=>$value) {
$array = explode('_',$value);
$position = end($array); <-- And that last is to split apart the constants I have in my files at the "_" section. if(strpos($value,'MANUFACTURER') != '') {<-- this simply says "if you find "MANUFACTURER" in that exploded list then continue on to do searches for *only* the stuff that has "MANUFACTURER" in the constant name. So I'm not quite sure where you see I'm actually setting the value for the constant?
>>The not defined error is likely coming from a constant that has not been defined. <<
No - what I mean is this:
if(strpos($value,'WIDTH') != '') {
$w = $width . '_' . $p;
if(!isset($w)) {
$imgwidth = '';
} else {
$imgwidth = ' width="' . constant($w) . '"';
}
echo $imgwidth; <---this echoes out the right value }
if(strpos($value,'WIDTH') != '') {
$w = $width . '_' . $p;
if(!isset($w)) {
$imgwidth = '';
} else {
$imgwidth = ' width="' . constant($w) . '"';
echo $imgwidth; <--- so does this }
}
if(strpos($value,'WIDTH') != '') {
$w = $width . '_' . $p;
if(!isset($w)) {
$imgwidth = '';
} else {
$imgwidth = ' width="' . constant($w) . '"';
}
}
echo $imgwidth; <-- this echoes out nothing. Alternatively, if I try this:
if(strpos($value,'IMAGE') != '') {
$i = $image . '_' . $p;
if(!isset($i)) {
$img = '';
} else {
$img = '<img src="images/' . constant($i)'";
insert the width, alt and height if statements here
$img .= $alt . $height . $width . ' />';
}
echo $img;
} then $img echoes out *only* the image stuff. The alt, width and height stuff are ignored.
Since the first two examples echo out the proper stuff, I *know* it's not an undefined constant (which is why the checks are in place for that anyway). It has something to do with how/where the variables are placed to echo stuff out.
Does that make more sense?
>>They are always set, from what I can see. You are defining (setting) the variable right before you check to see if it is set! <<I'm not sure where you're getting this?
Right here ...
if(strpos($value,'WIDTH') != '') {
$w = $width . '_' . $p; // <-- Here, to be exact
if(!isset($w)) {
$wis set. Of course it is set, you just set (defined) that variable the line above it.
I think you want to use defined [php.net] instead in this particular case.