Jump to content

Code Check


mcmuney

Recommended Posts

PROBLEM: After the modification, I get this error, "Fatal error: Call to a member function on a non-object in /home/rateomet/public_html/code/stats_sex.php on line 7"

I've started with this code, which is valid and functional. This results in showing a number for the various sexual orientation (ie 1,2,etc), because the labels are specified on a different table (ie 1=Straight, 2=Gay, etc):
[code]
<?php
// grab the total count
$total_query2 = "SELECT COUNT(sm_sex_orn) FROM sc_member_info";
$res2 = mysql_query($total_query2) or die(mysql_error());
$total_count = mysql_result($res2, 0, 0);

// start the table
echo '<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor=orange><tr bgcolor=orange><td><b><font face=Verdana size=1>Sign</td><td width=50 align=right><b><font face=Verdana size=1>Count</td><td width=50 align=right><b><font face=Verdana size=1>P%</td></tr>';

// go through each sexual orientation
$sign_query = "SELECT sm_sex_orn, COUNT(sm_sex_orn) AS sex_count FROM sc_member_info GROUP BY sm_sex_orn";
$res3 = mysql_query($sign_query) or die(mysql_error());
while ($info = mysql_fetch_assoc($res3))
{
  echo '<tr><td><font face=Verdana size=1>'..$info['sm_sex_orn'].'</td><td align=right><font face=Verdana size=1>'.$info['sex_count'].'</td><td align=right><font face=Verdana size=1>'.round(($info['sex_count'] / $total_count)*100).' %</td></tr>';
}

// finish the table with the total
echo '<tr><td colspan=3 height=2></td></tr><tr><tr align=right bgcolor=orange><td align=left><b><font face=Verdana size=1>Total Members</td><td><b><font face=Verdana size=1>'.$total_count.'</td><td><b><font face=Verdana size=1>100 %</td></tr></table>';
?>
[/code]

Now, I'm adding the following lines to the above code:
[code]
$sql = "select sc_sex_orien.sso_id, sc_sex_orien.sso_type, sc_member_info.sm_sex_orn from sc_member_info, sc_sex_orien where sc_sex_orien.sso_id=sc_member_info.sm_sex_orn";
$reset3 =$db->select_data($sql);
[/code]

And, replacing:
[code]
echo '<tr><td><font face=Verdana size=1>'..$info['sm_sex_orn'].'</td>
[/code]

With:
[code]
echo '<tr><td><font face=Verdana size=1>'.$db->get_field("sc_sex_orien","sso_type","sso_id",$rcset[0]['sm_sex_orn']).'</td>
[/code]

The full modified codes looks like this. This was done so that instead of displaying 1,2, etc, it actually displayes, Straght, Gay, etc:
[code]
<?php
// grab the total count
$total_query2 = "SELECT COUNT(sm_sex_orn) FROM sc_member_info";
$res2 = mysql_query($total_query2) or die(mysql_error());
$total_count = mysql_result($res2, 0, 0);
$sql = "select sc_sex_orien.sso_id, sc_sex_orien.sso_type, sc_member_info.sm_sex_orn from sc_member_info, sc_sex_orien where sc_sex_orien.sso_id=sc_member_info.sm_sex_orn";
$reset3 =$db->select_data($sql);


// start the table
echo '<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor=orange><tr bgcolor=orange><td><b><font face=Verdana size=1>Sign</td><td width=50 align=right><b><font face=Verdana size=1>Count</td><td width=50 align=right><b><font face=Verdana size=1>P%</td></tr>';

// go through each sexual orientation
$sign_query = "SELECT sm_sex_orn, COUNT(sm_sex_orn) AS sex_count FROM sc_member_info GROUP BY sm_sex_orn";
$res3 = mysql_query($sign_query) or die(mysql_error());
while ($info = mysql_fetch_assoc($res3))
{
  echo '<tr><td><font face=Verdana size=1>'.$db->get_field("sc_sex_orien","sso_type","sso_id",$rcset[0]['sm_sex_orn']).'</td><td align=right><font face=Verdana size=1>'.$info['sex_count'].'</td><td align=right><font face=Verdana size=1>'.round(($info['sex_count'] / $total_count)*100).' %</td></tr>';
}

// finish the table with the total
echo '<tr><td colspan=3 height=2></td></tr><tr><tr align=right bgcolor=orange><td align=left><b><font face=Verdana size=1>Total Members</td><td><b><font face=Verdana size=1>'.$total_count.'</td><td><b><font face=Verdana size=1>100 %</td></tr></table>';
?>
[/code]
Link to comment
Share on other sites

Guest docseussman
you are using $db as an object and calling one of it's member functions. In the code you posted you never created the object, $db = new objectname(). This would create the error you are experiencing.
Link to comment
Share on other sites

well $db-> is definatly the usage for calling a member function of an object(Class), double check the working page and see if you can find the definition of the class. If done correctly it will probably be done in a seperate page and then imported using an include or require near the top of the script. Maybe named database or something similar. If this is the case you can require it in your new script and use any of the functions it may contain.
Link to comment
Share on other sites

Absolutly, I just don't understand from the code given what the select_data function is supposed to do. You the code you have shown you never use the variable reset3 anywhere. The closest thing to it is res3 but it is assigned to another value on line 15 anyway. Now in looking line by line trying to figure the code out I see another call to a different function of the same $db object on line 18 ---- $db->get_field. 


What it looks like is that you are new pretty new to th scene and are trying to piece together some code that works from a different script to make it work with your site database. This is certainly a viable method of learning but I would suggest understanding the code that you are copying and pasting so that you know what it does and what needs to be modified so that it will work for your purpose.

I would suggest some of the awsome tutorials phpfreaks has to offer. I don't want you to be discouraged from posting questions here but to answer your question would be pretty much to post a tutorial which I think is beyond the scope of the forum.
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.