Click to See Complete Forum and Search --> : returning PHP arrays in a function...mysql involved..


bdg1983
09-14-2001, 07:27 AM
function get_posts() {

db_connect();

$sql = sprintf("select id, name, email, subject, date, post from posts");
$sql_result = mysql_query($sql);
if ($main_articles = mysql_fetch_array($sql_result)){

return $main_articles;

}

}



I'm learning how to work with class.FastTempllate, which can be found at http://www.webmasters.net.

The main problem here is that it returns only the first entry of the database. I tried using a 'while' statement to loop through it, however it still yielded one entry.

Can anyone tell me how I can return more than just one of the results?
Thanks.

Alex

Sweede
09-14-2001, 02:55 PM
Originally posted by maxl stylee:
<STRONG>
function get_posts() {

db_connect();

$sql = sprintf("select id, name, email, subject, date, post from posts");
$sql_result = mysql_query($sql);
if ($main_articles = mysql_fetch_array($sql_result)){

return $main_articles;

}

}



I'm learning how to work with class.FastTempllate, which can be found at http://www.webmasters.net.

The main problem here is that it returns only the first entry of the database. I tried using a 'while' statement to loop through it, however it still yielded one entry.

Can anyone tell me how I can return more than just one of the results?
Thanks.

Alex</STRONG>

man that's sooo wrong.

function get_posts() {

db_connect();

$sql = "select id, name, email, subject, date, post from posts";
$result = mysql_query($sql);

if ($result) {
while ( $row_data = mysql_fetch_array($result) {
$data_array[] = $row_data;
}

return $data_array;
} else {
return 0;
}
}


1) you dont use sprintf(, that is very un-neccessary.
2) when you call return, the function execution ends. you can only return 1 item in a function.

your data will be returned in a multi dimensional array like,

$array['idx']['field'];

that should work for you fine.

bdg1983
09-15-2001, 01:04 AM
Ok.
That worked, thanks.
But here's the problem..I don't want to type out everysingle one. Here's how it's going down..I don't know if you're acquianted with FastTemplate or not but here's the code that deals with it...



function get_posts() {

db_connect();

$sql = "select id, name, email, subject, date, post from posts";
$result = mysql_query($sql);

if ($result) {
while ($row_data = mysql_fetch_array($result)) {
$data_array[] = $row_data;
}

return $data_array;

} else {

return 0;
}

}

$posts = get_posts();

//example.php - generates output using FastTemplate

//include the class file and shiot.

include("class.FastTemplate.php");

//instantiate new object
$obj = new FastTemplate("tpls");

// assign names for template files
// "index" noew references the template "./mypage.tpl"
$obj-&gt;define(array("index" =&gt; "mypage.tpl"));


// assign values to FT variables within the template
// this may also be set up as an associative array of key-value pairs
$obj-&gt;assign(array('FNAME' =&gt; 'John',
'LNAME' =&gt; 'Doe',
'AGE' =&gt; '35',
'TEL' =&gt; '861-2767',
'EMAIL_ADDRESS' =&gt; 'alex@aol.com'));



$obj-&gt;assign(array(
'NAME' =&gt; $posts[0][name],
'ID' =&gt; $posts[0][id],
'EMAIL' =&gt; $posts[0][email],
'DATE' =&gt; $posts[0][date],
'SUBJECT' =&gt; $posts[0][subject],
'POST' =&gt; $posts[0][post],
));



//parse templage "index" and store in handler "result"
$obj-&gt;parse(result, "index");

//print contents of handler "result"
$obj-&gt;FastPrint(result);



Now, that all works fine and dandy, except it only returns the post in the [0] variable. Sure, I can change that number and get different posts, however I dont' want to do that. I want it to list all the pages.

The page that it comes out on looks like this:


&lt;!-- begin: mypage.tpl --&gt;
&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;b&gt;Name&lt;/b&gt;: {FNAME} {LNAME}
&lt;p&gt;
&lt;b&gt;Age&lt;/b&gt;: {AGE}
&lt;p&gt;
&lt;b&gt;Email address&lt;/b&gt;: {EMAIL_ADDRESS}
&lt;p&gt;
&lt;b&gt;Tel&lt;/b&gt;: {TEL}

{NAME}&lt;Br&gt;
{ID}&lt;BR&gt;
{EMAIL}&lt;BR&gt;
{DATE}&lt;BR&gt;
{SUBJECT}&lt;BR&gt;
{POST}&lt;BR&gt;


&lt;/body&gt;
&lt;/html&gt;
&lt;!-- end: mypage.tpl --&gt;


I have just been using PHP for like 4 months now so I may be in over my head, but you gotta learn by your mistakes so I'm trying new things.

Anything you can say to help is much appreciated.

Thanks.

Alex.