30secondstosam Posted August 2, 2010 Share Posted August 2, 2010 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 More sharing options...
Yesideez Posted August 2, 2010 Share Posted August 2, 2010 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 https://forums.phpfreaks.com/topic/209572-calling-function-in-table-row/#findComment-1094069 Share on other sites More sharing options...
30secondstosam Posted August 2, 2010 Author Share Posted August 2, 2010 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 https://forums.phpfreaks.com/topic/209572-calling-function-in-table-row/#findComment-1094078 Share on other sites More sharing options...
Yesideez Posted August 2, 2010 Share Posted August 2, 2010 You're welcome! By calling the function inside echo you're using the value it returns. Inside your function you're using echo again and not returning anything so nothing is being placed inside the calling echo. Link to comment https://forums.phpfreaks.com/topic/209572-calling-function-in-table-row/#findComment-1094079 Share on other sites More sharing options...
30secondstosam Posted August 2, 2010 Author Share Posted August 2, 2010 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 https://forums.phpfreaks.com/topic/209572-calling-function-in-table-row/#findComment-1094115 Share on other sites More sharing options...
Yesideez Posted August 2, 2010 Share Posted August 2, 2010 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); Link to comment https://forums.phpfreaks.com/topic/209572-calling-function-in-table-row/#findComment-1094118 Share on other sites More sharing options...
30secondstosam Posted August 2, 2010 Author Share Posted August 2, 2010 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 https://forums.phpfreaks.com/topic/209572-calling-function-in-table-row/#findComment-1094124 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.