Re: Need help with array/foreach
| From: | Jochem Maas | Date: | Fri, 14 Jan 2005 18:40:56 +0000 |
| Subject: | Re: Need help with array/foreach | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-206406@lists.php.net to get a copy of this message | ||
Collin Davis wrote:
Hi, Long time reader, first time poster. I've been using Dreamweaver to generate my PHP (I know - but I'm a designer... most of this makes absolutely zero sense to me), but I've run into something that I know could be done better. If you look here: http://pastebin.com/228804 you can see that I have several MySQL queries. Now I know that surely these can be concatenated into an array or something, but I have no idea how to do that -concatenation means sticking strings together, which is kind of what you would do to an array if you used the join() function on it. but anyway I grabbed this bit of code from you source file:
$db2->query("select pid,poster, ".
"date_format(posted, '%a %D %b %H:%i') as posted ".
"from pastebin where pid=".$db->f("parent_pid"));
you could do this with the sql:
$queries = array();
$queries['get_poster'] = '
select pid,poster,
date_format(posted, '%%a %%D %%b %%H:%%i') as posted
from pastebin where pid=%s';
$queries['get_poster_parents'] = '
select pid,poster,
date_format(posted, '%%a %%D %%b %%H:%%i') as posted
from pastebin where parent_pid=%s order by posted desc;"
(the double percentage signs are there for the benefit of sprintf())
the later on in the script:
$db2->query(sprintf( $queries['get_poster'],$db->f("parent_pid") ));
and
$db2->query(
sprintf( $queries['get_poster_parents'],$db->f("parent_pid")));
(sorry about the odd line wrapping)
to be honest it wasn't totally clear what you wanted to achieve (or why - the why is often telling and will others to help with possibly different solutions), but I hope this gives you something to go on.
I've googled until I'm blue in the face, read tutorials about arrays and foreach loops but I just can't make sense of it. Any help anybody couldI feel like that everytime I try reading up on character encoding/transcoding - my brain hurts and I feel the need to scream!
give me would be greatly appreciated. Thanks, Collin Davis