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
https://forums.phpfreaks.com/topic/239619-can-this-be-a-variable/
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; ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.