fetch an array of database results -
OK so im adding a little front end to my smf forum where by i want to pull certain posts (articles) from the database so that i can present them on my own template.
I have written a function that should pull an array of all results so that i can just use a foreach loop to display the fields i want? I hope that makes sense. Anyway here is my function.
<?php
function phparticles(){
$php = $this->query(\"select * from smf_messages where
ID_BOARD='26'\");
while($thearticle = $this->fetchArray($php)){
$articledata .=$thearticle[];
}
return $articledata;
}
?>
Because i should be returning many rows, im unsure how to return the results as one string that i can loop through. $articledata returns bugger all.
Basically i want to beable to reuse this function for other parts of the site, so if i only want to display the title in a link, i should only need the ID, and title fields. If i want to display the main text i should only need the body field.
So if the function returns all results i want to loop and pick out the fields that are necessary.
Greg K posted this at 15:13 — 5th March 2008.
He has: 2,145 posts
Joined: Nov 2003
Try this instead:
$articledata = array();
while($thearticle = $this->fetchArray($php)){
$articledata[] = $thearticle;
}
return $articledata;
Now the returned value will be an array of all the row results. NOTE: I'm not sure what all fields/data your table consists of, but would recommend only selecting the field you want (ie. title, body, author, date etc).
ie. maybe do the following, assuming the fields given as an example above:
function phparticles($field = 'body', $limit=0) {
$fields = array('title','body','author','date'); // If in class, set with other class varaiable
if (!in_array($field,$fields))
die ("Invalid Call to function.");
$limit = strval($limit); // Makes sure it is a number
$results = array();
$sql = "SELCET `" . $field . "` FROM `smf_messages` WHERE `ID_BOARD`='26'";
if ($limit > 0)
$sql .= " LIMIT " . $limit;
$dbResult = $this->query($sql);
while($results[] = $this->fetchArray($dbResult));
return $results;
}
This now lets you specify what field you want to use less resources to get what you actually want, (defaults to body of the message) and optionally allows you to limit how many it is getting (hopefully you will grow to have tons of posts , so maybe you only want to display the last 10...
Anyhow, just some thoughts and not 100% sure what you are looking for.
-Greg
benf posted this at 16:21 — 5th March 2008.
They have: 426 posts
Joined: Feb 2005
Greg ok. Thanks for the help but i did this - tell me what you think.
<?php
function phparticles($id){
$phparticle = \"select * from smf_messages where
ID_BOARD='$id'\";
$query = $this->query($phparticle);
while($getarticle = $this->fetchArray($query)){
$articledata .= \"<a href=\\"article.php?id=\".$getarticle[0].\"\\">\".$getarticle[6].\"</a>
<br />\";
}
return $articledata;
}
?>
So id is the id of the topic type or board etc that i want to use so i can reuse this function for all article types. I can add another parametre such as the limit if i need to!
Good Value Professional VPS Hosting
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.