Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query Results inside loop

Status
Not open for further replies.

tektipsismyfavorite

Technical User
May 4, 2006
57
US
I have created a calendar with PHP and i have a MySQL query that I would like to loop through the rows as each day is added to the page. I wasn't sure if this post needed to go into the MySQL forum, but what I would like to do is while my days are outputting, i'd like to see if the day of the month matches with the day in my row. I'm just not sure how to put the row into an array, and then increase that array.

I tried $rows = mysql_fetch_array($queryResult);

but then $rows[0] returns the first column, so I'm not entirely sure how to get the next record, once the first one has been outputted.

ie.
Code:
$r = 0
if($theday == $row[$r]['day']){
  echo $row[$r]['event'];
  $r++;
}

How can I do this?
 
Code:
while ($rows=mysql_fetch_assoc($queryresult)) {
//do something
}
[/code#]

this code iterates through the recordset row by row. for each row/record the datafields/columns are accessible in the $rows array.  if you use the "_assoc" variant of the fetch function the columns are named after the columns so $rows['days'] rather than $rows[0].
 
yeah after messing with it a lot and recently learning of "print_r" (very helpful!!!) I was able to figure it out. I ended up doing;
Code:
do {
	$row_bdays[] = $row_df_bdays['bdayid'].",".$row_df_bdays['name'].",".$row_df_bdays['dob'].",".$row_df_bdays['b_day'];
} while ($row_df_bdays = mysql_fetch_assoc($df_bdays));

and then when it came time to check my event against my day,
Code:
$thisbday = split(",",$row_bdays[$r]);
if(($d-1) == $thisbday[3]){
	echo $thisbday[1]."'s Birthday<br>";
	$r++;
}

now, i guess i have work on looping through those to get multiple events on the same day.
 
personally i hate do while loops. the logic is inverted and i seldom find it logical to perform a test at the end of a cycle rather than the start.

i also think you can probably achieve what you are doing in the do-while loop within a single query rather than iterating a query through a script and rebuilding an array.
 
All:
while(...){...} and do{...}while(...) are both very serious constructs and each has its appropriate place.

tektipsismyfavorite:
I don't think a while loop is appropriate here. Unless your script invokes mysql_fetch_assoc() before entering the do...while loop, you are guaranteed that the values you need for your concatenation:

[tt]$row_bdays[] = $row_df_bdays['bdayid'].",".$row_df_bdays['name'].",".$row_df_bdays['dob'].",".$row_df_bdays['b_day'];[/tt]

won't be available the first time the loop runs. And if you do actually invoke mysql_fetch_assoc() before the do..while loop:

[tt]$row_df_bdays = mysql_fetch_assoc($df_bdays);
do {
$row_bdays[] = $row_df_bdays['bdayid'].",".$row_df_bdays['name'].",".$row_df_bdays['dob'].",".$row_df_bdays['b_day'];
} while ($row_df_bdays = mysql_fetch_assoc($df_bdays));[/tt]


then you might as well just use a do-loop:

[tt]while ($row_df_bdays = mysql_fetch_assoc($df_bdays))
{
$row_bdays[] = $row_df_bdays['bdayid'].",".$row_df_bdays['name'].",".$row_df_bdays['dob'].",".$row_df_bdays['b_day'];
}[/tt]

and as jpadie has implied, since a while loop is used more often than a do...while loop, it's more readable by a general programmer.




Want the best answers? Ask the best questions! TANSTAAFL!
 
my bad. I didn't mention before that I had $row_df_bdays = mysql_fetch_assoc($df_bdays); prior to the do...while.

thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top