hoponhiggo Posted June 17, 2011 Share Posted June 17, 2011 Hi Guys Im still getting familiar with PHP and was wondering if and how i could turn a long string of code into a shorter variable to be used elswhere in my script? The long string of code is used to display a 'profile picture' of a logged in user and is as follows: <?php //to display image from source $dir = "prof_pics"; $sql = "SELECT prof_pic FROM users WHERE username = '{$_SESSION['MM_Username']}'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0) die("Username not found in database."); $row = mysql_fetch_array($res); echo "<img src='$dir/{$row['prof_pic']}' width='88' height='88' align= center><br>"; ?> I would like this pic to be displayed in numerous places on a webpage, in particular in a 'contact card', the code for which is here... echo " <b>$user[username]'s Profile</b><br><br> Email: $user[email]<br> "; Thanks in advance Link to comment https://forums.phpfreaks.com/topic/239619-can-this-be-a-variable/ Share on other sites More sharing options...
revraz Posted June 17, 2011 Share Posted June 17, 2011 Any string can be a variable. Link to comment https://forums.phpfreaks.com/topic/239619-can-this-be-a-variable/#findComment-1230919 Share on other sites More sharing options...
dragon_sa Posted June 17, 2011 Share Posted June 17, 2011 so all you would so is <?php //to display image from source $dir = "prof_pics"; $sql = "SELECT prof_pic FROM users WHERE username = '{$_SESSION['MM_Username']}'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0) die("Username not found in database."); $row = mysql_fetch_array($res); $pic="$dir/".$row['prof_pic']; $img="<img src=\"$pic\" width=\"88\" height=\"88\" align=\"center\"><br>"; echo $img; ?> and anywhere on your page you want it <?php echo $img; ?> Link to comment https://forums.phpfreaks.com/topic/239619-can-this-be-a-variable/#findComment-1230920 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.