Jump to content

MySql select returns "Array" when echo'd


Aaron

Recommended Posts

I added a new field (not custom, added in table) that has VATSIM ID. I got it working so it gets put in the table upon registration, but I need to fix it so it can be eddited in the edit profile page. The problem is if I echo $vatsimid it prints "Array". Help is appreciated. This is what I have so far:

Profile.php

public function editprofile()
{
 if(!Auth::LoggedIn())
 {
  $this->set('message', 'You must be logged in to access this feature!');
  $this->render('core_error.tpl');
  return;
 }
 $pilotid = Auth::$userinfo->pilotid;
 $this->set('userinfo', Auth::$userinfo);
 $this->set('customfields', PilotData::GetFieldData(Auth::$pilotid, true));
 $this->set('vatsimid', PilotData::GetVatsimId($pilotid));
 $this->set('ivaoid', PilotData::GetIvaoId(Auth::$pilotid));
 $this->set('bgimages', PilotData::GetBackgroundImages());
 $this->set('countries', Countries::getAllCountries());
 $this->set('pilotcode', PilotData::GetPilotCode(Auth::$userinfo->code, Auth::$userinfo->pilotid));
 $this->render('profile_edit.tpl');
}

PilotData

public function GetVatsimId($pilotid)
{
    $vatsimid = "SELECT vatsimid FROM phpvms_pilots WHERE pilotid='$pilotid'";
    return DB::get_results($vatsimid);
}
public function GetIvaoId($pilotid)
{
    $ivaoid = "SELECT ivaoid FROM phpvms_pilots WHERE pilotid='$pilotid'";
    return DB::get_results($ivaoid);
    echo DB::$error;
}

profile_edit.tpl (so far)

<?php
if ($vatsimid==0)
{
$vnchecked="checked";
$vychecked="";
}
else
{
$vychecked="checked";
$vnchecked="";
}
echo $vatsimid;
?>
 <tr><td>Are you a VATSIM Member: *</td>
<td><input type="radio" name="vatsim" id="VY" value="Y" onclick="VATSIMID()" <?php echo $vychecked ?>>Yes    <input type="radio" name="vatsim" id="VN" value="N" onclick="VATSIMID()" <?php echo $vnchecked ?>>No</td>
</tr>

Link to comment
Share on other sites

PilotData

public function GetVatsimId($pilotid)
{
	$vatsimid = "SELECT vatsimid FROM phpvms_pilots WHERE pilotid='$pilotid'";
	return DB::get_results($vatsimid);
}
public function GetIvaoId($pilotid)
{
	$ivaoid = "SELECT ivaoid FROM phpvms_pilots WHERE pilotid='$pilotid'";
	return DB::get_results($ivaoid);
	echo DB::$error;
}

^ Get rid of all that you don't need it, use PilotData::getFieldValue() as follows

profile_edit.tpl (so far)

<?php
$checked = false;
if (PilotData::getFieldValue($pilotid, 'vatsimid'))
{
$checked = true;
}
?>
 <tr><td>Are you a VATSIM Member: *</td>
<td><input type="radio" name="vatsim" id="VY" value="Y" onclick="VATSIMID()" <?php if($checked){ echo 'checked'; ?>>Yes	<input type="radio" name="vatsim" id="VN" value="N" onclick="VATSIMID()" <?php if(!$checked){ echo 'checked'; ?>>No</td>
</tr>

Link to comment
Share on other sites

  • Administrators

The command

DB::get_results($query);

returns an array from the database, even it is only one line that it is returning. If you only want one line returned and have it in object form then use

DB::get_row($query);

Then you will have access to it as

$variable->vatsimid

Link to comment
Share on other sites

I can't use the getFieldValue() function because it isn't a custom field. I added a whole new column for it. This is what I did so far, but if I echo $vatsimid right before the ?> it gives me an error 

Profile.php -> editprofile()

$this->set('vatsimid', PilotData::GetVatsimId(Auth::$pilotid));
$this->set('ivaoid', PilotData::GetIvaoId(Auth::$pilotid));

PilotData

public function GetVatsimId($pilotid)
{
    $query = "SELECT vatsimid FROM phpvms_pilots WHERE pilotid='$pilotid'";
    return DB::get_row($query);
    echo DB::$error;
}
public function GetIvaoId($pilotid)
{
    $query = "SELECT ivaoid FROM phpvms_pilots WHERE pilotid='$pilotid'";
    return DB::get_row($query);
    echo DB::$error;
}

profile_edit.tpl

<?php
if ($vatsimid==0)
{
$vnchecked = "checked";
$vychecked = "";
}
else
{
$vychecked = "checked";
$vnchecked = "";
}
echo $vatsimid;
?>
 <tr><td>Are you a VATSIM Member: *</td><td><input type="radio" name="vatsim" id="VY" value="Y" onclick="VATSIMID()" <?php echo $vychecked ?>>Yes    <input type="radio" name="vatsim" id="VN" value="N" onclick="VATSIMID()" <?php echo $vnchecked ?>>No</td></tr>
<tr><td>VATSIM ID:</td>
<td><div id="VATSIMfield"><script type="text/javascript">
VATSIMID();
</script>
</div>

Link to comment
Share on other sites

Guest lorathon

Try This.

public function GetVatsimId($pilotid)
{
	    $query = "SELECT vatsimid FROM phpvms_pilots WHERE pilotid='$pilotid'";
	   $res = DB::get_row($query);
	   echo DB::$error;
	   if($res) return $res->vatsimid;
	   else return 0;
}
public function GetIvaoId($pilotid)
{
	    $query = "SELECT ivaoid FROM phpvms_pilots WHERE pilotid='$pilotid'";
	    $res  = DB::get_row($query);
	    echo DB::$error;
	    if($res) return $res->ivaoid;
	   else return 0;
}

Link to comment
Share on other sites

Any ideas how to set the value in javascript? I want the current value of the vatsimid to show up there by default. Here's what I've tried:

function VATSIMID(){
if(document.getElementById('VY').checked) {
VATSIMfield.innerHTML="<input type='text' name='vatsimid' value='<?php echo $vatsimid ?>' maxlength='7' onKeyPress='return numbersonly(this, event)'/>"
}else if(document.getElementById('VN').checked) {
VATSIMfield.innerHTML="<input type='text' name='vatsimid' value='' disabled='disabled' style='background-color: #E6E6E6;'/>"
}
}

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

Loading...
×
×
  • Create New...