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
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;
}

Link to comment
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;
}

 

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

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 :)

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.