Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing an echo value to a function

Status
Not open for further replies.

pease

Programmer
Apr 2, 2009
29
GB
Not sure if I can explain this but here goes.

Im writing a few functions and the main one I will want to use is a simple straight forward "grab results from a SQL query". In the past Ive written each one seperately however Im now starting to get properly into OOP and am using classes (yay finally! I hear you cry!!!).

So using a few basic classes I have a simple pull results query of:

Code:
function pull_sponsors2() {
	$connector= new DbConnector();
	$result = $connector->query("SELECT * FROM sponsors");
	while($row = $connector->fetchArray($result)) {
	echo "<li><a href='".$row['url']."' title='".$row['name']."'><img src='images/sponsors/".$row['logo']."' alt='".$row['name']."' class='sponsorlogo'></a></li>\n";
	}
}

So I then thought, I can pass the query into the function to make it reusable ie.
Code:
function resueable_query($thequery) {
	$connector= new DbConnector();
	$result = $connector->query(thequery);
	while($row = $connector->fetchArray($result)) {
	echo "<li><a href='".$row['url']."' title='".$row['name']."'><img src='images/sponsors/".$row['logo']."' alt='".$row['name']."' class='sponsorlogo'></a></li>\n";
	}
}

great so far but heres where I come unstuck.

It won't always have the same table, therefore not the same output fields in the array and I may well want the results formatted differently.

With that in mind I thought, just pass the output string into the function as well.
eg
Code:
function resueable_query($thequery,$output_string) {
	$connector= new DbConnector();
	$result = $connector->query(thequery);
	while($row = $connector->fetchArray($result)) {
	echo $output_string;
	}
}

and thats where it fails, I get a "unexpected T_ENCAPSED_AND_WHITESPACE" error and no matter what I do I cant figure out how to correct it.

Basically I was calling it with:
Code:
resueable_query("SELECT * FROM sponsors","<li><a href='$row['url']' title='$row['name']'><img src='images/sponsors/$row['logo']' alt='$row['name']' class='sponsorlogo'></a></li>\n") {

Im guessing its to do with the ' chars but I've tried doing \' and that doesnt work. Am I missing a simple trick here? Am I doing this all wrong? or am I on the right lines but just making a slight mess of it?

 
It is the ' characters.

Just remove them from the $row['url'] variables and you should be fine: $row When used inside a string, arrays...st be shutdown to prevent damage to Unknown.
 
hmmmm its getting there but now I get the correct number of rows being returned, no errors given but no data in the rows.

eg
Code:
reusable_query("SELECT * FROM news","<li><a href='event_information.php?event=$row[id]'>$row[heading]</a></li>\n");

gives me 20 rows displayed (and there are 20 news items) but in the view source each row shows up as
Code:
<li><a href='event_information.php?event='></a></li>

so the variables dont appear to be being returned.
 
Hi

Yes, because the string is double-quoted, so the variables are replaced with their values. When the string is handled. That means, before the function is called.

You could solve it by single-quoting the string or ecaping the variables then using [tt]eval()[/tt] inside the [tt]function[/tt]. But I never had full success with that, so I would not recommend it.

My preferred way, however is slower then your original idea :
Code:
[b]function[/b] [COLOR=darkgoldenrod]resueable_query[/color][teal]([/teal][navy]$thequery[/navy][teal],[/teal][navy]$output_string[/navy][teal])[/teal]
[teal]{[/teal]
  [navy]$connector[/navy][teal]=[/teal] [b]new[/b] [COLOR=darkgoldenrod]DbConnector[/color][teal]();[/teal]
  [navy]$result[/navy] [teal]=[/teal] [navy]$connector[/navy][teal]->[/teal][COLOR=darkgoldenrod]query[/color][teal]([/teal]thequery[teal]);[/teal]
  [b]while[/b] [teal]([/teal][navy]$row[/navy] [teal]=[/teal] [navy]$connector[/navy][teal]->[/teal][COLOR=darkgoldenrod]fetchArray[/color][teal]([/teal][navy]$result[/navy][teal]))[/teal] [teal]{[/teal]
    [b]echo[/b] [COLOR=darkgoldenrod]preg_replace[/color][teal]([/teal][green][i]'/%(\w+)/e'[/i][/green][teal],[/teal][green][i]'$row[$1]'[/i][/green][teal],[/teal][navy]$output_string[/navy][teal]);[/teal]
  [teal]}[/teal]
[teal]}[/teal]

[COLOR=darkgoldenrod]reusable_query[/color][teal]([/teal][green][i]"SELECT * FROM news"[/i][/green][teal],[/teal][green][i]"<li><a href=\"event_information.php?event=%id\">%heading</a></li>\n"[/i][/green][teal]);[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top