White_Lily Posted September 28, 2012 Share Posted September 28, 2012 Sorry for all the problems im having im not entirly sure why of all them are occuring since the majority are things i have done in the past. Anyway - to the point of this new problem, i have created a user profile where the user can have a profile banner and an avatar, however the banner shows the default image even if there is an image in the database. here's the profile php code: <?php if(!$loggedUser && !$loggedPass){ echo '<div class="navigation">'; $nav = select("*", "pages", NULL, "pageType ASC"); $num = mysql_num_rows($nav); if($num != 0){ echo "<ul>"; while($get = mysql_fetch_assoc($nav)){ $filename = file_exists($_SERVER['DOCUMENT_ROOT'].$get["pageFileName"]); if($get["pageType"] <= 2 && $get["active"] == 1){ if($filename){ echo "<li><a href='".$GLOBALS["siteUrl"]."/".$get["pageFileName"]."'>".$get["pageName"]."</a></li>"; } } } echo "</ul>"; }else{ echo "No menu found."; } echo '</div>'; echo '<div class="content">'; echo '<h1>Welcome</h1>'; echo '<h3>Login</h3>'; include "inc/loginCheck.php"; echo '<form action="" method="POST">'; echo '<label>Username:</label><input type="text" name="username" />'; echo '<label>Password:</label><input type="password" name="password" />'; echo '<input type="submit" name="submit" value="Login" />'; echo '</form>'; echo '</div>'; }else{ echo '<div class="navigation">'; $nav = select("*", "pages", NULL, "pageType ASC"); $num = mysql_num_rows($nav); if($num != 0){ echo "<ul>"; while($get = mysql_fetch_assoc($nav)){ $filename = file_exists($_SERVER['DOCUMENT_ROOT'].$get["pageFileName"]); if($get["pageType"] > 0 && $get["active"] == 1){ if($filename){ $convertLink = str_replace("Home", "Profile", $get["pageName"]); echo "<li><a href='".$GLOBALS["siteUrl"]."/".$get["pageFileName"]."'>".$convertLink."</a></li>"; } }else{ echo "<li>No active navigation.</li>"; } } echo "<li><a href='logout.php'>Logout</a></li>"; echo "</ul>"; }else{ echo "No menu found."; } echo '</div>'; echo "<div class='banner'>"; echo "<div class='bannerImage'>"; $banner = getbanner("lightbox"); echo $banner; echo "</div>"; echo "</div>"; echo '<div class="content">'; echo "<div class='userDetails'>"; echo "<div class='avatarUser'>"; echo "</div>"; echo "<div class='accountDetails'>"; echo '<h1>'.$loggedUser."'s ".$page.'</h1>'; echo "</div>"; echo "</div>"; include "inc/profile.php"; echo '</div>'; } ?> and here is the funtion that the banner calls once the user has logged in: <?php //Banner Image(s) function getBanner($rel){ $query = select("*", "users", "username = '$loggedUser'"); $fetch = mysql_fetch_assoc($query); $bannerImg = $fetch["bannerImage"]; $link = "<a href='".$GLOBALS["siteUrl"]."/images/banners/".$bannerImg."' rel='".$rel."'>"; $Img = "<img src='".$GLOBALS["siteUrl"]."/images/banners/".$bannerImg."' />"; $close = "</a>"; $default = "<img src='".$GLOBALS["defaultImage"]."' />"; $acutalImg = $link.$Img.$close; if($actualImg){ return $actualImg; }else{ return $default; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/268894-profile-banner-image-problem/ Share on other sites More sharing options...
Psycho Posted September 28, 2012 Share Posted September 28, 2012 (edited) Ok, you make a call to the function getBanner() which is supposed to return either the user's banner or the default. And you're saying that the default is always returned right? So, here is that function: function getBanner($rel) { $query = select("*", "users", "username = '$loggedUser'"); $fetch = mysql_fetch_assoc($query); $bannerImg = $fetch["bannerImage"]; $link = "<a href='".$GLOBALS["siteUrl"]."/images/banners/".$bannerImg."' rel='".$rel."'>"; $Img = "<img src='".$GLOBALS["siteUrl"]."/images/banners/".$bannerImg."' />"; $close = "</a>"; $default = "<img src='".$GLOBALS["defaultImage"]."' />"; $acutalImg = $link.$Img.$close; if($actualImg) { return $actualImg; }else{ return $default; } } I see a few problems with that function. Even if you fixed the problem it would still not work as you want. Let's go through it line by line: $query = select("*", "users", "username = '$loggedUser'"); Looks like you have a custom function for running a SELECT query? Not sure why, but OK. 1) why are you selecting * when you only need the image? 2) $loggedUser has not been defined in the function so that value would always be empty. 3) Not sure why you are using the username instead of the userID which you should have in the table. $fetch = mysql_fetch_assoc($query); You should do a check to see if any record was even returned before you try and fetch the data. $bannerImg = $fetch["bannerImage"]; Yeah, you need to check if any record was returned first. $link = "<a href='".$GLOBALS["siteUrl"]."/images/banners/".$bannerImg."' rel='".$rel."'>"; $Img = "<img src='".$GLOBALS["siteUrl"]."/images/banners/".$bannerImg."' />"; $close = "</a>"; $default = "<img src='".$GLOBALS["defaultImage"]."' />"; $GLOBALS ?!?!? Really? That's very poor form. $acutalImg = $link.$Img.$close; Interesting name for that variable, don't you think? if($actualImg) { return $actualImg; }else{ return $default; } First of all, that condition is checking the wrong variable - you misspelled it when you set it previously. Second, that will ALWAYS return true since you just set the value before you did that check. You need to check if the returned value (if there was one) had a non-null value to determine whether to show the default or not. $actualImg will always have a value as you have coded it. I have rewritten the function below. You need to now pass the values for $loggedUser and $rel. Ideally I would also pass the values you are referencing using $GLOBAL, but I was lazy This should get you closer to what you want //Banner Image(s) function getBanner($loggedUser, $rel) { //Define return value as default $banner = "<img src='{$GLOBALS['defaultImage']}' />"; //Run query $query = select('bannerImage', 'users', "username='$loggedUser'"); //Determine if record was returned if($query != false && mysql_num_rows($query)>0) { $bannerImg = mysql_result($query, 0); //Determine if value was empty if(!empty($bannerImg)) { //Redefine return value $imgURL = "{$GLOBALS['siteUrl']}/images/banners/{$bannerImg}"; $banner = "<a href='{$imgURL}' rel='{$rel}'><img src='{$imgURL}' /></a>"; } } return $banner; } Edited September 28, 2012 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/268894-profile-banner-image-problem/#findComment-1381673 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.