Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-06-2012, 09:20 PM   PM User | #1
Xaust
New to the CF scene

 
Join Date: Nov 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Xaust is an unknown quantity at this point
Yeah, I suck at writing functions

Hey all, new here; no idea where to post. Heh, so I am a failure at writing functions; as I have not studied much on them.. Basically I am trying to make New = 1 and Used = 0 so it will post in place with a simple $coach_meta[$nu].. I know it is easy but hey, I suck.

Code:
function coach_meta2 ($nu = '') {

global $dbh;

$output = '';
$coach_meta = coach_meta($data['ID']);
$nu = ($coach_meta['ecpt_newused']);

if ($nu = "New") {

$nu = "1";

}
else
{
if ($nu = "Used") {

$nu = "0";

}

$nu = mysql_query($query);

while($data2 = mysql_fetch_assoc($nu)) {
$output = $data2;
}


}

return $output;

}
Anyone care to help an noob? haha

Last edited by Fou-Lu; 11-06-2012 at 10:12 PM.. Reason: Restored the post
Xaust is offline   Reply With Quote
Old 11-06-2012, 10:10 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This is php code, so this is definitely the wrong forum.
First, I wouldn't do this:
Code:
if ($nu = "New") { 

$nu = "1";

}
else 
{
	if ($nu = "Used") {

	$nu = "0";
	
}
It is bizarre to leave that burden on a function directly unless that's its actual job. Let it have $nu equating to only true and false. Let the caller decide what it considers true and false.
Next, don't do this:
Code:
	global $dbh;
Globalization is a debugging nightmare. Since they are global they are references, and will autodeclare if they do not exist. Changes out of scope are reflected in every scope, so this makes debugging exceedingly difficult. Unless there is a reason for it such as a locked function signature (used by callbacks in php such as usort, set_error_handler, etc), then always write to pass in a variable instead of globalizing it.

So effectively, we're now down to this:
PHP Code:
function coach_meta2($dbh$nu false)
{
    
$output '';
    
$coach_meta coach_meta($data['ID']);
    
// no, call this something different: $nu = ($coach_meta['ecpt_newused']);
    
$cm $coach_meta['ecpt_newused'];
    
$qry mysql_query($query); // again, something different
    
while($data2 mysql_fetch_assoc($qry))
    {
        
$output $data2// this will overwrite $output.  If you want to append, use .= instead.  If you do not want to append, use a LIMIT on the query and order in reverse.  Then pull just the first record
    
}

    return 
$output;

So, we now have a few problems.
- What is $data?
- What is $query?
- What is the purpose of both $nu and $dbh?
- What is the purpose of pulling $coach_meta, and ultimately the $cm I created from it?
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:52 AM.


Advertisement
Log in to turn off these ads.