PHP in Joomla - SELECT from SQL and Display Results

Hi there,

I am new to PHP and I am having a hard time retrieving and displaying any database data to my website.

My goal is to simply type a message in a form, then display the message.

I am using sorcerer for Joomla to input all of my PHP code. (Sorcerer automatically connects to my databse, so there is no need to include connection code - … e-database)

I have attached an image of what my table looks like - the current data is for testing purposes. I entered the data myself via myphpadmin.
Since I am still learning I figured I would just start simple with trying to retrieve and display the test data, but no luck. There is just nothing displayed except for my form.

Seems like it should be simple, I don’t get where I am going wrong.

Here is the code I have right now:


datetime - username:

Here is my bulletin board post!

<?php $db =& JFactory::getDBO(); $query = "SELECT * from #__bulletin_board"; $database->setQuery($query); $result = $database->loadResult(); ?>[/code]


You need to check if the post was submitted then display/manipulate the data.

Put this somewhere in your page, above the form is good.
if(isset($_POST[‘submit’])) {

Note: this simply shows you how to check if the form has been submitted and prints all $_POST values.
I’ll leave it as an exercise for you to do whatever you wish with all that data.

Hope that helps,
Red :wink:

If you use print_r() within

 tags it will output a nice formatted, easy to read array. Very helpful if you have a large array.

echo ‘

echo ‘

I use this:

<?php function debugging($debug, $var=false, $exit=false) { echo '
	$var === true ? var_dump($debug) : print_r($debug);
	echo '
'; $exit === true ? exit() : NULL; } // End Function. debugging($_POST, false, false); [/php] Note: the last two args are optional. Arg 1: the 'thing' to debug ($obj, strings, $variables, etc) Arg 2: provides the option to switch between print_r and var_dump Arg 3: provides the option to halt the script after the output or let it run. Feel free to use it. Red ;)

I have made some progress with returning the data from the table, but I am not exactly where I want to be. I am basing my code off of a similar project done a while ago, which returns the message only for the current user. For this new project I want all users to be included.

Right now, I am mostly concerned with the section between $db = JFactory::getDbo(); and the HTML form. I am not sure I really need the WHERE line, since I am not trying to be that specific (this is leftover from the older project). And I want to SELECT message, username, and datetime from the table, but can’t figure out how to pull information from multiple columns while also being efficient in my code. All I can seem to get is the message associated with my own username.

Here is my updated code:

	$action = $_POST['action'];
	if($action == 'insert'){
		$message = $_POST['postArea'];
		$bulletinPost = new stdClass();
		$bulletinPost -> username = $user ->username;
		$bulletinPost -> message = $message;
		$result = JFactory::getDbo() -> insertObject('#__bulletin_board', $bulletinPost);
		echo "Error";

$db = JFactory::getDbo();
$query = $db -> getQuery(true);

$query -> select('message');
$query -> from($db -> quoteName('#__bulletin_board'));
$query -> where($db -> quoteName('username')." = ".$db ->quote($user -> username));

$db -> setQuery($query);
$result = $db -> loadResult();

	echo '<div id="displayBox">';
	echo '<p class="postHeader">'.$postUsername.' - '.$postDateTime.':</p>';
	echo '<p>'.$result.'</p>';
	echo '<hr />';
	echo '</div>';
	echo "Error";

<!--FORM POST-->
<form name="formPost" method="POST">
<textarea cols="100" rows="2" name="postArea" placeholder="Welcome, Share a message on the bulletin board!"></textarea>
<fieldset class = "buttonPost">
<input type = "hidden" name = "action" value = "insert" />
<p><input type = "submit" name="submit" value="Post"></p>
Sponsor our Newsletter | Privacy Policy | Terms of Service