Jump to content

Can this be a variable?


hoponhiggo

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.