Page 1 of 1
Output formatted list of values from table
Posted: Wed Apr 18, 2012 8:23 am
by ourmaninparis
Hello,
I have been trying to produce a formatted list using PHP code. The output must be in this format and use the "return $value" code for output:
thing1a;thing1b
thing2a;thing2b
etc.
It is to be included in a Joomla extension called ContentBuilder.
I have adapted a section of code that was posted on another forum but I can only get it to return one set of values (thing1a;thing1b) even though there are many rows in the table. It currently returns the set of values for the last row in the table. I believe that I need to add an array to the below code and a variable that can store and then return all the values rather than just one value. However, I've been trying for two days and can't get a result. I should point out that I'm a marketing person not an IT expert and I'm just trying to produce a working example of something.
Code: Select all
<?php
$sql = "SELECT `Notice_Board_Name`, `id` FROM `sgj_noticeboards` WHERE `user_id`='43'";
JFactory::getDBO()->setQuery($sql);
$rows = JFactory::getDBO()->loadObjectList();
$value = '';
foreach($rows As $row )
{
$opt1 = $row->Notice_Board_Name;
$opt2 = $row->id;
$value = "$opt1;$opt2"."\n";
}
return $value;
?>
Any help would be greatly appreciated.
Shaun.
Re: Output formatted list of values from table
Posted: Wed Apr 18, 2012 8:43 am
by apexNSW
Hello,
Near the bottom of your code, your `;` was being interpreted by the php engine, because it was not enclosed in single quotes. See the corrections below, I hope it helps.
Regards,
apexNSW
Code: Select all
<?php
$sql = "SELECT `Notice_Board_Name`, `id` FROM `sgj_noticeboards` WHERE `user_id`='43'";
JFactory::getDBO()->setQuery($sql);
$rows = JFactory::getDBO()->loadObjectList();
$value = '';
foreach($rows As $row )
{
$opt1 = $row->Notice_Board_Name;
$opt2 = $row->id;
$value = $opt1.';'.$opt2."\n";
}
return $value;
?>
Re: Output formatted list of values from table
Posted: Wed Apr 18, 2012 8:47 am
by requinix
Swing and a miss, apex.
Every time that code executes (which it will because it's perfectly fine) it will overwrite the previous $value. What you need to use is the
.= operator which will
append the string to $value.
Re: Output formatted list of values from table
Posted: Wed Apr 18, 2012 9:03 am
by ourmaninparis
Hello,
Thanks for the quick replies. I had the ".=" in my original code and removed it because I had no values returned. Maybe it is an issue with Joomla/ContentBuilder interpretting the code correctly. Perhaps I can find another way of doing the same thing. Thanks.
Re: Output formatted list of values from table
Posted: Wed Apr 18, 2012 3:22 pm
by ourmaninparis
Hello again,
I think it must be the Joomla extension that cannot handle this type of code.
Have have tried another option to concaternate $value = $value."$opt1;$opt2"."\n"; but this doesn't work either. Any other way I can construct this code to get the required result output?
Thanks, Shaun
Re: Output formatted list of values from table
Posted: Wed Apr 18, 2012 3:33 pm
by x_mutatis_mutandis_x
Debug your code within the for loop (print_r($value);) and see what gets printed. If that doesn't work, run the query on a SQL client/command line and see what you get.
Re: Output formatted list of values from table
Posted: Thu Apr 19, 2012 9:23 am
by ourmaninparis
Thanks for the advice.
I think I have found the source of the problem. The \ (backslash) of "\n" is being stripped out of the code when saved in the Joomla extension hence the result is something like this value1;code1value2;code2, which is not displayed because of the missing line break.
I am using Joomla 1.7.2 with PHP 5.2.17. I tried with a more recent version of PHP but I get the same issue. Any suggestions? Maybe there is another way of doing a line break.
Re: Output formatted list of values from table
Posted: Thu Apr 19, 2012 9:59 am
by requinix
Try a "<br/>".
Re: Output formatted list of values from table
Posted: Thu Apr 19, 2012 10:03 am
by x_mutatis_mutandis_x
"\n" if printing in a file, or echo it to console. [text]<br />[/text] for displaying in browser.
Re: Output formatted list of values from table
Posted: Thu Apr 19, 2012 1:00 pm
by ourmaninparis
Thanks once again for the click replies. I need to return the values to a file. <BR/> works fine for echo in the browser but this is not what I need.
Maybe there is a way I can assign the echo result to a variable then return the variable?
I tried this but returns an error message - getting in a mess I think
Code: Select all
<?php
$sql = "SELECT `Notice_Board_Name`, `id` FROM `sgj_noticeboards` WHERE `user_id`='43'";
JFactory::getDBO()->setQuery($sql);
$rows = JFactory::getDBO()->loadObjectList();
$value = '';
$value2 = '';
foreach($rows As $row )
{
$opt1 = $row->Notice_Board_Name;
$opt2 = $row->id;
$value .= "$opt1;$opt2<br />";
$value2 = echo $value;
}
return $value2;
?>
Re: Output formatted list of values from table
Posted: Fri Apr 20, 2012 9:49 am
by x_mutatis_mutandis_x
Echo prints to screen/console. It doesn't return anything
Remove this line
Replace this line
with
Re: Output formatted list of values from table
Posted: Sat Apr 21, 2012 4:24 am
by ourmaninparis
Thanks for the reply but the <br /> can not be returned using 'return' so it doesn't work. I'm trying to find a way around the fact that I can't use "\n" so I tried <br /> which will echo the correct results. I figured there may be away of assigning the echo to a variable but apparently not.
Re: Output formatted list of values from table
Posted: Sat Apr 21, 2012 5:13 am
by ourmaninparis
I've just tried a new approach. I've included the part of the code containing the \ in an external file that is inserted but I still get the same issue.
Re: Output formatted list of values from table
Posted: Sat Apr 21, 2012 11:35 am
by ourmaninparis
Fixed it!
I think a backslash stripping module must be installed with Joomla. If I include two back slashes then it leaves one and the code works!
Thank you all for your help.