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!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Wow. Thanks. I always wondered what that "implode" would be useful for.
Now, here a follow-up question to the postings....
Since I have to use the while statement to populate the array, how do I re-execute the query further down the page? In ASP, there was something like a "movefirst" command (or something like that). Is there something similar in PHP?
With the top code just put the following code where you want the rows to show... (notice the whole big piece won't show by itself... u need the following code... the big piece of code has to be above the first one of the following code...
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Sorry, I worded my 2nd question incorrectly (pardon my technical ignorance).
Actually, what I'm trying to do is re-loop the results w/o re-executing the query. By creating a function, I believe you re-execute the query (which I'm trying to avoid sending the 2nd (or multiple) request(s) to the database). I wanted to execute the query only once and "re-use" the query results as many times as I want in the page.
With the code below, the first [b]while [/b] loop will populate the list ($listproductid[])...but the 2nd [b]while[/b] loop (of product names) will be empty...because the "pointer" is at the bottom of the query result (or something like that).
$query = "SELECT productid, productname FROM products WHERE categoryid = 1";
$listproductid = array();
while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
$listproductid[] = $row['productid'];
}
$listproductid = implode(',', $listproductid);
echo $listproductid;
// Re-execute query, but now print out product names....this will not work since the pointer is at the bottom of the $row array.
while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
echo $row['productname'];
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
You can do that in my previous examples (three examples... my first reply here).. that is why there is echo $html; there... the $html is the results... let me work out on your example here...
If you can store two variables into one (I'm not sure if you can and if you can than I don't know how), store $listproductid and $listproductname into one and than you have one echo or store both echo statements into a function such as...
I tried running this (using your 1st code example), but it didn't work...
The echo $listproductid did generate the list of productid's, but the echo $listproductname came out blank (empty). I believe the reason is that the pointer to the query result ($row) is at the bottom of the query result (after going thru the first while loop) and thus there is no more $row['productname'] to assign it to the variable $listproductname.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.