Jump to content

Foreach..I think


wookie

Recommended Posts

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

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

<?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>';
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.