wookie Posted March 6, 2008 Share Posted March 6, 2008 I've written the little snippet below that produces a members id and registerdate, what I want to be able to do is produce a list of these where the position = 0 (should be around 40 of them) If I understand things correctly from my reading this last two days I need to use foreach within this code snippet to get the job done, unfortunately all attempts have failed thus far, is it foreach I need to use and if so how do I apply it to the code I have already written? <?php $query = mysql_query("SELECT * FROM potdp WHERE position = '0'") or die("Error: ".mysql_error()); while($smffetch=mysql_fetch_array($query)) { $memberid = $smffetch["id"]; $registerdate= $smffetch["date"]; } $query_results = $memberid . '<br>' . $registerdate; ?> Many thanks in advance! Wookie Quote Link to comment https://forums.phpfreaks.com/topic/94621-foreachi-think/ Share on other sites More sharing options...
mainewoods Posted March 6, 2008 Share Posted March 6, 2008 $query = mysql_query("SELECT * FROM potdp WHERE position = '0'") --if your db field 'position' is defined as some numeric type field like integer, you need to leave off the single quotes around the '0' above As well, your 2 statements in your loop just kept assigning different values to the same variables. They will just end up have the value of the last row processed: $memberid = $smffetch["id"]; $registerdate= $smffetch["date"]; not sure what you really wanted to do in the loop Quote Link to comment https://forums.phpfreaks.com/topic/94621-foreachi-think/#findComment-484542 Share on other sites More sharing options...
wookie Posted March 6, 2008 Author Share Posted March 6, 2008 As well, your 2 statements in your loop just kept assigning different values to the same variables. They will just end up have the value of the last row processed: $memberid = $smffetch["id"]; $registerdate= $smffetch["date"]; not sure what you really wanted to do in the loop I want it to show the 40 or so records that is in the table where position is 0, at the moment it shows just the one, and as you correctly said...its the last one. I'm not entirely sure how to get all the reocrds and I wondered is using foreach was the way to go, but after reading for a few days now I have exhausted many ways of using this and am now at a bit of a loss as to where to go with it next. I had thought that assigning the id and date to variables that I was on the right track..but this code just produces one of the forty results I want. Any idea where I go from here? Regards Wookie Quote Link to comment https://forums.phpfreaks.com/topic/94621-foreachi-think/#findComment-484965 Share on other sites More sharing options...
Psycho Posted March 6, 2008 Share Posted March 6, 2008 <?php $query = mysql_query("SELECT * FROM potdp WHERE position = '0'") or die("Error: ".mysql_error()); while($smffetch=mysql_fetch_array($query)) { //Create the code here for displaying each record echo $smffetch["id"] . ': '. $smffetch["date"] . '<br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/94621-foreachi-think/#findComment-484979 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.