adamjones Posted February 22, 2009 Share Posted February 22, 2009 Hi. I have profile pages on my website for users, and want to incorporate a badge function, so on their profile page, people can see which badges they have, eg. 'Donator' etc.. In my database, it's simply the usernames, password, and in a new row, 'badges'. Here I would enter the badge names, all separated by commas, eg. 'donator,admin,vip' etc... And this is the code used on their profile page; <?php session_start(); require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $qry="SELECT * FROM members WHERE username='$_SESSION[user];'"; $result=mysql_query($qry); $getbadge = mysql_query($query) or die(mysql_error()); if($result) { while ($row = mysql_fetch_array($getbadge)) { $img = $row['badges']; echo "<img src='badges/".$img.".gif' alt='".$img."'>"; } else { echo "<p>".$error."</p><br>"; } } ?> So it will display the badge name '$img' with .gif at the end, for each user. But I get this error; "Parse error: syntax error, unexpected T_ELSE in /home/wowdream/public_html/habhub/profile.php on line 25" Quote Link to comment Share on other sites More sharing options...
tefuzz Posted February 22, 2009 Share Posted February 22, 2009 im new to PHP but it looks like you have a } in the wrong place. try this... <?php session_start(); require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $qry="SELECT * FROM members WHERE username='$_SESSION[user];'"; $result=mysql_query($qry); $getbadge = mysql_query($query) or die(mysql_error()); if($result) { while ($row = mysql_fetch_array($getbadge)) { $img = $row['badges']; echo "<img src='badges/".$img.".gif' alt='".$img."'>"; } } else { echo "<p>".$error."</p><br>"; } ?> Quote Link to comment Share on other sites More sharing options...
adamjones Posted February 22, 2009 Author Share Posted February 22, 2009 im new to PHP but it looks like you have a } in the wrong place. try this... <?php session_start(); require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $qry="SELECT * FROM members WHERE username='$_SESSION[user];'"; $result=mysql_query($qry); $getbadge = mysql_query($query) or die(mysql_error()); if($result) { while ($row = mysql_fetch_array($getbadge)) { $img = $row['badges']; echo "<img src='badges/".$img.".gif' alt='".$img."'>"; } } else { echo "<p>".$error."</p><br>"; } ?> Thanks, its working, but I get this error on the page now, where the badge should be; "Query was empty" Quote Link to comment Share on other sites More sharing options...
papaface Posted February 22, 2009 Share Posted February 22, 2009 You have an extra } at the bottom. Should be: <?php session_start(); require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $qry="SELECT * FROM members WHERE username='".$_SESSION['user']."'"; $result=mysql_query($qry); $getbadge = mysql_query($query) or die(mysql_error()); if($result) { while ($row = mysql_fetch_array($getbadge)) { $img = $row['badges']; echo "<img src='badges/".$img.".gif' alt='".$img."'>"; } else { echo "<p>".$error."</p><br>"; } ?> Quote Link to comment Share on other sites More sharing options...
tefuzz Posted February 22, 2009 Share Posted February 22, 2009 Thanks, its working, but I get this error on the page now, where the badge should be; "Query was empty" thats outta my league...haha. picking out the extra } was just something I noticed right away with the if and while statements. extra set of eyes always helps Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 22, 2009 Share Posted February 22, 2009 try this. <?php session_start(); require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $qry="SELECT * FROM members WHERE username='".$_SESSION['user']."'"; $result=mysql_query($qry)or die(mysql_error()); if (mysql_num_rows($result)) { while ($row = mysql_fetch_array($result)) { $img = $row['badges']; echo "<img src='badges/".$img.".gif' alt='".$img."'>"; } else { echo "<p>".$error."</p><br>"; } ?> Quote Link to comment Share on other sites More sharing options...
papaface Posted February 22, 2009 Share Posted February 22, 2009 Should be: <?php session_start(); require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $qry="SELECT * FROM members WHERE username='$_SESSION[user];'"; $result=mysql_query($qry); $getbadge = mysql_query($query) or die(mysql_error()); if($result) { while ($row = mysql_fetch_array($getbadge)) { $img = $row['badges']; echo "<img src='badges/".$img.".gif' alt='".$img."'>"; } }else { echo "<p>".$error."</p><br>"; } ?> I think. Quote Link to comment Share on other sites More sharing options...
adamjones Posted February 22, 2009 Author Share Posted February 22, 2009 Should be: <?php session_start(); require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $qry="SELECT * FROM members WHERE username='$_SESSION[user];'"; $result=mysql_query($qry); $getbadge = mysql_query($query) or die(mysql_error()); if($result) { while ($row = mysql_fetch_array($getbadge)) { $img = $row['badges']; echo "<img src='badges/".$img.".gif' alt='".$img."'>"; } }else { echo "<p>".$error."</p><br>"; } ?> I think. Thanks, but still get this error; "Query was empty" Quote Link to comment Share on other sites More sharing options...
papaface Posted February 22, 2009 Share Posted February 22, 2009 Try: <?php session_start(); require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $qry="SELECT * FROM `members` WHERE `username`='".$_SESSION['user']."'"; $result=mysql_query($qry); $getbadge = mysql_query($qry) or die(mysql_error()); if($result) { while ($row = mysql_fetch_array($getbadge)) { $img = $row['badges']; echo "<img src='badges/".$img.".gif' alt='".$img."'>"; } }else { echo "<p>".$error."</p><br>"; } ?> You were calling $Query instead of $qry. Try the above code. Quote Link to comment Share on other sites More sharing options...
adamjones Posted February 22, 2009 Author Share Posted February 22, 2009 Try: <?php session_start(); require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $qry="SELECT * FROM `members` WHERE `username`='".$_SESSION['user']."'"; $result=mysql_query($qry); $getbadge = mysql_query($qry) or die(mysql_error()); if($result) { while ($row = mysql_fetch_array($getbadge)) { $img = $row['badges']; echo "<img src='badges/".$img.".gif' alt='".$img."'>"; } }else { echo "<p>".$error."</p><br>"; } ?> You were calling $Query instead of $qry. Try the above code. Thank you SO much! You fixed it I'm just wondering.. If a user has more than one badge, eg 'donater,vip', etc.. stored in the database, how could I show this in my code, as it would come up with "badges/donate,vip.gif" - If that makes sense? Cheers. Quote Link to comment Share on other sites More sharing options...
papaface Posted February 22, 2009 Share Posted February 22, 2009 How is that stored in the database? As "donater,vip"? If so you could try: <?php session_start(); require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $qry="SELECT * FROM `members` WHERE `username`='".$_SESSION['user']."'"; $result=mysql_query($qry); $getbadge = mysql_query($qry) or die(mysql_error()); if($result) { while ($row = mysql_fetch_array($getbadge)) { $imgs = explode(",",$row['badges']); foreach ($imgs as $v) { echo "<img src='badges/".$v.".gif' alt='".$v."'>"; } } }else { echo "<p>".$error."</p><br>"; } ?> Quote Link to comment Share on other sites More sharing options...
adamjones Posted February 22, 2009 Author Share Posted February 22, 2009 How is that stored in the database? As "donater,vip"? If so you could try: <?php session_start(); require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $qry="SELECT * FROM `members` WHERE `username`='".$_SESSION['user']."'"; $result=mysql_query($qry); $getbadge = mysql_query($qry) or die(mysql_error()); if($result) { while ($row = mysql_fetch_array($getbadge)) { $imgs = explode(",",$row['badges']); foreach ($imgs as $v) { echo "<img src='badges/".$v.".gif' alt='".$v."'>"; } } }else { echo "<p>".$error."</p><br>"; } ?> ;o It works! Thank you so much for your help! Quote Link to comment 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.