Calling and returning variables in functions

// differing selects used in 8 sections of code

		while($col = $db->fetch_array($query)) {
			$query2 = $db->query("SELECT... ;


// many repetitive lines used in 8 sections of code, reduce to 1 hopefully

	        $col['row'] = ...
		$col['row'] = .= 'more guf';

		if ($col['row99'] == 1) {...
   

should use a function it seems,
but…
what variable name to get it to work?
should I use global / return?
function buildline($col[‘row’]) is not valid

// replace with function

		while($col = $db->fetch_array($query)) {
			$query2 = $db->query("SELECT... ;


// call function
		buildline();


// more code


// function
		function buildline() {

			// global $something; // ???
	        	$col['row'] = ...
			$col['row'] = .= 'more guf';

			if ($col['row99'] == 1) {...

			
			// return $something; // ???

		}




Well written functions accept all input data as call-time parameters and return the result they produce to the calling code.

Your code implies you are running queries inside of loops based on the result of an initial query. Don’t do this. It is extremely inefficient. Use a single, appropriate type, JOIN query to get the related data all at once.

Your pseudo code doesn’t provide sufficient detail to actually help you, but if you have name-numbered columns, your data is not normalized, making every operation on that data more compilated.

Sorry, code is actually from XMB Forum - php forum / Subversion / [r3058] /xmb19x/trunk/source/memcp.php
I used generic/loose code as I wasn’t sure what the policy was.
I don’t have name numbered columns.

        $query = $db->query(
            "SELECT t.tid, t.fid, t.icon, t.lastpost, t.subject, t.replies, r.uid AS lastauthor
             FROM ".X_PREFIX."favorites f
             INNER JOIN ".X_PREFIX."threads t USING (tid)
             LEFT JOIN ".X_PREFIX."members AS r ON SUBSTRING_INDEX(SUBSTRING_INDEX(t.lastpost, '|', 2), '|', -1) = r.username
             WHERE f.username='$xmbuser' AND f.type='subscription'
             ORDER BY t.lastpost DESC
             LIMIT {$mpage['start']}, $tpp"
        );
        $subnum = 0;
        $subscriptions = '';
        $tmOffset = ($timeoffset * 3600) + ($SETTINGS['addtime'] * 3600);
        while($fav = $db->fetch_array($query)) {
            $forum = getForum($fav['fid']);
            $forum['name'] = fnameOut($forum['name']);


            $lastpost = explode('|', $fav['lastpost']);

            // Translate "Anonymous" author.
            $lastpostname = trim( $lastpost[1] );
            if ( 'Anonymous' == $lastpostname ) {
                $lastpostname = $lang['textanonymous'];
            }

            $lastreplydate = gmdate($dateformat, $lastpost[0] + $tmOffset);
            $lastreplytime = gmdate($timecode, $lastpost[0] + $tmOffset);
            $lastpost = $lang['lastreply1'].' '.$lastreplydate.' '.$lang['textat'].' '.$lastreplytime.' '.$lang['textby'].' '.$lastpostname;
            $fav['subject'] = rawHTMLsubject(stripslashes($fav['subject']));

            if ($fav['icon'] != '') {
                $fav['icon'] = '<img src="'.$smdir.'/'.$fav['icon'].'" alt="" border="0" />';
            } else {
                $fav['icon'] = '';
            }

I was looking to add a function as wanted to get additional rows from the threads table and include them on the $fav[‘subject’] variable.

$forum = getForum($fav[‘fid’])* certainly gives an example of a function but trying to get my head around functions for string building and my head is swimming. I have done it without a function but it would be neater with it. Thanks anyway.

// from https://sourceforge.net/p/xmb-forum/code/HEAD/tree/xmb19x/trunk/source/include/functions.inc.php

function getForum($fid) {
    global $db;

    $forums = forumCache();
    while($forum = $db->fetch_array($forums)) {
        if (intval($forum['fid']) == intval($fid)) {
            return $forum;
        }
    }
    return FALSE;
}
Sponsor our Newsletter | Privacy Policy | Terms of Service