Echo out error message

Hi there,

I am trying to work out how to echo out an error message based on using the following method:


    public function generateRecommendations($text, $include=0){
        $exit_status = 0; //0: ok, 1: no results found - extended to generic search
        $rec = array();

        if(($include == 0) OR ($include == 1)) $rec = Conferences::generateConferenceRecommendationsByRating($rec, $text);
        
        if(count($rec)==0) {
            $rec = Recommendations::generateRecommendations('', 0);
            $exit_status = 1;
        }
        else {
            $rec = Recommendations::sortAndFilterRecommendations($rec);
        }
        return array($rec, $exit_status);
    }

I call the method like so:


 <?
  $rec = Recommendations::generateRecommendations($_GET['documents']);

  if($rec[1] == 1){
       echo "<div style='color:#ff0000; margin:6px 0 10px 0'>Sorry, the system could not find any
         recommendations based on your search criteria. Why don't you get started with the following.</div>";
   }

    $printed=0;
    $RECOMMENDATIONS_TO_PRINT=10;

    foreach($rec[0] as $theResults){
       if ($printed<$RECOMMENDATIONS_TO_PRINT)
         {
          echo "<a href='".SITEADDRESS."view/view-document.php?table=".$theResults['HoldingID']."&ID=".$theResults['TypeID']."'>".$theResults['Title']." <strong>(".$theResults['Rating']."&#37;)</strong></a><BR><BR>";
              $printed++;
          }
    }
  ?>

This code nearly works, but i think its the way i return as an array in the method. The error message does appear but the echo inside the “if ($printed<$RECOMMENDATIONS_TO_PRINT)” does not display as it should. I think it’s the for loop?

But i’m not quite sure, can anyone help? I tried using this:


foreach($rec[0][0] as $theResults){

But didn’t seem to work…

Can someone please help?

Thanks

Hi,

Not it’s just the one error message. The $RECOMMENDATIONS_TO_PRINT=10 is the amount of results to be shown from the array…

Hi billy,

Are you trying to display upto ten messages or will their be less or more?

I’d have to see the rest of the object to even make sense of that… especially the OTHER method being called, generateConferenceRecommendationsByRating.

Though I’ve got the feeling you’re using ‘objects for nothing’ here, and the piss poor scope handling of objects in AT&T Syntax (aka C syntax) languages could be part of whats screwing with you.

I also have the feeling the recursive calls aren’t helping matters much… In general the entire logic flow doesn’t make a whole lot of sense… though the bizzare willy-nilly indenting isn’t helping me understand it.

Oh, and there is no keyword operator ‘or’ in php last time I checked.

For starters I’d probable chop that down to:

public function generateRecommendations($text,$include=0) {
	if (($include==0) || ($include==1)) {
		$rec=array();
		$rec=Conferences::generateConferenceRecommendationsByRating($rec, $text);
		if (count($rec)>0) {
			return Recommendations::sortAndFilterRecommendations($rec);
		}
	}
	return false;
}


<?php
	if ($rec=Recommendations::generateRecommendations($_GET['documents'])) {
		$count=count($rec);
		if ($count>10) $count=10;
		for ($t=0; t<$count; $t++) {
			echo '
				<a href="',SITEADDRESS,'view/view-document.php?table=',$rec[$t]['HoldingID'],'&ID=',$rec[$t]['TypeID'],'">
					',$rec[$t]['Title'],'
					<strong>(',$rec[$t]['Rating'],'&#37;)</strong>
				</a><br /><br />';
		}
	} else {
		echo '
			<div style="color:#ff0000; margin:6px 0 10px 0">
				Sorry, the system could not find any recommendations based on your search criteria. Why don't you get started with the following.
			</div>';
	}

?>

But that’s mostly guessing with ‘generateConferenceRecommendationsByRating’ being black-box territory… though I’d probably rewrite that function much as I did your generateRecommendations one, not even bothering to pass it $rec (which seems to be done for no reason) and having it return false instead of an empty array on no results.

… and that’s assuming I even understood what you’re trying to do.

Gah, I used to know that… I’ve been away from this for too long. Of course if precedence on something like an OR statement is important, one has probably overthought their code. (In this case since there’s no other operations going on, I guess it doesn’t matter either way)

Thinking on this more, I’d probably NOT count them during the iteration, and instead be passing how many should be pulled up to the function showing them. If that’s calling a query, it’s called ‘limit’.

There is, it’s the same as || but with a lower precedence.

See here: http://php.net/manual/en/language.operators.logical.php