Output formatted list of values from table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ourmaninparis
Forum Newbie
Posts: 21
Joined: Wed Apr 18, 2012 8:09 am

Output formatted list of values from table

Post 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.
apexNSW
Forum Newbie
Posts: 2
Joined: Tue Apr 17, 2012 3:19 pm

Re: Output formatted list of values from table

Post 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;
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Output formatted list of values from table

Post by requinix »

Swing and a miss, apex.

Code: Select all

$value = "$opt1;$opt2"."\n";
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.
ourmaninparis
Forum Newbie
Posts: 21
Joined: Wed Apr 18, 2012 8:09 am

Re: Output formatted list of values from table

Post 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.
ourmaninparis
Forum Newbie
Posts: 21
Joined: Wed Apr 18, 2012 8:09 am

Re: Output formatted list of values from table

Post 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
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Output formatted list of values from table

Post 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.
ourmaninparis
Forum Newbie
Posts: 21
Joined: Wed Apr 18, 2012 8:09 am

Re: Output formatted list of values from table

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Output formatted list of values from table

Post by requinix »

Try a "<br/>".
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Output formatted list of values from table

Post by x_mutatis_mutandis_x »

"\n" if printing in a file, or echo it to console. [text]<br />[/text] for displaying in browser.
ourmaninparis
Forum Newbie
Posts: 21
Joined: Wed Apr 18, 2012 8:09 am

Re: Output formatted list of values from table

Post 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;
?>

x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Output formatted list of values from table

Post by x_mutatis_mutandis_x »

Echo prints to screen/console. It doesn't return anything

Remove this line

Code: Select all

$value2 = echo $value; 
Replace this line

Code: Select all

return $value2
with

Code: Select all

return $value
ourmaninparis
Forum Newbie
Posts: 21
Joined: Wed Apr 18, 2012 8:09 am

Re: Output formatted list of values from table

Post 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.
ourmaninparis
Forum Newbie
Posts: 21
Joined: Wed Apr 18, 2012 8:09 am

Re: Output formatted list of values from table

Post 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.
ourmaninparis
Forum Newbie
Posts: 21
Joined: Wed Apr 18, 2012 8:09 am

Re: Output formatted list of values from table

Post 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.
Post Reply