Jump to content
MakeWebGames

Adapt Viewuser for marriage mod?


Recommended Posts

Hey all,

I have a Viewuser.php with the following code loading the variables:

$q =
           $db->query(
                   "SELECT `userid`, `user_level`, `laston`, `last_login`,
                   `signedup`, `duties`, `donatordays`, `username`, `gender`,
                   `daysold`, `money`, `crystals`, `level`, `friend_count`,
                   `enemy_count`, `display_pic`, `hp`, `maxhp`, `gang`,
                   `fedjail`, `hospital`, `hospreason`, `jail`, `jail_reason`,
                   `bankmoney`, `cybermoney`, `lastip`, `lastip`,
                   `lastip_login`, `lastip_signup`, `staffnotes`, `cityname`,
                   `hNAME`, `gangNAME`, `fed_days`, `fed_reason`
                   FROM `users` `u`
                   INNER JOIN `cities` AS `c`
                   ON `u`.`location` = `c`.`cityid`
                   INNER JOIN `houses` AS `h`
                   ON `u`.`maxwill` = h.`hWILL`
                   LEFT JOIN `gangs` AS `g`
                   ON `g`.`gangID` = `u`.`gang`
                   LEFT JOIN `fedjail` AS `f`
                   ON `f`.`fed_userid` = `u`.`userid`
                   WHERE `u`.`userid` = {$_GET['u']}");

 

I want to be able to show who the person is married to (if any) in their viewuser profile. How would I change the mysql code above to load up who they're married to (the ID of who they're married to is in the users table under 'married') and display the username of that person further down in the code?

Or if there's another way to show who a user is married to in their viewuser profile, that would be fine too.

My viewuser.php (a free one I found on here):

<?php
$globals = DIRNAME(__FILE__) . '/globals.php';
if(file_exists($globals))
{
include_once($globals);
}
else
{
echo "globals file is missing"; exit; 
}



$_GET['u'] = isset($_GET['u']) ? abs(@intval(trim($_GET['u']))) : '0';

if(!$_GET['u']) 
{
   print "Invalid use of file";
$h->endpage();
exit;
} 
else
{
   $q=$db->query("SELECT `u`.`userid`,`u`.`sig`,`u`.`username`,`u`.`money`,`u`.`laston`,`u`.`signedup`, " .
             "`u`.`daysold`,`u`.`fedjail`,`u`.`maxhp`,`u`.`cred`,`u`.`friend_count`, " .
                         "`u`.`hospital`,`u`.`hospreason`,`u`.`jail`,`u`.`jail_reason`,`u`.`enemy_count`, " .
                         "`u`.`hp`,`u`.`level`,`u`.`gender`,`u`.`duties`,`u`.`bankmoney`,`u`.`mailban`, " .
                         "`u`.`user_level`,`u`.`donatordays`,`u`.`lastip`,`u`.`last_login`,`u`.`lastip_login`,`u`.`lastip_signup`, " .
                         "`u`.`gang`,`u`.`display_pic`,`u`.`staffnotes`,`us`.`strength`,`us`.`agility`, " .
                         "`us`.`guard`,`us`.`labour`,`us`.`IQ`,`c`.`cityname`,`h`.`hNAME`,`g`.`gangNAME`, " .
             "`f`.* FROM `users` `u` LEFT JOIN `userstats` `us` ON `u`.`userid`=`us`.`userid` " .
             "LEFT JOIN `cities` `c` ON `u`.`location`=`c`.`cityid` LEFT JOIN `houses` `h` " .
                         "ON `u`.`maxwill`=`h`.`hWILL` LEFT JOIN `gangs` `g` ON `g`.`gangID`=`u`.`gang` " .
             "LEFT JOIN `fedjail` `f` ON `f`.`fed_userid`=`u`.`userid` WHERE (`u`.`userid`=".$_GET['u'].")");
         if($db->num_rows($q) == 0)
         {
         echo "Sorry, we could not find a user with that ID, check your source.";
         $h->endpage();
         exit;
} 
else 
{

$r = $db->fetch_row($q);

if($r['user_level'] == 1) {
   $userl="Player";
} else if($r['userid'] == 206) {
   $userl="Owner";
} else if($r['userid'] == 3) {
   $userl="Owner";
} else if($r['user_level'] == 2) {
   $userl="Admin";
} else if($r['user_level'] == 0) {
   $userl="NPC";
}  else {$userl="Moderator";
}
if($r['fedjail']) {
   $ustat="<a href='fedjail'>Federal Jail</a>";
} else if($r['hospital']) {
   $ustat="<a href='hospital.php'>Hospital</a>";
} else if($r['jail']) {
   $ustat="<a href='jail.php'>Jail</a>";
} else {
   $ustat="-";
}

   $lon = ($r['laston'] > 0) ?date('F j',$r['laston']) : "Never";
   $sup = date('j F',$r['signedup']);
   $ts = $r['strength']+$r['agility']+$r['guard']+$r['labour']+$r['IQ'];
   $d = "";

if($r['laston'] > 0) {
   $la = time()-$r['laston'];
   $unit = "seconds";
if($la >= 60) {
   $la = (int) ($la/60);
   $unit = "minutes";
}
if($la >= 60) {
   $la = (int) ($la/60);
   $unit = "hours";
if($la >= 24) {
   $la = (int) ($la/24);
   $unit=  "days";
}
}
   $str = "$la $unit ago";
}
else
{
   $str="--";
}

if($r['last_login'] > 0)

{

$ll=time()-$r['last_login'];

$unit2="seconds";

if($ll >= 60)
{

$ll=(int) ($ll/60);

$unit2="minutes";

}

if($ll >= 60)

{

$ll=(int) ($ll/60);

$unit2="hours";

if($ll >= 24)

{

$ll=(int) ($ll/24);

$unit2="days";

}

}

$str2="$ll $unit2 ago";

}

else

{

 $str2="--";

}


if($r['user_level'] >1){ $r['username'] = "<font color=red>".stripslashes($r['username'])."</font>";}

elseif($r['donatordays']) { $r['username'] = "<font color=blue>".stripslashes($r['username'])."</font>";$d="<img src='donator.gif'/>"; }


if($r['laston'] >= time()-15*60) { $on="<font color=green><b>Online</b></font>"; } else { $on="<font color=red><b>Offline</b></font>"; }
print "
<div class='content'>
       <table width='60%'>


           <tr>
               <td colspan='4'>
                   <table width='100%'>
                       <tr>
                           <td width='100' align='center'>";
                           if($r['display_pic'])
       {
       print "<center><img src='".stripslashes(htmlentities($r['display_pic']))."' width='100' height='100' alt='User Display Pic' title='User Display Pic' /><br><br></center>";
       }
       else
       {
       print "<center><img src='http://i38.tinypic.com/29bh8d5.jpg' width='100' height='100' alt='No Display Pic' title='No Display Pic' /></center><br><br>";
       }
                       print "
       </td>
                           <td style='font-size:25px;' align='center'>
                               <span style='font-weight:bold;'>".stripslashes($r['username'])." [".number_format($r['userid'])."] $d</span><hr>
                           </td>
                       </tr>
                   </table>
               </td>
           </tr>
           <tr>
               <td width='18%'><h4>Name:</h4></td>
               <td width='32'><a href='viewuser.php?u=".number_format($r['userid'])."'>".stripslashes($r['username'])."</a></td>
               <td width='18%'><h4>Type:</h4></td>
               <td width='32%'> ".stripslashes(htmlentities($userl))."</td>
           </tr>
           <tr>
               <td><h4>Level:</h4></td>
               <td> ".number_format($r['level'])."</td>
               <td><h4>Cash:</h4></td>
               <td> \$".number_format($r['money'])."</td>
           </tr>
           <tr>
               <td><h4>Age:</h4></td>
               <td> ".number_format($r['daysold'])." Days</td>
               <td><h4>Last Active:</h4></td>
               <td> $str</td>
           </tr>
           <tr>
               <td><h4>Life:</h4></td>
               <td> ".number_format($r['hp'])."/".number_format($r['maxhp'])."</td>
               <td><h4>Posse:</h4></td>
               <td> ";
   if($r['gang'])
   {
   print "<a href='gangs.php?action=view&ID=".number_format($r['gang'])."'>".stripslashes(htmlentities($r['gangNAME']))."</a>";
   }
   else
   {
   print "None</td>";
   }
         print "
           </tr>
           <tr>
               <td><h4>Location:</h4></td>
               <td> ".stripslashes(htmlentities($r['cityname']))."</td>
               <td><h4>House:</h4></td>
               <td> <a href='estate.php'>".stripslashes(htmlentities($r['hNAME']))."</a></td>
           </tr>
   <tr>
       <td><h4>Gender:</h4></td>
       <td> {$r['gender']}</td>
   </tr>
       </table>
       <h3>Actions</h3>
       <hr>
           <table width='90%'>
               <tr>
                   <td width='30%' align='center'><a href='mailbox.php?action=compose&ID=".number_format($r['userid'])."'><img src='sendmail.jpg'></a></td>
                   <td width='30%' align='center'><a href='attack.php?ID=".number_format($r['userid'])."'><img src='profileattack.jpg'></a></td>
                   <td width='30%' align='center'><a href='mugging.php?ID=".number_format($r['userid'])."'><img src='mugbutton.jpg'></a></td>
                   <td width='30%' align='center'><a href='assassin.php?ID=".number_format($r['userid'])."'><img src='assbutton.jpg'></a></td>
                  <td width='30%' align='center'><a href='sendbomb.php?action=bomb&ID=".number_format($r['userid'])."'><img src='sendbombbutton.jpg'></a></td>
               </tr>
               <tr>
                   <td width='30%' align='center'><a href='sendcash.php?ID=".number_format($r['userid'])."'><img src='sendcash.jpg'></a></td>
                   <td width='30%' align='center'><a href='sendcrys.php?ID=".number_format($r['userid'])."'><img src='sendcred.jpg'></a></td>
                   <td width='30%' align='center'><a href='contactlist.php?action=add&ID=".number_format($r['userid'])."'><img src='sendcontact.jpg'></a></td>
                   <td width='30%' align='center'><a href='friendslist.php?action=add&ID=".number_format($r['userid'])."'><img src='addfriend.jpg'></a></td>
                  <td width='30%' align='center'><a href='blacklist.php?action=add&ID=".number_format($r['userid'])."'><img src='addenemy.jpg'></a></td>
               </tr>
           </table><hr>";

       if($r['sig'])
{
$sig = "{$r['sig']}";
}
else
{
$sig = 'This user has No Signature.';
}
$sig = $bbc->Parse($sig); 

print "

  <table width=600px cellspacing=0>
  <tr class='table'>
  <th>Profile Signature</th>
  </tr>
  <tr bgcolor='#dfdfdf'>
  <td>
<center><br />$sig<br /><br /></center>
  </td>
  </tr>
  </table><br /><br ><br /><br >";

  if($ir['user_level'] > 1)
   {   

if($r['trials'] > 0)
{
print "</br><b><font color=red>Has been put on trial {$r['trials']} time(s).</font></b>";
}
print "	<br /><br ></div>";
}
}
}
$h->endpage();
?>
Edited by realmoflegends
Link to comment
Share on other sites

Put this function in your global_func.php file so you can use it elsewhere.

 

/**
* getUserSpouse
* Fetches a users spouse if they have one. If they don't, return "Not married".
*
*@param int  $intUserid      The userid you want their spouse
*@param bool $blReturnLink   If TRUE then return link to user profile, else just return their name
*/
function getUserSpouse($intUserid = $_SESSION['userid'], $blReturnLink = TRUE) {
  global $db; //Ugh, really hate to global, but....
  $strDbQuery = "SELECT `username`, `userid` 
                 FROM `users`
                 WHERE `userid` IN(SELECT `married` 
                                   FROM `users` 
                                   WHERE `userid` = ". $intUserid .")";
  $objGet = $db->query($strDbQuery);
  if($db->num_rows($objGet ) == 0) {
      return "Not married"; 
  }

  $arrResult = $db->fetch_array($objGet);
  return $blReturnLink ? '<a href=\'viewuser.php?u='. $arrResult['userid'] .'\'>'. $arrResult['username'] .'</a>' : $arrResult['username'];
}

 

So, on your viewuser.php code, you can call it by;

echo '<strong>Spouse:</strong> '. getUserSpouse($_GET['u']);

 

Tip: It should be noted that using number_format() on your gang id and userid on your links will cause problems when you have id's over 1,000. https://eval.in/379222 (assuming you use the overly used abs( (int) $var ).

 

Note: I'm assuming you've done the validation on $_GET['u'] to ensure it's an integer.

Edited by sniko
Link to comment
Share on other sites

Perfect! Great work, sniko, works flawlessly in my game (after a bit of tinkering because my code is so cluttered at this point). I think you're the coding Jedi of this board now. :)

No problem. Glad I could be of assistance.

- Jedi, out!

Link to comment
Share on other sites

Weird. Here's the code section where I added the new code you gave me (new code is at the bottom):

 <tr>
               <td width='18%'><h4>Name:</h4></td>
               <td width='32'><a href='viewuser.php?u=".number_format($r['userid'])."'>".stripslashes($r['username'])."</a></td>
               <td width='18%'><h4>Type:</h4></td>
               <td width='32%'> ".stripslashes(htmlentities($userl))."</td>
           </tr>
           <tr>
               <td><h4>Level:</h4></td>
               <td> ".number_format($r['level'])."</td>
               <td><h4>Cash:</h4></td>
               <td> \$".number_format($r['money'])."</td>
           </tr>
           <tr>
               <td><h4>Age:</h4></td>
               <td> ".number_format($r['daysold'])." Days</td>
               <td><h4>Last Active:</h4></td>
               <td> $str</td>
           </tr>
           <tr>
               <td><h4>Life:</h4></td>
               <td> ".number_format($r['hp'])."/".number_format($r['maxhp'])."</td>
               <td><h4>Posse:</h4></td>
               <td> ";
   if($r['gang'])
   {
   print "<a href='gangs.php?action=view&ID=".number_format($r['gang'])."'>".stripslashes(htmlentities($r['gangNAME']))."</a>";
   }
   else
   {
   print "None</td>";
   }
         print "
           </tr>
           <tr>
               <td><h4>Location:</h4></td>
               <td> ".stripslashes(htmlentities($r['cityname']))."</td>
               <td><h4>House:</h4></td>
               <td> <a href='estate.php'>".stripslashes(htmlentities($r['hNAME']))."</a></td>
           </tr>
   <tr>
       <td><h4>Gender:</h4></td>
       <td> {$r['gender']}</td>
       <td><h4>Running Mate: </h4></td>
       <td>".getUserSpouse($_GET['u'])."</td>        
   </tr>
       </table>

 

Whole file is in the initial post. If the user is not married, it works fine. Otherwise it seems to cut off at "Posse:" and stops printing any more on the viewuser screen. What could be wrong?

Link to comment
Share on other sites

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.   Paste as plain text instead

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