Jump to content

Calling Function In Table Row


30secondstosam

Recommended Posts

Hi all!

 

First off, sorry if this question is a completely dumb one but i'm really new to PHP.

 

I'll paste the code to start with...

 

 

function getReasons ($id)

{

global $link;

$rst = mysql_query('SELECT r.name as reason FROM entries_feedback_comments as f LEFT JOIN reasons as r ON r.id = f.reason_id WHERE f.feedback_id ='. $id, $link);

while ($row = mysql_fetch_assoc($rst))

{

echo $row['reason'];

}

}

 

 

later on in the code.... inside a table structure i do this...

 

echo '<tr class="' . ($y?'even':'odd') . '">';

echo '<td colspan="10" class="freetext">'.getReasons($row['id']).'</td>';

echo '</tr>';

echo '<tr class="' . ($y?'even':'odd') . '">';

echo '<td colspan="10" class="freetext">'.$row["freetype"].'</td>';

echo '</tr>';

 

 

(I have left put the "freetype" bit of code in to show you that it works with this row but not with the function).

 

Basically, what I see is the result of the function outside of the table itself rather than in the table row.

Is there a reason for this? Does a function have to be put in specific places in the code?

 

Thank you for your help.

 

Sam

Link to comment
https://forums.phpfreaks.com/topic/209572-calling-function-in-table-row/
Share on other sites

Try this...

 

function getReasons ($id) {
   global $link;
  $result='';
   $rst = mysql_query('SELECT r.name as reason FROM entries_feedback_comments as f LEFT JOIN reasons as r ON r.id = f.reason_id WHERE f.feedback_id ='. $id, $link);
   while ($row = mysql_fetch_assoc($rst)) {
      $result.=$row['reason'];
   }
  return $result;
}

Try this...

 

function getReasons ($id) {
   global $link;
  $result='';
   $rst = mysql_query('SELECT r.name as reason FROM entries_feedback_comments as f LEFT JOIN reasons as r ON r.id = f.reason_id WHERE f.feedback_id ='. $id, $link);
   while ($row = mysql_fetch_assoc($rst)) {
      $result.=$row['reason'];
   }
  return $result;
}

 

Brilliant!! that worked perfectly. thank you so much for the help and quick responce.

+ i shall remember to wrap my code in the future!

excellent :)

one more thing seeing as you are clearly a genius... need to find out how to space out results from this loop!

 

function getReasons ($id)
{
global $link;
  	$result='';
$rst = mysql_query('SELECT r.name as reason FROM entries_feedback_comments as f LEFT JOIN reasons as r ON r.id = f.reason_id WHERE f.feedback_id ='. $id, $link);

while ($row = mysql_fetch_assoc($rst)) 
{
	$result.=$row['reason'];
}

return $result;
}

 

the result is shown like this...

e.g. - Result1Result2Result3

I want it to be "Result1 Result2 Result3"

 

do i have to put another loop inside the while?

Just add a space on the end as you build the string.

$result.=$row['reason'].' ';

 

This will add an extra space on the end so we can remove that with this:

return trim($result);

brilliant! i did this before:

$result.=$row['reason'].' ';

didnt work because i didnt use the "trim" bit.

 

thanks again :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.