Jump to content

Pseudo code


JPark

Recommended Posts

I am trying to rework a page written by someone else and there is a function

function generate_click_box($row)
{
$html = <<<HTML

		  <div style="height:1px; font-size:1px; width:181px; margin:0 auto; background:#036; overflow:hidden;"></div>
		  <div style="height:1px; font-size:1px; width:185px; margin:0 auto; background:#036; overflow:hidden;"></div>
		  <div style="height:1px; font-size:1px; width:187px; margin:0 auto; background:#036; overflow:hidden;"></div>
		  <div style="height:2px; font-size:2px; width:189px; margin:0 auto; background:#036; overflow:hidden;"></div>
		  <div id="%1\$s_toggle" style="height:45px; width:191px; margin:0 auto; background:#036; color:#fff; cursor:pointer; font-size:12px; font-weight:bold; text-align:center; padding:0; margin:0;"><div style="padding:2px; vertical-align:middle;">%2\$s</div></div>
		  <div style="height:2px; font-size:2px; width:189px; margin:0 auto; background:#036; overflow:hidden;"></div>
		  <div style="height:1px; font-size:1px; width:187px; margin:0 auto; background:#036; overflow:hidden;"></div>
		  <div style="height:1px; font-size:1px; width:185px; margin:0 auto; background:#036; overflow:hidden;"></div>
		  <div style="height:1px; font-size:1px; width:181px; margin:0 auto; background:#036; overflow:hidden;"></div>

HTML
;

$html_disabled = <<<HTML

		  <div style="height:1px; font-size:1px; width:181px; margin:0 auto; background: #cccccc; overflow:hidden;"></div>
		  <div style="height:1px; font-size:1px; width:185px; margin:0 auto; background: #cccccc; overflow:hidden;"></div>
		  <div style="height:1px; font-size:1px; width:187px; margin:0 auto; background: #cccccc; overflow:hidden;"></div>
		  <div style="height:2px; font-size:2px; width:189px; margin:0 auto; background: #cccccc; overflow:hidden;"></div>
		  <div style="height:45px; width:191px; margin:0 auto; background: #cccccc; color:#036; font-size:12px; font-weight:bold; text-align:center; padding:0; margin:0;"><div style="padding:2px; vertical-align:middle;">
		  	<a href="http://factfinder.census.gov/servlet/EconSectorServlet?caller=dataset&sv_name=*&_SectorId=%1\$s&ds_name=EC0700A1&_lang=en&_ts=272288383987" target="_blank" alt="Click for %2\$s data">%2\$s<br />COMPLETED</a><br /></div></div>
		  <div style="height:2px; font-size:2px; width:189px; margin:0 auto; background: #cccccc; overflow:hidden;"></div>
		  <div style="height:1px; font-size:1px; width:187px; margin:0 auto; background: #cccccc; overflow:hidden;"></div>
		  <div style="height:1px; font-size:1px; width:185px; margin:0 auto; background: #cccccc; overflow:hidden;"></div>
		  <div style="height:1px; font-size:1px; width:181px; margin:0 auto; background: #cccccc; overflow:hidden;"></div>

HTML
;


return ($row[4])
         ? sprintf($html, $row[3], htmlentities( $row[1] ) )
         : sprintf($html_disabled, htmlentities( $row[5] ), htmlentities( $row[1] ) );

}

 

Can someone translate (put into pseudo code) the following piece

 

return ($row[4])

        ? sprintf($html, $row[3], htmlentities( $row[1] ) )

        : sprintf($html_disabled, htmlentities( $row[5] ), htmlentities( $row[1] ) );

 

Thanks!

 

Joe

Link to comment
Share on other sites

That's called a ternary statement.

 

So if you have something of the form cond ? ifTrue : ifFalse then ifTrue will be returned if cond evalutes to true, and ifFalse will be returned otherwise.

 

You could rewrite it to the following:

if ($row[4]) {
return sprintf($html, $row[3], htmlentities( $row[1] ) );
}
else {
return sprintf($html_disabled, htmlentities( $row[5] ), htmlentities( $row[1] ) );
}

Link to comment
Share on other sites

Ok.  Makes a bit of sense...

 

So, why does my page crap out (nothing at all displays) if I change it to

return ($row[4])
         ? sprintf($html, $row[3], htmlentities( $row[1] ) )
         : echo "hello";

 

Isn't this saying that $row[4] is true then do sprintf($html, $row[3], htmlentities( $row[1] ) ) but if $row[4] is false then echo "hello"?

 

Joe

Link to comment
Share on other sites

Because echo doesn't return any value.

 

ok... : "hello"; does what I thought it should do to begin with but I don't quite understand what you wrote...

 

Is the problem from the return ($row[4])?  Is that why the page is looking for something to be returned?

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.